Drawing a second text below the first text - vb.net

I would like to draw 2 texts onto my PDF.
The first text should be aligned to the top left corner.
This works fine.
I'm using:
canvas = stamper.GetOverContent(i)
watermarkFont = iTextSharp.text.pdf.BaseFont.CreateFont(iTextSharp.text.pdf.BaseFont.HELVETICA, iTextSharp.text.pdf.BaseFont.CP1252, iTextSharp.text.pdf.BaseFont.NOT_EMBEDDED)
watermarkFontColor = iTextSharp.text.BaseColor.RED
canvas.MoveTo(0, 0) 'I think the canvas is the space that we draw onto. My documents always start at position X=0 and Y=0, so move to 0,0 should be fine
canvas.BeginText()
canvas.SetFontAndSize(watermarkFont, 12)
canvas.SetColorFill(watermarkFontColor)
canvas.ShowTextAligned(Element.ALIGN_TOP, uText, 0, 830, 0) 'is 830 the width of the available space?
canvas.EndText()
Now I would like to draw another text approximately 100 pixels below the first text.
I'm using:
canvas.MoveTo(0, 100) 'let's draw the second text at X=100, Y=100
canvas.BeginText()
canvas.SetFontAndSize(watermarkFont, 12)
canvas.SetColorFill(watermarkFontColor)
canvas.ShowTextAligned(Element.ALIGN_CENTER, uBewirtung, 0, 830, 0)
canvas.EndText()
The second text however doesn't show up at all.
I suspect I'm drawing outside the document, but I don't see my mistake.

The MoveTo() method is meant for drawing paths (lines amd shapes in graphics state), not text (in text state). It adds an m operator to the content stream. If you are a PDF specialist, you should use the SetTextMatrix() method inside your BT/ET text block: What does setTextMatrix of contentByte class in iText do?
Note the if; it is important. If you are not a PDF specialist, you shouldn't be toying around with those methods. You should use ColumnText.ShowTextAligned() instead of BeginText(), EndText() and all of the lines you added in-between. Those methods are meant for people who speak PDF syntax.

Related

PDFBOX vertical text position with RotationMagic

I am using PDFBOX to extract the word coordinates. The PDF has some horizontal and vertical text mixed in a page, Here is the PDF, in Page 9, it has vertical text.
If I use PDFBOX -RotationMagic option, I can extract the vertical text layout correctly. However, its text Position is not correct inside PDFTextStripper class function:WriteString().
How can I convert to original coordinates (e.g., origin is at top left).
In page 9, the "1.Introduction" is vertical text and after adding "-optionMagic", the coordinates inside writeString() is like this:
If I do not apply '-rotationMagic', the textPosition is like this. it splits "1:Introduciton" to multiple words. The first one is "1:"
How can I convert the first (rotated) coordinates to the second one. Why is the first one is (446.73, 1029.6)?

iText7 - create PDF with exact dimensions when printed - how?

