コード例 #1
0
ファイル: ColumnText.cs プロジェクト: pixelia-es/RazorPDF2
 /** Shows a line of text. Only the first line is written.
  * @param canvas where the text is to be written to
  * @param alignment the alignment. It is not influenced by the run direction
  * @param phrase the <CODE>Phrase</CODE> with the text
  * @param x the x reference position
  * @param y the y reference position
  * @param rotation the rotation to be applied in degrees counterclockwise
  * @param runDirection the run direction
  * @param arabicOptions the options for the arabic shaping
  */
 public static void ShowTextAligned(PdfContentByte canvas, int alignment, Phrase phrase, float x, float y, float rotation, int runDirection, int arabicOptions)
 {
     if (alignment != Element.ALIGN_LEFT && alignment != Element.ALIGN_CENTER
     && alignment != Element.ALIGN_RIGHT)
     alignment = Element.ALIGN_LEFT;
     canvas.SaveState();
     ColumnText ct = new ColumnText(canvas);
     float lly = -1;
     float ury = 2;
     float llx;
     float urx;
     switch (alignment) {
     case Element.ALIGN_LEFT:
         llx = 0;
         urx = 20000;
         break;
     case Element.ALIGN_RIGHT:
         llx = -20000;
         urx = 0;
         break;
     default:
         llx = -20000;
         urx = 20000;
         break;
     }
     if (rotation == 0) {
     llx += x;
     lly += y;
     urx += x;
     ury += y;
     }
     else {
     double alpha = rotation * Math.PI / 180.0;
     float cos = (float)Math.Cos(alpha);
     float sin = (float)Math.Sin(alpha);
     canvas.ConcatCTM(cos, sin, -sin, cos, x, y);
     }
     ct.SetSimpleColumn(phrase, llx, lly, urx, ury, 2, alignment);
     if (runDirection == PdfWriter.RUN_DIRECTION_RTL) {
     if (alignment == Element.ALIGN_LEFT)
         alignment = Element.ALIGN_RIGHT;
     else if (alignment == Element.ALIGN_RIGHT)
         alignment = Element.ALIGN_LEFT;
     }
     ct.Alignment = alignment;
     ct.ArabicOptions = arabicOptions;
     ct.RunDirection = runDirection;
     ct.Go();
     canvas.RestoreState();
 }
