Search code examples
javaautomated-testspdfbox

How to test PDF generated with PDFbox


I write a class that read pdf template and add some lines like:

List<PdfTextLine> textLines = pdfBankActPage.getTextLines();
    if (textLines != null) {
        for (PdfTextLine textLine : textLines) {
            contentStream.setFont(cyrillicFont, textLine.getFontSize());
            contentStream.beginText();
            contentStream.newLineAtOffset(textLine.getOffsetX(), textLine.getOffsetY());
                        contentStream.showText(textLine.getText());
            contentStream.endText();
        }
    }

My util class to store one-line info:

public class PdfTextLine {

    private Integer offsetX;
    private Integer offsetY;
    private String text;
    private Integer fontSize;

What is common approach how to test, that PDF was generated correct?


Solution

  • Since your question is lacking detail it is difficult to answer. There are different possibilities to verify that your content was added:

    1. Extract the text from the PDF and check whether your text is in there

      PDFTextStripper stripper = new PDFTextStripper();
      stripper.setStartPage(pageNumber);
      stripper.setEndPage(pageNumber);
      stripper.setAddMoreFormatting(false);
      String text = stripper.getText(this.document);
      
    2. Use a library like pdfcompare (which itself uses Pdfbox) to compare the pdf visually...