Following is an example program to add pages to a PDF document using Java.
import java.io.File;
import java.io.IOException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
public class AddingPagesToPdf {
public static void main(String args[]) throws IOException {
//Creating PDF document object
PDDocument document = new PDDocument();
File file = new File("C:/pdfBox/AddPages.pdf");
PDDocument.load(file);
for (int i=0; i<10; i++){
//Creating a blank page
PDPage blankPage = new PDPage();
//Adding the blank page to the document
document.addPage(blankPage);
}
//Saving the document
document.save("C:/pdfBox/AddPages_OP.pdf");
System.out.println("PDF created");
//Closing the document
document.close();
}
}
Following is an example program to split a PDF in to many using Java.
mport org.apache.pdfbox.multipdf.Splitter;
import org.apache.pdfbox.pdmodel.PDDocument;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Iterator;
public class SplittingPDF {
public static void main(String[] args) throws IOException {
//Loading an existing PDF document
File file = new File("C:/pdfBox/splitpdf_IP.pdf");
PDDocument doc = PDDocument.load(file);
//Instantiating Splitter class
Splitter splitter = new Splitter();
//splitting the pages of a PDF document
List<PDDocument> Pages = splitter.split(doc);
//Creating an iterator
Iterator<PDDocument> iterator = Pages.listIterator();
//Saving each page as an individual document
int i = 1;
while(iterator.hasNext()){
PDDocument pd = iterator.next();
pd.save("C:/pdfBox/splitOP"+ i++ +".pdf");
}
System.out.println("PDF splitted");
}
}
Java and PDF with iText. This article demonstrate how to create PDF files with Java and the iText library. In this tutorial iText version 5.0.x is used
iText is a Java library originally created by Bruno Lowagie which allows to create PDF, read PDF and manipulate them. The following tutorial will show how to create PDF files with iText. This tutorial assumes that you have basis in Java and Eclipse knowledge.
iText has a hierarchical structure. The smallest text unit is a “Chunk” which is a String with a pre-defined font. A “Phrase” combines several Chunks and allows to define line spacing. “Paragraph” is a subclass of “Phrase” and allows to define more layout attributes, e.g. margins. The class “Anchor” is a subclass of “Paragraph” and serves as the basis for hyperlinks in the generated PDF.
Create the following class “FirstPdf.java” . I assume that the code is pretty much self-explaining. I tried to add lots of comments to make it easier to understand. For more complex examples have a look at the iText Homepage.
package de.fusebes.itext.write;importjava.io.FileOutputStream;importjava.util.Date;importcom.itextpdf.text.Anchor;importcom.itextpdf.text.BadElementException;importcom.itextpdf.text.BaseColor;importcom.itextpdf.text.Chapter;importcom.itextpdf.text.Document;importcom.itextpdf.text.DocumentException;importcom.itextpdf.text.Element;importcom.itextpdf.text.Font;importcom.itextpdf.text.List;importcom.itextpdf.text.ListItem;importcom.itextpdf.text.Paragraph;importcom.itextpdf.text.Phrase;importcom.itextpdf.text.Section;importcom.itextpdf.text.pdf.PdfPCell;importcom.itextpdf.text.pdf.PdfPTable;importcom.itextpdf.text.pdf.PdfWriter;publicclassFirstPdf{privatestaticStringFILE="c:/temp/FirstPdf.pdf";privatestaticFont catFont =newFont(Font.FontFamily.TIMES_ROMAN,18,Font.BOLD);privatestaticFont redFont =newFont(Font.FontFamily.TIMES_ROMAN,12,Font.NORMAL,BaseColor.RED);privatestaticFont subFont =newFont(Font.FontFamily.TIMES_ROMAN,16,Font.BOLD);privatestaticFont smallBold =newFont(Font.FontFamily.TIMES_ROMAN,12,Font.BOLD);publicstaticvoid main(String[] args){try{Document document =newDocument();PdfWriter.getInstance(document,newFileOutputStream(FILE));
document.open();
addMetaData(document);
addTitlePage(document);
addContent(document);
document.close();}catch(Exception e){
e.printStackTrace();}}// iText allows to add metadata to the PDF which can be viewed in your Adobe// Reader// under File -> Propertiesprivatestaticvoid addMetaData(Document document){
document.addTitle("My first PDF");
document.addSubject("Using iText");
document.addKeywords("Java, PDF, iText");
document.addAuthor("Lars Vogel");
document.addCreator("Lars Vogel");}privatestaticvoid addTitlePage(Document document)throwsDocumentException{Paragraph preface =newParagraph();// We add one empty line
addEmptyLine(preface,1);// Lets write a big header
preface.add(newParagraph("Title of the document", catFont));
addEmptyLine(preface,1);// Will create: Report generated by: _name, _date
preface.add(newParagraph("Report generated by: "+System.getProperty("user.name")+", "+newDate(),//$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
smallBold));
addEmptyLine(preface,3);
preface.add(newParagraph("This document describes something which is very important ",
smallBold));
addEmptyLine(preface,8);
preface.add(newParagraph("This document is a preliminary version and not subject to your license agreement or any other agreement with vogella.com ;-).",
redFont));
document.add(preface);// Start a new page
document.newPage();}privatestaticvoid addContent(Document document)throwsDocumentException{Anchor anchor =newAnchor("First Chapter", catFont);
anchor.setName("First Chapter");// Second parameter is the number of the chapterChapter catPart =newChapter(newParagraph(anchor),1);Paragraph subPara =newParagraph("Subcategory 1", subFont);Section subCatPart = catPart.addSection(subPara);
subCatPart.add(newParagraph("Hello"));
subPara =newParagraph("Subcategory 2", subFont);
subCatPart = catPart.addSection(subPara);
subCatPart.add(newParagraph("Paragraph 1"));
subCatPart.add(newParagraph("Paragraph 2"));
subCatPart.add(newParagraph("Paragraph 3"));// add a list
createList(subCatPart);Paragraph paragraph =newParagraph();
addEmptyLine(paragraph,5);
subCatPart.add(paragraph);// add a table
createTable(subCatPart);// now add all this to the document
document.add(catPart);// Next section
anchor =newAnchor("Second Chapter", catFont);
anchor.setName("Second Chapter");// Second parameter is the number of the chapter
catPart =newChapter(newParagraph(anchor),1);
subPara =newParagraph("Subcategory", subFont);
subCatPart = catPart.addSection(subPara);
subCatPart.add(newParagraph("This is a very important message"));// now add all this to the document
document.add(catPart);}privatestaticvoid createTable(Section subCatPart)throwsBadElementException{PdfPTable table =newPdfPTable(3);// t.setBorderColor(BaseColor.GRAY);// t.setPadding(4);// t.setSpacing(4);// t.setBorderWidth(1);PdfPCell c1 =newPdfPCell(newPhrase("Table Header 1"));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 =newPdfPCell(newPhrase("Table Header 2"));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 =newPdfPCell(newPhrase("Table Header 3"));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
table.setHeaderRows(1);
table.addCell("1.0");
table.addCell("1.1");
table.addCell("1.2");
table.addCell("2.1");
table.addCell("2.2");
table.addCell("2.3");
subCatPart.add(table);}privatestaticvoid createList(Section subCatPart){List list =newList(true,false,10);
list.add(newListItem("First point"));
list.add(newListItem("Second point"));
list.add(newListItem("Third point"));
subCatPart.add(list);}privatestaticvoid addEmptyLine(Paragraph paragraph,int number){for(int i =0; i < number; i++){
paragraph.add(newParagraph(" "));}}}