繁体   English   中英

如何使用iText创建多个副本文档

[英]How to create multiple copies documents with iText

我正在使用iText生成一个PDF文档,其中包含几个几乎相同信息的副本。

例如:发票。 向客户提供一份副本,另一份提交,第三份提交给会计师以进行簿记。

所有副本必须完全相同,除了一小段文本表明谁是( 客户会计文件 ......)的副本。

有两种可能的情况(我不知道两种方案的解决方案是否相同):

a)每个副本都在不同的页面中

b)所有副本都在同一页面 (纸张将有切割孔以分离副本)。

将有一个包装器或帮助器类使用iText生成PDF,以便能够执行类似var pdf = HelperClass.CreateDocument(DocuemntInfo info); 多包拷贝问题将在此包装器/帮助器中解决。

iText为实现这一目标提供了什么? 我是否需要在不同的位置/页面中多次写入文档中的每个元素? 或者iText是否提供了一种方法来将一个副本写入文档然后将其复制到其他位置/页面?


注意:这是一个.Net项目,但我用java和c#标记了这个问题,因为这个qustion是关于如何正确使用iText的,答案将有助于两个语言开发人员。

如果每个副本都在不同的页面上 ,您可以创建一个新文档并在页面中多次复制。 在Java中使用iText你可以这样做:

// Create output PDF
Document document = new Document(PageSize.A4);
PdfWriter writer = PdfWriter.getInstance(document, outputStream);
document.open();
PdfContentByte cb = writer.getDirectContent();

// Load existing PDF
PdfReader reader = new PdfReader(templateInputStream);
PdfImportedPage page = writer.getImportedPage(reader, 1); 

// Copy first page of existing PDF into output PDF
document.newPage();
cb.addTemplate(page, 0, 0);
// Add your first piece of text here
document.add(new Paragraph("Customer")); 

// Copy second page of existing PDF into output PDF
document.newPage();
cb.addTemplate(page, 0, 0);
// Add your second piece of text here
document.add(new Paragraph("Accounting")); 

// etc...

document.close();

如果要将所有副本放在同一页面上 ,代码类似,但不是在addTemplate(page, 0, 0)中使用零, addTemplate(page, 0, 0)需要为正确的位置设置值; 要使用的数字取决于发票的大小和形状。

另请参见iText - 向现有PDF文件添加内容 - 上面的代码基于我在该答案中编写的代码。

这是我看到这个工作的方式。

PdfReader reader = new PdfReader( templatePDFPath );
Document doc = new Document();
PdfWriter writer = PdfWriter.createInstance( doc, new FileOutputStream("blah.pdf" ) );

PdfImportedPage inputPage = writer.getImportedPage( reader, 1 );

PdfDirectContent curPageContent = writer.getDirectContent();

String extraStuff[] = getExtraStuff();

for (String stuff : extraStuff) {
  curPageContent.saveState();
  curPageContent.addTemplate( inputPage /*, x, y*/ );
  curPageContent.restoreState();

  curPageContent.beginText();
  curPageContent.setTextMatrix(x, y);
  curPageContent.setFontAndSize( someFont, someSize );

  // the actual work:
  curPageContent.showText( stuff );

  curPageContent.EndText();       

  // save the contents of curPageContent out to the file and reset it for the next page.
  doc.newPage();
}

这是计算机上最少的工作。 相当高效,它将导致更小的PDF。 通过调整,您可以在N页上重复使用该页面的一个副本,而不是通过调整获得该页面的N个副本。

你可以做同样的事情,并使用addTemplate的“x,y”参数在同一页面上绘制它们。 由你决定。

PS:你需要事先setTextMatrix的坐标。

您也可以使用PDfCopy或PDfSmartCopy来执行此操作。

PdfReader reader = new PdfReader("Path\To\File");
Document doc = new Document();           
PdfCopy copier = new PdfCopy(doc, ms1);
//PdfSmartCopy copier = new PdfSmartCopy(doc, ms1);
doc.Open();
copier.CloseStream = false;    

PdfImportedPage inputPage = writer.GetImportedPage(reader, 1);
PdfContentByte curPageContent = writer.DirectContent;            

for (int i = 0; i < count; i++)
{           
    copier.AddPage(inputPage);          
}
doc.Close();                        
ms1.Flush();
ms1.Position = 0;

PdfCopy和PdfSmartCopy之间的区别在于PdfCopy复制每页的整个PDF,而PdfSmartCopy输出一个PDF,内部只包含一个副本,所有页面都引用它,从而导致文件更小,网络带宽更少,但它使用更多服务器上的内存,需要更长的时间来处理。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM