前序
使用pdfsharpcore请注意使用xgraphics基类,与system.drawing 的graphics类似,xgraphics 提供xcolor(颜色)、xpen(画笔)、xbrush(画刷)、xfont(字体)、xpoint(位置)等对象。提供很多画线,矩形,圆,扇形,多边形,图,文本等方法。源码请查看
1.设置pdf拥有者的密码,让pdf防篡改。
代码很简单设置pdfdocument.securitysettings.ownerpassword
pdfdocument doc = pdfreader.open(@"a.pdf", pdfdocumentopenmode.modify); doc.securitysettings.ownerpassword = "123"; var filepath = $"b.pdf"; doc.save(filepath);
2.pdf添加页眉和页脚
(1)添加页码显示
xstringformats 指定文本的位置:详请查看
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); } }
(2)添加页眉
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");
(3)添加页脚
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");
3.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公司kb88凯时官网登录的版权所有", font, brush, new xpoint(500, 500), stringformat); } doc.save("b.pdf");
4.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");
docker 模式,需要在 dockerfile 中添加如下配置
run apt-get update && apt-get -y install libfontconfig1
如需要指定字体,请将字段文件进行拷贝(比如雅黑)
copy /xx/xxx/simhei.ttf /usr/share/fonts/simhei.ttf