我用 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();
}
那是样本:
我应该在该代码中添加什么?问题是,矩形(可见签名)上没有图像。