iTextSharp vertical SignatureAppearance

懵懂的女人 提交于 2021-02-15 07:54:05

问题


I'm signing a document with token certificate:

        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);

The signature text is rendered at bottom of the page. How can I change to a vertical mode, in the right part of document, outside the content margins?

thanks


回答1:


First of all, to get some vertically oriented signature, the rectangle in which to visualize the signature should be somewhat more vertically oriented. Thus, in place of your

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

you should use something like

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

Now you clarified in comments that not only the visualization rectangle should have a vertical orientation but that the text also should be drawn vertically. iText by default creates visualizations with horizontal text. Thus, you have to use customized appearances.

As I am more at home with iText/Java, this example to customize a PdfSignatureAppearance appearance is in Java. It should be easy to transform to iTextSharp/C#, though.

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();

This example draws "Signed by me, myself and I" vertically in the page area rectangle.




回答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;
        }
    }

You can change the alpha value of the color, rotation angle (say, 45), text size and make a watermark to your document...

While above method uses DirectContent with absolute coordinates, the below method uses cell object with rotation property. Note that cell rotation can be multiple of 90, while with the 1st method you can have any angle...

    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();
    }

enjoy!



来源:https://stackoverflow.com/questions/28027141/itextsharp-vertical-signatureappearance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!