使用iTextSharp的(或C#PDF库),如何打开一个PDF,替换一些文本,然后再保存呢? [英] Using itextsharp (or any c# pdf library), how to open a PDF, replace some text, and save it again?

查看:3496
本文介绍了使用iTextSharp的(或C#PDF库),如何打开一个PDF,替换一些文本,然后再保存呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用iTextSharp的(或C#PDF库),我需要打开一个PDF,请用实际值一些占位符文本,并返回它作为一个byte []。



可有人建议如何做到这一点?我有一个看iText的文档和不能找出哪里开始。到目前为止,我是卡在如何从PDFReader获得源代码的PDF文档对象吧,我想我可能接近这个错误的方式。



谢谢很多


解决方案

在最后,我用PDFescape打开我现有的PDF文件,并将其放置在我需要在那里某种形式的字段

。把我的领域,然后再保存到我的创建PDF文件



HTTP:// WWW .pdfescape.com



然后我发现关于如何更换表单字段此博客条目:



HTTP: //www.johnnycode.com/blog/2010/03/05/using-a-template-to-programmatically-create-pdfs-with-c-and-itextsharp/



所有工作得很好!下面的代码:

 公共静态的byte []生成()
{
变种的templatePath = HttpContext的。 Current.Server.MapPath(〜/ my_template.pdf);

//基于:
// http://www.johnnycode.com/blog/2010/03/05/using-a-template-to-programmatically-create-pdfs -with-C-和iTextSharp的/
变种读卡器=新PdfReader(的templatePath);
变种outStream =新的MemoryStream();
变种模子=新PdfStamper(读卡器,outStream);

变种形式= stamper.AcroFields;
VAR fieldKeys = form.Fields.Keys;

的foreach(在fieldKeys串fieldKey)
{
如果(form.GetField(fieldKey)==MyTemplatesOriginalTextFieldA)
form.SetField(fieldKey,1234 );
如果(form.GetField(fieldKey)==MyTemplatesOriginalTextFieldB)
form.SetField(fieldKey,5678);
}

//拼合的形式,以便它不会是编辑/可用了
stamper.FormFlattening = TRUE;

stamper.Close();
reader.Close();

返回outStream.ToArray();
}


Using itextsharp (or any c# pdf library), i need to open a PDF, replace some placeholder text with actual values, and return it as a byte[].

Can someone suggest how to do this? I've had a look at the itext docs and can't figure out where to get started. So far i'm stuck on how to get the source pdf from a PDFReader to a Document object, i presume i'm probably approaching this the wrong way.

Thanks a lot

解决方案

In the end, i used PDFescape to open my existing PDF file, and place some form fields in where i need to put my fields, then save it again to create my PDF file.

http://www.pdfescape.com

Then i found this blog entry about how to replace form fields:

http://www.johnnycode.com/blog/2010/03/05/using-a-template-to-programmatically-create-pdfs-with-c-and-itextsharp/

All works nicely! Here's the code:

public static byte[] Generate()
{
  var templatePath = HttpContext.Current.Server.MapPath("~/my_template.pdf");

  // Based on:
  // http://www.johnnycode.com/blog/2010/03/05/using-a-template-to-programmatically-create-pdfs-with-c-and-itextsharp/
  var reader = new PdfReader(templatePath);
  var outStream = new MemoryStream();
  var stamper = new PdfStamper(reader, outStream);

  var form = stamper.AcroFields;
  var fieldKeys = form.Fields.Keys;

  foreach (string fieldKey in fieldKeys)
  {
    if (form.GetField(fieldKey) == "MyTemplatesOriginalTextFieldA")
      form.SetField(fieldKey, "1234");
    if (form.GetField(fieldKey) == "MyTemplatesOriginalTextFieldB")
      form.SetField(fieldKey, "5678");
  }

  // "Flatten" the form so it wont be editable/usable anymore  
  stamper.FormFlattening = true;  

  stamper.Close();
  reader.Close();

  return outStream.ToArray();
}

这篇关于使用iTextSharp的(或C#PDF库),如何打开一个PDF,替换一些文本,然后再保存呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