Java Code Examples for org.apache.pdfbox.pdmodel.PDResources#put()

The following examples show how to use org.apache.pdfbox.pdmodel.PDResources#put() . 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: AppearanceGeneratorHelper.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
private void validateAndEnsureAcroFormResources()
{
    // add font resources which might be available at the field 
    // level but are not at the AcroForm level to the AcroForm
    // to match Adobe Reader/Acrobat behavior        
    if (field.getAcroForm().getDefaultResources() == null)
    {
        return;
    }
    
    PDResources acroFormResources = field.getAcroForm().getDefaultResources();
    
    for (PDAnnotationWidget widget : field.getWidgets())
    {
        if (widget.getNormalAppearanceStream() != null && widget.getNormalAppearanceStream().getResources() != null)
        {
            PDResources widgetResources = widget.getNormalAppearanceStream().getResources();
            for (COSName fontResourceName : widgetResources.getFontNames())
            {
                try
                {
                    if (acroFormResources.getFont(fontResourceName) == null)
                    {
                        LOG.debug("Adding font resource " + fontResourceName + " from widget to AcroForm");
                        acroFormResources.put(fontResourceName, widgetResources.getFont(fontResourceName));
                    }
                }
                catch (IOException e)
                {
                    LOG.warn("Unable to match field level font with AcroForm font");
                }
            }
        }
    }
}
 
Example 4
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 5
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 6
Source File: PdfCompressImagesBatchController.java    From MyBox with Apache License 2.0 4 votes vote down vote up
@Override
public int handleCurrentPage() {
    int count = 0;
    try {
        PDPage sourcePage = doc.getPage(currentParameters.currentPage - 1);  // 0-based
        PDResources pdResources = sourcePage.getResources();
        pdResources.getXObjectNames();
        Iterable<COSName> iterable = pdResources.getXObjectNames();
        if (iterable == null) {
            return 0;
        }
        Iterator<COSName> pageIterator = iterable.iterator();
        while (pageIterator.hasNext()) {
            if (task.isCancelled()) {
                break;
            }
            COSName cosName = pageIterator.next();
            if (!pdResources.isImageXObject(cosName)) {
                continue;
            }
            PDImageXObject pdxObject = (PDImageXObject) pdResources.getXObject(cosName);
            BufferedImage sourceImage = pdxObject.getImage();
            PDImageXObject newObject = null;
            if (format == PdfImageFormat.Tiff) {
                ImageBinary imageBinary = new ImageBinary(sourceImage, threshold);
                imageBinary.setIsDithering(ditherCheck.isSelected());
                BufferedImage newImage = imageBinary.operate();
                newImage = ImageBinary.byteBinary(newImage);
                newObject = CCITTFactory.createFromImage(doc, newImage);

            } else if (format == PdfImageFormat.Jpeg) {
                newObject = JPEGFactory.createFromImage(doc, sourceImage, jpegQuality / 100f);
            }
            if (newObject != null) {
                pdResources.put(cosName, newObject);
                count++;
            }
            if (isPreview) {
                break;
            }
        }
        if (copyAllCheck.isSelected()) {
            targetDoc.getPage(currentParameters.currentPage - 1).setResources(pdResources);
        } else {
            targetDoc.addPage(sourcePage);
        }
    } catch (Exception e) {
        logger.error(e.toString());
    }
    return count;

}
 
Example 7
Source File: TestEmptySignatureField.java    From testarea-pdfbox2 with Apache License 2.0 4 votes vote down vote up
/**
 * <a href="http://stackoverflow.com/questions/37601092/pdfbox-identify-specific-pages-and-functionalities-recommendations">
 * PDFBox identify specific pages and functionalities recommendations
 * </a>
 * 
 * <p>
 * This test shows how to add an empty signature field with a custom appearance
 * to an existing PDF.
 * </p>
 */
@Test
public void testAddEmptySignatureField() throws IOException
{
    try (   InputStream sourceStream = getClass().getResourceAsStream("test.pdf");
            OutputStream output = new FileOutputStream(new File(RESULT_FOLDER, "test-with-empty-sig-field.pdf")))
    {
        PDFont font = PDType1Font.HELVETICA;
        PDResources resources = new PDResources();
        resources.put(COSName.getPDFName("Helv"), font);

        PDDocument document = Loader.loadPDF(sourceStream);
        PDAcroForm acroForm = new PDAcroForm(document);
        acroForm.setDefaultResources(resources);
        document.getDocumentCatalog().setAcroForm(acroForm);

        PDRectangle rect = new PDRectangle(50, 750, 200, 50);

        PDAppearanceDictionary appearanceDictionary = new PDAppearanceDictionary();
        PDAppearanceStream appearanceStream = new PDAppearanceStream(document);
        appearanceStream.setBBox(rect.createRetranslatedRectangle());
        appearanceStream.setResources(resources);
        appearanceDictionary.setNormalAppearance(appearanceStream);
        PDPageContentStream contentStream = new PDPageContentStream(document, appearanceStream);
        contentStream.setStrokingColor(Color.BLACK);
        contentStream.setNonStrokingColor(Color.LIGHT_GRAY);
        contentStream.setLineWidth(2);
        contentStream.addRect(0, 0, rect.getWidth(), rect.getHeight());
        contentStream.fill();
        contentStream.moveTo(1 * rect.getHeight() / 4, 1 * rect.getHeight() / 4);
        contentStream.lineTo(2 * rect.getHeight() / 4, 3 * rect.getHeight() / 4);
        contentStream.moveTo(1 * rect.getHeight() / 4, 3 * rect.getHeight() / 4);
        contentStream.lineTo(2 * rect.getHeight() / 4, 1 * rect.getHeight() / 4);
        contentStream.moveTo(3 * rect.getHeight() / 4, 1 * rect.getHeight() / 4);
        contentStream.lineTo(rect.getWidth() - rect.getHeight() / 4, 1 * rect.getHeight() / 4);
        contentStream.stroke();
        contentStream.setNonStrokingColor(Color.DARK_GRAY);
        contentStream.beginText();
        contentStream.setFont(font, rect.getHeight() / 5);
        contentStream.newLineAtOffset(3 * rect.getHeight() / 4, -font.getBoundingBox().getLowerLeftY() * rect.getHeight() / 5000);
        contentStream.showText("Customer");
        contentStream.endText();
        contentStream.close();

        PDSignatureField signatureField = new PDSignatureField(acroForm);
        signatureField.setPartialName("SignatureField");
        PDPage page = document.getPage(0);

        PDAnnotationWidget widget = signatureField.getWidgets().get(0);
        widget.setAppearance(appearanceDictionary);
        widget.setRectangle(rect);
        widget.setPage(page);

        page.getAnnotations().add(widget);
        acroForm.getFields().add(signatureField);

        document.save(output);
        document.close();
    }
}