Search in sources :

Example 71 with PDDocument

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;
}
Also used : PDPage(com.tom_roush.pdfbox.pdmodel.PDPage) COSName(com.tom_roush.pdfbox.cos.COSName) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) AffineTransform(com.tom_roush.harmony.awt.geom.AffineTransform) PDResources(com.tom_roush.pdfbox.pdmodel.PDResources) PDFormXObject(com.tom_roush.pdfbox.pdmodel.graphics.form.PDFormXObject) PDRectangle(com.tom_roush.pdfbox.pdmodel.common.PDRectangle) PDAcroForm(com.tom_roush.pdfbox.pdmodel.interactive.form.PDAcroForm) PDSignatureField(com.tom_roush.pdfbox.pdmodel.interactive.form.PDSignatureField) PDStream(com.tom_roush.pdfbox.pdmodel.common.PDStream)

Example 72 with PDDocument

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();
}
Also used : PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) File(java.io.File)

Example 73 with PDDocument

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;
}
Also used : PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) TestRendering(com.tom_roush.pdfbox.rendering.TestRendering) File(java.io.File)

Example 74 with PDDocument

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);
}
Also used : PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) TestRendering(com.tom_roush.pdfbox.rendering.TestRendering) File(java.io.File) Test(org.junit.Test)

Example 75 with PDDocument

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);
}
Also used : PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) PDAnnotationWidget(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget) TestRendering(com.tom_roush.pdfbox.rendering.TestRendering) File(java.io.File) Test(org.junit.Test)

Aggregations

PDDocument (com.tom_roush.pdfbox.pdmodel.PDDocument)137 File (java.io.File)80 Test (org.junit.Test)69 PDPage (com.tom_roush.pdfbox.pdmodel.PDPage)37 PDPageContentStream (com.tom_roush.pdfbox.pdmodel.PDPageContentStream)22 InputStream (java.io.InputStream)21 Bitmap (android.graphics.Bitmap)18 IOException (java.io.IOException)14 PDResources (com.tom_roush.pdfbox.pdmodel.PDResources)11 PDFRenderer (com.tom_roush.pdfbox.rendering.PDFRenderer)11 ArrayList (java.util.ArrayList)11 PDDocumentCatalog (com.tom_roush.pdfbox.pdmodel.PDDocumentCatalog)10 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 FileInputStream (java.io.FileInputStream)9 FileOutputStream (java.io.FileOutputStream)9 COSArray (com.tom_roush.pdfbox.cos.COSArray)8 PDEmbeddedFile (com.tom_roush.pdfbox.pdmodel.common.filespecification.PDEmbeddedFile)8 PDFont (com.tom_roush.pdfbox.pdmodel.font.PDFont)8 COSDictionary (com.tom_roush.pdfbox.cos.COSDictionary)7 Paint (android.graphics.Paint)6