コード例 #2
0
ファイル: TextField.cs プロジェクト: pixelia-es/RazorPDF2
 /**
 * Get the <code>PdfAppearance</code> of a text or combo field
 * @throws IOException on error
 * @throws DocumentException on error
 * @return A <code>PdfAppearance</code>
 */
 public PdfAppearance GetAppearance()
 {
     PdfAppearance app = GetBorderAppearance();
     app.BeginVariableText();
     if (text == null || text.Length == 0) {
         app.EndVariableText();
         return app;
     }
     bool borderExtra = borderStyle == PdfBorderDictionary.STYLE_BEVELED || borderStyle == PdfBorderDictionary.STYLE_INSET;
     float h = box.Height - borderWidth * 2 - extraMarginTop;
     float bw2 = borderWidth;
     if (borderExtra) {
         h -= borderWidth * 2;
         bw2 *= 2;
     }
     float offsetX = Math.Max(bw2, 1);
     float offX = Math.Min(bw2, offsetX);
     app.SaveState();
     app.Rectangle(offX, offX, box.Width - 2 * offX, box.Height - 2 * offX);
     app.Clip();
     app.NewPath();
     String ptext;
     if ((options & PASSWORD) != 0)
         ptext = ObfuscatePassword(text);
     else if ((options & MULTILINE) == 0)
         ptext = RemoveCRLF(text);
     else
         ptext = text; //fixed by Kazuya Ujihara (ujihara.jp)
     BaseFont ufont = RealFont;
     Color fcolor = (textColor == null) ? GrayColor.GRAYBLACK : textColor;
     int rtl = CheckRTL(ptext) ? PdfWriter.RUN_DIRECTION_LTR : PdfWriter.RUN_DIRECTION_NO_BIDI;
     float usize = fontSize;
     Phrase phrase = ComposePhrase(ptext, ufont, fcolor, usize);
     if ((options & MULTILINE) != 0) {
         float width = box.Width - 4 * offsetX - extraMarginLeft;
         float factor = ufont.GetFontDescriptor(BaseFont.BBOXURY, 1) - ufont.GetFontDescriptor(BaseFont.BBOXLLY, 1);
         ColumnText ct = new ColumnText(null);
         if (usize == 0) {
             usize = h / factor;
             if (usize > 4) {
                 if (usize > 12)
                     usize = 12;
                 float step = Math.Max((usize - 4) / 10, 0.2f);
                 ct.SetSimpleColumn(0, -h, width, 0);
                 ct.Alignment = alignment;
                 ct.RunDirection = rtl;
                 for (; usize > 4; usize -= step) {
                     ct.YLine = 0;
                     ChangeFontSize(phrase, usize);
                     ct.SetText(phrase);
                     ct.Leading = factor * usize;
                     int status = ct.Go(true);
                     if ((status & ColumnText.NO_MORE_COLUMN) == 0)
                         break;
                 }
             }
             if (usize < 4) {
                 usize = 4;
             }
         }
         ChangeFontSize(phrase, usize);
         ct.Canvas = app;
         float leading = usize * factor;
         float offsetY = offsetX + h - ufont.GetFontDescriptor(BaseFont.BBOXURY, usize);
         ct.SetSimpleColumn(extraMarginLeft + 2 * offsetX, -20000, box.Width - 2 * offsetX, offsetY + leading);
         ct.Leading = leading;
         ct.Alignment = alignment;
         ct.RunDirection = rtl;
         ct.SetText(phrase);
         ct.Go();
     }
     else {
         if (usize == 0) {
             float maxCalculatedSize = h / (ufont.GetFontDescriptor(BaseFont.BBOXURX, 1) - ufont.GetFontDescriptor(BaseFont.BBOXLLY, 1));
             ChangeFontSize(phrase, 1);
             float wd = ColumnText.GetWidth(phrase, rtl, 0);
             if (wd == 0)
                 usize = maxCalculatedSize;
             else
                 usize = Math.Min(maxCalculatedSize, (box.Width - extraMarginLeft - 4 * offsetX) / wd);
             if (usize < 4)
                 usize = 4;
         }
         ChangeFontSize(phrase, usize);
         float offsetY = offX + ((box.Height - 2*offX) - ufont.GetFontDescriptor(BaseFont.ASCENT, usize)) / 2;
         if (offsetY < offX)
             offsetY = offX;
         if (offsetY - offX < -ufont.GetFontDescriptor(BaseFont.DESCENT, usize)) {
             float ny = -ufont.GetFontDescriptor(BaseFont.DESCENT, usize) + offX;
             float dy = box.Height - offX - ufont.GetFontDescriptor(BaseFont.ASCENT, usize);
             offsetY = Math.Min(ny, Math.Max(offsetY, dy));
         }
         if ((options & COMB) != 0 && maxCharacterLength > 0) {
             int textLen = Math.Min(maxCharacterLength, ptext.Length);
             int position = 0;
             if (alignment == Element.ALIGN_RIGHT) {
                 position = maxCharacterLength - textLen;
             }
             else if (alignment == Element.ALIGN_CENTER) {
                 position = (maxCharacterLength - textLen) / 2;
             }
             float step = (box.Width - extraMarginLeft) / maxCharacterLength;
             float start = step / 2 + position * step;
             if (textColor == null)
                 app.SetGrayFill(0);
             else
                 app.SetColorFill(textColor);
             app.BeginText();
             foreach (Chunk ck in phrase) {
                 BaseFont bf = ck.Font.BaseFont;
                 app.SetFontAndSize(bf, usize);
                 StringBuilder sb = ck.Append("");
                 for (int j = 0; j < sb.Length; ++j) {
                     String c = sb.ToString(j, 1);
                     float wd = bf.GetWidthPoint(c, usize);
                     app.SetTextMatrix(extraMarginLeft + start - wd / 2, offsetY - extraMarginTop);
                     app.ShowText(c);
                     start += step;
                 }
             }
             app.EndText();
         }
         else {
             float x;
             switch (alignment) {
                 case Element.ALIGN_RIGHT:
                     x = extraMarginLeft + box.Width - (2 * offsetX);
                     break;
                 case Element.ALIGN_CENTER:
                     x = extraMarginLeft + (box.Width / 2);
                     break;
                 default:
                     x = extraMarginLeft + (2 * offsetX);
                     break;
             }
             ColumnText.ShowTextAligned(app, alignment, phrase, x, offsetY - extraMarginTop, 0, rtl, 0);
         }
     }
     app.RestoreState();
     app.EndVariableText();
     return app;
 }
