Search in sources :

Example 16 with PDResources

use of com.tom_roush.pdfbox.pdmodel.PDResources in project PdfBox-Android by TomRoush.

the class PDDefaultAppearanceString method copyNeededResourcesTo.

/**
 * Copies any needed resources from the document’s DR dictionary into the stream’s Resources
 * dictionary. Resources with the same name shall be left intact.
 */
void copyNeededResourcesTo(PDAppearanceStream appearanceStream) throws IOException {
    // make sure we have resources
    PDResources streamResources = appearanceStream.getResources();
    if (streamResources == null) {
        streamResources = new PDResources();
        appearanceStream.setResources(streamResources);
    }
    if (streamResources.getFont(fontName) == null) {
        streamResources.put(fontName, getFont());
    }
// todo: other kinds of resource...
}
Also used : PDResources(com.tom_roush.pdfbox.pdmodel.PDResources)

Example 17 with PDResources

use of com.tom_roush.pdfbox.pdmodel.PDResources 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 18 with PDResources

use of com.tom_roush.pdfbox.pdmodel.PDResources in project PdfBox-Android by TomRoush.

the class PDVisibleSigBuilder method createBackgroundLayerForm.

@Override
public void createBackgroundLayerForm(PDResources innerFormResource, PDRectangle bbox) throws IOException {
    // create blank n0 background layer form
    PDFormXObject n0Form = new PDFormXObject(pdfStructure.getTemplate().getDocument().createCOSStream());
    n0Form.setBBox(bbox);
    n0Form.setResources(new PDResources());
    n0Form.setFormType(1);
    innerFormResource.put(COSName.getPDFName("n0"), n0Form);
    Log.i("PdfBox-Android", "Created background layer form");
}
Also used : PDFormXObject(com.tom_roush.pdfbox.pdmodel.graphics.form.PDFormXObject) PDResources(com.tom_roush.pdfbox.pdmodel.PDResources)

Example 19 with PDResources

use of com.tom_roush.pdfbox.pdmodel.PDResources in project PdfBox-Android by TomRoush.

the class PDAcroForm method getDefaultResources.

/**
 * This will get the default resources for the AcroForm.
 *
 * @return The default resources or null if there is none.
 */
public PDResources getDefaultResources() {
    PDResources retval = null;
    COSBase base = dictionary.getDictionaryObject(COSName.DR);
    if (base instanceof COSDictionary) {
        retval = new PDResources((COSDictionary) base, document.getResourceCache());
    }
    return retval;
}
Also used : COSDictionary(com.tom_roush.pdfbox.cos.COSDictionary) PDResources(com.tom_roush.pdfbox.pdmodel.PDResources) COSBase(com.tom_roush.pdfbox.cos.COSBase)

Example 20 with PDResources

use of com.tom_roush.pdfbox.pdmodel.PDResources in project PdfBox-Android by TomRoush.

the class PDAcroForm method resolveNeedsScaling.

/**
 * Check if there needs to be a scaling transformation applied.
 *
 * @param annotation
 * @param rotation
 * @return the need for a scaling transformation.
 */
private boolean resolveNeedsScaling(PDAnnotation annotation, int rotation) {
    PDAppearanceStream appearanceStream = annotation.getNormalAppearanceStream();
    // Check if there is a transformation within the XObjects content
    PDResources resources = appearanceStream.getResources();
    if (resources != null && resources.getXObjectNames().iterator().hasNext()) {
        return true;
    }
    PDRectangle bbox = appearanceStream.getBBox();
    PDRectangle fieldRect = annotation.getRectangle();
    if (rotation == 90 || rotation == 270) {
        return Float.compare(bbox.getWidth(), fieldRect.getHeight()) != 0 || Float.compare(bbox.getHeight(), fieldRect.getWidth()) != 0;
    } else {
        return Float.compare(bbox.getWidth(), fieldRect.getWidth()) != 0 || Float.compare(bbox.getHeight(), fieldRect.getHeight()) != 0;
    }
}
Also used : PDAppearanceStream(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAppearanceStream) PDResources(com.tom_roush.pdfbox.pdmodel.PDResources) PDRectangle(com.tom_roush.pdfbox.pdmodel.common.PDRectangle)

Aggregations

PDResources (com.tom_roush.pdfbox.pdmodel.PDResources)44 PDPage (com.tom_roush.pdfbox.pdmodel.PDPage)13 PDRectangle (com.tom_roush.pdfbox.pdmodel.common.PDRectangle)13 PDDocument (com.tom_roush.pdfbox.pdmodel.PDDocument)11 COSName (com.tom_roush.pdfbox.cos.COSName)9 Matrix (com.tom_roush.pdfbox.util.Matrix)9 IOException (java.io.IOException)8 PDFont (com.tom_roush.pdfbox.pdmodel.font.PDFont)7 PDFormXObject (com.tom_roush.pdfbox.pdmodel.graphics.form.PDFormXObject)7 File (java.io.File)7 COSDictionary (com.tom_roush.pdfbox.cos.COSDictionary)6 COSBase (com.tom_roush.pdfbox.cos.COSBase)5 PDPageContentStream (com.tom_roush.pdfbox.pdmodel.PDPageContentStream)5 AffineTransform (com.tom_roush.harmony.awt.geom.AffineTransform)4 PDAppearanceContentStream (com.tom_roush.pdfbox.pdmodel.PDAppearanceContentStream)4 PDColor (com.tom_roush.pdfbox.pdmodel.graphics.color.PDColor)4 PDGraphicsState (com.tom_roush.pdfbox.pdmodel.graphics.state.PDGraphicsState)4 PDAppearanceStream (com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAppearanceStream)4 PDDocumentCatalog (com.tom_roush.pdfbox.pdmodel.PDDocumentCatalog)3 PDStream (com.tom_roush.pdfbox.pdmodel.common.PDStream)3