.net core-利用PdfSharpCore 操作PDF例項

Code的那些事發表於2022-12-26

前序

使用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

相關文章