通过iTextSharp更改PDF以使用新文本替换某些现有文本

时间:2012-03-09 14:47:55

标签: c# pdf itextsharp

请告诉我iTextSharp中是否有任何函数替换(pdx文件中的“xx”,“yy”)函数而不更改文件的其余部分。

2 个答案:

答案 0 :(得分:2)

简短回答:你不能用iText做到这一点。

更长的答案:PDF是一种显示格式,因此在呈现PDF时,会做出很多关于页面和字符布局和定位的决定。 iText in Action的第6章在介绍为什么它不是一项微不足道的任务时有一个很好的描述。您可以从Chapter 6免费阅读publisher's website

答案 1 :(得分:1)

我通过PDF表单字段找到了一种方法。

    String formFile = Server.MapPath("~/") + "source.pdf";

    String newFile = Server.MapPath("~/") + "sink.pdf";

    PdfReader reader = new PdfReader(formFile);

    PdfStamper stamper = new PdfStamper(reader, new FileStream(newFile, FileMode.Create));

    AcroFields fields = stamper.AcroFields;

    // set form fields

    fields.SetField("{TO}", "John Doe");

    fields.SetField("{FROM}", "2 Milky Way, London");

    stamper.FormFlattening = true;

    stamper.Close();
相关问题