Delete Attachments in PDF in Java

2019-07-25 05:42:35 Written by Koohji

This article demonstrates how to delete attachments and annotation attachments in a PDF document using Spire.PDF for Java.

Delete Attachments

import com.spire.pdf.attachments.PdfAttachmentCollection;

public class DeleteAttachments {

    public static void main(String[] args) {

        //load a PDF document
        PdfDocument doc = new PdfDocument();
        doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\Attachments.pdf");

        //get the attachments collection, not containing annotation attachments
        PdfAttachmentCollection attachments = doc.getAttachments();
        
        //remove all attachments
        attachments.clear();

        //remove a specific attachment
        //attachments.removeAt(0);

        //save to file
        doc.saveToFile("output/DeleteAttachments.pdf");
        doc.close();
    }
}

Delete Annotation Attachments

import com.spire.pdf.annotations.PdfAnnotation;
import com.spire.pdf.annotations.PdfAnnotationCollection;
import com.spire.pdf.annotations.PdfAttachmentAnnotationWidget;

public class DeleteAnnotationAttachments {

    public static void main(String[] args) {

        //load a PDF document
        PdfDocument doc = new PdfDocument();
        doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\Attachments.pdf");

        //loop through the pages
        for (int i = 0; i < doc.getPages().getCount(); i++) {

            //get the annotations collection
            PdfAnnotationCollection annotationCollection = doc.getPages().get(i).getAnnotationsWidget();

            //loop through the annotations
            for (Object annotation: annotationCollection) {
                
                //determine if an annotation is an instance of PdfAttachmentAnnotationWidget 
                if (annotation instanceof PdfAttachmentAnnotationWidget){
                    
                    //remove the attachment annotation
                    annotationCollection.remove((PdfAnnotation) annotation);
                }
            }
        }

        //save to file
        doc.saveToFile("output/DeleteAnnotationAttachments.pdf");
        doc.close();
    }
}

We have already demonstrated how to set PDF Document Properties in Java. This article we will show you how to set custom properties for PDF files in Java.

import com.spire.pdf.*;

public class PDFCustomProperties {
    public static void main(String[] args) throws Exception {

        String inputPath = "Sample.pdf";
        PdfDocument doc = new PdfDocument(inputPath);
        doc.loadFromFile(inputPath);

        //Set the custom properties
       doc.getDocumentInformation().setCustomProperty("Number", "123");
       doc.getDocumentInformation().setCustomProperty("Name", "Daisy");
       doc.getDocumentInformation().setCustomProperty("Company", "e-iceblue");"


        //Save the document to file
        doc.saveToFile("Output/result.pdf");
        doc.close();

    }
}

Effective screenshot after adding custom properties to PDF document:

Set custom properties for PDF files in Java

A bulleted list is a list with dots in the front and the same indentation for each item, while a numbered list with numbers in the front. Bulleted lists and numbered lists work much better than lengthy text content because, with a well-organized list, readers can easily get the structure and the point of each item. This article shows how to create lists from existing text in Word documents with Spire.Doc for Java.

Install Spire.Doc for Java

First of all, you're required to add the Spire.Doc.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.doc</artifactId>
        <version>14.8.4</version>
    </dependency>
</dependencies>

Create a Bulleted List and a Numbered List from Existing Text in a Word Document

Spire.Doc for Java provides two methods, ListFormat.applyBulletStyle() and ListFormat.applyNumberedStyle(), to create bulleted and numbered lists.

The detailed steps of creating bulleted and numbered lists are as follows.

  • Create an object of Document class.
  • Load a Word document from file using Document.loadFromFile() method.
  • Get the first section using Document.getSections().get() method.
  • Loop through the 4th to 6th paragraphs.
  • Apply bulleted list style to these paragraphs using ListFormat.applyBulletStyle() method and set their position using ListFormat.getCurrentListLevel().setNumberPosition() method.
  • Loop through the 10th to 12th paragraphs.
  • Apply numbered list style to these paragraphs using ListFormat.applyNumberedStyle() method and set their position using ListFormat.getCurrentListLevel().setNumberPosition() method.
  • Save the document using Document.saveToFile() method.
  • Java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.formatting.ListFormat;

public class CreateLists {
    public static void main(String[] args) {

        //Create an object of Document class
        Document document = new Document();

        //Load a Word document from file
        document.loadFromFile("C:/Samples/Sample2.docx");

        //Get the first section
        Section section = document.getSections().get(0);

        //Loop through the 4th to the 6th paragraphs
        for(int i = 3; i <= 5; i++){
            Paragraph para = section.getParagraphs().get(i);
            ListFormat listFormat = para.getListFormat();

            //Apply bulleted list style
            listFormat.applyBulletStyle();

            //Set list position
            listFormat.getCurrentListLevel().setNumberPosition(-10);
        }

        //Loop through the 10th to the 12th paragraphs
        for(int i = 9; i <= 11; i++){
            Paragraph para = section.getParagraphs().get(i);
            ListFormat listFormat = para.getListFormat();

            //Apply numbered list style
            listFormat.applyNumberedStyle();

            //Set list position
            listFormat.getCurrentListLevel().setNumberPosition(-10);

        }

        //Save the document
        document.saveToFile("CreateLists.docx", FileFormat.Docx_2013);
    }
}

Java: Create Bulleted or Numbered Lists from Existing Text in Word Documents

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

page 164