コード例 #3
0
        /**
        * 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;
        }
コード例 #4
0
 /**
 * Fits the text to some rectangle adjusting the font size as needed.
 * @param font the font to use
 * @param text the text
 * @param rect the rectangle where the text must fit
 * @param maxFontSize the maximum font size
 * @param runDirection the run direction
 * @return the calculated font size that makes the text fit
 */
 public static float FitText(Font font, String text, Rectangle rect, float maxFontSize, int runDirection)
 {
     ColumnText ct = null;
     int status = 0;
     if (maxFontSize <= 0) {
         int cr = 0;
         int lf = 0;
         char[] t = text.ToCharArray();
         for (int k = 0; k < t.Length; ++k) {
             if (t[k] == '\n')
                 ++lf;
             else if (t[k] == '\r')
                 ++cr;
         }
         int minLines = Math.Max(cr, lf) + 1;
         maxFontSize = Math.Abs(rect.Height) / minLines - 0.001f;
     }
     font.Size = maxFontSize;
     Phrase ph = new Phrase(text, font);
     ct = new ColumnText(null);
     ct.SetSimpleColumn(ph, rect.Left, rect.Bottom, rect.Right, rect.Top, maxFontSize, Element.ALIGN_LEFT);
     ct.RunDirection = runDirection;
     status = ct.Go(true);
     if ((status & ColumnText.NO_MORE_TEXT) != 0)
         return maxFontSize;
     float precision = 0.1f;
     float min = 0;
     float max = maxFontSize;
     float size = maxFontSize;
     for (int k = 0; k < 50; ++k) { //just in case it doesn't converge
         size = (min + max) / 2;
         ct = new ColumnText(null);
         font.Size = size;
         ct.SetSimpleColumn(new Phrase(text, font), rect.Left, rect.Bottom, rect.Right, rect.Top, size, Element.ALIGN_LEFT);
         ct.RunDirection = runDirection;
         status = ct.Go(true);
         if ((status & ColumnText.NO_MORE_TEXT) != 0) {
             if (max - min < size * precision)
                 return size;
             min = size;
         }
         else
             max = size;
     }
     return size;
 }
コード例 #5
0
ファイル: PdfPRow.cs プロジェクト: pixelia-es/RazorPDF2
 /**
 * @since	3.0.0 protected is now public static
 */
 public static float SetColumn(ColumnText ct, float left, float bottom, float right, float top)
 {
     if (left > right)
         right = left;
     if (bottom > top)
         top = bottom;
     ct.SetSimpleColumn(left, bottom, right, top);
     return top;
 }
コード例 #6
0
ファイル: PdfDocument.cs プロジェクト: pixelia-es/RazorPDF2
 //    [M4] Adding a PdfPTable
 /** Adds a <CODE>PdfPTable</CODE> to the document.
 * @param ptable the <CODE>PdfPTable</CODE> to be added to the document.
 * @throws DocumentException on error
 */
 internal void AddPTable(PdfPTable ptable)
 {
     ColumnText ct = new ColumnText(writer.DirectContent);
     // if the table prefers to be on a single page, and it wouldn't
     //fit on the current page, start a new page.
     if (ptable.KeepTogether && !FitsPage(ptable, 0f) && currentHeight > 0)  {
         NewPage();
     }
     // add dummy paragraph if we aren't at the top of a page, so that
     // spacingBefore will be taken into account by ColumnText
     if (currentHeight > 0) {
         Paragraph p = new Paragraph();
         p.Leading = 0;
         ct.AddElement(p);
     }
     ct.AddElement(ptable);
     bool he = ptable.HeadersInEvent;
     ptable.HeadersInEvent = true;
     int loop = 0;
     while (true) {
         ct.SetSimpleColumn(IndentLeft, IndentBottom, IndentRight, IndentTop - currentHeight);
         int status = ct.Go();
         if ((status & ColumnText.NO_MORE_TEXT) != 0) {
             text.MoveText(0, ct.YLine - IndentTop + currentHeight);
             currentHeight = IndentTop - ct.YLine;
             break;
         }
         if (IndentTop - currentHeight == ct.YLine)
             ++loop;
         else
             loop = 0;
         if (loop == 3) {
             Add(new Paragraph("ERROR: Infinite table loop"));
             break;
         }
         NewPage();
     }
     ptable.HeadersInEvent = he;
 }