Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/372.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 PDFWriter中的ByteArrayOutputStream大小为零?_Java_Pdf_Itext_Pdf Generation - Fatal编程技术网

Java PDFWriter中的ByteArrayOutputStream大小为零?

Java PDFWriter中的ByteArrayOutputStream大小为零?,java,pdf,itext,pdf-generation,Java,Pdf,Itext,Pdf Generation,我已经打破了我的头从过去的一个小时,我不能解决这个问题…我知道它很容易用java编写的PDF文件,但我的outputstream大小是零,。。。这是我的密码 Document document = new Document(); try { ByteArrayOutputStream dsf = new ByteArrayOutputStream(); PdfWriter writer = PdfWriter.getInstance(docu

我已经打破了我的头从过去的一个小时,我不能解决这个问题…我知道它很容易用java编写的PDF文件,但我的outputstream大小是零,。。。这是我的密码

 Document document = new Document();
      try
    {
        ByteArrayOutputStream dsf = new ByteArrayOutputStream();
        PdfWriter writer = PdfWriter.getInstance(document, dsf);
        document.open();
        document.add(new Paragraph("Some content here"));

        // Set attributes here
        document.addAuthor("Lokesh Gupta");
        document.addCreationDate();
        document.addCreator("HowToDoInJava.com");
        document.addTitle("Set Attribute Example");
        document.addSubject("An example to show how attributes can be added to pdf files.");
        if (frontPageMap != null && !frontPageMap.isEmpty()) {

            PdfPTable table = new PdfPTable(frontPageMap.size()); // creating
                                                                    // columns.
            table.setWidthPercentage(100); // Width 100%
            table.setSpacingBefore(10f); // Space before table
            table.setSpacingAfter(10f); // Space after table

            for (Map.Entry<String, Object> entry : frontPageMap.entrySet()) {
                PdfPCell tempCell = new PdfPCell(new Paragraph(entry.getKey()));
                tempCell.setBorderColor(BaseColor.BLUE);
                tempCell.setPaddingLeft(10);
                tempCell.setHorizontalAlignment(Element.ALIGN_CENTER);
                tempCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                table.addCell(tempCell);
            }
            document.add(table);

        }
        System.out.println("Size Of Byte Array is "+dsf.size());
        ByteArrayInputStream bInput = new ByteArrayInputStream(dsf.toByteArray());
        file = new DefaultStreamedContent(bInput, "pdf", fileName);
        document.close();
        writer.close();
Document Document=新文档();
尝试
{
ByteArrayOutputStream dsf=新建ByteArrayOutputStream();
PdfWriter writer=PdfWriter.getInstance(文档,dsf);
document.open();
添加(新段落(“此处的某些内容”);
//在此处设置属性
文件作者(“Lokesh Gupta”);
document.addCreationDate();
document.addCreator(“HowToDoInJava.com”);
document.addTitle(“设置属性示例”);
addSubject(“显示如何将属性添加到pdf文件的示例”);
if(frontPageMap!=null&&!frontPageMap.isEmpty()){
PdfPTable table=新的PdfPTable(frontPageMap.size());//正在创建
//列。
表.setWidthPercentage(100);//宽度100%
table.setspacebefore(10f);//表前的空格
table.setspaceafter(10f);//表后的空格
对于(Map.Entry:frontPageMap.entrySet()){
PdfPCell tempCell=newpdfpcell(新段落(entry.getKey());
tempCell.setboordercolor(BaseColor.BLUE);
tempCell.setPaddingLeft(10);
tempCell.setHorizontalAlignment(Element.ALIGN_CENTER);
tempCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
表2.addCell(tempCell);
}
文件。添加(表);
}
System.out.println(“字节数组的大小为”+dsf.Size());
ByteArrayInputStream bInput=新的ByteArrayInputStream(dsf.toByteArray());
file=新的DefaultStreamedContent(bInput,“pdf”,文件名);
document.close();
writer.close();
注意:我也可以打印地图键值,但下载PDF时大小为零


在尝试访问内容之前,您可能应该完成PDF的创建(使用
document.close()
writer.close()
):

    //Finish writing the PDF
    document.close();
    writer.close();
    //now check what's been written:
    System.out.println("Size Of Byte Array is "+dsf.size());
    ByteArrayInputStream bInput = new ByteArrayInputStream(dsf.toByteArray());
    file = new DefaultStreamedContent(bInput, "pdf", fileName);

在尝试访问内容之前,您可能应该完成PDF的创建(使用
document.close()
writer.close()
):

    //Finish writing the PDF
    document.close();
    writer.close();
    //now check what's been written:
    System.out.println("Size Of Byte Array is "+dsf.size());
    ByteArrayInputStream bInput = new ByteArrayInputStream(dsf.toByteArray());
    file = new DefaultStreamedContent(bInput, "pdf", fileName);
添加此代码。在这里,您完成了pdf文件的编写,然后可以打印文件的大小。根据内容,我的大小可以为零,但生成的pdf文件中应该写入所有记录……干杯:)


添加此代码。在这里,您完成了pdf文件的编写,然后可以打印文件的大小。根据内容,my的大小可以为零,但生成的pdf文件中应该写入所有记录……干杯:)

当您
close()时,将写入pdf的最后字节
文档。当我查看您的代码时,我发现您忽略了这一点;您试图在关闭文档之前对字节数组进行处理。这是错误的,原因很明显。第一个字节是在您
打开()时写入的
文档。你声称的
dsf
不包含任何字节是错误的。它至少包含PDF文件的头:
PDF-1.
等等。当然,如果你看文件的大小,那看起来可能是0字节,因为它只有十几个字节。但它并不像你所说的那样是零。把writer行放到前面系统退出。print@PSo非常感谢,PDF的最后字节是在您
close()
文档时写入的。当我查看您的代码时,我发现您忽略了这一点;您试图在关闭文档之前对字节数组进行处理。这显然是错误的。第一个字节是在
open()时写入的
文档。你声称的
dsf
不包含任何字节是错误的。它至少包含PDF文件的头:
PDF-1.
等等。当然,如果你看文件的大小,那看起来可能是0字节,因为它只有十几个字节。但它并不像你所说的那样是零。把writer行放到前面系统退出。print@PSo谢谢你,伙计