gpt4 book ai didi

java - 使用 iText 库在 pdf 中插入隐藏摘要

转载 作者:行者123 更新时间:2023-12-01 18:44:25 25 4
gpt4 key购买 nike

我搜索一种使用 iText 库 (Java) 将摘要(字节数组或字符串)插入 PDF 文件的方法。我使用以下方法从字符串创建摘要:

private String crypt(double x, ByteArrayOutputStream baos) throws UnsupportedEncodingException, NoSuchAlgorithmException{
MessageDigest md = MessageDigest.getInstance("SHA-256");
md.update(String.valueOf(x).getBytes("UTF-8"));
md.update(String.valueOf(baos).getBytes("UTF-8"));
byte[] digest = md.digest();

StringBuffer sb = new StringBuffer();
for(byte d:digest){
sb.append(Integer.toHexString(0xFF & d));
}
return sb.toString();
}

摘要不应在 PDF 中看到,但必须将其提取出来进行比较。

最佳答案

此类私有(private)数据可以存储在PieceInfo字典中:

A page-piece dictionary (PDF 1.3) may be used to hold private conforming product data. The data may be associated with a page or form XObject by means of the optional PieceInfo entry in the page object (see Table 30) or form dictionary (see Table 95). Beginning with PDF 1.4, private data may also be associated with the PDF document by means of the PieceInfo entry in the document catalogue (see Table 28).

(section 14.5 of ISO 32000-1)

就您而言,文档目录中的PieceInfo似乎最合适。

使用 iText,您可以在其中存储数据并使用下面的 DocumentPieceInfo 帮助器类再次检索它们:

存储文档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值是一个字典,其中存储了私有(private)数据。不过,它可能是任何东西。要处理其他程序生成的私有(private)数据,您可能需要一些变化。

关于java - 使用 iText 库在 pdf 中插入隐藏摘要,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18462211/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com