Skip to content

Pdf Builder serves as an extension to itextpdf5 library to facilitate the process of making PDF using the Java language.

License

Notifications You must be signed in to change notification settings

silvagpmiguel/pdf-builder

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PDF Builder CI/CD OSSRH Maven Central

Pdf Builder serves as an extension to itextpdf5 library to facilitate the process of making PDF using the Java language.

Features

  • Easily generate a PDF or get his data with PdfBuilder
  • Make your own PageEvent and connect it to PdfBuilder (usefull for repeating patterns every page/chapter start/end)
  • Create new PDF elements easily with ElementFactory (tables, images, text..)
  • Insert a new page after an element render (valid with tables, images and phrases)

Usage

PdfBuilder

Generic Usage

new PdfBuilder()
    .open() // Prepares document to start writing new elements (mandatory)
    .addElement(...) // Add Elements(PdfPTable, Font, .. | TableWrapper, FontWrapper, ..)
    .close() // Close streams/document
    .build() // Build the entire PDF
    .generatePdf(PDF_PATH); // Generate PDF at PDF_PATH
 /* .getPdfData(); get Pdf data as a byte[] */

Set PageEvent in PdfBuilder

/* Using constructor */
new PdfBuilder(new PageEvent()) 
/* Using setter */
new PdfBuilder().setPageEvent(new PageEvent)...

Set a PageEvent and a List of Elements

/* PdfBuilder build method will build all elements in the list */
new PdfBuilder(new PageEvent(), List<ElementWrapper>);

ElementFactory

/* Creates a table cell, where you can define cell styles (DEFAULT: BORDERED) */
ElementFactory.newCell()
/* Creates a font (DEFAULT: family - HELVETICA, size - 12) */
ElementFactory.newFont()
/* Creates a new Table with NUM_COLS columns */
ElementFactory.newTable(NUM_COLS)
/* Automatically creates a new Table with the contents of the List */
ElementFactory.newTable(List<List<String>> rows)
/* Automatically creates a new Table with a custom header */
ElementFactory.newTable(List<List<String>> rows, int headerRows, CellWrapper headerStyle)
/* Automatically creates a new Table with a custom header and footer */
ElementFactory.newTable(
    List<List<String>> rows,
    int headerRows, CellWrapper headerStyle, FontWrapper headerFont,
    int footerRows, CellWrapper footerStyle, CellWrapper footerFont)
/* Creates a new Image that needs to be placed in resources */
ElementFactory.newImage(RESOURCES_RELATIVE_PATH)
/* Creates a phrase with TEXT */
ElementFactory.newPhrase(TEXT)

PageEvent

@Override
public void onStartPage(PdfWriter writer, Document document) {
    // gets writer and document in every page start state
}
@Override
public void onEndPage(PdfWriter writer, Document document) {
    // gets writer and document in every page end state
}

Examples

Pdf Generation

CellWrapper NO_BORDER = ElementFactory.newCell().withBorder(Rectangle.NO_BORDER);
CellWrapper BORDERED = ElementFactory.newCell();
FontWrapper bold = ElementFactory.newCell().withBold().withSize(12f);
FontWrapper normal = ElementFactory.newFont().withSize(10f);
new PdfBuilder()
    .open()
    .addImage(pdfBuilder.newImage("logo.png"))
    .addElement(pdfBuilder.newPhrase("Title"))
    .addElement(pdfBuilder.newTable(2)
        .addTextRow(Arrays.asList("TITLE1", "TITLE2"), NO_BORDER, bold)
        .addTextRow(Arrays.asList("BODY1", "BODY2"), BORDERED, normal))
    .close()
    .build()
    .generatePdf();

Add a Logo and a Custom Title every Page Start

public class PageEvent{
    public PageEvent(){ pageNum = 0; }
    @Override
    public void onStartPage(PdfWriter writer, Document document) {
        pageNum++;
        try {
            document.add(ElementFactory.newImage("logo.png")
                .withSpacingAfter(10));
            document.add(ElementFactory.newTable(2)
                .addTextRow("Hello World", "Page "+pageNum)
                .withSpacingAfter(10));
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }
}
<dependency>
  <groupId>com.github.silvagpmiguel</groupId>
  <artifactId>pdf-builder</artifactId>
  <version>${pdf-builder.version}</version>
</dependency>

License

Apache License 2.0, see LICENSE.

About

Pdf Builder serves as an extension to itextpdf5 library to facilitate the process of making PDF using the Java language.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Languages