Save a copy for a pdf file - Java PDF

Java examples for PDF:Document

Description

Save a copy for a pdf file

Demo Code


import java.io.*;
import org.apache.pdfbox.pdmodel.*;
import org.apache.pdfbox.util.*;

public class PDFTest {

    public static void main(String[] args) {
        PDDocument pd;/* w  w w .  jav a  2  s  .c om*/
        BufferedWriter wr;
        try {
            File input = new File("pdfbox/pdf/extracting/ASCII.pdf"); 
            File output = new File("pdfbox/pdf/extracting/text.txt"); 
            // data
            pd = PDDocument.load(input);

            System.out.println(pd.getNumberOfPages());
            System.out.println(pd.isEncrypted());

            pd.save("pdfbox/pdf/extracting/CopyOfInvoice.pdf"); 

            PDFTextStripper stripper = new PDFTextStripper();
            stripper.setStartPage(1); // Start extracting from page 3
            stripper.setEndPage(1); // Extract till page 5

            wr = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(output)));

            stripper.writeText(pd, wr);

            if (pd != null) {
                pd.close();
            }
            // I use close() to flush the stream.
            wr.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Related Tutorials