Creating And Manipulating PDF Documents In Java

Posted By : Kiran Sharma | 29-Jun-2017

Apache PDFBox is an open source java tool that allows us to work with pdf documents. It comes as a JAR file with the help of which we can create new PDF documents and can manipulate existing documents.

The functionalities provided by this API are:

1. We can extract text from pdf files.

2.By using PDFBox we can print pdf file using java print api.

3. Using this api , pdf files can be stored as png of jpeg files.

4.Using this, we can add digital signature to our files.

Here we are learning how to create a pdf documents and how to write content in that.

 

How to work with PDFBox

=>creating and saving pdf doc.

1.for creating pdf document, PDDocument class is instantiated first.

2.for saving the document, we use save() method of PDDocument class.

3. for closing the document, we use close() method of PDDocument class.

=>creating pages

1.for creating pdf page, we need to instantiate PDPage class.

=>adding pages

1.for adding page to document, we can use addPage() method of PDDocument class and the object of PDPage is passed as parameter.

2.After adding the pages, call save() and close() method of PDDocument class.

Code:

 

import org.apache.pdfbox.pdmodel.PDDocument;

PDDocument document = new PDDocument();

File file = new File(filename);

PDPage page = new PDPage(new PDRectangle(PDRectangle.A5.getHeight(), PDRectangle.A4.getWidth()));

document.addPage(page);

//PDPageContentStream is used to write content on PDF page.

PDPageContentStream contentStream = new PDPageContentStream(document, page, AppendMode.APPEND, true);

PDStreamUtils.write(contentStream, "TITLE", PDType1Font.TIMES_ROMAN, 20, 40, 585, new Color(0, 0, 0));

contentStream.close();//mandatory to close

float margin = 10;

float yStartNewPage = page.getMediaBox().getHeight() - (10 * margin);

float tableWidth = page.getMediaBox().getWidth() + (18 * margin);

boolean drawContent = true;

float yStart = yStartNewPage;

float bottomMargin = 80;

float pageTopMargin = 80;

//BaseTable is used to create table on PDF

BaseTable table = new BaseTable(yStart, yStartNewPage, pageTopMargin, bottomMargin, tableWidth, margin * 3,document, page, true, drawContent);

LineStyle lineStyle3 = new LineStyle(Color.BLACK, 1);

LineStyle lineStyle4 = LineStyle.produceDotted(Color.BLACK, 1);

ArrayList<Row<PDPage>> rows = new ArrayList();

// rows creation with height

for (int i = 0; i <=2 ; i++) {

rows.add(table.createRow(30f));

rows.get(i).setHeight(5);

}

Cell<PDPage> cell= rows.get(0).createCell(19f, "[1][0]");

cell.setBottomBorderStyle(lineStyle4);

cell.setLeftBorderStyle(lineStyle3);

cell = rows.get(0).createCell(19f, "[1][1]");

cell.setLeftBorderStyle(lineStyle4);

cell.setBottomBorderStyle(lineStyle4);

cell = rows.get(0).createCell(19f, "[1][2]");

cell.setLeftBorderStyle(lineStyle4);

cell.setBottomBorderStyle(lineStyle4);

cell = rows.get(1).createCell(19f, "[2][0]");

cell.setBottomBorderStyle(lineStyle4);

cell = rows.get(1).createCell(19f, "[2][1]");

cell.setLeftBorderStyle(lineStyle4);

cell.setBottomBorderStyle(lineStyle4);

cell = rows.get(1).createCell(19f, "[2][2]");

cell.setLeftBorderStyle(lineStyle4);

cell.setBottomBorderStyle(lineStyle4);

cell = rows.get(2).createCell(19f, "[3][0]");

cell.setLeftBorderStyle(lineStyle3);

cell = rows.get(2).createCell(19f, "[3][1]");

cell.setLeftBorderStyle(lineStyle4);

cell = rows.get(2).createCell(19f, "[3][2]");

cell.setLeftBorderStyle(lineStyle4);

// setting font size and alignment of the cell data.

for (int i = 0; i <rows.size(); i++) {

for(int j=0;j<=2;j++){

rows.get(i).getCells().get(j).setFontSize(12f);

rows.get(i).getCells().get(j).setAlign(HorizontalAlignment.CENTER);;

}

}

table.draw();

PDPageContentStream contentStream1 = new PDPageContentStream(document, page, AppendMode.APPEND, true);

PDStreamUtils.write(contentStream1, "Bhaasha Sàrl, Rue Galilée 7, CH-1400 Yverdon-les-Bains",PDType1Font.HELVETICA, 8, 180, 50, new Color(102, 102, 102));

PDStreamUtils.write(contentStream1,"# 00000 " + CustomDateUtils.getDateTimeStringByFormatOfDate(System.currentTimeMillis(),

"dd-MM-yyyy HH:mm:ss", "Long", false),

PDType1Font.TIMES_ROMAN, 8, 250, 35, new Color(102, 102, 102));

contentStream1.close();

document.save(file);

document.close();

 

About Author

Author Image
Kiran Sharma

Kiran has good knowledge of java with Servlets, JSPs, Spring, and hibernate frameworks. She is very honest towards her work. Her hobby is listening to music.

Request for Proposal

Name is required

Comment is required

Sending message..