PDF文件上增加电子签章

cs 复制代码
 try
 {
     using (PdfReader pdfReader = new PdfReader(sourcePdfPath))
     using (PdfWriter pdfWriter = new PdfWriter(destinationPdfPath))
     using (PdfDocument pdfDoc = new PdfDocument(pdfReader, pdfWriter))
     {
         var page = pdfDoc.GetFirstPage();
         ImageData imageData = ImageDataFactory.Create(stampImagePath);
         var imgWidth = imageData.GetWidth();
         var imgHeight = imageData.GetHeight();

         // 获取页面高度  
         var pageHeight = page.GetPageSize().GetHeight();

         // 获取页面宽度  
         var pageWidth = page.GetPageSize().GetWidth();

         // 设置图像的位置  
         float x = pageWidth - 200; // x 坐标
         float y = 0; // y 坐标  
         // 创建 PdfCanvas  
         var canvas = new PdfCanvas(page);
         canvas.SaveState();
         if (pageWidth > pageHeight)
         {
             // 上下翻转图像  
             canvas.ConcatMatrix(1, 0, 0, -1, x , y + pageHeight); // y轴翻转  
         }
        
         // 创建 Image 对象  
         var image = new Image(imageData);
         image.SetFixedPosition(0, 0);
         image.ScaleToFit(imgWidth * 20 / 100, imgHeight * 20 / 100); // 可选,具体根据需要调整  

         // 嵌入图像到当前页面  
         var document = new Document(pdfDoc);
         document.Add(image);

         document.Close(); // 保证文档被正确关闭  

         MessageBox.Show("已签名成功!!");
     }
 }
 catch (iText.Kernel.Exceptions.PdfException ex)
 {
     Console.WriteLine($"PdfException: {ex.Message}");
 }
 catch (Exception ex)
 {
     Console.WriteLine($"Exception: {ex.Message}");
 }

stampImagePath为图片的路径,sourcePdfPath为PDF的路径,destinationPdfPath为目标PDF路径。

相关推荐
inksci几秒前
Vue 3 打开 el-dialog 时使 el-input 获取焦点
前端·javascript·vue.js
martian66535 分钟前
医学影像系统性能优化与调试技术:深度剖析与实践指南
开发语言·系统安全·dicom
y1021210436 分钟前
Pyhton训练营打卡Day27
java·开发语言·数据结构
AA-代码批发V哥1 小时前
Java类一文分解:JavaBean,工具类,测试类的深度剖析
java·开发语言
chilavert3181 小时前
从RPA项目说说RPC和MQ的使用。
开发语言·qt·rpc·rabbitmq
小乖兽技术1 小时前
在 .NET 8 开发的WinForms 程序中展示程序版本号的几种方式
开发语言·c#·.net
zyhomepage2 小时前
科技的成就(六十八)
开发语言·人工智能·科技·算法·内容运营
slandarer2 小时前
MATLAB | R2025a 更新了哪些有趣的东西?
开发语言·matlab
瑞雪兆丰年兮2 小时前
数学实验(Matlab编程基础)
开发语言·算法·matlab·数学实验
努力的小帅2 小时前
C++_STL_map与set
开发语言·数据结构·c++·学习·leetcode·刷题