.net?core利用PdfSharpCore操作PDF實例教程
前序
使用PdfSharpCore請注意使用XGraphics基類,與System.Drawing 的Graphics類似,XGraphics 提供XColor(顏色)、XPen(畫筆)、XBrush(畫刷)、XFont(字體)、XPoint(位置)等對象。提供很多畫線,矩形,圓,扇形,多邊形,圖,文本等方法。源碼請查看https://github.com/ststeiger/PdfSharpCore/blob/master/PdfSharpCore/Drawing/XGraphics.cs
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 指定文本的位置:詳請查看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); } }
(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公司版權所有", 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
總結
到此這篇關于.net core利用PdfSharpCore操作PDF實例的文章就介紹到這了,更多相關.net core用PdfSharpCore操作PDF內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
ASP.NET?Core應用JWT進行用戶認證及Token的刷新方案
本文詳細講解了ASP.NET?Core應用JWT進行用戶認證及Token的刷新方案,文中通過示例代碼介紹的非常詳細。對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-12-12解決 .NET Core 中 GetHostAddressesAsync 引起的 EnyimMemcached 死鎖問題
這篇文章主要介紹了解決 .NET Core 中 GetHostAddressesAsync 引起的 EnyimMemcached 死鎖問題的相關資料,需要的朋友可以參考下2016-09-09基于Dapper實現(xiàn)分頁效果 支持篩選、排序、結果集總數(shù)等
這篇文章主要為大家詳細介紹了基于Dapper實現(xiàn)分頁效果,支持篩選,排序,結果集總數(shù),多表查詢,非存儲過程,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07