ColumnText和truncate问题 [英] ColumnText and truncate issue

查看:195
本文介绍了ColumnText和truncate问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用ITextSharp,并在操作第二版中使用IText,特别是第74页使用ColumnText对象。

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

我正在使用加载PDF文件PDFReader和PDFStamper类,并尝试在PDF文档中添加多个元素。

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.

我已阅读有关ColumnText.HasMoreText检查以及Go()返回的内容。在这种情况下,它将返回NO_MORE_COLUMNS,我认为是2。

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.

文本显示为019 298397 4795 69 7989990000,您可以看到它开始覆盖自身。

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.

推荐答案

好的,我终于找到了一些时间来看看你的例子。

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

首先:你定义一个 Font ,但你不能在任何地方使用它。默认字体用于短语(Helvetica 12pt,领先18pt)。

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

领先18pt,意味着你永远无法在 y = 634 y = 642 之间插入文本。让这个 y = 634 y = 652

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

这篇关于ColumnText和truncate问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