Itext PdfReader getNumberOfPages()始终只返回一页

Itext PdfReader getNumberOfPages()始终只返回一页,itext,pdf-reader,Itext,Pdf Reader,我有一个代码,动态创建字节输入流,然后将其转换为PDF。我使用的是iText 5.1.2版本。代码调用Pdfreader上的getNumberOfPages()方法对其进行迭代并生成完整的PDF,但每次该方法返回1时。只打印第一页。应该放在第二页的数据正在丢失。 当我看到要转换成PDF的字符串内容时,我可以看到来自上游的完整数据。下面是我的代码: int getPageCount(String str) throws Exception{ Document doc = new Docum

我有一个代码,动态创建字节输入流,然后将其转换为PDF。我使用的是iText 5.1.2版本。代码调用Pdfreader上的getNumberOfPages()方法对其进行迭代并生成完整的PDF,但每次该方法返回1时。只打印第一页。应该放在第二页的数据正在丢失。 当我看到要转换成PDF的字符串内容时,我可以看到来自上游的完整数据。下面是我的代码:

int getPageCount(String str) throws Exception{
    Document doc = new Document();
    ByteArrayOutputStream out = new ByteArrayOutputStream();;
    str = str.replaceAll("</lf>", "\n");
    PdfCopy copy = new PdfCopy(doc, out);
    doc.open();
    PdfReader reader;
    reader = new PdfReader(new ByteArrayInputStream(str.getBytes()));
    int i = reader.getNumberOfPages();
    for (int page = 0; page < i;) {
        copy.addPage(copy.getImportedPage(reader, ++page));
    }
    copy.freeReader(reader);
    doc.close();
    out.flush();
    return i;
}
int getPageCount(字符串str)引发异常{
单据单据=新单据();
ByteArrayOutputStream out=新建ByteArrayOutputStream();;
str=str.replaceAll(“,”\n”);
PdfCopy copy=新的PdfCopy(文件,输出);
doc.open();
PDF阅读器;
reader=newpdfreader(newbytearrayinputstream(str.getBytes());
int i=reader.getNumberOfPages();
对于(int page=0;page
根据我对您问题的理解,您试图估算将某个字符串打印到pdf文档会占用多少页面

更好的方法(iText7!!)是:

public static void main(String[] args)
{
    for(int i=0;i<100;i++)
        System.out.println(i + "\t" + getPageCount(lipsum(i)));
}

static int getPageCount(String str)
{
    OutputStream out = new ByteArrayOutputStream();
    PdfWriter writer = new PdfWriter(out);
    PdfDocument pdfDocument = new PdfDocument(writer);
    Document layoutDocument = new Document(pdfDocument);
    layoutDocument.add(new Paragraph(str));
    return pdfDocument.getNumberOfPages();
}

static String lipsum(int nofRepeats)
{
    String retval = "";
    String base = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
    for(int i=0;i<nofRepeats;i++)
    {
        retval += base;
    }
    return retval;
}
publicstaticvoidmain(字符串[]args)
{

对于(int i=0;i,根据我对您的问题的理解,您试图估算将某个字符串打印到pdf文档中会占用多少页面

更好的方法(iText7!!)是:

public static void main(String[] args)
{
    for(int i=0;i<100;i++)
        System.out.println(i + "\t" + getPageCount(lipsum(i)));
}

static int getPageCount(String str)
{
    OutputStream out = new ByteArrayOutputStream();
    PdfWriter writer = new PdfWriter(out);
    PdfDocument pdfDocument = new PdfDocument(writer);
    Document layoutDocument = new Document(pdfDocument);
    layoutDocument.add(new Paragraph(str));
    return pdfDocument.getNumberOfPages();
}

static String lipsum(int nofRepeats)
{
    String retval = "";
    String base = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
    for(int i=0;i<nofRepeats;i++)
    {
        retval += base;
    }
    return retval;
}
publicstaticvoidmain(字符串[]args)
{

对于(int i=0;iHi和Stack Overflow,欢迎使用),请花点时间浏览以了解您在这里的道路(并获得您的第一个徽章),阅读如何创建并检查,以增加获得反馈和有用答案的机会。我是否正确读取了源代码?您获取字符串,执行一些字符串操作,将其编码为字节数组,然后尝试将其读取为PDF?很幸运,您可以阅读它并添加PDF…您好,欢迎使用堆栈溢出,请花点时间阅读o通过网络了解您在这里的生活方式(并赢得您的第一枚徽章),阅读如何创建,并进行检查,以增加获得反馈和有用答案的机会。我是否正确读取了源代码?你获取一个字符串,执行一些字符串操作,将其编码为字节数组,然后尝试将其读取为PDF?幸运的是,你可以读取它并添加一个PDF…这似乎是一个iText 7答案,而问题为了避免在读者的回答中出现挫折,你至少应该在回答中明确指出。如果你没有绑定到一个特定版本的ITEXT,考虑使用PFDHTML。这似乎是一个ITEXT 7答案,而问题明确地引用了版本5.1.2。在读者的回答中,你至少应该在回答中明确指出。如果你没有绑定到一个特定版本的iTXT,请考虑使用PFDHTML。