c# PDFSharp 给已有的pdf文件添加文字页脚(文字水印)

使用c# PDFSharp 版本:1.50.5147 给已有的PDF文件每页添加页脚

 private void Button_Click(object sender, RoutedEventArgs e)
{
	System.Drawing.Text.PrivateFontCollection pfcFonts = new System.Drawing.Text.PrivateFontCollection();
            string strFontPath = @"C:\Windows\Fonts\simfang.ttf";//字体设置为微软雅黑
            pfcFonts.AddFontFile(strFontPath);
            XPdfFontOptions options = new XPdfFontOptions(PdfFontEncoding.Unicode, PdfFontEmbedding.Always);
            XFont font = new XFont(pfcFonts.Families[0], 12, XFontStyle.Regular, options);

            // 加载PDF文件
            PdfDocument inputDocument = PdfReader.Open("C:\\Users\\36142\\Documents\\WXWork\\1688850881984293\\Cache\\File\\2023-09\\paper_test.pdf", PdfDocumentOpenMode.Modify);

            // 循环所有页面
            for (int pageNumber = 0; pageNumber < inputDocument.PageCount; pageNumber++)
            {
                // 获取页面
                PdfPage page = inputDocument.Pages[pageNumber];

                // 创建字体和格式
                //XFont font = new XFont("Verdana", 8, XFontStyle.Regular);
                XStringFormat format = new XStringFormat();
                format.Alignment = XStringAlignment.Center;

                // 添加页数和页脚
                string text = string.Format("试卷编号:31929418,第{0}页,共{1}页", pageNumber + 1, inputDocument.PageCount);
                XSize size = page.MediaBox.Size;
                XRect rect = new XRect(0, size.Height - 35, size.Width, 10);
                XGraphics gfx = XGraphics.FromPdfPage(page);
                gfx.DrawString(text, font, XBrushes.Black, rect, format);
            }
            // 保存PDF文件
            inputDocument.Save("C:\\Users\\36142\\Desktop\\pdf\\output.pdf");
}```
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值