C# PDF 填值 填充数据

时间:2019-06-13
本文章向大家介绍C# PDF 填值 填充数据,主要包括C# PDF 填值 填充数据使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
 看效果图
 
/// <summary>
        /// 赛事结果PDF
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        private static string PDFDownRunLoad(RunningModel model)
        {
            try
            {
                //BaseFont font = BaseFont.CreateFont(@"C:\Users\IT-016\Documents\Kitematic\jenkins\var\jenkins_home\war\css\font-awesome\fonts\fontawesome-webfont.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
                //获取PDF模板文件

                string templateFile="";
                if (model.MasterGroupType== "101")
                    templateFile = AppDomain.CurrentDomain.BaseDirectory + @"PDF\Dom\e-Cert-V1-10K.pdf";
                else
                    templateFile = AppDomain.CurrentDomain.BaseDirectory + @"PDF\Dom\e-Cert-V1-5K.pdf";


                //输出生成的PDF文件
                var fileName = @"PDF\" + model.RunId + "-" + model.AthleteNO + "_Run.pdf";
                string tempPDF = AppDomain.CurrentDomain.BaseDirectory + fileName;

                PdfDocument pdf = new PdfDocument();
                pdf.LoadFromFile(templateFile);

                //获取PDF文档第一页
                PdfPageBase page = pdf.Pages[0];

                //添加文本水印到第一页,并设置格式
                PdfTilingBrush brush = new PdfTilingBrush(new SizeF(page.Canvas.ClientSize.Width, page.Canvas.ClientSize.Height));
                //brush.Graphics.SetTransparency(0.3f);
                //brush.Graphics.Save();
                //brush.Graphics.TranslateTransform(brush.Size.Width / 2, brush.Size.Height / 2);
                //brush.Graphics.RotateTransform(-45);
                //y 控制高度   增大下滑 
                //x 控制长度   增大右滑
                Font font = new Font("华康俪粗黑", 32, FontStyle.Bold);
                PdfTrueTypeFont trueTypeFont = new PdfTrueTypeFont(font, true);

                Font font2 = new Font("华康俪中黑", 2, FontStyle.Regular);
                PdfTrueTypeFont trueTypeFont2 = new PdfTrueTypeFont(font, true);

                var run_Model = JsonConvert.DeserializeObject<ExcelModel>(model.SGL_Run_IPbusiness);

                brush.Graphics.DrawString(model.AENName, trueTypeFont, PdfBrushes.Black, 310, 373-15, new PdfStringFormat(PdfTextAlignment.Center));
                //brush.Graphics.DrawString(model.AthleteNO, new PdfFont(PdfFontFamily.Helvetica, 18), PdfBrushes.Black, 440, 208, new PdfStringFormat(PdfTextAlignment.Center));
                brush.Graphics.DrawString(model.AthleteNO, new PdfFont(PdfFontFamily.Helvetica, 16), PdfBrushes.Black, 190+195, 273+254, new PdfStringFormat(PdfTextAlignment.Center));
                brush.Graphics.DrawString(run_Model.FinishTime, new PdfFont(PdfFontFamily.Helvetica, 16), PdfBrushes.Black, 190+195, 273+297, new PdfStringFormat(PdfTextAlignment.Center));
                brush.Graphics.DrawString(run_Model.NetTime, new PdfFont(PdfFontFamily.Helvetica, 16), PdfBrushes.Black, 190+195, 273+341, new PdfStringFormat(PdfTextAlignment.Center));


                brush.Graphics.Restore();
                brush.Graphics.SetTransparency(1);
                page.Canvas.DrawRectangle(brush, new RectangleF(new PointF(0, 0), page.Canvas.ClientSize));

                //保存文档
                pdf.SaveToFile(tempPDF);
                return @"\" + fileName;
            }
            catch (Exception ex)
            {
                return "";
            }
        }
View Code


 具体代码如上图所示


下载DLL地址
http://note.youdao.com/noteshare?id=445c577476c7a69b3e87e121d538f6b8




原文地址:https://www.cnblogs.com/Mrly/p/11017794.html