ColumnText and truncate issue

三世轮回 提交于 2019-12-24 17:06:05

问题


I am currently using ITextSharp, and following along with IText in Action Second edition, specifically page 74 working with the ColumnText object.

I am loading a PDF file using the PDFReader and PDFStamper classes, and trying to add multiple Elements in the PDF document.

 PdfStamper pdfStamper = new PdfStamper(pdfReader, fileStream);
 PdfContentByte canvas = pdfStamper.GetOverContent(1);
 Rectangle size = pdfReader.GetPageSizeWithRotation(1);


 ColumnText ct = new ColumnText(canvas);
 //I have multiple calls  like below, with different x,y coords
 ct.SetSimpleColumn(10, 634, 100, 642);
 ct.AddText(new Chunk("1234567890 999 9 9 999000 0 877"));
 ct.Go();

What I am noticing is the text will stop and then start writing over itself (the same column?). I basically need it to just stop when it gets to the end of the boundary. I do not need the extra text to show in another column.

I have read about the ColumnText.HasMoreText check, and also what the Go() returns. In this case, it will return NO_MORE_COLUMNS which I think is 2.

I am not understanding how to get the truncating to work correctly without the text writing on top of itself.

The text displays as "019 298397 4795 69 7989990000" which you can see it starts to overwrite on itself.

If anyone can assist me with this Id appreciate it. If i need clarification, let me know too.


回答1:


OK, I've finally found some time to look at your example.

First this: you define a Font, but you don't use it anywhere. The default font is used for the Phrase (Helvetica 12pt, with a leading of 18pt).

A leading of 18pt, means that you'll never be able to fit the text between y = 634 and y = 642. Let's make this y = 634 and y = 652.

Finally, you're using text mode. Most of the times it's better to use composite mode. When you change your code like this, it will work:

PdfReader reader = new PdfReader("resources/hello.pdf");
using (FileStream fileStream = new FileStream("PDF/Test2.pdf", FileMode.Create, FileAccess.Write)) {
    PdfStamper stamper = new PdfStamper(pdfReader, fileStream);
    ColumnText ct = new ColumnText(stamper.GetOverContent(1));
    ct.SetSimpleColumn(10, 634, 100, 652, 0, Element.ALIGN_LEFT);
    ct.AddElement(new Phrase("1234567890 999 9 9 999000 0 877"));
    ct.Go();
    stamper.Close();
}
reader.Close();


来源:https://stackoverflow.com/questions/20257667/columntext-and-truncate-issue

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