我一直在打破我的头从过去1小时我无法解决这个问题...我知道它很容易在Java中写PDF,但我的输出流大小为零,...这是我的代码..PDFWriter中的ByteArrayOutputStream大小为零?
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();
注:我能打印地图键值也不过当我下载PDF大小为零。
当您关闭()文档时,会写入PDF的最终字节。当我查看你的代码时,我发现你忽略了这一点;您在关闭文档之前尝试使用字节数组*。由于明显的原因,这是错误的。 –
当您打开()文档时,会写入第一个字节。你指称'dsf'不包含任何字节是错误的。它至少包含PDF文件的标题:“PDF-1”等。当然,如果你看看文件的大小,那看起来好像是0字节,因为它只有十几个字节。但是,如你所说,它不是**零。 –
把作家行放在System.out.print – PSo