Here am going to explaing how to create a A4 size PDF using PDFBox library
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;
/**
*
* @author Bharathi
*/
public class Printing1 {
public static void main(String[] args) throws Exception {
PDDocument document = new PDDocument();
PDPage page = new PDPage(PDPage.PAGE_SIZE_A4);
document.addPage(page);
PDPageContentStream contentStream = new PDPageContentStream(document, page);
PDFont pdfFont = PDType1Font.HELVETICA;
float fontSize = 12;
contentStream.beginText();
contentStream.setFont(pdfFont, fontSize);
contentStream.moveTextPositionByAmount(100, 100);
contentStream.drawString("Customer Name : Bharathiraja");
contentStream.endText();
contentStream.close();
document.save("d:/demo123.pdf");
document.close();
System.out.println("Done");
}
}
-----------------------------------------------------
Step1: Create a empty PDF document using PDDocument class
Step2: Create a page based on your requirement. PDPage has more options like A4,A3 etc
Step3: ContentStream is used to write text inside the document. It uses the x axis and y axis concept to place text in the Document
Step4: close the stream and save the document inside your hard disk and close the document
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;
/**
*
* @author Bharathi
*/
public class Printing1 {
public static void main(String[] args) throws Exception {
PDDocument document = new PDDocument();
PDPage page = new PDPage(PDPage.PAGE_SIZE_A4);
document.addPage(page);
PDPageContentStream contentStream = new PDPageContentStream(document, page);
PDFont pdfFont = PDType1Font.HELVETICA;
float fontSize = 12;
contentStream.beginText();
contentStream.setFont(pdfFont, fontSize);
contentStream.moveTextPositionByAmount(100, 100);
contentStream.drawString("Customer Name : Bharathiraja");
contentStream.endText();
contentStream.close();
document.save("d:/demo123.pdf");
document.close();
System.out.println("Done");
}
}
-----------------------------------------------------
Step1: Create a empty PDF document using PDDocument class
Step2: Create a page based on your requirement. PDPage has more options like A4,A3 etc
Step3: ContentStream is used to write text inside the document. It uses the x axis and y axis concept to place text in the Document
Step4: close the stream and save the document inside your hard disk and close the document
No comments:
Post a Comment