org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject Java Examples

The following examples show how to use org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject. 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: PdfContentImagePreprocessor.java    From tika-server with Apache License 2.0 7 votes vote down vote up
private void processImagesFromResources(PDResources resources) throws IOException {
    for (COSName xObjectName : resources.getXObjectNames()) {
        PDXObject xObject = resources.getXObject(xObjectName);

        if (xObject instanceof PDFormXObject) {
            processImagesFromResources(((PDFormXObject) xObject).getResources());
        } else if (xObject instanceof PDImageXObject) {
            PDImageXObject img = (PDImageXObject) xObject;
            if (!img.getImage().getColorModel().hasAlpha())
                return;

            PDImageXObject cpy = makeImageObjectCopy(img);
            resources.put(xObjectName, cpy);
            imagesWereChanged = true;
        }
    }
}
 
Example #2
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 #3
Source File: PdfFontExtractor.java    From FontVerter with GNU Lesser General Public License v3.0 6 votes vote down vote up
private void extractFontResources(PDResources resources) throws IOException {
    for (COSName key : resources.getFontNames()) {
        PDFont font = resources.getFont(key);
        extractStrategy.extract(font);
    }

    for (COSName name : resources.getXObjectNames()) {
        PDXObject xobject = resources.getXObject(name);
        if (xobject instanceof PDFormXObject) {
            PDFormXObject xObjectForm = (PDFormXObject) xobject;
            PDResources formResources = xObjectForm.getResources();

            if (formResources != null)
                extractFontResources(formResources);
        }
    }
}
 
Example #4
Source File: DrawObject.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
@Override
public void process(Operator operator, List<COSBase> arguments) throws IOException
{
    if (arguments.isEmpty())
    {
        throw new MissingOperandException(operator, arguments);
    }
    COSBase base0 = arguments.get(0);
    if (!(base0 instanceof COSName))
    {
        return;
    }
    COSName name = (COSName) base0;
    PDXObject xobject =  context.getResources().getXObject(name);
    ((PDFMarkedContentExtractor) context).xobject(xobject);

    if (xobject instanceof PDTransparencyGroup)
    {
        context.showTransparencyGroup((PDTransparencyGroup) xobject);
    }
    else if (xobject instanceof PDFormXObject)
    {
        PDFormXObject form = (PDFormXObject) xobject;
        context.showForm(form);
    }
}
 
Example #5
Source File: DrawObject.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public void process(Operator operator, List<COSBase> operands) throws IOException
{
    if (operands.isEmpty())
    {
        throw new MissingOperandException(operator, operands);
    }
    COSBase base0 = operands.get(0);
    if (!(base0 instanceof COSName))
    {
        return;
    }
    COSName objectName = (COSName) base0;
    PDXObject xobject = context.getResources().getXObject(objectName);

    if (xobject == null)
    {
        throw new MissingResourceException("Missing XObject: " + objectName.getName());
    }
    else if (xobject instanceof PDImageXObject)
    {
        PDImageXObject image = (PDImageXObject)xobject;
        context.drawImage(image);
    }
    else if (xobject instanceof PDTransparencyGroup)
    {
        getContext().showTransparencyGroup((PDTransparencyGroup) xobject);
    }
    else if (xobject instanceof PDFormXObject)
    {
        getContext().showForm((PDFormXObject) xobject);
    }
}
 
Example #6
Source File: PDVisibleSigBuilder.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public void createAppearanceDictionary(PDFormXObject holderForml,
                                       PDSignatureField signatureField) throws IOException
{
    PDAppearanceDictionary appearance = new PDAppearanceDictionary();
    appearance.getCOSObject().setDirect(true);

    PDAppearanceStream appearanceStream = new PDAppearanceStream(holderForml.getCOSObject());

    appearance.setNormalAppearance(appearanceStream);
    signatureField.getWidgets().get(0).setAppearance(appearance);

    pdfStructure.setAppearanceDictionary(appearance);
    LOG.info("PDF appearance dictionary has been created");
}
 
Example #7
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 #8
Source File: PDVisibleSigBuilder.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public void insertInnerFormToHolderResources(PDFormXObject innerForm,
                                             PDResources holderFormResources)
{
    holderFormResources.put(COSName.FRM, innerForm);
    pdfStructure.setInnerFormName(COSName.FRM);
    LOG.info("Now inserted inner form inside holder form");
}
 
