I am using OpenPDF to generate PDF document with header and footer images added to PDF. A paragraph needs to be added to center of the PDF with some of the text content is dynamic based on input request and everything needs surrounded by border. I am able to generate the PDf with header and footer added to it but having problems adding paragraph to center of the pdf and border. Any help is appreciated.
I tried different ways by adding table or using canvas but could not solve it.
public void generatePDF(String firstName, String lastName){
try {
Document doc = new Document();
PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(DEST));
//setting font family, color
Font font = new Font(Font.HELVETICA, 16, Font.BOLDITALIC, Color.RED);
doc.open();
addHeaderImageToPDF(doc,writer);
Paragraph para = new Paragraph("Hello! This PDF is created for "+firstName+" "+lastName, font);
para.setAlignment(Element.ALIGN_MIDDLE);
doc.add(para);
addFooterImageToPDF(doc,writer);
addBorderToPDF(doc);
doc.close();
writer.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void addBorderToPDF(Document document){
Rectangle rect= new Rectangle(531.5f,820,38.5f,0); // you can resize rectangle
rect.enableBorderSide(1);
rect.enableBorderSide(2);
rect.enableBorderSide(4);
rect.enableBorderSide(8);
rect.setBorderColor(Color.BLACK);
rect.setBorderWidth(2);
document.add(rect);
}
private void addHeaderImageToPDF(Document document, PdfWriter writer){
try {
Image image = Image.getInstance(HEADER_IMAGE_PATH);
//image = Image.getInstance(IMAGE_PATH);
image.scalePercent(50f);
//image.setAbsolutePosition(0, (float) (PageSize.A4.getHeight() - 20.0));
image.setAbsolutePosition(40, 675);
System.out.println(image.getScaledHeight());
document.add(image);
image.scaleToFit(100f, 100f);
//document.add(image);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void addFooterImageToPDF(Document document, PdfWriter writer){
try {
Image image = Image.getInstance(FOOTER_IMAGE_PATH);
//image = Image.getInstance(IMAGE_PATH);
image.scalePercent(50f);
//image.setAbsolutePosition(0, (float) (PageSize.A4.getHeight() - 20.0));
image.setAbsolutePosition(40, 450);
System.out.println(image.getScaledHeight());
document.add(image);
image.scaleToFit(100f, 100f);
//document.add(image);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Wanted to add paragraph at the center and whole content of PDF should be surrounded by border with dark blue color. Any help is appreciated.