0

我用 pdfbox 创建了 pdf(使用 PDResources、PDXObjectForm、PDAppearanceDictionary 等)。我在 pdf 上有 Visible 签名。

当我看到pdf时,我错过了一些流。

4 0 obj
<</Type /XObject//Resources <</ProcSet [/PDF /Text /ImageB /ImageC /ImageI]/XObject <</n0 9 0 R/n1 10 0 R>>>>/BBox [0 0 100 100]/FormType 1/Length 11 0 R>>
stream

endstream
endobj

8 0 obj
<</Type /XObject/Subtype /Form/Resources <</XObject <</FRM0 4 0 R >>/ProcSet [/PDF /Text /ImageB /ImageC /ImageI]>>/BBox [0 0 100 100]/Matrix [1 0 0 1 0 0]/FormType 1/Length 13 0 R>>
stream

endstream
endobj

pdf结构(XObjects)创建得很好。但我不知道这里应该有什么流。或者如何创建它?或者应该有哪些流?

PDDocument template = new PDDocument();



    try {

           ...

        PDStream formstream = new PDStream(template);
        OutputStream os = formstream.createOutputStream();

        PDXObjectForm form = new PDXObjectForm(formstream);
        PDResources res = new PDResources();
        form.setResources(res);
        form.setBBox(formrect);
        form.setMatrix(transform);
        form.setFormType(1);

        PDAppearanceStream dic = new PDAppearanceStream(form.getCOSStream());
        PDAppearanceDictionary appearance = new PDAppearanceDictionary();
        appearance.setNormalAppearance(dic);
        appearance.getCOSObject().setDirect(true);


        PDRectangle innerRect = new PDRectangle();
        innerRect.setUpperRightX(100);
        innerRect.setUpperRightY(100);
        innerRect.setLowerLeftX(0);
        innerRect.setLowerLeftY(0);

        PDStream formStream2 = new PDStream(template);
        PDXObjectForm innerForm = new PDXObjectForm(formStream2);


        OutputStream os1 = formStream2.createOutputStream();
        PDResources innerResources = new PDResources();
        innerForm.setResources(innerResources);
        innerForm.setBBox(innerRect);
        innerForm.setFormType(1);

        innerForm.getResources().getCOSDictionary().setItem(COSName.PROC_SET, defaultRes);
        res.addXObject(innerForm, "FRM");

        PDStream DSStream = new PDStream(template);
        OutputStream os2 = DSStream.createOutputStream();
        PDXObjectForm dsForm = new PDXObjectForm(DSStream);

        dsForm.setBBox(formrect);
        dsForm.setMatrix(transform);

        PDResources dsRes = new PDResources();
        dsForm.setResources(dsRes);
        dsForm.setFormType(1);


        PDFont firstFont = PDTrueTypeFont.loadTTF(template, new File("/MyFont.ttf"));
        PDFont secondFont = PDTrueTypeFont.loadTTF(template, new File("/MyFont.TTF"));
        secondFont.setFontEncoding(new WinAnsiEncoding());
        firstFont.setFontEncoding(new WinAnsiEncoding());

        Map<String, PDFont> fonts = new HashMap<String, PDFont>();

        PDStream imageFormStream = new PDStream(template);
        OutputStream os3 = imageFormStream.createOutputStream();
        PDXObjectForm imageForm = new PDXObjectForm(imageFormStream);

        imageForm.setBBox(formrect);
        imageForm.setMatrix(transform);
        PDResources imgRes = new PDResources();
            imgRes.getCOSObject().setDirect(true);
        imgRes.addFont(firstFont);
        imageForm.setResources(imgRes);

        imageForm.setFormType(1);
        innerResources.addXObject(dsForm, "n");
        innerResources.addXObject(imageForm, "n");

        imgRes.addXObject(jpeg, "img");

        appendRawCommands(os2, "% DSBlank");
        appendRawCommands(os3, "q 101 0 0 24.53 2 12.24 cm /img0 Do Q");
        appendRawCommands(os3, "\n");
        appendRawCommands(os3, "BT");
        appendRawCommands(os3, "\n");
        appendRawCommands(os3, "1 0 0 1 2 33 Tm");
        appendRawCommands(os3, "\n");
        appendRawCommands(os3, "101 0 Td");
        appendRawCommands(os3, "\n");
        appendRawCommands(os3, "-101 0 Td");
        appendRawCommands(os3, "\n");
        appendRawCommands(os3, "ET");
        appendRawCommands(os3, "\n");
        appendRawCommands(os3, "BT");
        appendRawCommands(os3, "\n");
        appendRawCommands(os3, "1 0 0 1 106 37 Tm");
        appendRawCommands(os3, "\n");
        appendRawCommands(os3, "/F1 12 Tf");
        appendRawCommands(os3, "\n");
        appendRawCommands(os3, "(TEXT HERE)Tj");
        appendRawCommands(os3, "\n");
        appendRawCommands(os3, "1 0 0 1 106 25 Tm");
        appendRawCommands(os3, "\n");
        appendRawCommands(os3, "(10 11:12:13)Tj");
        appendRawCommands(os3, "\n");
        appendRawCommands(os3, "1 0 0 1 106 13 Tm");
        appendRawCommands(os3, "\n");
        appendRawCommands(os3, "(GMT+06:00 2013)Tj");
        appendRawCommands(os3, "\n");
        appendRawCommands(os3, "ET");

        os1.close();
        os.close();
        os3.close();
        os2.close();

        sigField.getWidget().setAppearance(appearance);
        COSDictionary widgetDict = sigField.getWidget().getDictionary();
        widgetDict.setNeedToBeUpdate(true);

        defaultRes.setDirect(true);
        widgetDict.setItem(COSName.DR, res.getCOSObject());

        PDResources resSecondForAcro = new PDResources();
        resSecondForAcro.addXObject(innerForm, "FRM");

        resSecondForAcro.addFont(secondFont);            

        COSBase cosBase=resSecondForAcro.getCOSObject();
        cosBase.setDirect(true);
        acroFormDC.setItem(COSName.DR, cosBase);

        COSDocument visualSignature = template.getDocument();
                    ...

        COSWriter wr = new COSWriter(new FileOutputStream("/new.pdf"));
        wr.write(visualSignature);
        wr.close();
        template.close();

    } finally {
        template.close();
    }

那是样本:

关联

我应该在该代码中添加什么?问题是,矩形(可见签名)上没有图像。

4

1 回答 1

0

您将命令添加到os2os3但既不添加os也不添加os1.

您可能想添加一些类似的东西

appendRawCommands(os, "q 1 0 0 1 0 0 cm /FRM0 Do Q\n");
appendRawCommands(os1, "q 1 0 0 1 0 0 cm /n0 Do Q\n");
appendRawCommands(os1, "q 1 0 0 1 0 0 cm /n1 Do Q\n");

话虽这么说,为什么要使用如此复杂的结构。如果您使用 Adob​​e 在通过激活或停用此类 xobject 来说明签名状态时使用的 xobject 名称(即/FRM/n0/n2;仅在 Acrobat 6 之前还有其他层),我可以了解,但是您使用不同的名称/FRM0/n1,只有/n0是相同的。因此,您可以立即使用带有图像和文本的流作为正常外观。

如果您想支持旧的分层外观,您应该更准确地遵守 Adob​​e® 的 Acrobat® SDK数字签名外观纸的符号。

PS:看起来一个人不应该PDStream OutputStreams同时打开多个这样的。相反,应该检索这样一个输出流,写入它,关闭它,然后才继续对 PDF 对象进行其他操作。

于 2013-07-08T16:31:46.150 回答