Java Code Examples for org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject#setBBox()

The following examples show how to use org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject#setBBox() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PDVisibleSigBuilder.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
@Override
public void createImageForm(PDResources imageFormResources, PDResources innerFormResource,
                            PDStream imageFormStream, PDRectangle bbox, AffineTransform at,
                            PDImageXObject img) throws IOException
{
    PDFormXObject imageForm = new PDFormXObject(imageFormStream);
    imageForm.setBBox(bbox);
    imageForm.setMatrix(at);
    imageForm.setResources(imageFormResources);
    imageForm.setFormType(1);

    imageFormResources.getCOSObject().setDirect(true);

    COSName imageFormName = COSName.getPDFName("n2");
    innerFormResource.put(imageFormName, imageForm);
    COSName imageName = imageFormResources.add(img, "img");
    pdfStructure.setImageForm(imageForm);
    pdfStructure.setImageFormName(imageFormName);
    pdfStructure.setImageName(imageName);
    LOG.info("Created image form");
}
 
Example 2
Source File: PDVisibleSigBuilder.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public void createHolderForm(PDResources holderFormResources, PDStream holderFormStream,
                             PDRectangle bbox)
{
    PDFormXObject holderForm = new PDFormXObject(holderFormStream);
    holderForm.setResources(holderFormResources);
    holderForm.setBBox(bbox);
    holderForm.setFormType(1);
    pdfStructure.setHolderForm(holderForm);
    LOG.info("Holder form has been created");

}
 
Example 3
Source File: PDVisibleSigBuilder.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public void createInnerForm(PDResources innerFormResources,
                            PDStream innerFormStream,
                            PDRectangle bbox)
{
    PDFormXObject innerForm = new PDFormXObject(innerFormStream);
    innerForm.setResources(innerFormResources);
    innerForm.setBBox(bbox);
    innerForm.setFormType(1);
    pdfStructure.setInnerForm(innerForm);
    LOG.info("Another form (inner form - it will be inside holder form) has been created");
}
 
Example 4
Source File: PDVisibleSigBuilder.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
@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.info("Created background layer form");
}
 
Example 5
Source File: Overlay.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
private COSName createOverlayXObject(PDPage page, LayoutPage layoutPage)
{
    PDFormXObject xobjForm = new PDFormXObject(layoutPage.overlayContentStream);
    xobjForm.setResources(new PDResources(layoutPage.overlayResources));
    xobjForm.setFormType(1);
    xobjForm.setBBox(layoutPage.overlayMediaBox.createRetranslatedRectangle());
    AffineTransform at = new AffineTransform();
    switch (layoutPage.overlayRotation)
    {
        case 90:
            at.translate(0, layoutPage.overlayMediaBox.getWidth());
            at.rotate(Math.toRadians(-90));
            break;
        case 180:
            at.translate(layoutPage.overlayMediaBox.getWidth(), layoutPage.overlayMediaBox.getHeight());
            at.rotate(Math.toRadians(-180));
            break;
        case 270:
            at.translate(layoutPage.overlayMediaBox.getHeight(), 0);
            at.rotate(Math.toRadians(-270));
            break;
        default:
            break;
    }
    xobjForm.setMatrix(at);
    PDResources resources = page.getResources();
    return resources.add(xobjForm, "OL");
}
 
Example 6
Source File: LayerUtility.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
/**
 * Imports a page from some PDF file as a Form XObject so it can be placed on another page
 * in the target document.
 * <p>
 * You may want to call {@link #wrapInSaveRestore(PDPage) wrapInSaveRestore(PDPage)} before invoking the Form XObject to
 * make sure that the graphics state is reset.
 * 
 * @param sourceDoc the source PDF document that contains the page to be copied
 * @param page the page in the source PDF document to be copied
 * @return a Form XObject containing the original page's content
 * @throws IOException if an I/O error occurs
 */
