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...
}
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;
}
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");
}
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;
}
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;
}
}
Aggregations