这样的私有数据可以存储在PieceInfo字典中:
可以使用分页词典 (PDF 1.3) 来保存符合要求的私有产品数据。数据可以通过页面对象(参见表 30)或表单字典(参见表 95)中的可选PieceInfo条目与页面或表单 XObject 相关联。从 PDF 1.4 开始,私有数据也可以通过文档目录中的PieceInfo条目与 PDF 文档相关联(参见表 28)。
(ISO 32000-1第 14.5 节)
在您的情况下,文档目录中的PieceInfo似乎最合适。
DocumentPieceInfo
使用 iText,您可以在其中存储数据并使用下面的帮助程序类再次检索它们:
存储文档PieceInfo数据
PdfName appName = new PdfName("MYAPP");
PdfName dataName = new PdfName("Hash");
DocumentPieceInfo dpi = new DocumentPieceInfo();
PdfReader reader = new PdfReader(...);
dpi.addPieceInfo(reader, appName, dataName, new PdfString(data));
PdfStamper stamper = new PdfStamper(reader, ...);
stamper.close();
检索文档PieceInfo数据
PdfName appName = new PdfName("MYAPP");
PdfName dataName = new PdfName("Hash");
DocumentPieceInfo dpi = new DocumentPieceInfo();
PdfReader reader = new PdfReader("target/test-outputs/test-with-piece-info.pdf");
PdfObject myData = dpi.getPieceInfo(reader, appName, dataName);
DocumentPieceInfo
助手类
public class DocumentPieceInfo
{
static PdfName PIECE_INFO = new PdfName("PieceInfo");
static PdfName LAST_MODIFIED = new PdfName("LastModified");
static PdfName PRIVATE = new PdfName("Private");
void addPieceInfo(PdfReader reader, PdfName app, PdfName name, PdfObject value)
{
PdfDictionary catalog = reader.getCatalog();
PdfDictionary pieceInfo = catalog.getAsDict(PIECE_INFO);
if (pieceInfo == null)
{
pieceInfo = new PdfDictionary();
catalog.put(PIECE_INFO, pieceInfo);
}
PdfDictionary appData = pieceInfo.getAsDict(app);
if (appData == null)
{
appData = new PdfDictionary();
pieceInfo.put(app, appData);
}
PdfDictionary privateData = appData.getAsDict(PRIVATE);
if (privateData == null)
{
privateData = new PdfDictionary();
appData.put(PRIVATE, privateData);
}
appData.put(LAST_MODIFIED, new PdfDate());
privateData.put(name, value);
}
PdfObject getPieceInfo(PdfReader reader, PdfName app, PdfName name)
{
PdfDictionary catalog = reader.getCatalog();
PdfDictionary pieceInfo = catalog.getAsDict(PIECE_INFO);
if (pieceInfo == null)
return null;
PdfDictionary appData = pieceInfo.getAsDict(app);
if (appData == null)
return null;
PdfDictionary privateData = appData.getAsDict(PRIVATE);
if (privateData == null)
return null;
return privateData.get(name);
}
}
此类假定Private值是一个字典,而私有数据又存储在该字典中。不过,它可能是任何东西。要处理其他程序生成的私有数据,您可能需要一些变化。