iText7 MoveText 和矩形对象
iText7 MoveText and rectangle object
当我使用 pdfCanvas 对象时,我有 MoveText
方法,我可以在其中设置 x 和 y 坐标,但我在 Paragraph 对象中看不到它?第二件事是为什么我需要矩形对象我没有将任何矩形添加到 pdf 只是文本。我希望文本占据页面的整个宽度。如果我在pdfCanvas中只有MoveText方法,我可以获取字体和文本的大小,然后计算centeredWidth和centeredHeight吗?
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();
}
首先,请注意您使用的是 iText API 的不同部分,一侧为 PdfCanvas
,另一侧为 Canvas
:
PdfCanvas
只是对写入 PDF 的说明的薄包装。使用此 class 时,您必须自己确定要从何处开始文本行、在哪里换行、在字符、单词和行之间添加多少 space 等等。
另一方面,Canvas
(和 Document
)具有自己的布局引擎,您只需使用要处理的 PdfCanvas
和您想要的坐标范围对其进行初始化它运行,然后你给它输入段落、表格等,Canvas
安排得当。
因此,您基本上可以选择,是最想自己安排一切,还是最想把这个任务留给 iText。
话虽如此,让我们看看您的问题:
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
主要是为Canvas
和Document
的自动布局设计的。尽管如此,您可以使用 SetFixedPosition
重载在给定坐标处静态排列它们。
Second thing is why do I need the rectangle object I am not adding any Rectangle to the pdf just Text.
您需要矩形来告诉 Canvas
在(理论上是无限的)PdfCanvas
坐标平面上的哪个位置,它将安排您给它的对象。
Where I want the text to take the full width of the page.
然后使用该页面的完整裁剪框,假设您指的是屏幕上页面或最终打印产品的完整 可见 宽度。你可以使用 PdfPage
方法 GetCropBox
.
来获取它
如果您不确定有哪些“页面全宽”,请查看 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?
您没有获取字体的大小,您设置它(使用SetFontAndSize
,如您在你的代码)。您还设置了字体。您设置的 PdfFont
对象有很好的 GetWidth
重载来确定使用该字体绘制的某些文本的宽度。结合上面提到的裁剪框和简单的数学运算,您可以计算简单文本绘图所需的一切。
当我使用 pdfCanvas 对象时,我有 MoveText
方法,我可以在其中设置 x 和 y 坐标,但我在 Paragraph 对象中看不到它?第二件事是为什么我需要矩形对象我没有将任何矩形添加到 pdf 只是文本。我希望文本占据页面的整个宽度。如果我在pdfCanvas中只有MoveText方法,我可以获取字体和文本的大小,然后计算centeredWidth和centeredHeight吗?
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();
}
首先,请注意您使用的是 iText API 的不同部分,一侧为 PdfCanvas
,另一侧为 Canvas
:
PdfCanvas
只是对写入 PDF 的说明的薄包装。使用此 class 时,您必须自己确定要从何处开始文本行、在哪里换行、在字符、单词和行之间添加多少 space 等等。
Canvas
(和 Document
)具有自己的布局引擎,您只需使用要处理的 PdfCanvas
和您想要的坐标范围对其进行初始化它运行,然后你给它输入段落、表格等,Canvas
安排得当。
因此,您基本上可以选择,是最想自己安排一切,还是最想把这个任务留给 iText。
话虽如此,让我们看看您的问题:
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
主要是为Canvas
和Document
的自动布局设计的。尽管如此,您可以使用 SetFixedPosition
重载在给定坐标处静态排列它们。
Second thing is why do I need the rectangle object I am not adding any Rectangle to the pdf just Text.
您需要矩形来告诉 Canvas
在(理论上是无限的)PdfCanvas
坐标平面上的哪个位置,它将安排您给它的对象。
Where I want the text to take the full width of the page.
然后使用该页面的完整裁剪框,假设您指的是屏幕上页面或最终打印产品的完整 可见 宽度。你可以使用 PdfPage
方法 GetCropBox
.
如果您不确定有哪些“页面全宽”,请查看 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?
您没有获取字体的大小,您设置它(使用SetFontAndSize
,如您在你的代码)。您还设置了字体。您设置的 PdfFont
对象有很好的 GetWidth
重载来确定使用该字体绘制的某些文本的宽度。结合上面提到的裁剪框和简单的数学运算,您可以计算简单文本绘图所需的一切。