ItextSharp将文本适合页面 [英] ItextSharp fit text to a page
问题描述
我是意大利人,对不起我的英语。
我正在尝试使用itextSharp将txt文件转换为pdf文件。
这是我的代码:
i'm italian and i'm sorry for my english. I'm trying to use itextSharp to convert a txt file into pdf file. this is my code:
String l = file.ReadLine() + "\r\n";
while (l != null)
{
iTextSharp.text.Font contentFont = iTextSharp.text.FontFactory.GetFont("Arial", 8, iTextSharp.text.Font.NORMAL);
//Chunk line = new Chunk(l, contentFont);
Paragraph p2 = new Paragraph(l,contentFont);
oDoc.Add(p2);
oDoc.Add(Chunk.NEWLINE);
l = file.ReadLine();
}
oDoc.Close();
文本页面是一个多页面文件,所以我想在pdf文件中重现相同的内容。
The text page is a multi page file, so I would like to reproduce the same on the pdf file.
当我在我的txt文件中读到第2页时,我需要在pdf文件中创建一个新页面。
When i read "Page 2" on my txt file, I need to create a new page in the pdf file.
我想将第2页之前的所有文字放在一个pdf页面中。
I would like to put all the text before page 2 in only one pdf page.
我如何只在一个pdf页面中填写所有文本?
How can i fit all the text in only one pdf page?
非常感谢,对不起我的英语
Thank's so much and sorry for my english
推荐答案
嗯,如果您在PDF页面结束前到达文本页面的末尾,则只需调用 oDoc.newPage()
即可。替代方案并非如此简单。
Well, if you reach the end of a text page before the end of a PDF page, you can just call oDoc.newPage()
. The alternative isn't so simple.
唯一的简单方法就是在每个页面上创建一个文本字段,并设置多行标志。然后,您将字体大小设置为零,该字段将自动选择一个字体大小,该字体大小将调整字体大小以适应可用空间(在一些合理的限制范围内)。
The only Easy Way To Do It would be to create a text field on each page, with the multiline flag set. You then set the font size to zero and the field will automatically pick a font size that will size the font to fit the available space (within some reasonable limits).
您也可以使用 ColumnText
并调用 go(true)
。这将模拟布局,允许您在实际将文本绘制到内容流之前调整实际字体大小。
You could also use a ColumnText
and call go(true)
. This will "simulate" layout allowing you to make adjustments to the actual font size prior to actually drawing the text to a content stream.
这篇关于ItextSharp将文本适合页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!