Example #9
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 #10
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 #11
Source File: LayerUtility.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Places the given form over the existing content of the indicated page (like an overlay).
 * The form is enveloped in a marked content section to indicate that it's part of an
 * optional content group (OCG), here used as a layer. This optional group is returned and
 * can be enabled and disabled through methods on {@link PDOptionalContentProperties}.
 * <p>
 * You may want to call {@link #wrapInSaveRestore(PDPage) wrapInSaveRestore(PDPage)} before calling this method to make
 * sure that the graphics state is reset.
 *
 * @param targetPage the target page
 * @param form the form to place
 * @param transform the transformation matrix that controls the placement of your form. You'll
 * need this if your page has a crop box different than the media box, or if these have negative
 * coordinates, or if you want to scale or adjust your form.
 * @param layerName the name for the layer/OCG to produce
 * @return the optional content group that was generated for the form usage
 * @throws IOException if an I/O error occurs
 */
public PDOptionalContentGroup appendFormAsLayer(PDPage targetPage,
        PDFormXObject form, AffineTransform transform,
        String layerName) throws IOException
{
    PDDocumentCatalog catalog = targetDoc.getDocumentCatalog();
    PDOptionalContentProperties ocprops = catalog.getOCProperties();
    if (ocprops == null)
    {
        ocprops = new PDOptionalContentProperties();
        catalog.setOCProperties(ocprops);
    }
    if (ocprops.hasGroup(layerName))
    {
        throw new IllegalArgumentException("Optional group (layer) already exists: " + layerName);
    }

    PDRectangle cropBox = targetPage.getCropBox();
    if ((cropBox.getLowerLeftX() < 0 || cropBox.getLowerLeftY() < 0) && transform.isIdentity())
    {
        // PDFBOX-4044 
        LOG.warn("Negative cropBox " + cropBox + 
                 " and identity transform may make your form invisible");
    }

    PDOptionalContentGroup layer = new PDOptionalContentGroup(layerName);
    ocprops.addGroup(layer);

    PDPageContentStream contentStream = new PDPageContentStream(
            targetDoc, targetPage, AppendMode.APPEND, !DEBUG);
    contentStream.beginMarkedContent(COSName.OC, layer);
    contentStream.saveGraphicsState();
    contentStream.transform(new Matrix(transform));
    contentStream.drawForm(form);
    contentStream.restoreGraphicsState();
    contentStream.endMarkedContent();
    contentStream.close();

    return layer;
}
 
Example #12
Source File: PDVisibleSigBuilder.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public void injectProcSetArray(PDFormXObject innerForm, PDPage page,
                               PDResources innerFormResources,  PDResources imageFormResources,
                               PDResources holderFormResources, COSArray procSet)
{
    innerForm.getResources().getCOSObject().setItem(COSName.PROC_SET, procSet);
    page.getCOSObject().setItem(COSName.PROC_SET, procSet);
    innerFormResources.getCOSObject().setItem(COSName.PROC_SET, procSet);
    imageFormResources.getCOSObject().setItem(COSName.PROC_SET, procSet);
    holderFormResources.getCOSObject().setItem(COSName.PROC_SET, procSet);
    LOG.info("Inserted ProcSet to PDF");
}
 
Example #13
Source File: DrawObject.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public void process(Operator operator, List<COSBase> arguments) throws IOException
{
    if (arguments.isEmpty())
    {
        throw new MissingOperandException(operator, arguments);
    }
    COSBase base0 = arguments.get(0);
    if (!(base0 instanceof COSName))
    {
        return;
    }
    COSName name = (COSName) base0;

    if (context.getResources().isImageXObject(name))
    {
        // we're done here, don't decode images when doing text extraction
        return;
    }
    
    PDXObject xobject = context.getResources().getXObject(name);

    if (xobject instanceof PDTransparencyGroup)
    {
        context.showTransparencyGroup((PDTransparencyGroup) xobject);
    }
    else if (xobject instanceof PDFormXObject)
    {
        PDFormXObject form = (PDFormXObject) xobject;
        context.showForm(form);
    }
}
 
Example #14
Source File: PDFStreamEngine.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Shows a form from the content stream.
 *
 * @param form form XObject
 * @throws IOException if the form cannot be processed
 */
public void showForm(PDFormXObject form) throws IOException
{
    if (currentPage == null)
    {
        throw new IllegalStateException("No current page, call " +
                "#processChildStream(PDContentStream, PDPage) instead");
    }
    if (form.getCOSObject().getLength() > 0)
    {
        processStream(form);
    }
}
 
Example #15
Source File: PdfDenseMergeTool.java    From testarea-pdfbox2 with Apache License 2.0 5 votes vote down vote up
void merge(PDDocument sourceDoc, PDPage page) throws IOException
{
    PDRectangle pageSizeToImport = page.getCropBox();
    BoundingBoxFinder boundingBoxFinder = new BoundingBoxFinder(page);
    boundingBoxFinder.processPage(page);
    Rectangle2D boundingBoxToImport = boundingBoxFinder.getBoundingBox();
    double heightToImport = boundingBoxToImport.getHeight();
    float maxHeight = pageSize.getHeight() - topMargin - bottomMargin;
    if (heightToImport > maxHeight)
    {
        throw new IllegalArgumentException(String.format("Page %s content too large; height: %s, limit: %s.", page, heightToImport, maxHeight));
    }

    if (gap + heightToImport > yPosition - (pageSize.getLowerLeftY() + bottomMargin))
    {
        newPage();
    }
    yPosition -= heightToImport + gap;

    LayerUtility layerUtility = new LayerUtility(document);
    PDFormXObject form = layerUtility.importPageAsForm(sourceDoc, page);

    currentContents.saveGraphicsState();
    Matrix matrix = Matrix.getTranslateInstance(0, (float)(yPosition - (boundingBoxToImport.getMinY() - pageSizeToImport.getLowerLeftY())));
    currentContents.transform(matrix);
    currentContents.drawForm(form);
    currentContents.restoreGraphicsState();
}
 
Example #16
Source File: JoinPages.java    From testarea-pdfbox2 with Apache License 2.0 5 votes vote down vote up
/**
 * @see #testJoinSmallAndBig()
 */
void join(PDDocument target, PDDocument topSource, PDDocument bottomSource) throws IOException {
    LayerUtility layerUtility = new LayerUtility(target);
    PDFormXObject topForm = layerUtility.importPageAsForm(topSource, 0);
    PDFormXObject bottomForm = layerUtility.importPageAsForm(bottomSource, 0);

    float height = topForm.getBBox().getHeight() + bottomForm.getBBox().getHeight();
    float width, topMargin, bottomMargin;
    if (topForm.getBBox().getWidth() > bottomForm.getBBox().getWidth()) {
        width = topForm.getBBox().getWidth();
        topMargin = 0;
        bottomMargin = (topForm.getBBox().getWidth() - bottomForm.getBBox().getWidth()) / 2;
    } else {
        width = bottomForm.getBBox().getWidth();
        topMargin = (bottomForm.getBBox().getWidth() - topForm.getBBox().getWidth()) / 2;
        bottomMargin = 0;
    }

    PDPage targetPage = new PDPage(new PDRectangle(width, height));
    target.addPage(targetPage);


    PDPageContentStream contentStream = new PDPageContentStream(target, targetPage);
    if (bottomMargin != 0)
        contentStream.transform(Matrix.getTranslateInstance(bottomMargin, 0));
    contentStream.drawForm(bottomForm);
    contentStream.transform(Matrix.getTranslateInstance(topMargin - bottomMargin, bottomForm.getBBox().getHeight()));
    contentStream.drawForm(topForm);
    contentStream.close();
}
 
Example #17
Source File: ExtractText.java    From testarea-pdfbox2 with Apache License 2.0 5 votes vote down vote up
void removeToUnicodeMaps(PDResources pdResources) throws IOException
{
    COSDictionary resources = pdResources.getCOSObject();

    COSDictionary fonts = asDictionary(resources, COSName.FONT);
    if (fonts != null)
    {
        for (COSBase object : fonts.getValues())
        {
            while (object instanceof COSObject)
                object = ((COSObject)object).getObject();
            if (object instanceof COSDictionary)
            {
                COSDictionary font = (COSDictionary)object;
                font.removeItem(COSName.TO_UNICODE);
            }
        }
    }

    for (COSName name : pdResources.getXObjectNames())
    {
        PDXObject xobject = pdResources.getXObject(name);
        if (xobject instanceof PDFormXObject)
        {
            PDResources xobjectPdResources = ((PDFormXObject)xobject).getResources();
            removeToUnicodeMaps(xobjectPdResources);
        }
    }
}
 
Example #18
Source File: PDFBoxTree.java    From Pdf2Dom with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void processFontResources(PDResources resources, FontTable table) throws IOException
{
    String fontNotSupportedMessage = "Font: {} skipped because type '{}' is not supported.";

    for (COSName key : resources.getFontNames())
    {
        PDFont font = resources.getFont(key);
        if (font instanceof PDTrueTypeFont)
        {
            table.addEntry( font);
            log.debug("Font: " + font.getName() + " TTF");
        }
        else if (font instanceof PDType0Font)
        {
            PDCIDFont descendantFont = ((PDType0Font) font).getDescendantFont();
            if (descendantFont instanceof PDCIDFontType2)
                table.addEntry(font);
            else
                log.warn(fontNotSupportedMessage, font.getName(), font.getClass().getSimpleName());
        }
        else if (font instanceof PDType1CFont)
            table.addEntry(font);
        else
            log.warn(fontNotSupportedMessage, font.getName(), font.getClass().getSimpleName());
    }

    for (COSName name : resources.getXObjectNames())
    {
        PDXObject xobject = resources.getXObject(name);
        if (xobject instanceof PDFormXObject)
        {
            PDFormXObject xObjectForm = (PDFormXObject) xobject;
            PDResources formResources = xObjectForm.getResources();
            if (formResources != null && formResources != resources && formResources.getCOSObject() != resources.getCOSObject())
                processFontResources(formResources, table);
        }
    }

}
 
Example #19
Source File: PdfContentTypeChecker.java    From tika-server with Apache License 2.0 5 votes vote down vote up
private void getImagesFromResources(PDResources resources) throws IOException {
    for (COSName xObjectName : resources.getXObjectNames()) {
        PDXObject xObject = resources.getXObject(xObjectName);

        if (xObject instanceof PDFormXObject) {
            getImagesFromResources(((PDFormXObject) xObject).getResources());
        } else if (xObject instanceof PDImageXObject) {
            //((PDImageXObject) xObject).getImage();
            imagesCount++;
        }
    }
}
 
Example #20
Source File: ImageExtractor.java    From inception with Apache License 2.0 5 votes vote down vote up
@Override protected void processOperator(Operator operator, List<COSBase> operands)
    throws IOException
{
    String operation = operator.getName();
    if ("Do".equals(operation)) {
        COSName objectName = (COSName) operands.get(0);
        PDXObject xobject = getResources().getXObject(objectName);

        if (xobject instanceof PDImageXObject) {
            PDImageXObject image = (PDImageXObject) xobject;
            Matrix ctmNew = getGraphicsState().getCurrentTransformationMatrix();
            PDRectangle pageRect = this.getCurrentPage().getCropBox();
            float w = ctmNew.getScalingFactorX();
            float h = ctmNew.getScalingFactorY();
            float x = ctmNew.getTranslateX();
            float y = pageRect.getHeight() - ctmNew.getTranslateY() - h;
            buffer.add(new ImageOperator(x, y, w, h));
        }
        else if (xobject instanceof PDFormXObject) {
            PDFormXObject form = (PDFormXObject) xobject;
            showForm(form);
        }
    }
    else {
        super.processOperator(operator, operands);
    }
}
 
Example #21
Source File: PDAppearanceCharacteristicsDictionary.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * This will retrieve the alternate icon.
 * 
 * @return the alternate icon.
 */
public PDFormXObject getAlternateIcon()
{
    COSBase i = this.getCOSObject().getDictionaryObject(COSName.IX);
    if (i instanceof COSStream)
    {
        return new PDFormXObject((COSStream)i);
    }
    return null;
}
 
Example #22
Source File: PDAppearanceCharacteristicsDictionary.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * This will retrieve the rollover icon.
 * 
 * @return the rollover icon
 */
public PDFormXObject getRolloverIcon()
{
    COSBase i = this.getCOSObject().getDictionaryObject(COSName.RI);
    if (i instanceof COSStream)
    {
        return new PDFormXObject((COSStream)i);
    }
    return null;
}
 
Example #23
Source File: PageDrawer.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void showForm(PDFormXObject form) throws IOException
{
    if (isContentRendered())
    {
        super.showForm(form);
    }
}
 
Example #24
Source File: PDXObject.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Creates a new XObject instance of the appropriate type for the COS stream.
 *
 * @param base The stream which is wrapped by this XObject.
 * @param resources
 * @return A new XObject instance.
 * @throws java.io.IOException if there is an error creating the XObject.
 */
public static PDXObject createXObject(COSBase base, PDResources resources) throws IOException
{
    if (base == null)
    {
        // TODO throw an exception?
        return null;
    }

    if (!(base instanceof COSStream))
    {
        throw new IOException("Unexpected object type: " + base.getClass().getName());
    }

    COSStream stream = (COSStream)base;
    String subtype = stream.getNameAsString(COSName.SUBTYPE);

    if (COSName.IMAGE.getName().equals(subtype))
    {
        return new PDImageXObject(new PDStream(stream), resources);
    }
    else if (COSName.FORM.getName().equals(subtype))
    {
        ResourceCache cache = resources != null ? resources.getResourceCache() : null;
        COSDictionary group = (COSDictionary)stream.getDictionaryObject(COSName.GROUP);
        if (group != null && COSName.TRANSPARENCY.equals(group.getCOSName(COSName.S)))
        {
            return new PDTransparencyGroup(stream, cache);
        }
        return new PDFormXObject(stream, cache);
    }
    else if (COSName.PS.getName().equals(subtype))
    {
        return new PDPostScriptXObject(stream);
    }
    else
    {
        throw new IOException("Invalid XObject Subtype: " + subtype);
    }
}
 
Example #25
Source File: PDAbstractContentStream.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Draws the given Form XObject at the current location.
 *
 * @param form Form XObject
 * @throws IOException if the content stream could not be written
 * @throws IllegalStateException If the method was called within a text block.
 */
public void drawForm(PDFormXObject form) throws IOException
{
    if (inTextMode)
    {
        throw new IllegalStateException("Error: drawForm is not allowed within a text block.");
    }

    writeOperand(resources.add(form));
    writeOperator(OperatorName.DRAW_OBJECT);
}
 
Example #26
Source File: PDPageContentStream.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Create a new appearance stream. Note that this is not actually a "page" content stream.
 *
 * @param doc The document the appearance is part of.
 * @param form The XObject form to add to.
 * @param outputStream The output stream to write to.
 * @throws IOException If there is an error writing to the page contents.
 */
public PDPageContentStream(PDDocument doc, PDFormXObject form, OutputStream outputStream)
        throws IOException
{
    this.document = doc;

    output = outputStream;
    this.resources = form.getResources();

    formatDecimal.setMaximumFractionDigits(4);
    formatDecimal.setGroupingUsed(false);
}
 
Example #27
Source File: PDAppearanceCharacteristicsDictionary.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * This will retrieve the normal icon.
 * 
 * @return the normal icon.
 */
public PDFormXObject getNormalIcon()
{
    COSBase i = this.getCOSObject().getDictionaryObject(COSName.I);
    if (i instanceof COSStream)
    {
        return new PDFormXObject((COSStream)i);
    }
    return null;
}
 
Example #28
Source File: PDPageContentStream.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Draws the given Form XObject at the current location.
 *
 * @param form Form XObject
 * @throws IOException if the content stream could not be written
 * @throws IllegalStateException If the method was called within a text block.
 */
public void drawForm(PDFormXObject form) throws IOException
{
    if (inTextMode)
    {
        throw new IllegalStateException("Error: drawForm is not allowed within a text block.");
    }

    writeOperand(resources.add(form));
    writeOperator(OperatorName.DRAW_OBJECT);
}
 
Example #29
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 #30
Source File: PDAcroForm.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Check if there is a translation needed to place the annotations content.
 * 
 * @param appearanceStream
 * @return the need for a translation transformation.
 */
private boolean resolveNeedsTranslation(PDAppearanceStream appearanceStream)
{
    boolean needsTranslation = true;

    PDResources resources = appearanceStream.getResources();
    if (resources != null && resources.getXObjectNames().iterator().hasNext())
    {
        Iterator<COSName> xObjectNames = resources.getXObjectNames().iterator();

        while (xObjectNames.hasNext())
        {
            try
            {
                // if the BBox of the PDFormXObject does not start at 0,0
                // there is no need do translate as this is done by the BBox definition.
                PDXObject xObject = resources.getXObject(xObjectNames.next());
                if (xObject instanceof PDFormXObject)
                {
                    PDRectangle bbox = ((PDFormXObject)xObject).getBBox();
                    float llX = bbox.getLowerLeftX();
                    float llY = bbox.getLowerLeftY();
                    if (Float.compare(llX, 0) != 0 && Float.compare(llY, 0) != 0)
                    {
                        needsTranslation = false;
                    }
                }
            }
            catch (IOException e)
            {
                // we can safely ignore the exception here
                // as this might only cause a misplacement
            }
        }
        return needsTranslation;
    }
    
    return true;
}