<< back
PD4Document API
PD4Document API provides basic PDF manipulation tools like PDF document merge, extracting of document meta-data, counting a number of pages, updating document meta-data (Author, Keywords, Subject etc), resetting of a document password or updating document permissions.
The simplest use case: 2 PDF documents merge using a static utility method. The
method assumes, the documents are not protected with passwords and no special
processing is needed (like meta-data retrieval/update).
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.zefer.pd4ml.PD4Document;
public class PD4DocumentDemo {
public static void main( String[] args ) throws FileNotFoundException, IOException {
PD4Document.mergePDFs(new FileInputStream(args[0]), new FileInputStream(args[1]), new FileOutputStream(args[2]));
}
}
Some more complicated merge approach. PDF documents are read individually (which
makes possible to read document meta-data and read password-protected PDFs). The
result is written protected with a new password and limited permissions.
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.zefer.pd4ml.PD4Constants;
import org.zefer.pd4ml.PD4Document;
public class PD4DocumentDemo {
public static void main( String[] args ) throws FileNotFoundException, IOException {
PD4Document doc1 = new PD4Document(new FileInputStream(args[0]), "secret_password1");
System.out.println("document #1: " + doc1.getNumberOfPages() + "pages");
PD4Document doc2 = new PD4Document(new FileInputStream(args[2]), "secret_password2");
System.out.println("document #2: " + doc1.getNumberOfPages() + "pages");
doc1.append(doc2);
System.out.println("resulting document: " + doc1.getNumberOfPages() + "pages");
// set new password to the resulting document and allow degraded print and annotations only
doc1.write(new FileOutputStream(args[2]), "new_password",
PD4Constants.AllowDegradedPrint | PD4Constants.AllowAnnotate);
}
}
PDF Document meta-data update sample. Obvious.
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.zefer.pd4ml.PD4Document;
public class PD4DocumentDemo {
public static void main( String[] args ) throws FileNotFoundException, IOException {
PD4Document doc = new PD4Document(new FileInputStream(args[0]), null);
System.out.println("document author: " + doc.getAuthor());
doc.setTitle("Document Modification Test");
doc.setSubject("PD4Document API test");
doc.setKeywords("key1, key2");
doc.setModDate(); // set modification date to NOW
doc.write(new FileOutputStream(args[1]), null, -1); // no password, default permissions
}
}
|