简体   繁体   中英

ItextSharp fit text to a page

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM