Apache PdfBox Api:
- Apache pdf-box library is an open source project.
- This library can be used for creating pdf,extracting text from pdf,bookmarking pdf etc.
- jar files required:- pdfbox-1.7.1.jar, fontbox-1.7.1.jar, commons-logging-1.1.1.jar .
Apache Project Structure:
PdfBox Demo java Program:
package com.sandeep.pdfbox.demo;
import java.io.IOException;
import org.apache.pdfbox.exceptions.COSVisitorException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
public class PdfBoxDemo {
/**
* @param args
*/
public static void main(String[] args) {
PDDocument document=null;
try {
document = new PDDocument();
PDPage page = new PDPage();
document.addPage( page );
PDFont font = PDType1Font.HELVETICA_BOLD;
PDPageContentStream contentStream = new PDPageContentStream(document, page);
contentStream.beginText();
contentStream.setFont( font, 12 );
contentStream.moveTextPositionByAmount( 100, 700 );
contentStream.drawString( "Hi I am sandeep patel.This demonstrates the apache pdfbox api." );
contentStream.endText();
contentStream.close();
document.save("D:\sandeep-demo.pdf");
document.close();
} catch (IOException e) {
e.printStackTrace();
} catch (COSVisitorException e) {
e.printStackTrace();
}
}
}