使用iTextSharp将大型隐藏文本/字符串存储到PDF [英] Store large hidden text/string to a PDF using iTextSharp

查看:225
本文介绍了使用iTextSharp将大型隐藏文本/字符串存储到PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在隐藏的某处隐藏PDF文档中存储一个大字符串。现在我有一个隐藏的文本字段,我正在写这个文本。问题是当字符串大小增加到 10MB 时,我开始得到 OutOfMemory 错误。

I want to store a large string in PDF document somewhere hidden. Right now I have a hidden text field in which I am writing that text. The problem is that when the string size increased upto 10MB I start getting OutOfMemory errors.

使用 iTextSharp 将一些大的隐藏字符串/文本存储到PDF文档的最佳方法是什么?该文本/字符串也应该在以后检索。

What will be the best way to store some large hidden string/text to PDF document using iTextSharp? That text/string should be retrieved later as well.

推荐答案

此类私人数据可以存储在 PieceInfo 字典,也参考 David回答OP的后续问题

Such private data can be stored in PieceInfo dictionaries, also cf. David's answer to the OP's follow-up question.

< a href =https://stackoverflow.com/a/18465343/1729265>旧答案使用iText库在pdf中插入隐藏的摘要的答案显示了如何使用 PieceInfo 一般使用iText / Java的字典(与iTextSharp / C#的差异在这里应该是最小的。)

This answer to the older question "Insert hidden digest in pdf using iText library" shows how to make use of PieceInfo dictionaries in general using iText/Java (differences to iTextSharp/C# should be minimal here).

当OP谈论10 MB及以上的数据时,他可能会想要使用PDF流而不是字符串。

As the OP talks about data 10 MB and up, he may want to use PDF streams instead of strings.

旧答案中提供的 DocumentPieceInfo 助手类可用于像这样的大数据的PDF流(再次在Java中,因为我主要生活在Java端,并且再次移植到C#应该很容易):

The DocumentPieceInfo helper class provided in that older answer can be used with PDF streams for BIG DATA like this (again in Java as I'm mostly living on the Java side, and again porting to C# should be easy):

PdfName appName = new PdfName("MYAPP");
PdfName dataName = new PdfName("BigData");

DocumentPieceInfo dpi = new DocumentPieceInfo();

PdfReader reader = new PdfReader(...);
PdfStamper stamper = new PdfStamper(reader, ...);

InputStream in = ... BIG DATA INPUT STREAM ...;
PdfStream stream = new PdfStream(in, stamper.getWriter());
stream.flateCompress();
PdfIndirectObject ref = stamper.getWriter().addToBody(stream);
stream.writeLength();
in.close();

dpi.addPieceInfo(reader, appName, dataName, ref.getIndirectReference());

stamper.close();



检索文档 PieceInfo 数据



Retrieving document PieceInfo data

PdfName appName = new PdfName("MYAPP");
PdfName dataName = new PdfName("BigData");

DocumentPieceInfo dpi = new DocumentPieceInfo();

PdfReader reader = new PdfReader("target/test-outputs/test-with-piece-info.pdf");

PdfObject myDataObject = dpi.getPieceInfo(reader, appName, dataName);
myDataObject = PdfReader.getPdfObject(myDataObject);
byte[] myData = PdfReader.getStreamBytes((PRStream)myDataObject)

这篇关于使用iTextSharp将大型隐藏文本/字符串存储到PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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