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

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

c# 复制代码
 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");
}```
相关推荐
diving deep28 分钟前
脚本速览-python
开发语言·python
一生了无挂40 分钟前
Java处理JSON技巧教学(从基础到高阶实战全覆盖)
java·开发语言·json
swordbob1 小时前
Spring 单例 Bean 是线程安全的吗?
java·开发语言
小小编程路2 小时前
C++ 异常 完整讲解
开发语言·c++
AI科技星2 小时前
数术工坊 · 第四卷 橡皮泥江湖(拓扑学)【完整定稿】
c语言·开发语言·汇编·electron·概率论·拓扑学
张忠琳3 小时前
【Go 1.26.4】Golang Select 深度解析
开发语言·后端·golang
沉默王二3 小时前
LlamaIndex 开源 LiteParse,零云依赖搞定扫描件PDF
pdf·开源
用户395240998804 小时前
SqlSugar 连接 PostgreSQL 报错 42P01: relation does not exist 的排查与修复
c#
AC赳赳老秦4 小时前
OpenClaw+Power Apps 实战:自动生成 Power Apps 应用、连接 Excel 数据源
大数据·开发语言·python·serverless·excel·deepseek·openclaw