c#使⽤iTextSharp填充PDF模版⽂件本⽂适⽤于导出pdf简历、合同之类业务,需先⽤pdf⼯具制作好pdf模版
pdf;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;幼兔
远方的爱
using System.Text;
using System.Threading.Tasks;
namespace Common
{
public class PDFHelper
{
///使⽤⽰例:
///简单模版⽂件可以在线⽣成(/windows/)使⽤Form Field创建需要填充的内容的控件
///var template_url = @"C:\Urs\Song\Desktop\tmpplate.pdf";
///var save_pdf_url = @"C:\Urs\Song\Desktop\new.pdf";
///var dic = PDFHelper.ReadForm(template_url);
///dic["name"] = "张三";
///dic["id_card"] = "111111111111";
///PDFHelper.FillForm(template_url, save_pdf_url, dic);
///
/// 读取pdf模版中的标签
///
/// pdf模版⽂件路径
///冰凌花的寓意
public static Dictionary
ReadForm(string pdfTemplate)
底线作文
{
Dictionary
dic = new Dictionary
();
PdfReader pdfReader = null;
try
{
pdfReader = new PdfReader(pdfTemplate);奋怎么读
AcroFields pdfFormFields = pdfReader.AcroFields;
千万别买空气能热水器foreach (KeyValuePair
de in pdfFormFields.Fields)
{
dic.Add(de.Key, "");
}
}
catch (Exception ex)
{
LogHelper.Error(ex.Message);
}
finally
{
if (pdfReader != null)
{
马祖道pdfReader.Clo();
爱受伤了pdfReader.Clo();
}
}
return dic;
}
///
/// 向pdf模版填充内容,并⽣成新的⽂件 ///
///
模版路径
///
⽣成⽂件保存路径
///
标签字典(即模版中需要填充的控件列表)
public static void FillForm(string pdfTemplate, string newFile, Dictionary
dic) { PdfReader pdfReader = null; PdfStamper pdfStamper = null; try { pdfReader = new PdfReader(
pdfTemplate); pdfStamper = new PdfStamper(pdfRead de in dic) { pdfFormFields.SetField(de.Key, de.Value); } pdfStamper.FormFlattening = true; } catch (Exception ex) { LogHelper.Error(ex.Message); } finally {
发布过程中遇到了win2008服务器上字体⽂件不存在的问题,特字体⽂件上传备份下