Java Code Examples for org.apache.pdfbox.cos.COSDictionary#removeItem()

The following examples show how to use org.apache.pdfbox.cos.COSDictionary#removeItem() . 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: FillInForm.java    From testarea-pdfbox2 with Apache License 2.0 6 votes vote down vote up
/**
 * <a href="https://stackoverflow.com/questions/49048556/pdfbox-set-field-value-doesnt-work">
 * PDFBox set field value doesn't work
 * </a>
 * <br/>
 * <a href="https://www.inps.it/Nuovoportaleinps/image.aspx?iIDModulo=7712&tipomodulo=1">
 * SR16_ANF_DIP.pdf
 * </a>
 * <p>
 * Indeed, the form field in question is hidden. Thus, one has to un-hide it
 * to make it visible.
 * </p>
 */
@Test
public void testFillLikeBarbara() throws IOException
{
    try (   InputStream originalStream = getClass().getResourceAsStream("SR16_ANF_DIP.pdf") )
    {
        PDDocument pdfDocument = Loader.loadPDF(originalStream);
        PDAcroForm acroForm = pdfDocument.getDocumentCatalog().getAcroForm();

        if (acroForm != null)
        {
            PDTextField pdfField = (PDTextField) acroForm.getField("info_15a");
            pdfField.getWidgets().get(0).setHidden(false);// <===
            pdfField.setValue("xxxxxx");
        }

        pdfDocument.setAllSecurityToBeRemoved(true);
        COSDictionary dictionary = pdfDocument.getDocumentCatalog().getCOSObject();
        dictionary.removeItem(COSName.PERMS);

        pdfDocument.save(new File(RESULT_FOLDER, "SR16_ANF_DIP-filled.pdf"));
        pdfDocument.close();
    }
}
 
Example 2
Source File: COSWriter.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * This will write the trailer to the PDF document.
 *
 * @param doc The document to create the trailer for.
 *
 * @throws IOException If there is an IOError while writing the document.
 */
protected void doWriteTrailer(COSDocument doc) throws IOException
{
    getStandardOutput().write(TRAILER);
    getStandardOutput().writeEOL();

    COSDictionary trailer = doc.getTrailer();
    //sort xref, needed only if object keys not regenerated
    Collections.sort(getXRefEntries());
    COSWriterXRefEntry lastEntry = getXRefEntries().get( getXRefEntries().size()-1);
    trailer.setLong(COSName.SIZE, lastEntry.getKey().getNumber()+1);
    // Only need to stay, if an incremental update will be performed
    if (!incrementalUpdate) 
    {
      trailer.removeItem( COSName.PREV );
    }
    if (!doc.isXRefStream())
    {
        trailer.removeItem( COSName.XREF_STM );
    }
    // Remove a checksum if present
    trailer.removeItem( COSName.DOC_CHECKSUM );

    COSArray idArray = trailer.getCOSArray(COSName.ID);
    if (idArray != null)
    {
        idArray.setDirect(true);
    }

    trailer.accept(this);
}
 
Example 3
Source File: PDFMergerUtility.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
private void cleanupFieldCOSDictionary(COSDictionary fieldCos)
{
    //TODO: align that list with the PDF spec. Vurrently only based on sample forms
    fieldCos.removeItem(COSName.F);
    fieldCos.removeItem(COSName.MK);
    fieldCos.removeItem(COSName.P);
    fieldCos.removeItem(COSName.RECT);
    fieldCos.removeItem(COSName.SUBTYPE);
    fieldCos.removeItem(COSName.TYPE);
}
 
Example 4
Source File: PDFMergerUtility.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
private void cleanupWidgetCOSDictionary(COSDictionary widgetCos, boolean removeDAEntry)
{
    //TODO: align that list with the PDF spec. Vurrently only based on sample forms
    // Acrobat removes the DA entry only for the first widget
    if (removeDAEntry)
    {
        widgetCos.removeItem(COSName.DA);
    }
    widgetCos.removeItem(COSName.FT);
    widgetCos.removeItem(COSName.T);
    widgetCos.removeItem(COSName.V);
}
 
Example 5
Source File: RemoveStrikeoutComment.java    From testarea-pdfbox2 with Apache License 2.0 5 votes vote down vote up
/**
 * <a href="https://stackoverflow.com/questions/45812696/pdfbox-delete-comment-maintain-strikethrough">
 * PDFBox delete comment maintain strikethrough
 * </a>
 * <br/>
 * <a href="https://expirebox.com/files/3d955e6df4ca5874c38dbf92fc43b5af.pdf">
 * only_fields.pdf
 * </a>
 * <a href="https://file.io/DTvqhC">
 * (alternative download)
 * </a>
 * <p>
 * The OP only wanted the comment removed, not the strike-through. Thus, we must
 * not remove the annotation but merely the comment building attributes.
 * </p>
 */
@Test
public void testRemoveLikeStephanImproved() throws IOException {
    final COSName POPUP = COSName.getPDFName("Popup");
    try (InputStream resource = getClass().getResourceAsStream("only_fields.pdf")) {
        PDDocument document = Loader.loadPDF(resource);
        List<PDAnnotation> annotations = new ArrayList<>();
        PDPageTree allPages = document.getDocumentCatalog().getPages();

        List<COSObjectable> objectsToRemove = new ArrayList<>();

        for (int i = 0; i < allPages.getCount(); i++) {
            PDPage page = allPages.get(i);
            annotations = page.getAnnotations();

            for (PDAnnotation annotation : annotations) {
                if ("StrikeOut".equals(annotation.getSubtype()))
                {
                    COSDictionary annotationDict = annotation.getCOSObject();
                    COSBase popup = annotationDict.getItem(POPUP);
                    annotationDict.removeItem(POPUP);
                    annotationDict.removeItem(COSName.CONTENTS); // plain text comment
                    annotationDict.removeItem(COSName.RC);       // rich text comment
                    annotationDict.removeItem(COSName.T);        // author

                    if (popup != null)
                        objectsToRemove.add(popup);
                }
            }

            annotations.removeAll(objectsToRemove);
        }

        document.save(new File(RESULT_FOLDER, "only_fields-removeImproved.pdf"));
    }
}
 
Example 6
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);
        }
    }
}