简体   繁体   English

如何测试PDFbox生成的PDF

[英]How to test PDF generated with PDFbox

I write a class that read pdf template and add some lines like:我编写了一个 class 来读取 pdf 模板并添加一些行,例如:

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:我的 util class 存储一行信息:

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?什么是常见的测试方法,PDF 生成正确?

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从 PDF 中提取文本并检查您的文本是否在其中

    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...使用pdfcompare 之类的库(它本身使用 Pdfbox)在视觉上比较 pdf...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM