Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用writeSelectedRows时出现iText PDF表格错误_Java_Pdf_Itext - Fatal编程技术网

Java 使用writeSelectedRows时出现iText PDF表格错误

Java 使用writeSelectedRows时出现iText PDF表格错误,java,pdf,itext,Java,Pdf,Itext,我有下一个错误。 我正在尝试使用iText创建一个特定格式的PDF。我选择在页面的每个部分使用表格,因为我需要的格式都有表格。好的,我已经做了所有的事情,我创建了表并用doc.add(table)方法添加了它,这很好,但是我需要将表设置到一个特定的位置。所以我选择使用table.writeSelectedRows()方法,这很好 错误来了,这是我的代码: table_SectionTwo.addCell(cell_White); table_SectionTwo.

我有下一个错误。 我正在尝试使用iText创建一个特定格式的PDF。我选择在页面的每个部分使用表格,因为我需要的格式都有表格。好的,我已经做了所有的事情,我创建了表并用
doc.add(table)
方法添加了它,这很好,但是我需要将表设置到一个特定的位置。所以我选择使用
table.writeSelectedRows()
方法,这很好

错误来了,这是我的代码:

table_SectionTwo.addCell(cell_White);
                table_SectionTwo.addCell(cell_White);
                table_SectionTwo.addCell(p);
                table_SectionTwo.addCell(cell_OrderDate);   
                table_SectionTwo.addCell(cell_CustomerOrderDate);
                table_SectionTwo.addCell(cell_OrderNumberSection);                                                              

    float[] columnWidths = new float[] {38f, 105f, 90f};
                    table_SectionTwo.setTotalWidth(columnWidths);
                    table_SectionTwo.setLockedWidth(true);
                    table_SectionTwo.completeRow();                 
                    table_SectionTwo.writeSelectedRows(0, -1, 260f, 770f, super.getPdfWriter().getDirectContent());                     
                    doc.add(table_SectionTwo);
如您所见,如果我执行此代码,将添加相同的表2次

问题是当我删除doc.add(table)时,我这样做只是为了使用
table.writeSelectedRows()
将一个表添加到特定位置。这就是我的代码保持不变的方式:

table_SectionTwo.writeSelectedRows(0, -1, 260f, 770f, super.getPdfWriter().getDirectContent());                     
//super.addTable(table_SectionTwo);
我评论了
doc.add(表)
。 这应该只写一个表。但这不起作用。当我这样做时:

ExceptionConverter: java.io.IOException: The document has no pages.
    at com.itextpdf.text.pdf.PdfPages.writePageTree(PdfPages.java:113)
    at com.itextpdf.text.pdf.PdfWriter.close(PdfWriter.java:1217)
    at com.itextpdf.text.pdf.PdfDocument.close(PdfDocument.java:777)
    at com.itextpdf.text.Document.close(Document.java:398)
    at PDFConstructor.CloseDocument(PDFConstructor.java:85)
    at InvoicePDF.CloseDocument(InvoicePDF.java:58)
    at Demo.main(Demo.java:72)
奇怪的是,当我对
doc.add(table)
进行注释时,这不起作用,而当我对
table.writeSelectedRows()进行注释时,
doc.add(table)
效果很好。 只有当我对
doc.add(table)
进行了注释并且
table.writeSelectedRows()未注释时,才会发生此错误


请帮帮我

虽然您在问题中没有提供足够的信息,但我认为问题是由于您没有定义表格的宽度造成的

做这个测试:询问桌子的总高度。如果iText返回0,那么您忘记定义表的宽度;如果它不返回0,那么iText知道宽度,这可能是因为您明确定义了宽度,也可能是因为您使用了document.add(table),它根据document对象的页面度量计算了表的维度


如果还有其他问题,您必须提供更多信息。

我从您的问题中了解到,您希望在文档中指定位置写入特定行。如果正确,super.getPdfWriter().getDirectContent())有必要吗?我不这么认为。要分析这一部分,我需要你的整个代码片段或这段代码的演示版本,它们都解释了这一点

第二:在内部,itext也使用PdfContentByte使用PdfPRow编写PdfPTable&还记得根据作者(Bruno)的说法,itext是基于构建器模式构建的。如果前面的行对您没有任何意义,请跳过它

您当前正在向表中添加内容,甚至在设置表的所有必需属性之前

table_SectionTwo.addCell(cell_White);
table_SectionTwo.addCell(cell_White);
table_SectionTwo.addCell(p);
table_SectionTwo.addCell(cell_OrderDate);   
table_SectionTwo.addCell(cell_CustomerOrderDate);
table_SectionTwo.addCell(cell_OrderNumberSection);  
会是这样的

float[] columnWidths = new float[] {38f, 105f, 90f};
PdfPTable table_SectionTwo= new PdfPTable(clmnWdthTpHdr);
table_SectionTwo.setTotalWidth(500.0f);
table_SectionTwo.setWidthPercentage(100.0f);
table_SectionTwo.setLockedWidth(true);
3.不要使用super.getPdfWriter().getDirectContent())。因为上面的代码显示您正在使用文档,所以我认为您也必须编写以下代码段(类似于:lol)

在try-catch中使用pdfWrtr.getDirectContent();相反 这些都是基于我的代码分析。 还有一点例外

ExceptionConverter: java.io.IOException: The document has no pages.
    at com.itextpdf.text.pdf.PdfPages.writePageTree(PdfPages.java:113)
...............................
 at InvoicePDF.CloseDocument(InvoicePDF.java:58)
    at Demo.main(Demo.java:72)
当文档中没有添加任何内容时,这是一个典型的错误。可能在步骤4中引发(并忽略)异常(根据Itext in Action),也可能您正在执行步骤5(document.close())(尽管步骤4中存在异常)。因此,请附加Demo.java 如果以上内容不够清楚,无法帮助您

ExceptionConverter: java.io.IOException: The document has no pages.
    at com.itextpdf.text.pdf.PdfPages.writePageTree(PdfPages.java:113)
...............................
 at InvoicePDF.CloseDocument(InvoicePDF.java:58)
    at Demo.main(Demo.java:72)