首页 > 其他分享 >.Net Core 操作PDF模板

.Net Core 操作PDF模板

时间:2023-02-12 21:44:20浏览次数:35  
标签:Core run doc Pdf XFont PDF new Net PdfSharpCore

1.安装PdfSharpCore

 

 

 2.

PdfSharpCore.Pdf.PdfDocument doc = PdfSharpCore.Pdf.IO.PdfReader.Open(temppath, PdfDocumentOpenMode.Modify);//创建一个文档实例,temppath为模板文件路径

XFont font = new XFont("SimHei", 10);//字体
PdfSharpCore.Pdf.AcroForms.PdfAcroForm form = doc.AcroForm;//获取文档的表单,这个表单就是模板里配置的表单 foreach (var fieldName in form.Fields.Names) { var run = form.Fields[fieldName] as PdfTextField; var text = run.Name; //表单字段 run.ReadOnly = false;//先去掉只读属性 run.Value = new PdfSharpCore.Pdf.PdfString(value,PdfStringEncoding.Unicode);//中文编码转译,不然会乱码 run.ReadOnly = true;//设置只读 }

3.也可直接在某一处添加文字、图片等

using (var xGraphics = XGraphics.FromPdfPage(doc.Pages[0]))
{
     font = new XFont("SimHei", 10);
//这里是绘制了一个条形码 XFont BarcodeFont = new XFont("Code 128", 14, XFontStyle.Regular)
BarCode b = BarCode.FromType(CodeType.Code3of9Standard, "条形码数据", new XSize(100, 25));
xGraphics.DrawBarCode(b, new XPoint(80, 7));
     //绘制文字
     xGraphics.DrawString("这是文字", font, XBrushes.Black, new XPoint(100, 45));
     xGraphics.Save(); 
}

doc.Save(smallfilePath + $"{date}_{(j + 1)}.pdf");
doc.Close();
doc.Dispose();

 

 

标签:Core,run,doc,Pdf,XFont,PDF,new,Net,PdfSharpCore
From: https://www.cnblogs.com/ljh19/p/17114786.html

相关文章