0

我正在尝试使用 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();
4

1 回答 1

0

由于研究我评论中引用的样本正是 [Tuan] 所需要的,因此我将其制定为答案:

iText in Action - 2nd Edition第 6 章中的示例Stationery.java主要展示了如何使用给定 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实例化useStationary应该存储在某个变量中,并在执行Stationery后关闭。createPdf

于 2013-05-07T09:13:25.827 回答