您的位置:首頁技術文章
文章詳情頁

.net core利用PdfSharpCore操作PDF實例教程

瀏覽:227日期:2022-06-09 08:54:21
目錄
  • 前序
  • 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

        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內容請搜索以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持!

        標簽: ASP.NET
        国产综合久久一区二区三区