错过了PDF格式(PDFBOX)流? [英] missed stream in pdf (pdfbox)?

查看:264
本文介绍了错过了PDF格式(PDFBOX)流?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建PDF与PDFBOX(使用PDResources,PDXObjectForm,PDAppearanceDictionary等)。我对PDF可见签名。

I've create pdf with pdfbox (using PDResources, PDXObjectForm, PDAppearanceDictionary and so on). I have Visible signature on pdf.

当我看的PDF,我已经错过了一些流。

when I see the pdf, I have missed some stream.

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结构(XObject对象)创建良好。但我不知道是什么流应该在这里。或如何创建?还是应该有什么流?

pdf structure (XObjects) is created well. but I don't know what streams should be here. or how to create that? or what streams should there be?

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();
    }

这是示例:

链接

我应该怎么添加到code? 问题是,没有关于矩形没有图像(上可见签名)。

what should I add to that code? the problem is that , there is no image on Rectangle ( on visible signature).

推荐答案

您添加命令 OS2 OS3 但既不操作系统也没有 OS1。

You add commands to os2 and os3 but neither to os nor to os1.

您可能要添加的东西沿着

You might want to add something along the lines of

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");

这是说,你为什么用这么复杂的结构。如果你使用它的Adobe曾经使用时,启用或关闭这种形式XObject对象(即 / FRM / N0 所示的签名状态了XObject名/ N2 ;的Acrobat 6只前还有其他层,也是如此),我能理解,但是你用不同的名字 / FRM0 / N1 ,只有< STRONG> / N0 是相同的。因此,你可以,而不是立即使用您的流,图像和文字为正常的外观。

This being said, why do you use such a complicated structure. If you used the xobject names which Adobe used to use when it illustrated the signature state by activating or deactivating such form xobjects (i.e. /FRM, /n0, and /n2; only before Acrobat 6 there were other layers, too), I could understand, but you use different names /FRM0 and /n1, only /n0 is identical. Thus, you could instead immediately use your stream with image and text as normal appearance.

如果你想支持旧的分层出场,你应该更准确​​地保持使用Adobe®的ACROBAT®SDK 数字签名外观纸。

If you want to support the old layered appearances, you should more accurately keep to the notations of Adobe®'s Acrobat® SDK Digital Signature Appearances paper.

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

PS: It looks like one should not have multiple such PDStream OutputStreams open simultaneously. Instead one should retrieve such an output stream, write to it, close it, and only then continue with other manipulations of the PDF objects.

这篇关于错过了PDF格式(PDFBOX)流?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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