参考文档 :https://www.cnblogs.com/ibeisha/p/itextsharp-pdf.html

程序demo 地址:https://github.com/hudean/itextsharpDemo

 

我这边收到一份word版的文档,首先,我们将其另存为PDF文件。其次,在https://www.pdfescape.com/open/这个网站,将PDF文件上传上去,进行在线编辑。 

根据pdf模板文件添加数据生成新的pdf与pdf添加读取二维码

 

 

也可以通过PDF软件工具,进行编辑。这里,我把需要填写部分的内容,全画上表单元素,设置元素的name,后面根据name 属性对其进行赋值。具体操作,将text放置指定处,然后鼠标右键,看到如下界面:

根据pdf模板文件添加数据生成新的pdf与pdf添加读取二维码

 

 这里Name属性就是后面我们要用到的。目前它支持的元素有:

根据pdf模板文件添加数据生成新的pdf与pdf添加读取二维码

 

 

1、使用itextsharp 生成pdf文件 引用itextsharp 包

DataTable 转成pdf文件

 private static void Method()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("id", typeof(int));
            dt.Columns.Add("name", typeof(string));
            dt.Columns.Add("age", typeof(int));
            dt.Columns.Add("address", typeof(string));
            dt.Clear();
            DataRow dr = dt.NewRow();
            dr["id"] = 1; dr["name"] = "cscc"; dr["age"] = 26; dr["address"] = "江西省";
            dt.Rows.Add(dr);
            dt.AcceptChanges();
            bool b = ITextSharpHelper2.ConvertDataTableToPDF(dt, @"C:\Users\Administrator\Desktop\testPDF.pdf", "C://WINDOWS//FONTS//SIMSUN.TTC,1", 9);
            Console.WriteLine($"{b}");
            Console.ReadKey();
        }

ITextSharpHelper2帮助类

public class ITextSharpHelper2
    {
        /// <summary>
        /// 根据pdf模板写入数据生成新的pdf
        /// </summary>
        /// <param name="saveFile">保存的新pdf路径</param>
        /// <param name="sourceFile">原pdf路径</param>
        public static void AddNewPdf(string saveFile, string sourceFile)
        {
            //写入新的pdf地址
            //sourceFile = @"C:\Users\Administrator\Desktop\ABC\temp.pdf";
            //sourceFile = @"C:\Users\Administrator\Desktop\temp123.pdf";
            iTextSharp.text.pdf.PdfDocument document = new iTextSharp.text.pdf.PdfDocument();
            //读取的源pdf文件
            iTextSharp.text.pdf.PdfReader pdfReader = new iTextSharp.text.pdf.PdfReader(sourceFile);
            PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(saveFile, FileMode.OpenOrCreate));
            AcroFields pdfFormFields = pdfStamper.AcroFields;
            pdfStamper.FormFlattening = true;
            //BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
            //BaseFont simheiBase = BaseFont.CreateFont(@"C:\Windows\Fonts\simhei.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
            BaseFont simheiBase = BaseFont.CreateFont(@"C:\Windows\Fonts\simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            pdfFormFields.AddSubstitutionFont(simheiBase);
            Dictionary<string, string> para = new Dictionary<string, string>();
            para.Add($"Numbering", "12369999995");
            for (int i = 1; i < 38; i++)
            {
                para.Add($"Numbering{i}", "12365");
            }
            foreach (KeyValuePair<string, string> parameter in para)
            {
                pdfStamper.AcroFields.SetField(parameter.Key, parameter.Value);
            }
            //pdfStamper.AcroFields.SetField("Names", "李朝强");
            //pdfStamper.AcroFields.SetField("chk", "yes", true);
            pdfStamper.Close();
            pdfReader.Close();
        }

        /// <summary>
        /// pdf文件添加图片
        /// </summary>
        /// <param name="oldPath">源PDF文件</param>
        /// <param name="newPath">新PDF文件</param>
        /// <param name="imPath">图片地址</param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        public static void AddImg(string oldPath, string newPath, string imPath, float x, float y)
        {
            iTextSharp.text.pdf.PdfDocument doc = new iTextSharp.text.pdf.PdfDocument();


            iTextSharp.text.pdf.PdfReader pdfReader = new iTextSharp.text.pdf.PdfReader(oldPath);
            PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(newPath, FileMode.OpenOrCreate));
            //获取PDF指定页面内容
            var pdfContentByte = pdfStamper.GetOverContent(1);
            //添加图片
            Image image = Image.GetInstance(imPath);

            //设置图片大小
            image.ScaleToFit(100, 100);
            //设置图片位置
            image.SetAbsolutePosition(x, y);
            pdfContentByte.AddImage(image);
            pdfStamper.FormFlattening = true;
            pdfStamper.Close();
            pdfReader.Close();

        }


        /// <summary>
        /// DataTable 转成Pdf文件
        /// </summary>
        /// <param name="Data"></param>
        /// <param name="PDFFile"></param>
        /// <param name="FontPath"></param>
        /// <param name="FontSize"></param>
        /// <returns></returns>
        public static bool ConvertDataTableToPDF(DataTable Data, string PDFFile, string FontPath, float FontSize)
        {
            Document document = new Document();
            PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(PDFFile, FileMode.Create));
            document.Open();
            BaseFont baseFont =
                BaseFont.CreateFont(
                FontPath,
                BaseFont.IDENTITY_H,
                BaseFont.NOT_EMBEDDED);
            iTextSharp.text.Font font = new iTextSharp.text.Font(baseFont, FontSize);
            PdfPTable table = new PdfPTable(Data.Columns.Count);
            for (int i = 0; i < Data.Rows.Count; i++)
            {
                for (int j = 0; j < Data.Columns.Count; j++)
                {
                    table.AddCell(new Phrase(Data.Rows[i][j].ToString(), font));
                }
            }
            document.Add(table);
            document.Close();
            writer.Close();
            return true;
        }

    }
View Code

相关文章:

  • 2021-06-03
  • 2022-12-23
  • 2021-12-14
  • 2021-12-22
  • 2021-07-02
  • 2022-12-23
  • 2021-09-23
  • 2021-12-24
猜你喜欢
  • 2021-07-11
  • 2021-12-24
  • 2022-12-23
  • 2022-12-23
  • 2021-12-23
  • 2021-08-05
相关资源
相似解决方案