I have a pdf that I want to grab a specific piece of info from.
My issue isn't how do I get it, my issue is I'm not under standing the coordinates of the rectangle.
I get the x,y, height & width, but not as it related to the actual page.
lets say for arguments sake I have a business letter with the date to the far right and address to far left. How do I grab the date?
ex:
(space space space space space space space space ) 01/02/2017
Mr Jones
some Address
blah, blah, blah.
Dear....
When ever I have tried to reference where I think the date should be, I end up with an empty value.
Thanks for any thoughts and or suggestions.
This is just one of the tries:
Rectangle rect = new Rectangle(96, 822, 72, 8);
This is harder than you might think.
Content isn't just placed on a page.
It's placed on a box on a page.
Both the content, the box, and the page are allowed to have custom transformations, and rotations.
And then of course there is the issue of how the coordinate system is defined. The origin usually lies in the bottom left corner. But this is again something that can be configured.
I would debug your problem by implementing a simple class that extends SimpleTextExtractionStrategy. In the method eventOccurred you should see events passing by of type TextRenderInfo. Those events contain the text being rendered and the bounding box. Print out the text, and its corresponding bounding box. Then at least you know where iText thinks your content is.
Related
I'm trying to print some A5 pages from a C# application but I'm getting unexpected results. The output is vertically offset by about 110mm so the output starts halfway down the page and is offset horizontally by about 20mm. The output starts off the left hand side of the page (so it's hard to measure the exact offset). The output is also clipped horizontally and vertically.
For example, a
dc.DrawRectangle (0, 0, 100, 100)
draws a box halfway down the page and half the box is missing due to being off the left hand edge. The size seems OK though.
I must be missing something really obvious but I can't quite see it myself. Does anyone know what might be causing these offsets?
This may provide a hint:
// Rectangle describes page minus margins.
Rect rectPage = new Rect(marginPage.Left, marginPage.Top,
dlg.PrintableAreaWidth - (marginPage.Left + marginPage.Right),
dlg.PrintableAreaHeight - (marginPage.Top + marginPage.Bottom));
// Draw rectangle to reflect user's margins.
dc.DrawRectangle(null, pn, rectPage);
From : Charles Petzold's Apps = Code With Markup
I'm trying to draw a string using either textrenderer.drawtext, graphics.drawstring or graphicspath.addstring - the main purpose is to extract all fonts to bitmaps to edit them and use them as bitmaps with shaders in a game.
With textrenderer.drawtext and graphics.drawstring, I get a padding on top of varying degrees - so I try graphicspath.addstring. I extract the font family's ascent height and descent height, but they are wildly unusable with emheight. (using ascent and descent with emheight is how microsoft suggest you do what I am trying to do - via http://msdn.microsoft.com/en-us/library/xwf9s90b%28v=vs.110%29.aspx. Has anyone successfully ever draw pixel perfect fonts using C#? Every time I ever try or look it up, textrenderer and graphics always' padding always screwed up drawing and this new graphicspath method seems to have an issue with using a specific scale.
The usual methods using TextRenderer or MeasureString will give you a SizeF, containing the bounds of the string you measure. Most formats include a little slack so you can compose text by adding strings together.
The aim of theses methods is to help create blocks of text by letting you measure when a line will be full or how many pixels to advance for the next line.
They are not really meant for maesuring single characters.
For this there is a special stringformat GenericTypographic as described here which leaves out the white space.
To get an even more precise measurement one can use GraphicsPath.AddString and then GetBounds, maybe after switching antialias off..
Now, if you wanted to draw a single character precisely, say centered on a Button this would do the job.
But you know all that and your aim is different - if I understand you correctl,y you want to create Bitmaps from each character in order to later join them to form text. This means you need them to line up correctly vertically, ie sit on the same baseline.
The sizes of the characters don't help you here; now, normally you'd need the baseline of each charcater, which you don't get, at least not for anything descending like 'f' or even just ',' etc..
But it wouldn't help you either because in GDI you don't print/draw to the baseline anyway..
What you should do, imo is either draw one long string with all characters, so that they're all lined up right and then cut out the characters one by one. Or you could draw each character on its own, but suffix all or some characters you know to have ascenders and descenders and then only pick the first columns from the result.
So the only way I figured out how to do this is is to first draw the string to a graphicpath, then measure all the empty spots in the graphic path, and get it's height only after I've measure every spot, then redraw the string (I have an attempt counter to limit attempts but increase em to pixel accuracy) taking the old size and new size into account by a modifier and then extract the final size and store it.
Only I got to get around the BS of every font having a weird top padding that isn't associated with it's ascent and internal overflow (ex: Ñ), as well as descent, in refrence to a 0,0 point, this way.
I'm essentially re-writing a document viewer with markups to move away from a COTS product and so far everything has been working VERY well. My code is based off of Mark Miller's Extensions to DrawTools (http://www.codeproject.com/Articles/17893/Extensions-to-DrawTools).
The old viewer stores pages and their markups based on x/y coordinates in inches and I have had NO trouble converting this to a pixel-based coordinate system and converting lines, boxes etc to the new viewer. The lines and boxes show up exactly where they are supposed to and have the correct size.
The problem has been displaying text markups, no matter what I do they always end up MUCH smaller than they should be.
I'm doing:
UserControl->OnPaint()
Create a Matrix Transform for:
Scale
Rotate
Translate
Apply Matrix to Graphics Object
Call method that draws the Page Image and then all of the Markups.
I have the X/Y Coords and Font Size of the Text to draw, and the resulting string DOES end up at the correct coordinates but the text is WAY too small. The really bizarre part about this is the original viewer is written in .Net so I know that the Font and Size SHOULD relate especially since everything else scales so well.
Here is an example of what I'm talking about. Please ignore the BackColor and Border of the "This is some Text", I haven't gotten around to getting that transformed yet since I've been so focused on getting the TEXT right.
Original:
My Result:
I ended up having to rework everything to be Inch Unit based. The font simply doesn't have an easy way of scaling between the units and the Inch Unit turned out to be the easiest solution.
I am printing a rdlc report to Dot matrix printer using this code . The report is in the format of a bill receipt. What i want is that the printer should stop exactly at the point where the receipt is fully printed (i.e. the end of the text to be printed) and not take in the rest of the paper and print nothing on it.
As loads of receipts are to be printed and there can be long queues, it will be time consuming if the end user has to reload the paper (which has been wasted) back to the printer. Hence i am looking for way by which the printer can stop the paper intake as soon as the text to be printed is complete.
It sounds like what you're asking for is how to print receipts as a continuous stream, instead of page by page. The code you linked to is entirely page driven and page oriented. You can work within a page-dominated view system, but you're going to have to do extra work.
You should be able to print multiple things on one page by changing the PrintPage code to output more than one image chunk per page. You will have to manage the offset of each rectangle to place the 2nd image below the 1st, and you will have to figure out what you want to do with the next report page does not fit on the space remaining on the current page. I expect the output will be clipped to the current page, so if you draw outside the page boundaries (past the end of the page) nothing will be output.
You could choose to advance to the next page when the next report image does not fit in the space remaining on the current page.
Or, you could print as much as will fit on this page, and then print the rest of that report image on the top of the next page. To do this, you will need to calculate the intersection of the remaining space on page 1 and the report image rectangle, and save the height of that intersection. End the page. When page 2 rolls around, print that same report image again, but this time offset the output rect by the height of the rectangle that was printed on the previous page. This is assuming a clipping rectangle is already in place on the print device handle (which is very likely).
The downside of splitting a report across pages like this is that you may end up with graphs or even lines of text that are cut off or split between pages. These will be hard to read. If you know the font height and line spacing and how the report is laid out I suppose you could make a calculated guess about where the lines of text fall in the output rectangle and round your output rectangle down to the nearest whole multiple of the line height, but this is getting deep into hacky kludge territory.
I have a series of points in a GraphicsPath; for our purpose lets assume its the outline of an uppercase B. I want to be able to be able to draw only the bottom portion that would resemble an uppercase L.
I'd like to be able to select a window of points from the GraphicsPath. Is there a handy way to do this without doing point interpolation; ie have to write code to calculate slope math and possibly derivatives?
I don't believe that there's a way to actually tell the Graphics class to "stop" halfway through a path, or somehow create a new path that intersects with a bounding box (without implementing the method yourself), but if you're just doing this so you can draw a certain part of the path, then you should be able to achieve what you want by setting the clipping region.
See the Graphics.ClipBounds property, which takes a RectangleF, or Graphics.Clip, which is a Region (the former is generally easier to use, unless you already have a Region instance).
If you need to use the partial path for something else then I'm not aware of any built-in way to do it.