架构师_程序员_码农网

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

搜索
查看: 20739|回复: 0

[插件库] itextsharp填充PDF表单域

[复制链接]
发表于 2016-4-12 10:26:29 | 显示全部楼层 |阅读模式
  1. using iTextSharp.text.pdf;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;

  7. namespace TempPDF.Function
  8. {
  9.     public class PdfHelper
  10.     {
  11.         public static void Test1(string TempPDF,string newPDF)
  12.         {
  13.             System.IO.File.Delete(newPDF);
  14.             BaseFont font = BaseFont.CreateFont(@"C:\Windows\Fonts\simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
  15.             //获取PDF模板文件
  16.             //string templateFile = @"E:\Test Demo\Practice\PDF\New_Blank_Document.pdf";
  17.             //输出生成的PDF文件
  18.             //string tempPDF = @"E:\Test Demo\Practice\PDF\output.pdf";

  19.             //创建 PdfReader
  20.             PdfReader reader = new PdfReader(TempPDF);
  21.             
  22.                 PdfStamper stamper = new PdfStamper(reader, new FileStream(newPDF, FileMode.Create));
  23.                
  24.                     //stamper.Writer.CloseStream = false;

  25.                     //进行PDF字段操作
  26.                     AcroFields from = stamper.AcroFields;
  27.                     //填充PDF里的字段内容
  28.                     from.SetFieldProperty("Name", "textfont", font, null);
  29.                     from.SetField("Name", "测试");
  30.                     stamper.FormFlattening = true;
  31.                     stamper.Close();
  32.                
  33.                 reader.Close();
  34.             
  35.         }

  36.         /// <summary>
  37.         /// 填充表单文本域
  38.         /// </summary>
  39.         /// <param name="templatePath"></param>
  40.         /// <param name="newFilePath"></param>
  41.         /// <param name="parameters"></param>
  42.         public static void GetEnPdf(string templatePath, string newFilePath, Dictionary<string, string> parameters)
  43.         {
  44.             System.IO.File.Delete(newFilePath);
  45.             PdfReader pdfReader = new PdfReader(templatePath);
  46.             PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(newFilePath,FileMode.Create));
  47.             //获取域的集合;
  48.             AcroFields pdfFormFields = pdfStamper.AcroFields;
  49.             BaseFont BF_Light = BaseFont.CreateFont(@"C:\Windows\Fonts\simsun.ttc,0", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
  50.             //为需要赋值的域设置值;
  51.             foreach (KeyValuePair<string, string> parameter in parameters)
  52.             {
  53.                 pdfFormFields.SetFieldProperty(parameter.Key, "textfont", BF_Light, null);
  54.                 pdfFormFields.SetField(parameter.Key, parameter.Value);
  55.             }
  56.             //这句很重要,如果为false那么生成的PDF文件还能编辑,一定要设为true;
  57.             pdfStamper.FormFlattening = true;
  58.             pdfStamper.Close();
  59.             pdfReader.Close();
  60.         }
  61.     }
  62. }
复制代码






上一篇:Aspose.Words操作word生成PDF文档
下一篇:jQuery修改input输入框type属性时报错的处理
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

免责声明:
码农网所发布的一切软件、编程资料或者文章仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。如有侵权请邮件与我们联系处理。

Mail To:help@itsvse.com

QQ|小黑屋|架构师 ( 鲁ICP备14021824号-2 )|网站地图

GMT+8, 2026-4-12 19:58

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表