ITextSharp PdfCopy 使用示例
我正在尝试使用 ItextSharp 中的 PdfSmartCopy 但我在 c# 中找不到任何相关示例。
我的想法是,我有一个包含表单字段的 pdf,并且这些字段将 700kb 添加到 pdf 文档的大小。没有表单字段的原始文档为 100kb。 欢迎任何其他建议,尤其是持续减小 pdf 大小。
(我用adobe acrobat优化了生成的PDF,它把它减少到44kb。所以一定是某个地方出了问题。) 有什么办法可以减小PDF的大小吗?
编辑:FormFlatenning 没有帮助。 pdf 模板文件仅包含文本、线条和表格,没有图像。
这是我的代码片段
PdfReader reader = new PdfReader(GetTemplateBytes());
pst = new PdfStamper(reader, Response.OutputStream);
var acroFields = pst.AcroFields;
pst.FormFlattening = true;
pst.FreeTextFlattening = true;
SetFieldsInternal(acroFields);
pst.Close();
I am trying to use PdfSmartCopy from ItextSharp but I cannot find any relevant examples in c#.
The ideea is that I have a pdf containing form fields and the fields add 700kb to the size of the pdf document. The original document without form fields was 100kb.
Any other sugestions are welcome, especially o reduce the pdf size consistently.
(I optimised the generated PDF with adobe acrobat, and it reduced it to 44kb. So there must be a glitch somewhere.)
Is there any way to reduce the PDF size?
Edit: FormFlatenning doesn't help. The pdf template file contains only text, lines and tables, no images.
here's my code snippet
PdfReader reader = new PdfReader(GetTemplateBytes());
pst = new PdfStamper(reader, Response.OutputStream);
var acroFields = pst.AcroFields;
pst.FormFlattening = true;
pst.FreeTextFlattening = true;
SetFieldsInternal(acroFields);
pst.Close();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
下面是一个 2008 VB.Net 示例,使用 ITextSharp PDFCopy 将多个 PDF 文件复制到 1 个多页 PDF 文件中。这将复制除底层链接之外的所有内容。它似乎完美地复制了所有注释,至少我找不到任何它没有复制的注释。
注意:您的项目中必须引用 ITextSharp。
输入参数:
fileArray – pdf 文件数组。
outPutPDF – 输出多页 PDF 文档的完整路径和名称。
Here is a 2008 VB.Net example of using ITextSharp PDFCopy to copy multiple PDF files into 1 multi-page PDF file. This will copy everything except underlying links. It appears to copy all annotations perfectly, at least I could not find any it did not copy.
Note: You must have ITextSharp referenced in your project.
Input Parameters:
fileArray – an array of pdf files.
outPutPDF – full path and name to output multipage PDF document.
在调用
pst.close()
之前调用reader.removeUnusedObjects()
...无需展平。要缩小更多内容,您可以使用
pst.setFullCompression()
。 YMMV。编辑:就示例而言,我建议使用 iText in Action,第二版。那里有很多各种各样的例子,包括 PdfCopy 和 PdfCopy。 PDF智能复制。本书中的所有代码示例都可以在线获取。
买书我不挣钱,但通过无数次网上互动认识了作者,并把他当朋友。
Call
reader.removeUnusedObjects()
before callingpst.close()
... no need for flattening.To shrink things a little more you can
pst.setFullCompression()
. YMMV.EDIT: As far as examples goes, I recommend getting iText in Action, 2nd edition. Lots of examples of all sorts of things in there, including PdfCopy & PdfSmartCopy. All the code samples from the book are available on line.
I don't make any money if you buy the book, but know the author from numerous online interactions, and consider him a friend.