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");
}```
相关推荐
笃励20 分钟前
Java面试题二
java·开发语言·python
jyan_敬言28 分钟前
【Linux】Linux命令与操作详解(一)文件管理(文件命令)、用户与用户组管理(创建、删除用户/组)
linux·运维·服务器·c语言·开发语言·汇编·c++
赛男丨木子丿小喵28 分钟前
Gridview配置数据源--信任服务器证书
c#
FL162386312932 分钟前
[C#]C# winform部署yolov11-pose姿态估计onnx模型
开发语言·yolo·c#
笑非不退43 分钟前
C++ 异步编程 并发编程技术
开发语言·c++
爱写代码的刚子1 小时前
C++知识总结
java·开发语言·c++
martian6651 小时前
QT开发:基于Qt实现的交通信号灯模拟器:实现一个带有倒计时功能的图形界面应用
开发语言·qt
冷琴19961 小时前
基于java+springboot的酒店预定网站、酒店客房管理系统
java·开发语言·spring boot
缘友一世1 小时前
macOS .bash_profile配置文件优化记录
开发语言·macos·bash
tekin2 小时前
macos 中使用macport安装,配置,切换多版本php,使用port 安装php扩展方法总结
开发语言·macos·php·port·mac多版本php安装管理·port-select