Esempio n. 1
0
 public void SetClippingMask(float width, float height, float x, float y)
 {
     template = topTemplate.CreateTemplate(width, height);
     topTemplate.AddTemplate(template, x, topTemplate.Height - height - y);
     currentWidth  = width;
     currentHeight = height;
 }
Esempio n. 2
0
 /**
 * Gets the button appearance.
 * @throws IOException on error
 * @throws DocumentException on error
 * @return the button appearance
 */
 public PdfAppearance GetAppearance()
 {
     PdfAppearance app = GetBorderAppearance();
     Rectangle box = new Rectangle(app.BoundingBox);
     if ((text == null || text.Length == 0) && (layout == LAYOUT_LABEL_ONLY || (image == null && template == null && iconReference == null))) {
         return app;
     }
     if (layout == LAYOUT_ICON_ONLY && image == null && template == null && iconReference == null)
         return app;
     BaseFont ufont = RealFont;
     bool borderExtra = borderStyle == PdfBorderDictionary.STYLE_BEVELED || borderStyle == PdfBorderDictionary.STYLE_INSET;
     float h = box.Height - borderWidth * 2;
     float bw2 = borderWidth;
     if (borderExtra) {
         h -= borderWidth * 2;
         bw2 *= 2;
     }
     float offsetX = (borderExtra ? 2 * borderWidth : borderWidth);
     offsetX = Math.Max(offsetX, 1);
     float offX = Math.Min(bw2, offsetX);
     tp = null;
     float textX = float.NaN;
     float textY = 0;
     float fsize = fontSize;
     float wt = box.Width - 2 * offX - 2;
     float ht = box.Height - 2 * offX;
     float adj = (iconFitToBounds ? 0 : offX + 1);
     int nlayout = layout;
     if (image == null && template == null && iconReference == null)
         nlayout = LAYOUT_LABEL_ONLY;
     Rectangle iconBox = null;
     while (true) {
         switch (nlayout) {
             case LAYOUT_LABEL_ONLY:
             case LAYOUT_LABEL_OVER_ICON:
                 if (text != null && text.Length > 0 && wt > 0 && ht > 0) {
                     fsize = CalculateFontSize(wt, ht);
                     textX = (box.Width - ufont.GetWidthPoint(text, fsize)) / 2;
                     textY = (box.Height - ufont.GetFontDescriptor(BaseFont.ASCENT, fsize)) / 2;
                 }
                 goto case LAYOUT_ICON_ONLY;
             case LAYOUT_ICON_ONLY:
                 if (nlayout == LAYOUT_LABEL_OVER_ICON || nlayout == LAYOUT_ICON_ONLY)
                     iconBox = new Rectangle(box.Left + adj, box.Bottom + adj, box.Right - adj, box.Top - adj);
                 break;
             case LAYOUT_ICON_TOP_LABEL_BOTTOM:
                 if (text == null || text.Length == 0 || wt <= 0 || ht <= 0) {
                     nlayout = LAYOUT_ICON_ONLY;
                     continue;
                 }
                 float nht = box.Height * 0.35f - offX;
                 if (nht > 0)
                     fsize = CalculateFontSize(wt, nht);
                 else
                     fsize = 4;
                 textX = (box.Width - ufont.GetWidthPoint(text, fsize)) / 2;
                 textY = offX - ufont.GetFontDescriptor(BaseFont.DESCENT, fsize);
                 iconBox = new Rectangle(box.Left + adj, textY + fsize, box.Right - adj, box.Top - adj);
                 break;
             case LAYOUT_LABEL_TOP_ICON_BOTTOM:
                 if (text == null || text.Length == 0 || wt <= 0 || ht <= 0) {
                     nlayout = LAYOUT_ICON_ONLY;
                     continue;
                 }
                 nht = box.Height * 0.35f - offX;
                 if (nht > 0)
                     fsize = CalculateFontSize(wt, nht);
                 else
                     fsize = 4;
                 textX = (box.Width - ufont.GetWidthPoint(text, fsize)) / 2;
                 textY = box.Height - offX - fsize;
                 if (textY < offX)
                     textY = offX;
                 iconBox = new Rectangle(box.Left + adj, box.Bottom + adj, box.Right - adj, textY + ufont.GetFontDescriptor(BaseFont.DESCENT, fsize));
                 break;
             case LAYOUT_LABEL_LEFT_ICON_RIGHT:
                 if (text == null || text.Length == 0 || wt <= 0 || ht <= 0) {
                     nlayout = LAYOUT_ICON_ONLY;
                     continue;
                 }
                 float nw = box.Width * 0.35f - offX;
                 if (nw > 0)
                     fsize = CalculateFontSize(wt, nw);
                 else
                     fsize = 4;
                 if (ufont.GetWidthPoint(text, fsize) >= wt) {
                     nlayout = LAYOUT_LABEL_ONLY;
                     fsize = fontSize;
                     continue;
                 }
                 textX = offX + 1;
                 textY = (box.Height - ufont.GetFontDescriptor(BaseFont.ASCENT, fsize)) / 2;
                 iconBox = new Rectangle(textX + ufont.GetWidthPoint(text, fsize), box.Bottom + adj, box.Right - adj, box.Top - adj);
                 break;
             case LAYOUT_ICON_LEFT_LABEL_RIGHT:
                 if (text == null || text.Length == 0 || wt <= 0 || ht <= 0) {
                     nlayout = LAYOUT_ICON_ONLY;
                     continue;
                 }
                 nw = box.Width * 0.35f - offX;
                 if (nw > 0)
                     fsize = CalculateFontSize(wt, nw);
                 else
                     fsize = 4;
                 if (ufont.GetWidthPoint(text, fsize) >= wt) {
                     nlayout = LAYOUT_LABEL_ONLY;
                     fsize = fontSize;
                     continue;
                 }
                 textX = box.Width - ufont.GetWidthPoint(text, fsize) - offX - 1;
                 textY = (box.Height - ufont.GetFontDescriptor(BaseFont.ASCENT, fsize)) / 2;
                 iconBox = new Rectangle(box.Left + adj, box.Bottom + adj, textX - 1, box.Top - adj);
                 break;
         }
         break;
     }
     if (textY < box.Bottom + offX)
         textY = box.Bottom + offX;
     if (iconBox != null && (iconBox.Width <= 0 || iconBox.Height <= 0))
         iconBox = null;
     bool haveIcon = false;
     float boundingBoxWidth = 0;
     float boundingBoxHeight = 0;
     PdfArray matrix = null;
     if (iconBox != null) {
         if (image != null) {
             tp = new PdfTemplate(writer);
             tp.BoundingBox = new Rectangle(image);
             writer.AddDirectTemplateSimple(tp, PdfName.FRM);
             tp.AddImage(image, image.Width, 0, 0, image.Height, 0, 0);
             haveIcon = true;
             boundingBoxWidth = tp.BoundingBox.Width;
             boundingBoxHeight = tp.BoundingBox.Height;
         }
         else if (template != null) {
             tp = new PdfTemplate(writer);
             tp.BoundingBox = new Rectangle(template.Width, template.Height);
             writer.AddDirectTemplateSimple(tp, PdfName.FRM);
             tp.AddTemplate(template, template.BoundingBox.Left, template.BoundingBox.Bottom);
             haveIcon = true;
             boundingBoxWidth = tp.BoundingBox.Width;
             boundingBoxHeight = tp.BoundingBox.Height;
         }
         else if (iconReference != null) {
             PdfDictionary dic = (PdfDictionary)PdfReader.GetPdfObject(iconReference);
             if (dic != null) {
                 Rectangle r2 = PdfReader.GetNormalizedRectangle(dic.GetAsArray(PdfName.BBOX));
                 matrix = dic.GetAsArray(PdfName.MATRIX);
                 haveIcon = true;
                 boundingBoxWidth = r2.Width;
                 boundingBoxHeight = r2.Height;
             }
         }
     }
     if (haveIcon) {
         float icx = iconBox.Width / boundingBoxWidth;
         float icy = iconBox.Height / boundingBoxHeight;
         if (proportionalIcon) {
             switch (scaleIcon) {
                 case SCALE_ICON_IS_TOO_BIG:
                     icx = Math.Min(icx, icy);
                     icx = Math.Min(icx, 1);
                     break;
                 case SCALE_ICON_IS_TOO_SMALL:
                     icx = Math.Min(icx, icy);
                     icx = Math.Max(icx, 1);
                     break;
                 case SCALE_ICON_NEVER:
                     icx = 1;
                     break;
                 default:
                     icx = Math.Min(icx, icy);
                     break;
             }
             icy = icx;
         }
         else {
             switch (scaleIcon) {
                 case SCALE_ICON_IS_TOO_BIG:
                     icx = Math.Min(icx, 1);
                     icy = Math.Min(icy, 1);
                     break;
                 case SCALE_ICON_IS_TOO_SMALL:
                     icx = Math.Max(icx, 1);
                     icy = Math.Max(icy, 1);
                     break;
                 case SCALE_ICON_NEVER:
                     icx = icy = 1;
                     break;
                 default:
                     break;
             }
         }
         float xpos = iconBox.Left + (iconBox.Width - (boundingBoxWidth * icx)) * iconHorizontalAdjustment;
         float ypos = iconBox.Bottom + (iconBox.Height - (boundingBoxHeight * icy)) * iconVerticalAdjustment;
         app.SaveState();
         app.Rectangle(iconBox.Left, iconBox.Bottom, iconBox.Width, iconBox.Height);
         app.Clip();
         app.NewPath();
         if (tp != null)
             app.AddTemplate(tp, icx, 0, 0, icy, xpos, ypos);
         else {
             float cox = 0;
             float coy = 0;
             if (matrix != null && matrix.Size == 6) {
                 PdfNumber nm = matrix.GetAsNumber(4);
                 if (nm != null)
                     cox = nm.FloatValue;
                 nm = matrix.GetAsNumber(5);
                 if (nm != null)
                     coy = nm.FloatValue;
             }
             app.AddTemplateReference(iconReference, PdfName.FRM, icx, 0, 0, icy, xpos - cox * icx, ypos - coy * icy);
         }
         app.RestoreState();
     }
     if (!float.IsNaN(textX)) {
         app.SaveState();
         app.Rectangle(offX, offX, box.Width - 2 * offX, box.Height - 2 * offX);
         app.Clip();
         app.NewPath();
         if (textColor == null)
             app.ResetGrayFill();
         else
             app.SetColorFill(textColor);
         app.BeginText();
         app.SetFontAndSize(ufont, fsize);
         app.SetTextMatrix(textX, textY);
         app.ShowText(text);
         app.EndText();
         app.RestoreState();
     }
     return app;
 }
        /**
        * Gets the main appearance layer.
        * <p>
        * Consult <A HREF="http://partners.adobe.com/asn/developer/pdfs/tn/PPKAppearances.pdf">PPKAppearances.pdf</A>
        * for further details.
        * @return the main appearance layer
        * @throws DocumentException on error
        * @throws IOException on error
        */
        public PdfTemplate GetAppearance()
        {
            if (IsInvisible()) {
                PdfTemplate t = new PdfTemplate(writer);
                t.BoundingBox = new Rectangle(0, 0);
                writer.AddDirectTemplateSimple(t, null);
                return t;
            }
            if (app[0] == null) {
                PdfTemplate t = app[0] = new PdfTemplate(writer);
                t.BoundingBox = new Rectangle(100, 100);
                writer.AddDirectTemplateSimple(t, new PdfName("n0"));
                t.SetLiteral("% DSBlank\n");
            }
            if (app[1] == null && !acro6Layers) {
                PdfTemplate t = app[1] = new PdfTemplate(writer);
                t.BoundingBox = new Rectangle(100, 100);
                writer.AddDirectTemplateSimple(t, new PdfName("n1"));
                t.SetLiteral(questionMark);
            }
            if (app[2] == null) {
                String text;
                if (layer2Text == null) {
                    StringBuilder buf = new StringBuilder();
                    buf.Append("Digitally signed by ").Append(PdfPKCS7.GetSubjectFields((X509Certificate)certChain[0]).GetField("CN")).Append('\n');
                    buf.Append("Date: ").Append(signDate.ToString("yyyy.MM.dd HH:mm:ss zzz"));
                    if (reason != null)
                        buf.Append('\n').Append("Reason: ").Append(reason);
                    if (location != null)
                        buf.Append('\n').Append("Location: ").Append(location);
                    text = buf.ToString();
                }
                else
                    text = layer2Text;
                PdfTemplate t = app[2] = new PdfTemplate(writer);
                t.BoundingBox = rect;
                writer.AddDirectTemplateSimple(t, new PdfName("n2"));
                if (image != null) {
                    if (imageScale == 0) {
                        t.AddImage(image, rect.Width, 0, 0, rect.Height, 0, 0);
                    }
                    else {
                        float usableScale = imageScale;
                        if (imageScale < 0)
                            usableScale = Math.Min(rect.Width / image.Width, rect.Height / image.Height);
                        float w = image.Width * usableScale;
                        float h = image.Height * usableScale;
                        float x = (rect.Width - w) / 2;
                        float y = (rect.Height - h) / 2;
                        t.AddImage(image, w, 0, 0, h, x, y);
                    }
                }
                Font font;
                if (layer2Font == null)
                    font = new Font();
                else
                    font = new Font(layer2Font);
                float size = font.Size;

                Rectangle dataRect = null;
                Rectangle signatureRect = null;

                if (Render == SignatureRender.NameAndDescription ||
                    (Render == SignatureRender.GraphicAndDescription && this.SignatureGraphic != null)) {
                    // origin is the bottom-left
                    signatureRect = new Rectangle(
                        MARGIN,
                        MARGIN,
                        rect.Width / 2 - MARGIN,
                        rect.Height - MARGIN);
                    dataRect = new Rectangle(
                        rect.Width / 2 +  MARGIN / 2,
                        MARGIN,
                        rect.Width - MARGIN / 2,
                        rect.Height - MARGIN);

                    if (rect.Height > rect.Width) {
                        signatureRect = new Rectangle(
                            MARGIN,
                            rect.Height / 2,
                            rect.Width - MARGIN,
                            rect.Height);
                        dataRect = new Rectangle(
                            MARGIN,
                            MARGIN,
                            rect.Width - MARGIN,
                            rect.Height / 2 - MARGIN);
                    }
                }
                else {
                    dataRect = new Rectangle(
                        MARGIN,
                        MARGIN,
                        rect.Width - MARGIN,
                        rect.Height * (1 - TOP_SECTION) - MARGIN);
                }

                if (Render == SignatureRender.NameAndDescription) {
                    string signedBy = Legacy.Text.Pdf.PdfPKCS7.GetSubjectFields(this.certChain[0]).GetField("CN");
                    Rectangle sr2 = new Rectangle(signatureRect.Width - MARGIN, signatureRect.Height - MARGIN );
                    float signedSize = FitText(font, signedBy, sr2, -1, runDirection);

                    ColumnText ct2 = new ColumnText(t);
                    ct2.RunDirection = runDirection;
                    ct2.SetSimpleColumn(new Phrase(signedBy, font), signatureRect.Left, signatureRect.Bottom, signatureRect.Right, signatureRect.Top, signedSize, Element.ALIGN_LEFT);

                    ct2.Go();
                }
                else if (Render == SignatureRender.GraphicAndDescription) {
                    ColumnText ct2 = new ColumnText(t);
                    ct2.RunDirection = runDirection;
                    ct2.SetSimpleColumn(signatureRect.Left, signatureRect.Bottom, signatureRect.Right, signatureRect.Top, 0, Element.ALIGN_RIGHT);

                    Image im = Image.GetInstance(SignatureGraphic);
                    im.ScaleToFit(signatureRect.Width, signatureRect.Height);

                    Paragraph p = new Paragraph();
                    // must calculate the point to draw from to make image appear in middle of column
                    float x = 0;
                    // experimentation found this magic number to counteract Adobe's signature graphic, which
                    // offsets the y co-ordinate by 15 units
                    float y = -im.ScaledHeight + 15;

                    x = x + (signatureRect.Width - im.ScaledWidth) / 2;
                    y = y - (signatureRect.Height - im.ScaledHeight) / 2;
                    p.Add(new Chunk(im, x + (signatureRect.Width - im.ScaledWidth) / 2, y, false));
                    ct2.AddElement(p);
                    ct2.Go();
                }

                if (size <= 0) {
                    Rectangle sr = new Rectangle(dataRect.Width, dataRect.Height);
                    size = FitText(font, text, sr, 12, runDirection);
                }
                ColumnText ct = new ColumnText(t);
                ct.RunDirection = runDirection;
                ct.SetSimpleColumn(new Phrase(text, font), dataRect.Left, dataRect.Bottom, dataRect.Right, dataRect.Top, size, Element.ALIGN_LEFT);
                ct.Go();

            }
            if (app[3] == null && !acro6Layers) {
                PdfTemplate t = app[3] = new PdfTemplate(writer);
                t.BoundingBox = new Rectangle(100, 100);
                writer.AddDirectTemplateSimple(t, new PdfName("n3"));
                t.SetLiteral("% DSBlank\n");
            }
            if (app[4] == null && !acro6Layers) {
                PdfTemplate t = app[4] = new PdfTemplate(writer);
                t.BoundingBox = new Rectangle(0, rect.Height * (1 - TOP_SECTION), rect.Right, rect.Top);
                writer.AddDirectTemplateSimple(t, new PdfName("n4"));
                Font font;
                if (layer2Font == null)
                    font = new Font();
                else
                    font = new Font(layer2Font);
                float size = font.Size;
                String text = "Signature Not Verified";
                if (layer4Text != null)
                    text = layer4Text;
                Rectangle sr = new Rectangle(rect.Width - 2 * MARGIN, rect.Height * TOP_SECTION - 2 * MARGIN);
                size = FitText(font, text, sr, 15, runDirection);
                ColumnText ct = new ColumnText(t);
                ct.RunDirection = runDirection;
                ct.SetSimpleColumn(new Phrase(text, font), MARGIN, 0, rect.Width - MARGIN, rect.Height - MARGIN, size, Element.ALIGN_LEFT);
                ct.Go();
            }
            int rotation = writer.reader.GetPageRotation(page);
            Rectangle rotated = new Rectangle(rect);
            int n = rotation;
            while (n > 0) {
                rotated = rotated.Rotate();
                n -= 90;
            }
            if (frm == null) {
                frm = new PdfTemplate(writer);
                frm.BoundingBox = rotated;
                writer.AddDirectTemplateSimple(frm, new PdfName("FRM"));
                float scale = Math.Min(rect.Width, rect.Height) * 0.9f;
                float x = (rect.Width - scale) / 2;
                float y = (rect.Height - scale) / 2;
                scale /= 100;
                if (rotation == 90)
                    frm.ConcatCTM(0, 1, -1, 0, rect.Height, 0);
                else if (rotation == 180)
                    frm.ConcatCTM(-1, 0, 0, -1, rect.Width, rect.Height);
                else if (rotation == 270)
                    frm.ConcatCTM(0, -1, 1, 0, 0, rect.Width);
                frm.AddTemplate(app[0], 0, 0);
                if (!acro6Layers)
                    frm.AddTemplate(app[1], scale, 0, 0, scale, x, y);
                frm.AddTemplate(app[2], 0, 0);
                if (!acro6Layers) {
                    frm.AddTemplate(app[3], scale, 0, 0, scale, x, y);
                    frm.AddTemplate(app[4], 0, 0);
                }
            }
            PdfTemplate napp = new PdfTemplate(writer);
            napp.BoundingBox = rotated;
            writer.AddDirectTemplateSimple(napp, null);
            napp.AddTemplate(frm, 0, 0);
            return napp;
        }