I'm creating a simple PDF using iText7 (C#) but I need it to be printed at exactly the right size. Here's my code:
PdfWriter writer = new PdfWriter("output.pdf");
PdfDocument pdf = new PdfDocument(writer);
pdf.SetDefaultPageSize(iText.Kernel.Geom.PageSize.LETTER);
var page = pdf.AddNewPage();
page.SetCropBox(new iText.Kernel.Geom.Rectangle(36, 36, 7.5f * 72, 10 * 72));
PdfCanvas canvas = new PdfCanvas(page);
canvas.SetStrokeColor(ColorConstants.BLACK).SetLineWidth(3);
canvas.MoveTo(36, 36);
canvas.LineTo(36, 36 + 72); // Draw a line 1 inch long
canvas.LineTo(36 + 72, 36 + 72); // Draw a second line, perpendicular to the first, also 1 inch long
canvas.ClosePathStroke();
pdf.Close();
If I right-click the resulting PDF and select "Print", my triangle is off the bottom of the page.
When I open the resulting PDF in the PDF program I'm using (PDF Architect), it gives me a few options:
If I just click "Print", it gives me lines that are 1 1/16" long and start about 1/8" from the edge of the page, so by default PDF Architect seems to be taking the contents of my crop box and expanding it to the maximum page availability.
If I click on "Fit" before clicking "Print", that results in the desired output - lines 1" long, starting 1/2" from each side of the page. That works but is error-prone - too easy to forget to click "Fit" every time.
Is there a way to generate a PDF that contains information that says "I'm targeting this document at letter size, but I'm staying 1/2" away from all the edges, so when you print, if the printer has margins <= 1/2 inch you should be fine, and just print it exactly how I've described without any shrinking or enlarging"?
You will not be able to completely control this from the PDF document. The PDF processor (e.g. viewing application) or the printer (driver) will always be able to scale the content up or down.
Apparently, PDF Architect has the "Fit" option enabled by default, so it scales the page to the selected paper size.
You are setting a crop box of 7.5x10 in. I assume you're printing to Letter sized (8.5x11 in) paper. So the 7.5x10 page will indeed be scaled up, and your content will become slightly larger.
Is there a way to generate a PDF that contains information that says "I'm targeting this document at letter size, but I'm staying 1/2" away from all the edges, so when you print, if the printer has margins <= 1/2 inch you should be fine, and just print it exactly how I've described without any shrinking or enlarging"?
I would not set the crop box. When the pages in the PDF document are Letter size and the output paper is also Letter size, it should not matter whether the "Fit" option is enabled or not, as not scaling needs to happen. It's definitely not a fool proof solution, but at least it's less error prone.

How the value of the tj operator is generated in a pdf document (justified text)

