When I use the pdfCanvas object I have the MoveText
method where I can set the x and y coordinate but I don't see that in the Paragraph object? Second thing is why do I need the rectangle object I am not adding any Rectangle to the pdf just Text. Where I want the text to take the full width of the page. Can I get the size of the font and text and then calculate the centeredWidth and centeredHeight if I only have MoveText method in pdfCanvas?
for (int i = 1; i <= numberOfPages; i )
{
PdfPage pdfPage = pdfDocument.GetPage(i);
iText.Kernel.Geom.Rectangle pageSizeWithRotation = pdfPage.GetPageSizeWithRotation();
float n2 = 15F;
float n3 = pageSizeWithRotation.GetHeight() - 10F;
float frontSize = 6.25f;
PdfCanvas pdfCanvas = new PdfCanvas(pdfPage);
iText.Kernel.Geom.Rectangle rectangle = new iText.Kernel.Geom.Rectangle(100, 100, 100, 100);
Canvas canvas = new Canvas(pdfCanvas, rectangle);
PdfFont font = PdfFontFactory.CreateFont("C:\\Windows\\Fonts\\ARIALN.TTF");
Paragraph p = new Paragraph()
.Add(disclaimerText)
.SetFont(font)
.SetFontSize(frontSize)
.SetTextAlignment(TextAlignment.CENTER);
canvas.Add(p);
canvas.Close();
//pdfCanvas.BeginText()
// .SetFillColorRgb(0, 0, 0)
// .SetFontAndSize(PdfFontFactory.CreateFont("C:\\Windows\\Fonts\\ARIALN.TTF"), frontSize)
// .MoveText(n2, n3)
// .ShowText(disclaimerText)
// .EndText();
}
CodePudding user response:
First of all, please be aware that you are using different parts of the iText API with PdfCanvas
on one side and Canvas
on the other side:
PdfCanvas
is merely a thin wrapper for the instructions written into the PDF. When using this class, you have to determine yourself where you want to start text lines, where to break the lines, how much space to add between characters, words, and lines, and so on.
Canvas
(and Document
) on the other hand features its own layout engine, you only initialize it with the PdfCanvas
to work on and the coordinate ranges in which you want it to operate, and then you feed it paragraphs, tables, etc. which Canvas
arranges properly.
Thus, you essentially have the choice, do you foremost want to arrange everything yourself, or do you foremost want to leave that task to iText.
That being said, let's look at your questions:
When I use the pdfCanvas object I have the
MoveText
method where I can set the x and y coordinate but I don't see that in the Paragraph object?
Paragraph
s mostly are designed for automatic layout by Canvas
and Document
. Nonetheless, you can statically arrange them at given coordinates using the SetFixedPosition
overloads.
Second thing is why do I need the rectangle object I am not adding any Rectangle to the pdf just Text.
You need the rectangle to tell Canvas
where on the (theoretically endless) PdfCanvas
coordinate plane it shall arrange the objects you give it.
Where I want the text to take the full width of the page.
Then use the full crop box of that page, assuming you mean the full visible width of the page on screen or the final printed product. You can get it using the PdfPage
method GetCropBox
.
If you are not sure which "full widths of the page" there are, have a look at this answer.
Can I get the size of the font and text and then calculate the centeredWidth and centeredHeight if I only have MoveText method in pdfCanvas?
You don't get the size of the font, you set it (using SetFontAndSize
as you show in your code). You also set the font. And the PdfFont
object you set has nice GetWidth
overloads to determine the widths of some text drawn using that font. That in combination with the crop box mentioned above and trivial math allows you to calculate everything you need for simple text drawing.