How can We set the footer on every page of pdf dynamically by using Java code

21 Views Asked by At

I have a code that I am showing in PDF but, I want to set this content at the footer of pdf. The pages are created dynamically, which means the Page changes according to the content in the pdf. So I want to add this content in the footer and want the footer on every page of the pdf. Help me to solve this problem.

package practise;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import com.itextpdf.text.pdf.draw.LineSeparator;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class NewPdf{
public static void main(String[] args) throws FileNotFoundException, DocumentException, IOException {
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("D:\\Osama\\Osama Main\\Travel\\newpdf.pdf"));
    document.open();
    
    Font simpleFont = new Font(Font.FontFamily.HELVETICA, 7);
    Font simpleFont2  = new Font(Font.FontFamily.HELVETICA, 10);
    LineSeparator line = new LineSeparator();
    line.setLineColor(new BaseColor(0, 0, 0)); // Set line color (black in this example)
    line.setLineWidth(1.5f);
    
    document.add(line);
    document.add(new Paragraph(" "));
    
    PdfPTable table = new PdfPTable(3);
    float[] columnWidths = {20f, 60f, 20f};
    table.setWidths(columnWidths);
    table.setWidthPercentage(100); // Set the table width to 100% of the page width
    
    PdfPCell cell12 = createCell("", simpleFont);
    try {
        Image ticketImageimg = Image.getInstance("D:\\Osama\\Osama Main\\Travel\\protected.png");
        ticketImageimg.scaleToFit(50, 50);
        Paragraph imgParagraph = new Paragraph();
        imgParagraph.add(new Chunk(ticketImageimg, 0, 0, true));
        imgParagraph.add(new Paragraph("       Powered by PenGuin", simpleFont));
        imgParagraph.setAlignment(Element.ALIGN_CENTER);
        cell12.addElement(imgParagraph);
        cell12.setBorder(0);
    }catch(Exception e){
        
    }
    PdfPCell cell13 = createCell(""
            + "PH Travel Ltd t/a Perfect Holidays\r\n"
            + "68A George Lane,South Woodford, London E18 1LW", simpleFont2);
    cell13.setHorizontalAlignment(Element.ALIGN_CENTER);
    cell13.setBorder(0);
    
    PdfPCell cell14 = createCell("", simpleFont);
    try {
        Image ticketImageimg = Image.getInstance("D:\\Osama\\Osama Main\\Travel\\iata.png");
        ticketImageimg.scaleToFit(50, 50);
        Paragraph imgParagraph = new Paragraph();
        imgParagraph.add(new Chunk(ticketImageimg, 0, 0, true));
        imgParagraph.add(new Paragraph("       01/Mar/2024 11:20:57", simpleFont));
        imgParagraph.setAlignment(Element.ALIGN_CENTER);
        cell14.addElement(imgParagraph);
        cell14.setBorder(0);
    }catch(Exception e){
        
    }
    table.addCell(cell12);
    table.addCell(cell13);
    table.addCell(cell14);
    document.add(table);
    document.close();
}

private static PdfPCell createCell(String text, Font font) {
    PdfPCell cell = new PdfPCell(new Phrase(text, font));
    cell.setHorizontalAlignment(Element.ALIGN_LEFT);
    cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
    return cell;
}

}

0

There are 0 best solutions below