You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iText7追加模式下旋转页面数字签名显示异常求助

解决iText7追加模式下旋转页面签名外观异常问题

问题根源:你调用的page.SetIgnorePageRotationForContent(true)仅对页面原有内容的渲染生效,签名属于新增的表单注释外观,不受该设置影响。要修正旋转页面上的签名显示问题,需要直接针对PdfSignatureAppearance做配置。

方案1:让签名外观忽略页面旋转(保持原始方向)

使用iText7为PdfSignatureAppearance提供的SetIgnorePageRotation(true)方法,该设置会直接作用于签名的layer2内容,使其不受页面旋转影响,始终以正常方向显示。

修改后的代码示例:

ItextTextPdf.PdfReader reader = new ItextTextPdf.PdfReader(stream);
using (MemoryStream ms = new MemoryStream())
{
    PdfWriter writer = new PdfWriter(ms);
    StampingProperties stamping = new StampingProperties().UseAppendMode();
    PdfSigner signer = new PdfSigner(reader, writer, stamping);
    PdfSignatureAppearance signatureAppearance = signer.GetSignatureAppearance();
    
    signatureAppearance.SetReason(Reason);
    signatureAppearance.SetLocation(Location); // 修正原代码笔误:将Reason改为Location
    
    Rectangle signRectangle = new iText.Kernel.Geom.Rectangle(llx, lly, width, height);
    var page = signer.GetDocument().GetPage(SignatureSettings.Page);
    
    // 移除无效的页面设置,改用签名外观专属的忽略旋转配置
    signatureAppearance.SetPageRect(signRectangle)
                      .SetPageNumber(SignatureSettings.Page)
                      .SetIgnorePageRotation(true); // 关键设置
    
    bouncyCastleCrypto.Org.BouncyCastle.X509.X509CertificateParser cp = new bouncyCastleCrypto.Org.BouncyCastle.X509.X509CertificateParser();
    bouncyCastleCrypto.Org.BouncyCastle.X509.X509Certificate[] pk = new bouncyCastleCrypto.Org.BouncyCastle.X509.X509Certificate[] { cp.ReadCertificate(cert.RawData) };
    signer.SignDetached(new AlliumDMSSigner.X509Certificate2Signature(cert, HashAlgorithmName), pk, null, null, null, 0, PdfSigner.CryptoStandard.CMS);
}

方案2:让签名外观跟随页面旋转(适配旋转方向)

如果需要签名与页面旋转方向保持一致,可先获取页面旋转角度,调整签名矩形坐标或直接设置签名外观的旋转角度:

ItextTextPdf.PdfReader reader = new ItextTextPdf.PdfReader(stream);
using (MemoryStream ms = new MemoryStream())
{
    PdfWriter writer = new PdfWriter(ms);
    StampingProperties stamping = new StampingProperties().UseAppendMode();
    PdfSigner signer = new PdfSigner(reader, writer, stamping);
    PdfSignatureAppearance signatureAppearance = signer.GetSignatureAppearance();
    
    signatureAppearance.SetReason(Reason);
    signatureAppearance.SetLocation(Location);
    
    Rectangle signRectangle = new iText.Kernel.Geom.Rectangle(llx, lly, width, height);
    var page = signer.GetDocument().GetPage(SignatureSettings.Page);
    int pageRotation = page.GetRotation();
    
    // 方式1:调整签名矩形以适配页面旋转
    Rectangle rotatedRect = signRectangle.Rotate(pageRotation, page.GetPageSize());
    signatureAppearance.SetPageRect(rotatedRect).SetPageNumber(SignatureSettings.Page);
    
    // 方式2:直接设置签名外观的旋转角度,与页面保持一致
    // signatureAppearance.SetRotation(pageRotation);
    
    bouncyCastleCrypto.Org.BouncyCastle.X509.X509CertificateParser cp = new bouncyCastleCrypto.Org.BouncyCastle.X509.X509CertificateParser();
    bouncyCastleCrypto.Org.BouncyCastle.X509.X509Certificate[] pk = new bouncyCastleCrypto.Org.BouncyCastle.X509.X509Certificate[] { cp.ReadCertificate(cert.RawData) };
    signer.SignDetached(new AlliumDMSSigner.X509Certificate2Signature(cert, HashAlgorithmName), pk, null, null, null, 0, PdfSigner.CryptoStandard.CMS);
}

内容的提问来源于stack exchange,提问作者mihai stoica

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 12:03:22