使用SetSimpleColumn()时,使用itext生成的PDF变为“已损坏” [英] PDF generated with itext becomes 'corrupted' when using SetSimpleColumn()

查看:1480
本文介绍了使用SetSimpleColumn()时,使用itext生成的PDF变为“已损坏”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



首先我想指出,stackowerflow过去曾帮助我解决很多问题,谢谢大家。但是现在我遇到了一个问题,我还没有提供解决方案,这让我发疯了。我不是母语为英语的人,对于任何语言错误都很抱歉。


First I would like to point out that stackowerflow helped me with many problems in the past, so thank you all. But now I have come to problem that I haven't fount a solution for yet and it's driving me crazy. I'm not native english speaker, so sorry for any language mistakes.

所以这就是:

我正在使用 itextsharp 库生成pdf(顺便说一下,这是一个很棒的库)。我开始使用某种pdf格式/模板,我正在添加填充数据。我正在使用 PdfReader 来读取模板pdf并通过调用 PdfStamper 方法 GetOverContent(pageNum) 我得到的单个页面 PdfContentByte 。使用 PdfContentByte 我正在添加我的文本/数据( BeginText EndText 用于每一页)。我用方法 ShowTextAligned 添加的大部分文本。一切都好,生成的pdf包含我的文本。问题开始于我必须添加'columned'文本。我使用以下代码执行此操作:

I'm generating pdf with itextsharp library(great library by the way). I'm starting with some kind of pdf form/template, to which i'm adding 'fill-out' data. I'm using PdfReader to read template pdf and by caling PdfStamper method GetOverContent(pageNum) for individual pages I get PdfContentByte. With that PdfContentByte I'm adding my text/data (BeginText and EndText is used on every page). Most of text I add with method ShowTextAligned. That all ok, generated pdf contains my text. The problem begins where i have to add 'columned' text. I do that with following code:

ColumnText ct = new ColumnText(cb);//cb is PdfContentByte
Phrase p = new Phrase(txt, FontFactory.GetFont(DEFAULT_FONT, BaseFont.CP1250, true, font_size));
ct.SetSimpleColumn(p, x, y, x+width, y+height, 10, alignment);
ct.Go();
setDefaultFont();//sets font to PdfContentByte again with setFontAndSize and SetColorFill

圆柱形文本添加此代码确定,但文本(在同一页面上/相同 PdfContentByte )在此后添加 ShowTextAligned 在Acrobat Reader中不可见。

Columned text is added with this code OK, but the text(on that same page/same PdfContentByte) added AFTER this with ShowTextAligned is not visible in Acrobat Reader.

这是'有趣'部分 - 用foxit阅读器打开的同一个pdf文件中的文字很好/可见/确定。

Here is the 'fun' part - that text in same pdf file opened with foxit reader is fine/visible/ok.

添加 ColumnText 后添加 ShowTextAligned 的文字是在acrobat阅读器中看不到,但在foxit阅读器中可以看到就好了。此问题存在于一个页面内,新页面重置此问题( PdfContentByte 下一页是新的)。

So text added with ShowTextAligned after adding ColumnText is not visible in acrobat reader but visible in foxit reader just fine. This problem exists inside one page, new page resets this problem (PdfContentByte for next page is new).

我的解决方法是在 ShowTextAligned的所有调用之后添加所有 ColumnText 。直到今天,当客户打印出使用acrobat reader生成的pdf时,在打印文档后,显示pdf包含错误的消息,并且应该联系pdf的作者。 Adobe Reader的版本是10.1.1。问题不在客户计算机上,同样的事情在我的计算机上。

My workaround for that was to add all ColumnText AFTER all calls of ShowTextAligned. That worked till today, when customer printed out generated pdf with acrobat reader, which after printing the document, displayed message that pdf contains error and that author of pdf should be contacted. Version of Adobe Reader is 10.1.1. Problem is not in customer computer, same thing hapens on my computer.

在研究网络后,我安装了Adobe Acrodat Pro试用版,其中包含Preflight工具,用于分析pdf(据我所知)。此工具输出警告操作员的内容状态流无效。在这里,我被困住了。我相信问题存在于添加的 ColumnText 中,因为没有它们生成的文档会导致显示/打印没有问题,预检状态找不到问题。

After researching the web I installed Adobe Acrodat Pro Trial which contains tool Preflight, which is purposed for analyzing pdfs (as far I understand). This tool outputs warning "Invalid content state stream for operator". And here I'm stucked. I belive the problem exists inside added ColumnText, because document generated without them causes no problem displaying/printing and Preflight states "No problem found".

有可能我错过了一些事实,问题出在我的代码中......

It is possible that i'm missing some fact and that the problem is in my code...

请帮助我,因为我' m runnig out of ideas。
我希望这篇文章能够在某一天帮助其他人解决同样的问题。
我无法附加示例pdf,因为它包含敏感数据,但如果没有其他方法,我将重新创建场景/代码。

Please help me, because i'm runnig out of ideas. I hope this post will help someday someone else with the same problem. I cannot attach sample pdf because it contains sensitive data, but if there is no other way, i'll recreate the scenario/code.

推荐答案

所以回答我的问题/问题:
当使用PdfContentByte写入pdf并使用方法 ShowTextAligned 时,你必须调用 BeginText 在写完之前和完成后你必须调用 EndText 。所以我做了。但是如果你想要添加一些其他元素(比如ColumnText,Image和其他任何东西),在调用 EndText 之前你不能这样做。如果你这样做,生成的pdf将有问题/损坏。

So to answer my question/problem: When writing to pdf using PdfContentByte and using method ShowTextAligned you have to call BeginText before writing and after you are finished you have to call EndText. So i did. BUT if you want to add some other element(like ColumnText, Image and probably anything else) you can't do that before you call EndText. If you do, generated pdf will be 'problematical'/corrupted.

所以在伪代码中跟随错误:

So in pseudocode following is wrong:

BeginText();
ShowtextAligned();
AddImage();
ShowtextAligned();
EndText();

正确用法是:

BeginText();
ShowtextAligned();
EndText();
AddImage();
BeginText();
ShowtextAligned();
EndText();

我希望有一天能在某个地方为某人提供帮助。

I hope this will help someone someday somewhere.

这篇关于使用SetSimpleColumn()时,使用itext生成的PDF变为“已损坏”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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