使用 iText 库在 pdf 中插入隐藏的摘要 [英] Insert hidden digest in pdf using iText library

查看:50
本文介绍了使用 iText 库在 pdf 中插入隐藏的摘要的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I search a method for insert a digest (byte array or String) into PDF file using iText library (Java). I create the digest from a String with this method:

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 中应该看不到摘要,但必须将其提取出来进行比较.

The digest should be not seen in PDF, but it must be exctracted for comparison.

推荐答案

这样的私有数据可以存储在PieceInfo字典中:

Such private data can be stored in PieceInfo dictionaries:

分页词典 (PDF 1.3) 可用于保存符合要求的私有产品数据.数据可以通过页面对象(参见表 30)或表单字典(参见表 95)中的可选 PieceInfo 条目与页面或表单 XObject 相关联.从 PDF 1.4 开始,私有数据也可以通过文档目录中的 PieceInfo 条目与 PDF 文档相关联(参见表 28).

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).

(第 14.5 节ISO 32000-1)

在您的情况下,文档目录中的 PieceInfo 似乎最合适.

In your case the PieceInfo in the document catalogue seem most apropos.

使用 iText,您可以将数据存储在那里,然后使用下面的 DocumentPieceInfo 辅助类再次检索它们:

Using iText you can store data there and retrieve them back again like this using the DocumentPieceInfo helper class below:

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 值是一个字典,其中存储了私有数据.不过,它可能是任何东西.要处理其他程序生成的私人数据,您可能需要进行一些更改.

This class assumes the Private value to be a dictionary in which in turn the private data are stored. It may be anything, though. To process the private data generated by other programs, you may need some variation.

这篇关于使用 iText 库在 pdf 中插入隐藏的摘要的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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