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");
}```
相关推荐
Scout-leaf3 天前
WPF新手村教程(三)—— 路由事件
c#·wpf
用户298698530143 天前
程序员效率工具:Spire.Doc如何助你一键搞定Word表格排版
后端·c#·.net
mudtools4 天前
搭建一套.net下能落地的飞书考勤系统
后端·c#·.net
玩泥巴的5 天前
搭建一套.net下能落地的飞书考勤系统
c#·.net·二次开发·飞书
唐宋元明清21885 天前
.NET 本地Db数据库-技术方案选型
windows·c#
郑州光合科技余经理5 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
lindexi5 天前
dotnet DirectX 通过可等待交换链降低输入渲染延迟
c#·directx·d2d·direct2d·vortice
feifeigo1235 天前
matlab画图工具
开发语言·matlab
dustcell.5 天前
haproxy七层代理
java·开发语言·前端
norlan_jame5 天前
C-PHY与D-PHY差异
c语言·开发语言