ItextSharp fit text to a page

柔情痞子 提交于 2019-12-11 06:54:41

问题


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();

The text page is a multi page file, so I would like to reproduce the same on the pdf file.

When i read "Page 2" on my txt file, I need to create a new page in the pdf file.

I would like to put all the text before page 2 in only one pdf page.

How can i fit all the text in only one pdf page?

Thank's so much and sorry for my english


回答1:


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).

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.



来源:https://stackoverflow.com/questions/4805767/itextsharp-fit-text-to-a-page

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!