使用iText创建PDF

时间:2013-05-06 17:18:17

标签: java itext

我正在尝试使用iText创建PDF,但我遇到了很多困难。简而言之,我想做的是:

  • 阅读模板pdf
  • 在内存中复制模板
  • 在副本上绘制一个表格
  • 将副本pdf写入输出流

到目前为止,它看起来像这样

// read in template pdf
InputStream templateStream = getServletContext().getResourceAsStream(labelsTemplate);
PdfReader reader = new PdfReader(templateStream);

// create a table in a new document
Document document = new Document();
PdfCopy copy = new PdfCopy(document, os);
document.open();

PdfPTable table = new PdfPTable(2);
PdfPCell cell;
cell = new PdfPCell(new Phrase("row 1; cell 1"));
table.addCell(cell);
cell = new PdfPCell(new Phrase("row 1; cell 2"));
table.addCell(cell);
document.add(table);

有人可以解释一旦我使用PdfReader读取它后如何制作模板的副本吗?有没有办法将表格写入模板副本而不是新文档?

对于将来的参考资料,这是我所做的:

response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "inline;filename=\"scheduler-labels.pdf\"");
ServletOutputStream os = response.getOutputStream();

// read in template pdf
InputStream templateStream = getServletContext().getResourceAsStream(labelsTemplate);
PdfReader reader = new PdfReader(templateStream);

// make new pdf document to draw table and output to memory
Document document = new Document(reader.getPageSize(1));
ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
PdfWriter.getInstance(document, baos);

// write table
document.open();
PdfPTable table = new PdfPTable(2);
table.setWidthPercentage(110);
PdfPCell cell;
cell = new PdfPCell(new Phrase("row 1; cell 1"));
table.addCell(cell);
cell = new PdfPCell(new Phrase("row 1; cell 2"));
table.addCell(cell);
cell = new PdfPCell(new Phrase("row 2; cell 1"));
table.addCell(cell);
cell = new PdfPCell(new Phrase("row 2; cell 2"));
table.addCell(cell);
document.add(table);
document.close();

// read in newly generated table pdf
PdfReader tableReader = new PdfReader(baos.toByteArray());
ByteArrayOutputStream baosCombined = new ByteArrayOutputStream();
PdfStamper stamper = new PdfStamper(tableReader, baosCombined);

// get a page from the template pdf
PdfImportedPage page = stamper.getImportedPage(reader, 1);

// add to background of table pdf
PdfContentByte background;
background = stamper.getUnderContent(1);
background.addTemplate(page, 0, 0);

stamper.close();
tableReader.close();
reader.close();

// write to servlet output
baosCombined.writeTo(os);
os.flush();
os.close();

1 个答案:

答案 0 :(得分:0)

在研究我的评论中引用的样本时,正是[Tuan]所需要的,我将其作为答案来表达:

来自Stationery.javachapter 6的示例iText in Action — 2nd Edition基本上展示了如何使用给定PDF的内容作为新PDF的背景(类似文具),同时用新的PDF填充其前景内容。

中央代码如下:

public class Stationery extends PdfPageEventHelper
{
    [...]

    public void createPdf(String filename) throws Exception
    {
        // step 1
        Document document = new Document(PageSize.A4, 36, 36, 72, 36);
        // step 2
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
        useStationary(writer);
        // step 3
        document.open();
        // step 4
        [... add content to PDF ...]
        // step 5
        document.close();
    }

    [...]

    public void useStationary(PdfWriter writer) throws IOException
    {
        writer.setPageEvent(this);
        PdfReader reader = new PdfReader(STATIONERY);
        page = writer.getImportedPage(reader, 1);
    }

    public void onEndPage(PdfWriter writer, Document document)
    {
        writer.getDirectContentUnder().addTemplate(page, 0, 0);
    }

    [...]
}

由于隐式close()调用已被越来越多地删除,PdfReader reader nowerdays中实例化的useStationary应存储在Stationery的某个变量中,并在{{{}}之后关闭1}}已执行。