PDF - Yash-777/LearnJava GitHub Wiki

Apache PDFBox MVN

Document Creation using PDFBox.

Create a Blank PDF This small sample shows how to create a new PDF document using PDFBox.

// Create a new empty document
PDDocument document = new PDDocument();

// Create a new blank page and add it to the document
PDPage blankPage = new PDPage();
document.addPage( blankPage );

// Save the newly created document
document.save("BlankPage.pdf");

// finally make sure that the document is properly closed.
document.close();

Example:

public class Report_PDF {
    
    // Create a new font object selecting one of the PDF base fonts
    static PDFont fontPlain = PDType1Font.HELVETICA;
    static PDFont fontBold = PDType1Font.HELVETICA_BOLD;

    static String saveAs = "D:/Yash/docc1.pdf";
    public static void main(String[] args)throws IOException {
        
        // Create a new empty document
        PDDocument document = new PDDocument();
        
        // Create a new blank page and add it to the document
        PDPage page = new PDPage(PDRectangle.A4);
        document.addPage( page );
        
        getPageSpaces(page);
        
        // Start a new content stream which will "hold" the to be created content
        PDPageContentStream pageCS = new PDPageContentStream(document, page);
        
        try {
            // Define a text content stream using the selected font.
            pageCS.beginText();
            pageCS.setFont( fontBold, 12 );
            //setting the coordinates from where the text will be displayed on the page (first one is the column, second is for row)
            pageCS.newLineAtOffset(15, 750);
            
            //drawPageTitle();
            
            pageCS.showText( "Hello World" );
            
            //contentStream.newLine(); // Must call beginText() before newLine()
            pageCS.endText(); // You must call beginText() before calling endText.
        } finally {
            // Make sure that the content stream is closed:
            pageCS.close();
        }
        // drawBoundingBox(document, page); // https://stackoverflow.com/a/52969119/5081877

        PDPage page2 = new PDPage();
        page2 = customPaperSize(page2);
        document.addPage( page2 );
        
        // Save the results and ensure that the document is properly closed:
        document.save( new File( saveAs ) );
        document.close();
    }
    public static void getPageSpaces(PDPage page) {
        float x_Spaces= page.getMediaBox().getWidth();
        float y_Spaces = page.getMediaBox().getHeight();
        System.out.format("Total Page Cells [X:Width=%f]:[Y:Height=%f]\n", x_Spaces, y_Spaces);
    }
    public static PDPage customPaperSize(PDPage page) { // PDRectangle.A4
        PDRectangle rectangle = new PDRectangle();
        rectangle.setLowerLeftX(20);
        rectangle.setLowerLeftY(20);
        rectangle.setUpperRightX(615); // PAGE_WIDTH
        rectangle.setUpperRightY(815); // PAGE_HEIGHT
        
        page.setMediaBox(rectangle);
        page.setCropBox(rectangle);
        page.setArtBox(rectangle);
        return page;
    }
}

Boxable, A High Level API to Creates Table On Top of Apache PdfboxMaven, github

Example: https://ulfdittmer.com/view?PdfboxTable Jars required google.guava, SLF4J API

static PDFont fontBold = PDType1Font.HELVETICA_BOLD;
public static void tableContent(PDDocument document, PDPage page) throws IOException {
    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 yStart = yStartNewPage;
    float bottomMargin = 70;
    // y position is your coordinate of top left corner of the table
    float yPosition = 550;
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm");
    
    BaseTable table = new BaseTable(yPosition, yStartNewPage,
            bottomMargin, tableWidth, margin, document, page, true, drawContent);
    
    createRow(table,"Cell Name", "Cell Value");
    createRow(table,"Data",dateFormat.format(Calendar.DATE));
    
    table.draw();
}
public static void createRow(BaseTable table, String left, String right) {
    // the parameter is the row height
    be.quodlibet.boxable.Row<PDPage> row = table.createRow(20);
    // the first parameter is the cell width
    Cell<PDPage> cell = row.createCell(50, left);
    cell.setFont(fontBold);
    cell.setFontSize(20);
    // vertical alignment
    cell.setValign(VerticalAlignment.MIDDLE);
    // border style
    cell.setTopBorderStyle(new LineStyle(Color.BLACK, 10));
    
    cell = row.createCell(50, right);
    cell.setFontSize(15);
    cell.setFont(fontBold);
    table.addHeaderRow(row);
}
⚠️ **GitHub.com Fallback** ⚠️