I can't understand and find how the value of the tj operator is generated??
Here I paste result before and after changes in the display of the text (on the second block I changed the position Left-Justice and then again comeback to Centered)
I think pdf use some of prng, but what kind of, I can't find
HElp please
[(\003\024\027\005\003\030\036\b)-114.267(\003\006\007\024\036\b)-113.297(\026\002\024\003\032\020\b)-113.337(\b)-111.574(#\024\002\f\005\002\021\003\007\004\f\005\b)-117.089(\003\006\002\003\b)-114.08
[(\003\024\027\005\003\030\036\b)-114.366(\003\006\007\024\036\b)-113.297(\026\002\024\003\032\020\b)-113.327(\b)-111.693(#\024\002\f\005\002\021\003\007\004\f\005\b)-116.98(\003\006\002\003\b)-114.188
First of all, the PDF format does not explicitly support text justification. PDF does not even know text column definitions to justify text in!
All the PDF format supports is
setting or changing the text matrix (and text line matrix), scaling, character and word spacing explicitly and
drawing text pieces which implicitly changes the text matrix.
Thus, if a PDF processor changes the justification of a line of text, it actually first has to have determined
which text pieces belong together and form that line of text;
text pieces can be given as arguments of the Tj or TJ instructions (or more seldom the " or ' instructions); in simple cases the whole line is drawn using a single instruction but you cannot count on that in general; and
what the left and right borders of the text column are to justify between;
e.g. these borders might be standard values assumed by the processor for certain page formats or derived from the current clip path.
Having determined these data, the procedure differs for different kinds of justification:
left justification - position the text matrix at the left text column border at the height of the line and simple let the text drawing instructions follow;
right justification - calculate the width of the drawn line using the current font, position the text matrix at the right text column border minus that width at the height of the line, and let the text drawing instructions follow;
center justification - calculate the width of the drawn line using the current font, position the text matrix at the middle of the text column minus half that width at the height of the line, and let the text drawing instructions follow;
full justification - calculate the width of the drawn line using the current font, set the character spacing and word spacing (using the Tc and Tw instructions, probably with a tweak of the Tz horizontal scaling) to use up the difference between that width and the text column width, position the text matrix at the left text column border at the height of the line, and let the text drawing instructions follow;
or calculate the width of the drawn line using the current font, change the text drawing instructions to use up the difference between that width and the text column width (e.g. using the numeric TJ array argument values), position the text matrix at the left text column border at the height of the line, and let the changed text drawing instructions follow;
or even apply a combination of these methods.
(The changes applied when doing a full justification - character spacing, word spacing, changes of text drawing instructions - obviously additionally are undone when later again changing to another type of justification...)
Positioning the text matrix can happen using the Tm, Td, TD, and T* instructions.
By the way, the positioning and scaling of the text also is influenced by the current transformation matrix. Thus, cm instructions can also be used for justification. But this is less likely than the use of the instructions mentioned above...
Unfortunately you merely supplied an excerpt from the array argument of a TJ instruction before and after such a justification job. One sees that the numeric elements of that array change very slightly. Whether this actually is the justification itself (as per the second option of the full justification above) or merely some computational inaccuracy cannot be told without the context.

Display Fixed Width Text in Winforms using VB.NET

I need to write some fixed-width font (i.e Courier New or Consolas) text to a .net Winforms window in the Paint event - not using a label or any other winforms control - rather using a graphics object method to render the text onto the form's client area.
I am having difficulty aligning text on different lines under headings even though it is fixed width font. How can I get a precise measurement of the width of a single character in the fixed-width font? How can I get 2 lines to print out aligned horizontally in successive text out calls?
For example:
Heading 1 Heading 2
Short Other text
A bit longer Still aligned?
I need a separate call to render each cell of text under Heading 2. For argument's sake - let's say column 1 items are printed in black and column 2 are printed in blue - we can't use the same text out call for the entire line.
Graphics.MeasureString may be what you are looking for.
Ok so here is the code that works the way I want using MeasureString. A string is printed twice. One time using a single call to DrawString. The second time, character by character in a loop. What I needed was that the 2 strings should appear identical but I was having trouble getting the correct horizontal position of each char when drawing the second string. You can put this code into the Paint event of a form to try it out (set the form font to Consolas or other fixed width font):
Dim i As Single
Dim sf As StringFormat
Dim String1 As String = "Here is out test string"
Dim CharSizeF As SizeF
sf = StringFormat.GenericTypographic
CharSizeF = e.Graphics.MeasureString(String1, Me.Font, 10000, sf)
CharSizeF.Width /= String1.Length
e.Graphics.DrawString(String1, Me.Font, Brushes.Black, 0, 0, sf)
For Each c As Char In String1
e.Graphics.DrawString(c.ToString, Me.Font, Brushes.Black, i * CharSizeF.Width, CharSizeF.Height, sf)
i += 1
Next
Microsoft also helped with:
http://msdn.microsoft.com/en-us/library/957webty.aspx
To obtain metrics suitable for adjacent strings in layout (for example, when implementing
formatted text), use the MeasureCharacterRanges method or one of the MeasureString methods that
takes a StringFormat and pass GenericTypographic. Also ensure the TextRenderingHint for the
Graphics is AntiAlias.

Using whole space of pdf file

I am using prawn to create pdf file but it always leaves some spaces/margins around the page. Can't we use whole space of the pdf file not leaving any margins around?
Thanks !!!
Are you referring to the page bounds ?
The general space that is consumed on the page can be shown by the example code:
require 'prawn/core'
require 'prawn/layout'
Prawn::Document.generate('padded_box.pdf') do
stroke_bounds
text "Margin box"
padded_box(25) do
stroke_bounds
text "Bounding box padded by 25 on all sides from the margins"
padded_box(50) do
stroke_bounds
text "Bounding box padded by 50 on all sides from the parent bounds"
end
end
end
This will draw the bounds of the page showing the margin. There is a gap, which is the margins typically defined for the printing area