use of com.tom_roush.pdfbox.pdmodel.PDDocument in project PdfBox-Android by TomRoush.
the class PDFTemplateCreator method buildPDF.
/**
* Build a PDF with a visible signature step by step, and return it as a stream.
*
* @param properties
* @return InputStream
* @throws IOException
*/
public InputStream buildPDF(PDVisibleSignDesigner properties) throws IOException {
Log.i("PdfBox-Android", "pdf building has been started");
PDFTemplateStructure pdfStructure = pdfBuilder.getStructure();
// we create array of [Text, ImageB, ImageC, ImageI]
pdfBuilder.createProcSetArray();
// create page
pdfBuilder.createPage(properties);
PDPage page = pdfStructure.getPage();
// create template
pdfBuilder.createTemplate(page);
PDDocument template = pdfStructure.getTemplate();
// create /AcroForm
pdfBuilder.createAcroForm(template);
PDAcroForm acroForm = pdfStructure.getAcroForm();
// AcroForm contains signature fields
pdfBuilder.createSignatureField(acroForm);
PDSignatureField pdSignatureField = pdfStructure.getSignatureField();
// create signature
// TODO
// The line below has no effect with the CreateVisibleSignature example.
// The signature field is needed as a "holder" for the /AP tree,
// but the /P and /V PDSignatureField entries are ignored by PDDocument.addSignature
pdfBuilder.createSignature(pdSignatureField, page, "");
// that is /AcroForm/DR entry
pdfBuilder.createAcroFormDictionary(acroForm, pdSignatureField);
// create AffineTransform
pdfBuilder.createAffineTransform(properties.getTransform());
AffineTransform transform = pdfStructure.getAffineTransform();
// rectangle, formatter, image. /AcroForm/DR/XObject contains that form
pdfBuilder.createSignatureRectangle(pdSignatureField, properties);
pdfBuilder.createFormatterRectangle(properties.getFormatterRectangleParameters());
PDRectangle bbox = pdfStructure.getFormatterRectangle();
pdfBuilder.createSignatureImage(template, properties.getImage());
// create form stream, form and resource.
pdfBuilder.createHolderFormStream(template);
PDStream holderFormStream = pdfStructure.getHolderFormStream();
pdfBuilder.createHolderFormResources();
PDResources holderFormResources = pdfStructure.getHolderFormResources();
pdfBuilder.createHolderForm(holderFormResources, holderFormStream, bbox);
// that is /AP entry the appearance dictionary.
pdfBuilder.createAppearanceDictionary(pdfStructure.getHolderForm(), pdSignatureField);
// inner form stream, form and resource (holder form contains inner form)
pdfBuilder.createInnerFormStream(template);
pdfBuilder.createInnerFormResource();
PDResources innerFormResource = pdfStructure.getInnerFormResources();
pdfBuilder.createInnerForm(innerFormResource, pdfStructure.getInnerFormStream(), bbox);
PDFormXObject innerForm = pdfStructure.getInnerForm();
// inner form must be in the holder form as we wrote
pdfBuilder.insertInnerFormToHolderResources(innerForm, holderFormResources);
// Image form is in this structure: /AcroForm/DR/FRM/Resources/XObject/n2
pdfBuilder.createImageFormStream(template);
PDStream imageFormStream = pdfStructure.getImageFormStream();
pdfBuilder.createImageFormResources();
PDResources imageFormResources = pdfStructure.getImageFormResources();
pdfBuilder.createImageForm(imageFormResources, innerFormResource, imageFormStream, bbox, transform, pdfStructure.getImage());
pdfBuilder.createBackgroundLayerForm(innerFormResource, bbox);
// now inject procSetArray
pdfBuilder.injectProcSetArray(innerForm, page, innerFormResource, imageFormResources, holderFormResources, pdfStructure.getProcSet());
COSName imageFormName = pdfStructure.getImageFormName();
COSName imageName = pdfStructure.getImageName();
COSName innerFormName = pdfStructure.getInnerFormName();
// now create Streams of AP
pdfBuilder.injectAppearanceStreams(holderFormStream, imageFormStream, imageFormStream, imageFormName, imageName, innerFormName, properties);
pdfBuilder.createVisualSignature(template);
pdfBuilder.createWidgetDictionary(pdSignatureField, holderFormResources);
InputStream in = getVisualSignatureAsStream(pdfStructure.getVisualSignature());
Log.i("PdfBox-Android", "stream returning started, size= " + in.available());
// we must close the document
template.close();
// return result of the stream
return in;
}
use of com.tom_roush.pdfbox.pdmodel.PDDocument in project PdfBox-Android by TomRoush.
the class PDVisibleSignDesigner method calculatePageSizeFromFile.
private void calculatePageSizeFromFile(String filename, int page) throws IOException {
// create PD document
PDDocument document = PDDocument.load(new File(filename));
// calculate height and width of document page
calculatePageSize(document, page);
document.close();
}
use of com.tom_roush.pdfbox.pdmodel.PDDocument in project PdfBox-Android by TomRoush.
the class PDAcroFormFlattenTest method flattenAndCompare.
/*
* Flatten and compare with generated image samples.
*/
private static boolean flattenAndCompare(String sourceUrl, String targetFileName) throws IOException {
generateSamples(sourceUrl, targetFileName);
File inputFile = new File(IN_DIR, targetFileName);
File outputFile = new File(OUT_DIR, targetFileName);
PDDocument testPdf = PDDocument.load(inputFile);
testPdf.getDocumentCatalog().getAcroForm().flatten();
testPdf.setAllSecurityToBeRemoved(true);
assertTrue(testPdf.getDocumentCatalog().getAcroForm().getFields().isEmpty());
testPdf.save(outputFile);
testPdf.close();
// compare rendering
TestRendering testRendering = new TestRendering();
testRendering.setUp();
testRendering.render(outputFile);
removeAllRenditions(inputFile);
inputFile.delete();
outputFile.delete();
return true;
}
use of com.tom_roush.pdfbox.pdmodel.PDDocument in project PdfBox-Android by TomRoush.
the class PDAcroFormInstrumentationTest method testFlatten.
@Test
public void testFlatten() throws IOException {
PDDocument testPdf = PDDocument.load(testContext.getAssets().open(IN_DIR + "/" + "AlignmentTests.pdf"));
testPdf.getDocumentCatalog().getAcroForm().flatten();
assertTrue(testPdf.getDocumentCatalog().getAcroForm().getFields().isEmpty());
File file = new File(OUT_DIR, "AlignmentTests-flattened.pdf");
testPdf.save(file);
// compare rendering
TestRendering testRendering = new TestRendering();
testRendering.setUp();
testRendering.render(file);
}
use of com.tom_roush.pdfbox.pdmodel.PDDocument in project PdfBox-Android by TomRoush.
the class PDAcroFormInstrumentationTest method testFlattenWidgetNoRef.
/*
* Same as above but remove the page reference from the widget annotation
* before doing the flatten() to ensure that the widgets page reference is properly looked up
* (PDFBOX-3301)
*/
@Test
public void testFlattenWidgetNoRef() throws IOException {
PDDocument testPdf = PDDocument.load(testContext.getAssets().open(IN_DIR + "/" + "AlignmentTests.pdf"));
PDAcroForm acroForm = testPdf.getDocumentCatalog().getAcroForm();
for (PDField field : acroForm.getFieldTree()) {
for (PDAnnotationWidget widget : field.getWidgets()) {
widget.getCOSObject().removeItem(COSName.P);
}
}
testPdf.getDocumentCatalog().getAcroForm().flatten();
// 36 non widget annotations shall not be flattened
assertEquals(36, testPdf.getPage(0).getAnnotations().size());
assertTrue(testPdf.getDocumentCatalog().getAcroForm().getFields().isEmpty());
File file = new File(OUT_DIR, "AlignmentTests-flattened-noRef.pdf");
testPdf.save(file);
// compare rendering
TestRendering testRendering = new TestRendering();
testRendering.setUp();
testRendering.render(file);
}
Aggregations