【问题标题】:iTextSharp vertical SignatureAppearanceiTextSharp 垂直签名外观
【发布时间】:2015-01-19 14:40:54
【问题描述】:

我正在使用令牌证书签署文件:

        var cp = new Org.BouncyCastle.X509.X509CertificateParser();
        var chain = new[] { cp.ReadCertificate(cert.RawData) };

        var externalSignature = new X509Certificate2Signature(cert, "SHA-1");

        var pdfReader = new PdfReader(origem);

        var signedPdf = new FileStream(destino, FileMode.Create);

        var pdfStamper = PdfStamper.CreateSignature(pdfReader, signedPdf, '\0');

        var sig = pdfStamper.SignatureAppearance;

        sig.SetVisibleSignature(new Rectangle(50, 0, 500, 50), pdfReader.NumberOfPages, "Signature");
        sig.SignatureRenderingMode = PdfSignatureAppearance.RenderingMode.DESCRIPTION;
        sig.Layer2Text = "Assinado digitalmente por " + cert.SubjectName.Name;
        sig.Layer2Font = new Font(Font.FontFamily.TIMES_ROMAN, 7);

        MakeSignature.SignDetached(sig, externalSignature, chain, null, null, null, 0, CryptoStandard.CMS);

签名文本呈现在页面底部。如何在文档右侧、内容边距之外更改为垂直模式?

谢谢

【问题讨论】:

  • 更改sig.SetVisibleSignaturenew Rectangle(50, 0, 500, 50)参数。
  • 谢谢,但似乎没有改变文本方向。
  • 似乎没有改变文本方向 - 不,它没有。您是否暗示在“垂直模式”中?
  • 你说得对,问的问题不够好。我试图找出某种方法将文本自下而上放在垂直列中。打印时,内容为纵向,签名为横向。还是不知道能不能解释清楚。
  • 好的。 iText 允许您自己为签名生成外观,例如在你的“垂直模式”。不知道有没有支持的更简单的方法。

标签: c# itextsharp smartcard signature


【解决方案1】:

首先,要获得一些垂直方向的签名,用于可视化签名的矩形应该更垂直一些。因此,代替你的

sig.SetVisibleSignature(new Rectangle(50, 0, 500, 50), pdfReader.NumberOfPages, "Signature");

你应该使用类似的东西

sig.SetVisibleSignature(new Rectangle(50, 0, 50, 500), pdfReader.NumberOfPages, "Signature");

现在您在 cmets 中阐明了不仅可视化矩形应该具有垂直方向,而且文本也应该垂直绘制。 iText 默认使用水平文本创建可视化。因此,您必须使用自定义外观。

由于我更熟悉 iText/Java,所以这个自定义 PdfSignatureAppearance appearance 的示例是用 Java 编写的。不过,它应该很容易转换为 iTextSharp/C#。

appearance.setVisibleSignature(rectangle, PAGENUMBER, SIGNATURENAME);

// customize appearance layer 2 to display text vertically
PdfTemplate layer2 = appearance.getLayer(2);
layer2.transform(new AffineTransform(0, 1, -1, 0, rectangle.getWidth(), 0));
Font font = new Font();
font.setColor(BaseColor.WHITE);
font.setSize(10);
ColumnText ct2 = new ColumnText(layer2);
ct2.setRunDirection(PdfWriter.RUN_DIRECTION_NO_BIDI);
ct2.setSimpleColumn(new Phrase("Signed by me, myself and I", font), 0, 0, rectangle.getHeight(), rectangle.getWidth(), 15, Element.ALIGN_CENTER);

ct2.go();

此示例在页面区域rectangle 中垂直绘制“我、我自己和我签名”。

【讨论】:

  • 干得好!提前致谢。只需对 C# 进行一些更改,例如设置道具: var ct2 = new ColumnText(layer2) { RunDirection = PdfWriter.RUN_DIRECTION_NO_BIDI };
  • 有没有办法让所有文档、肖像或风景尽可能出现在最右侧?也许是按文档宽度计算的?
  • 出现在最右侧 - 您可以从相关页面的裁剪框中获取最右侧的坐标。
【解决方案2】:
    public bool drawVerticalText(string _text, Color _color, int _angle, int _size, int _left, int _top)
    {
        try
        {
            BaseColor bc = new BaseColor(_color.R, _color.G, _color.B, _color.A);
            PdfContentByte cb = writer.DirectContent;
            BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.EMBEDDED);
            //int width = baseFont.GetWidth(_text);
            cb.BeginText();
            cb.SetColorFill(CMYKColor.RED);
            cb.SetFontAndSize(bf, _size);
            cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, _text, _left, document.Top - _top, _angle);
            cb.EndText();
            document.Close();
            return true;
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            return false;
        }
    }

您可以更改颜色的 alpha 值、旋转角度(例如 45)、文本大小并为您的文档添加水印...

虽然上述方法使用具有绝对坐标的 DirectContent,但以下方法使用具有旋转属性的单元格对象。请注意,单元格旋转可以是 90 的倍数,而使用第一种方法,您可以有任何角度...

    public void drawVerticalText2()
    {
        PdfPTable table = new PdfPTable(4);
        float[] widths = new float[] { 1.25f, 1.55f, 0.35f, 0.35f };
        table.SetWidths(widths);

        PdfPCell horizontalCell = new PdfPCell(new Phrase("I'm horizontal"));
        horizontalCell.Border = BORDERS.BOX;
        horizontalCell.HorizontalAlignment = 1;
        table.AddCell(horizontalCell);

        PdfPCell horizontalMirroredCell = new PdfPCell(new Phrase("I'm horizontal mirrored"));
        horizontalMirroredCell.Border = BORDERS.BOX;
        horizontalMirroredCell.HorizontalAlignment = 1;
        horizontalMirroredCell.Rotation = 180;
        table.AddCell(horizontalMirroredCell);

        PdfPCell verticalCell = new PdfPCell(new Phrase("I'm vertical"));
        verticalCell.Border = BORDERS.BOX;
        verticalCell.HorizontalAlignment = 1;
        verticalCell.Rotation = 90;
        table.AddCell(verticalCell);

        PdfPCell verticalMirroredCell = new PdfPCell(new Phrase("I'm vertical  mirrored"));
        verticalMirroredCell.Border = BORDERS.BOX;
        verticalMirroredCell.HorizontalAlignment = 1;
        verticalMirroredCell.Rotation = -90;
        table.AddCell(verticalMirroredCell);

        table.SpacingBefore = 20f;
        table.SpacingAfter = 30f;

        document.Add(table);
        document.Close();
    }

享受吧!

【讨论】:

    猜你喜欢
    • 2020-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-20
    • 2017-04-28
    相关资源
    最近更新 更多