0

How to define page size for dynamic pages?? I have created A4 size page and added to PDDocument. First page is working as expected but the only problem with second page on-words.

public class PdfBoxSample01{
               
    public static void main(String[] args) throws IOException {
        PDDocument document = new PDDocument();
        PDPage pdPag = new PDPage(PDRectangle.A4);
        document.addPage(pdPag);
        try {
            PDPageContentStream contentStream = new PDPageContentStream(document, pdPag, true, true);
            BaseTable parentTable = createParentTable(document, pdPag);
   }    
}
private static BaseTable createParentTable(PDDocument document, PDPage page) {
    float yCordinate = page.getCropBox().getUpperRightY() - 30;
    float margin = 50;
    // starting y position is whole page height subtracted by top and bottom margin
    float yStartNewPage = page.getMediaBox().getHeight() - (2 * margin);
    // we want table across whole page width (subtracted by left and right margin ofcourse)
    float tableWidth = page.getMediaBox().getWidth() - (2 * margin);

    boolean drawContent = true;
    float bottomMargin = 70;
    // y position is your coordinate of top left corner of the table
    float yPosition = yCordinate;

    BaseTable table = null;
    try {
        table = new BaseTable(yPosition, yStartNewPage,
        bottomMargin, tableWidth, margin, document, page, true, drawContent);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return table;
}
Mario Codes
  • 689
  • 8
  • 15
  • 1
    "but the only problem with second page on-words" what do you mean? What were you expecting, and what happened instead? – Tilman Hausherr Nov 03 '20 at 08:53
  • If you observe my code, "PDPage pdPag = new PDPage(PDRectangle.A4); document.addPage(pdPag);" Here I'm creating A4 size page object and adding into the document. After that I'm creating Base Table object and filling some dynamic data. If dynamic records are more means some of the records will print in second page. In second page top margin is more(I feel its not A4 size page). – shankar1987 Nov 03 '20 at 17:04
  • then pass parameters for a bigger page, e.g A3 or custom. Or create smaller graphics. – Tilman Hausherr Nov 04 '20 at 09:51

0 Answers0