使用iTextSharp替换PDF便签为图片的位置偏移、旋转问题问询
解决iTextSharp替换PDF便签为图片时的位置对齐与旋转冲突问题
核心逻辑说明
你遇到的冲突源于PDF页面旋转属性和注释坐标、内容绘制坐标系的不匹配:设置stamper.RotateContents = false可以让注释原始坐标直接对齐,但插入的内容会继承页面旋转属性。我们只需要手动给图片添加反向旋转抵消页面旋转,同时根据旋转角度调整坐标即可。
修改步骤
- 初始化
PdfStamper后保留stamper.RotateContents = false配置 - 遍历每页时获取页面旋转角度、页面宽高参数
- 插入图片时根据页面旋转角度,调整图片旋转角度和绘制坐标
完整修改代码
修改LoadData方法
public void LoadData() { Stream inputPdfStream = new FileStream(txtFilePDF.Text, FileMode.Open, FileAccess.Read, FileShare.Read); Stream outputPdfStream = new FileStream("result.pdf", FileMode.Create, FileAccess.Write, FileShare.None); reader = new PdfReader(inputPdfStream); stamper = new PdfStamper(reader, outputPdfStream); // 保留该配置保证注释坐标可以直接匹配 stamper.RotateContents = false; // 遍历页面查找便签 for (int x = 1; x <= reader.NumberOfPages; x++) { PdfDictionary page = reader.GetPageN(x); pdfContentByte = stamper.GetOverContent(x); PdfArray annots = page.GetAsArray(PdfName.ANNOTS); // 新增:获取当前页面旋转角度和页面尺寸 int pageRotation = reader.GetPageRotation(x); Rectangle pageSize = reader.GetPageSize(x); float pageWidth = pageSize.Width; float pageHeight = pageSize.Height; try { for (int i = 0; i < annots.Count(); i++) { PdfDictionary sticky = annots.GetAsDict(i); PdfArray stickyRect = sticky.GetAsArray(PdfName.RECT); // 获取便签坐标 stickyRectangle = new PdfRectangle( stickyRect.GetAsNumber(0).FloatValue, stickyRect.GetAsNumber(1).FloatValue, stickyRect.GetAsNumber(2).FloatValue, stickyRect.GetAsNumber(3).FloatValue ); string Content = ""; try { Content = sticky.GetAsString(PdfName.CONTENTS).ToString(); } catch { Content = ""; } if (Content.ToString().Trim().ToLower() == "trinhky") { // 传入新增的页面参数 GetHinhAnh(txtUserTrinhKy.Text, pageRotation, pageWidth, pageHeight); } } } catch { } } // 移除所有便签(需要时取消注释) // reader.RemoveAnnotations(); // 释放资源 stamper.Close(); reader.Close(); inputPdfStream.Close(); outputPdfStream.Close(); }
修改GetHinhAnh方法
public void GetHinhAnh(string UserName, int pageRotation, float pageWidth, float pageHeight) { try { // 获取签名图片 System.Drawing.Image chuky = ((System.Drawing.Image)(Properties.Resources.ResourceManager.GetObject(UserName))); // 转换为iTextSharp支持的图片格式 iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(chuky, iTextSharp.text.BaseColor.WHITE, false); float imgWidth = 100; float imgHeight = 80; image.ScaleAbsolute(imgWidth, imgHeight); float finalX = stickyRectangle.Left; float finalY = stickyRectangle.Bottom; // 根据页面旋转角度反向调整图片旋转和坐标,抵消页面旋转影响 switch (pageRotation) { case 90: image.RotationDegrees = -90; finalX = pageWidth - stickyRectangle.Bottom - imgHeight; finalY = stickyRectangle.Left; break; case 180: image.RotationDegrees = -180; finalX = pageWidth - stickyRectangle.Left - imgWidth; finalY = pageHeight - stickyRectangle.Bottom - imgHeight; break; case 270: // 适配270度旋转的测试场景 image.RotationDegrees = -270; finalX = stickyRectangle.Bottom; finalY = pageHeight - stickyRectangle.Left - imgWidth; break; default: // 无旋转页面无需调整 break; } image.SetAbsolutePosition(finalX, finalY); pdfContentByte.AddImage(image); } catch { } }
内容的提问来源于stack exchange,提问作者Hoàng Nhân
相关产品推荐
相关产品推荐

