Ejemplo n.º 1
0
        public void Write(Document document, PdfWriter writer, PdfContentByte pdfContent)
        {
            Phrase header = new Phrase(_paragraph, _font);

            ColumnText.ShowTextAligned(pdfContent, Element.ALIGN_CENTER, header,
                                       document.Right - ColumnText.GetWidth(header), document.Bottom - 5, 0); // document.Top 为Page的margin.top
        }
Ejemplo n.º 2
0
        private static float GetWidthText(string text, string font, float size, int style)
        {
            Font   font1  = RegistrerFont(font, size, style, BaseColor.BLACK);
            Phrase phrase = new Phrase(text, font1);

            return(ColumnText.GetWidth(phrase));
        }
Ejemplo n.º 3
0
        public void Write(Document document, PdfWriter writer, PdfContentByte pdfContent)
        {
            Phrase header = new Phrase(document.PageNumber.ToString(), _font);

            float horizonCenter = (document.Right - ColumnText.GetWidth(header)) / 2;
            float verticalTop   = document.Bottom;

            ColumnText.ShowTextAligned(pdfContent, Element.ALIGN_CENTER, header,
                                       horizonCenter, verticalTop, 0); // document.Top 为Page的margin.top
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 指定pdf模板文件添加图片以及文字
        /// </summary>
        /// <param name="tempFilePath">源文件路径</param>
        /// <param name="createdPdfPath">插入图片后文件路径</param>
        /// <param name="pdfImages">图片对象集合</param>
        public void PdfSign(string tempFilePath, string createdPdfPath, List <PdfImage> pdfImages, List <PdfText> pdfTexts)
        {
            PdfReader  pdfReader  = null;
            PdfStamper pdfStamper = null;

            try
            {
                pdfReader  = new PdfReader(tempFilePath);
                pdfStamper = new PdfStamper(pdfReader, new FileStream(createdPdfPath, FileMode.OpenOrCreate));

                float width  = pdfReader.GetPageSizeWithRotation(1).Width;
                float height = pdfReader.GetPageSizeWithRotation(1).Height;
                if (m_PaperWidth == 0 || m_PaperHeight == 0)
                {
                    m_PaperWidth  = (float)(width / 72 * 25.4);
                    m_PaperHeight = (float)(height / 72 * 25.4);
                }
                float cmpOne = width, cmpTwo = height, cmpThr = m_PaperWidth, cmpFour = m_PaperHeight;
                if (height > width)
                {
                    cmpOne = height;
                    cmpTwo = width;
                }
                if (m_PaperHeight > m_PaperWidth)
                {
                    cmpThr  = m_PaperHeight;
                    cmpFour = m_PaperWidth;
                }
                float transScalH = cmpOne / cmpThr;
                float transScalV = cmpTwo / cmpFour;
                foreach (var pdfImage in pdfImages)
                {
                    Image img = Image.GetInstance(pdfImage.ImagePath);
                    if (img != null)
                    {
                        try
                        {
                            PdfContentByte pdfContentByte = pdfStamper.GetOverContent(pdfImage.PageNum);
                            img.ScaleToFit(pdfImage.FitWidth * transScalH, pdfImage.FitHeight * transScalV);
                            img.SetAbsolutePosition((pdfImage.AbsoluteX) * transScalH, (pdfImage.AbsoluteY) * transScalV);
                            img.Rotation = (float)(pdfImage.Rotation / 180 * Math.PI);
                            pdfContentByte.AddImage(img);
                        }
                        catch (Exception)
                        {
                            continue;
                        }
                    }
                }
                foreach (var pdfText in pdfTexts)
                {
                    string               fontPath = SystemFontsFolder + "\\" + pdfText.FontName;
                    BaseFont             baseFont = BaseFont.CreateFont(fontPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);//获取系统的字体
                    iTextSharp.text.Font font     = new iTextSharp.text.Font(baseFont, pdfText.Size);
                    Phrase               phrase   = new Phrase(pdfText.TxtContent, font);
                    float recWidth = ColumnText.GetWidth(phrase);

                    float centX = pdfText.AbsoluteX * transScalH;
                    float centY = pdfText.AbsoluteY * transScalV;
                    try
                    {
                        PdfContentByte pdfContentByte = pdfStamper.GetOverContent(pdfText.PageNum);
                        ColumnText.ShowTextAligned(pdfContentByte, pdfText.Alignment, phrase, centX, centY, pdfText.Rotation);
                    }
                    catch (Exception)
                    {
                        continue;
                    }
                }
                pdfStamper.FormFlattening = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (pdfStamper != null)
                {
                    pdfStamper.Close();
                }

                if (pdfReader != null)
                {
                    pdfReader.Close();
                }

                pdfStamper = null;
                pdfReader  = null;
            }
        }
Ejemplo n.º 5
0
        /**
         * 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);
        }