目录前序1.设置PDF拥有者的密码,让PDF防篡改。2.PDF添加页眉和页脚(2)添加页眉(3)添加页脚3.PDF添加水印文字4.PDF 添加图片总结
使用PdfSharpCore请注意使用XGraphics基类,与System.Drawing 的Graphics类似,XGraphics 提供XColor(颜色)、XPen(画笔)、XBrush(画刷)、XFont(字体)、XPoint(位置)等对象。提供很多画线,矩形,圆,扇形,多边形,图,文本等方法。源码请查看https://github.com/ststeiger/PdfSharpCore/blob/master/PdfSharpCore/Drawing/XGraphics.cs
代码很简单设置PdfDocument.SecuritySettings.OwnerPassword
PdfDocument doc=PdfReader.Open(@”a.pdf”, PdfDocumentOpenMode.Modify);
doc.SecuritySettings.OwnerPassword=”123″;
var filePath=$”b.pdf”;
doc.Save(filePath);
doc.SecuritySettings.OwnerPassword=”123″;
var filePath=$”b.pdf”;
doc.Save(filePath);
(1)添加页码显示
XStringFormats 指定文本的位置:详请查看https://github.com/ststeiger/PdfSharpCore/blob/master/PdfSharpCore/Drawing/XStringFormats.cs
XFont font=new XFont(“SimHei”, 8);
XBrush brush=XBrushes.Black;
PdfDocument doc=PdfReader.Open(@”a.pdf”, PdfDocumentOpenMode.Modify);
for (int i=0; i < doc.Pages.Count; i++)
{
PdfPage page=doc.Pages[i];
XRect layoutRectangle=new XRect(0, page.Height – font.Height, page.Width, font.Height);
using (XGraphics gfx=XGraphics.FromPdfPage(page))
{
gfx.DrawString(
$”第{(i + 1).ToString()}页/共{doc.Pages.Count}页”,
font,
brush,
layoutRectangle,
XStringFormats.BottomLeft);
}
}
XBrush brush=XBrushes.Black;
PdfDocument doc=PdfReader.Open(@”a.pdf”, PdfDocumentOpenMode.Modify);
for (int i=0; i < doc.Pages.Count; i++)
{
PdfPage page=doc.Pages[i];
XRect layoutRectangle=new XRect(0, page.Height – font.Height, page.Width, font.Height);
using (XGraphics gfx=XGraphics.FromPdfPage(page))
{
gfx.DrawString(
$”第{(i + 1).ToString()}页/共{doc.Pages.Count}页”,
font,
brush,
layoutRectangle,
XStringFormats.BottomLeft);
}
}
XFont font=new XFont(“SimHei”, 8);
XBrush brush=new XSolidBrush(XColor.FromArgb(128, 255, 0, 0));
XPoint point=new XPoint(90, 20);
PdfDocument doc=PdfReader.Open(@”a.pdf”, PdfDocumentOpenMode.Modify);
for (int i=0; i < doc.Pages.Count; i++)
{
var renderer=XGraphics.FromPdfPage(doc.Pages[i]);
XSize pageSize=renderer.PageSize;
renderer.DrawString(“xxx有限公司”, font, brush, point);
XPen pen=new XPen(XBrushes.Gray, 0.5f);
renderer.DrawLine(pen, point.X, point.Y, pageSize.Width – point.X, point.Y);
}
doc.Save(“b.pdf”);
XBrush brush=new XSolidBrush(XColor.FromArgb(128, 255, 0, 0));
XPoint point=new XPoint(90, 20);
PdfDocument doc=PdfReader.Open(@”a.pdf”, PdfDocumentOpenMode.Modify);
for (int i=0; i < doc.Pages.Count; i++)
{
var renderer=XGraphics.FromPdfPage(doc.Pages[i]);
XSize pageSize=renderer.PageSize;
renderer.DrawString(“xxx有限公司”, font, brush, point);
XPen pen=new XPen(XBrushes.Gray, 0.5f);
renderer.DrawLine(pen, point.X, point.Y, pageSize.Width – point.X, point.Y);
}
doc.Save(“b.pdf”);
XFont font=new XFont(“SimHei”, 8);
XBrush brush=new XSolidBrush(XColor.FromArgb(128, 255, 0, 0));
PdfDocument doc=PdfReader.Open(@”a.pdf”, PdfDocumentOpenMode.Modify);
for (int i=0; i < doc.Pages.Count; i++)
{
var renderer=XGraphics.FromPdfPage(doc.Pages[i]);
XSize pageSize=renderer.PageSize;
XPoint point=new XPoint(90, pageSize.Height-20);
renderer.DrawString(“xxx有限公司”, font, brush, point);
XPen pen=new XPen(XBrushes.Gray, 0.5f);
renderer.DrawLine(pen, point.X, point.Y-10, pageSize.Width – point.X, point.Y-10);
}
doc.Save(“b.pdf”);
XBrush brush=new XSolidBrush(XColor.FromArgb(128, 255, 0, 0));
PdfDocument doc=PdfReader.Open(@”a.pdf”, PdfDocumentOpenMode.Modify);
for (int i=0; i < doc.Pages.Count; i++)
{
var renderer=XGraphics.FromPdfPage(doc.Pages[i]);
XSize pageSize=renderer.PageSize;
XPoint point=new XPoint(90, pageSize.Height-20);
renderer.DrawString(“xxx有限公司”, font, brush, point);
XPen pen=new XPen(XBrushes.Gray, 0.5f);
renderer.DrawLine(pen, point.X, point.Y-10, pageSize.Width – point.X, point.Y-10);
}
doc.Save(“b.pdf”);
XFont font=new XFont(“SimHei”, 8);
XBrush brush=new XSolidBrush(XColor.FromArgb(128, 255, 0, 0));
PdfDocument doc=PdfReader.Open(@”a.pdf”, PdfDocumentOpenMode.Modify);
for (int i=0; i < doc.Pages.Count; i++)
{
XStringFormat stringFormat=new XStringFormat();
stringFormat.Alignment=XStringAlignment.Center;
stringFormat.LineAlignment=XLineAlignment.Center;
PdfPage page=doc.Pages[i];
var gfx=XGraphics.FromPdfPage(page, XPageDirection.Downwards);
gfx.DrawString(
$”xxx公司版权所有”,
font,
brush,
new XPoint(500, 500),
stringFormat);
}
doc.Save(“b.pdf”);
XBrush brush=new XSolidBrush(XColor.FromArgb(128, 255, 0, 0));
PdfDocument doc=PdfReader.Open(@”a.pdf”, PdfDocumentOpenMode.Modify);
for (int i=0; i < doc.Pages.Count; i++)
{
XStringFormat stringFormat=new XStringFormat();
stringFormat.Alignment=XStringAlignment.Center;
stringFormat.LineAlignment=XLineAlignment.Center;
PdfPage page=doc.Pages[i];
var gfx=XGraphics.FromPdfPage(page, XPageDirection.Downwards);
gfx.DrawString(
$”xxx公司版权所有”,
font,
brush,
new XPoint(500, 500),
stringFormat);
}
doc.Save(“b.pdf”);
//第一步先加载PDF文件
PdfDocument doc=PdfReader.Open(@”a.pdf”, PdfDocumentOpenMode.Modify);
//导入图片(地址,文件流)
var background=XImage.FromFile(@”QRCode.png”);
// var background=XImage.FromStream(()=> stream);
//指定PDF 的页
PdfPage page=doc.Pages[0];
var gfx=XGraphics.FromPdfPage(page, XPageDirection.Downwards);
//写入指定位置
gfx.DrawImage(background, 20, 20, 250, 140);
doc.Save(“b.pdf”);
PdfDocument doc=PdfReader.Open(@”a.pdf”, PdfDocumentOpenMode.Modify);
//导入图片(地址,文件流)
var background=XImage.FromFile(@”QRCode.png”);
// var background=XImage.FromStream(()=> stream);
//指定PDF 的页
PdfPage page=doc.Pages[0];
var gfx=XGraphics.FromPdfPage(page, XPageDirection.Downwards);
//写入指定位置
gfx.DrawImage(background, 20, 20, 250, 140);
doc.Save(“b.pdf”);
docker 模式,需要在 dockerfile 中添加如下配置
RUN apt-get update && apt-get -y install libfontconfig1
如需要指定字体,请将字段文件进行拷贝(比如雅黑)
COPY /xx/xxx/SIMHEI.TTF /usr/share/fonts/SIMHEI.TTF
到此这篇关于.net core利用PdfSharpCore操作PDF实例的文章就介绍到这了,更多相关.net core用PdfSharpCore操作PDF内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
您可能感兴趣的文章:.NET使用DinkToPdf将HTML转成PDF的示例代码利用C#/VB.NET实现将PDF转为Word.Net 对于PDF生成以及各种转换的操作详解如何使用Net将HTML简历导出为PDF格式
© 版权声明
文章版权归作者所有,未经允许请勿转载。