iTextSharp.text.pdf.TextField.GetAppearance C# (CSharp) Метод

GetAppearance() публичный Метод

public GetAppearance ( ) : PdfAppearance
Результат PdfAppearance
        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;
        }

Usage Example

        // Add annotation to PDF using iTextSharp
        private void addTextAnnotationToPDF(string filePath, string contents, int pageNum, int x, int y, int width, int height)
        {
            PdfReader pdfReader = null;
            PdfStamper pdfStamp = null;

            try
            {
                using (var inStream = new FileStream(filePath, FileMode.Open))
                {
                    pdfReader = new PdfReader(inStream);
                }

                using (var outStream = new FileStream(filePath, FileMode.Create))
                {
                    pdfStamp = new PdfStamper(pdfReader, outStream, (char)0, true);

                    var rect = new iTextSharp.text.Rectangle((float)x, (float)y, (float)x + width, (float)y + height);

                    // Generating the annotation's appearance using a TextField
                    TextField textField = new TextField(pdfStamp.Writer, rect, null);
                    textField.Text = contents;
                    textField.FontSize = 8;
                    textField.TextColor = BaseColor.DARK_GRAY;
                    textField.BackgroundColor = new BaseColor(Color.LightGoldenrodYellow);
                    textField.BorderColor = new BaseColor(Color.BurlyWood);
                    textField.Options = TextField.MULTILINE;
                    textField.SetExtraMargin(2f, 2f);
                    textField.Alignment = Element.ALIGN_TOP | Element.ALIGN_LEFT;
                    PdfAppearance appearance = textField.GetAppearance();

                    // Create the annotation
                    PdfAnnotation annotation = PdfAnnotation.CreateFreeText(pdfStamp.Writer, rect, null, new PdfContentByte(null));
                    annotation.SetAppearance(PdfName.N, appearance);
                    annotation.Flags = PdfAnnotation.FLAGS_READONLY | PdfAnnotation.FLAGS_LOCKED | PdfAnnotation.FLAGS_PRINT;
                    annotation.Put(PdfName.NM, new PdfString(Guid.NewGuid().ToString()));

                    // Add annotation to PDF
                    pdfStamp.AddAnnotation(annotation, pageNum);
                    pdfStamp.Close();
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Could not add signature image to PDF with error: " + ex.Message);
            }
        }
All Usage Examples Of iTextSharp.text.pdf.TextField::GetAppearance