public PDFormXObject importPageAsForm(PDDocument sourceDoc, PDPage page) throws IOException
{
    importOcProperties(sourceDoc);

    PDStream newStream = new PDStream(targetDoc, page.getContents(), COSName.FLATE_DECODE);
    PDFormXObject form = new PDFormXObject(newStream);

    //Copy resources
    PDResources pageRes = page.getResources();
    PDResources formRes = new PDResources();
    cloner.cloneMerge(pageRes, formRes);
    form.setResources(formRes);

    //Transfer some values from page to form
    transferDict(page.getCOSObject(), form.getCOSObject(), PAGE_TO_FORM_FILTER, true);

    Matrix matrix = form.getMatrix();
    AffineTransform at = matrix.createAffineTransform();
    PDRectangle mediaBox = page.getMediaBox();
    PDRectangle cropBox = page.getCropBox();
    PDRectangle viewBox = (cropBox != null ? cropBox : mediaBox);

    //Handle the /Rotation entry on the page dict
    int rotation = page.getRotation();

    //Transform to FOP's user space
    //at.scale(1 / viewBox.getWidth(), 1 / viewBox.getHeight());
    at.translate(mediaBox.getLowerLeftX() - viewBox.getLowerLeftX(),
            mediaBox.getLowerLeftY() - viewBox.getLowerLeftY());
    switch (rotation)
    {
    case 90:
        at.scale(viewBox.getWidth() / viewBox.getHeight(), viewBox.getHeight() / viewBox.getWidth());
        at.translate(0, viewBox.getWidth());
        at.rotate(-Math.PI / 2.0);
        break;
    case 180:
        at.translate(viewBox.getWidth(), viewBox.getHeight());
        at.rotate(-Math.PI);
        break;
    case 270:
        at.scale(viewBox.getWidth() / viewBox.getHeight(), viewBox.getHeight() / viewBox.getWidth());
        at.translate(viewBox.getHeight(), 0);
        at.rotate(-Math.PI * 1.5);
        break;
    default:
        //no additional transformations necessary
    }
    //Compensate for Crop Boxes not starting at 0,0
    at.translate(-viewBox.getLowerLeftX(), -viewBox.getLowerLeftY());
    if (!at.isIdentity())
    {
        form.setMatrix(at);
    }

    BoundingBox bbox = new BoundingBox();
    bbox.setLowerLeftX(viewBox.getLowerLeftX());
    bbox.setLowerLeftY(viewBox.getLowerLeftY());
    bbox.setUpperRightX(viewBox.getUpperRightX());
    bbox.setUpperRightY(viewBox.getUpperRightY());
    form.setBBox(new PDRectangle(bbox));

    return form;
}
 
Example 7
Source File: NativePdfBoxVisibleSignatureDrawer.java    From dss with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public void draw() throws IOException {
	try (PDDocument doc = new PDDocument(); ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
		
		PDPage originalPage = document.getPage(parameters.getPage() - 1);
		SignatureFieldDimensionAndPositionBuilder dimensionAndPositionBuilder = 
				new SignatureFieldDimensionAndPositionBuilder(parameters, originalPage, pdFont);
		SignatureFieldDimensionAndPosition dimensionAndPosition = dimensionAndPositionBuilder.build();
		// create a new page
		PDPage page = new PDPage(originalPage.getMediaBox());
		doc.addPage(page);
		PDAcroForm acroForm = new PDAcroForm(doc);
           doc.getDocumentCatalog().setAcroForm(acroForm);
           PDSignatureField signatureField = new PDSignatureField(acroForm);
           PDAnnotationWidget widget = signatureField.getWidgets().get(0);
           List<PDField> acroFormFields = acroForm.getFields();
           acroForm.setSignaturesExist(true);
           acroForm.setAppendOnly(true);
           acroForm.getCOSObject().setDirect(true);
           acroFormFields.add(signatureField);

           PDRectangle rectangle = getPdRectangle(dimensionAndPosition, page);
           widget.setRectangle(rectangle);
           
           PDStream stream = new PDStream(doc);
           PDFormXObject form = new PDFormXObject(stream);
           PDResources res = new PDResources();
           form.setResources(res);
           form.setFormType(1);
           
           form.setBBox(new PDRectangle(rectangle.getWidth(), rectangle.getHeight()));
           
           PDAppearanceDictionary appearance = new PDAppearanceDictionary();
           appearance.getCOSObject().setDirect(true);
           PDAppearanceStream appearanceStream = new PDAppearanceStream(form.getCOSObject());
           appearance.setNormalAppearance(appearanceStream);
           widget.setAppearance(appearance);
           
           try (PDPageContentStream cs = new PDPageContentStream(doc, appearanceStream))
           {
           	rotateSignature(cs, rectangle, dimensionAndPosition);
           	setFieldBackground(cs, parameters.getBackgroundColor());
           	setText(cs, dimensionAndPosition, parameters);
           	setImage(cs, doc, dimensionAndPosition, parameters.getImage());
           }
           
           doc.save(baos);
           
           try (ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()))
           {
       		signatureOptions.setVisualSignature(bais);
       		signatureOptions.setPage(parameters.getPage() - 1);
           }
           
       }
}