示例#1
0
        public void PaintText(string textColor, string fontPath, float fontSize, float textLeading, float textSpacing, string textContent, float textX, float textY, bool isH)
        {
            CMYK_Color cmyk_titleColor = new CMYK_Color().GetCMYK_Color(textColor);

            FontFactory.Register(fontPath);
            BaseFont bfChinese = BaseFont.CreateFont(fontPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

            PdfContentByte contentByte = pdfWriter.DirectContent;

            contentByte.BeginText();
            contentByte.SetCMYKColorFill(cmyk_titleColor.C, cmyk_titleColor.M, cmyk_titleColor.Y, cmyk_titleColor.K);
            contentByte.SetCMYKColorStroke(cmyk_titleColor.C, cmyk_titleColor.M, cmyk_titleColor.Y, cmyk_titleColor.K);
            contentByte.SetFontAndSize(bfChinese, fontSize);
            contentByte.SetLeading(textLeading);
            contentByte.SetCharacterSpacing(textSpacing);
            //contentByte.SetTextMatrix(textX, textY);
            string p1 = "^[]】}》>))』@[{}【{《<((『‖︱ ︳♀♂]$";

            for (int i = 0; i < textContent.Length; i++)
            {
                contentByte.SetTextMatrix(textX, textY - (10f * i));
                if (Regex.IsMatch(textContent[i].ToString(), p1))
                {
                    contentByte.ShowTextAligned(Element.ALIGN_CENTER, textContent[i].ToString(), textX + 1f, textY - (10f * i), -90);
                }
                else
                {
                    contentByte.ShowText(textContent[i].ToString());
                }

                contentByte.NewlineText();
            }
            contentByte.Stroke();
            contentByte.EndText();
        }
示例#2
0
// ===========================================================================
        public void Write(Stream stream)
        {
            // step 1
            using (Document document = new Document()) {
                // step 2
                PdfWriter writer = PdfWriter.GetInstance(document, stream);
                // step 3
                document.Open();
                // step 4
                PdfContentByte canvas = writer.DirectContent;
                String         text   = "AWAY again";
                BaseFont       bf     = BaseFont.CreateFont();
                canvas.BeginText();
                // line 1
                canvas.SetFontAndSize(bf, 16);
                canvas.MoveText(36, 806);
                canvas.MoveTextWithLeading(0, -24);
                canvas.ShowText(text);
                // line 2
                canvas.SetWordSpacing(20);
                canvas.NewlineShowText(text);
                // line 3
                canvas.SetCharacterSpacing(10);
                canvas.NewlineShowText(text);
                canvas.SetWordSpacing(0);
                canvas.SetCharacterSpacing(0);
                // line 4
                canvas.SetHorizontalScaling(50);
                canvas.NewlineShowText(text);
                canvas.SetHorizontalScaling(100);
                // line 5
                canvas.NewlineShowText(text);
                canvas.SetTextRise(15);
                canvas.SetFontAndSize(bf, 12);
                canvas.SetColorFill(BaseColor.RED);
                canvas.ShowText("2");
                canvas.SetColorFill(GrayColor.GRAYBLACK);
                // line 6
                canvas.SetLeading(56);
                canvas.NewlineShowText("Changing the leading: " + text);
                canvas.SetLeading(24);
                canvas.NewlineText();
                // line 7
                PdfTextArray array = new PdfTextArray("A");
                array.Add(120);
                array.Add("W");
                array.Add(120);
                array.Add("A");
                array.Add(95);
                array.Add("Y again");
                canvas.ShowText(array);
                canvas.EndText();

                canvas.SetColorFill(BaseColor.BLUE);
                canvas.BeginText();
                canvas.SetTextMatrix(360, 770);
                canvas.SetTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL);
                canvas.SetFontAndSize(bf, 24);
                canvas.ShowText(text);
                canvas.EndText();

                canvas.BeginText();
                canvas.SetTextMatrix(360, 730);
                canvas.SetTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_STROKE);
                canvas.SetFontAndSize(bf, 24);
                canvas.ShowText(text);
                canvas.EndText();

                canvas.BeginText();
                canvas.SetTextMatrix(360, 690);
                canvas.SetTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE);
                canvas.SetFontAndSize(bf, 24);
                canvas.ShowText(text);
                canvas.EndText();

                canvas.BeginText();
                canvas.SetTextMatrix(360, 650);
                canvas.SetTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_INVISIBLE);
                canvas.SetFontAndSize(bf, 24);
                canvas.ShowText(text);
                canvas.EndText();

                PdfTemplate template = canvas.CreateTemplate(200, 36);
                template.SetLineWidth(2);
                for (int i = 0; i < 6; i++)
                {
                    template.MoveTo(0, i * 6);
                    template.LineTo(200, i * 6);
                }
                template.Stroke();

                canvas.SaveState();
                canvas.BeginText();
                canvas.SetTextMatrix(360, 610);
                canvas.SetTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL_CLIP);
                canvas.SetFontAndSize(bf, 24);
                canvas.ShowText(text);
                canvas.EndText();
                canvas.AddTemplate(template, 360, 610);
                canvas.RestoreState();

                canvas.SaveState();
                canvas.BeginText();
                canvas.SetTextMatrix(360, 570);
                canvas.SetTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_STROKE_CLIP);
                canvas.SetFontAndSize(bf, 24);
                canvas.ShowText(text);
                canvas.EndText();
                canvas.AddTemplate(template, 360, 570);
                canvas.RestoreState();

                canvas.SaveState();
                canvas.BeginText();
                canvas.SetTextMatrix(360, 530);
                canvas.SetTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE_CLIP);
                canvas.SetFontAndSize(bf, 24);
                canvas.ShowText(text);
                canvas.EndText();
                canvas.AddTemplate(template, 360, 530);
                canvas.RestoreState();

                canvas.SaveState();
                canvas.BeginText();
                canvas.SetTextMatrix(360, 490);
                canvas.SetTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_CLIP);
                canvas.SetFontAndSize(bf, 24);
                canvas.ShowText(text);
                canvas.EndText();
                canvas.AddTemplate(template, 360, 490);
                canvas.RestoreState();
            }
        }
示例#3
0
        /// <summary>
        /// 输出多行文字
        /// </summary>
        /// <param name="textColor"></param>
        /// <param name="fontPath"></param>
        /// <param name="fontSize"></param>
        /// <param name="textLeading"></param>
        /// <param name="textSpacing"></param>
        /// <param name="textContent"></param>
        /// <param name="textX"></param>
        /// <param name="textY"></param>
        public void PaintContent(string textColor, string fontPath, float fontSize, float textLeading, float textSpacing, string textContent, float textX, float textY, float maxWidth, int maxLine)
        {
            textContent = textContent.Replace("\n", "&hh;");
            textContent = textContent.Replace("&hh;", "\r\n");

            CMYK_Color cmyk_titleColor = new CMYK_Color().GetCMYK_Color(textColor);

            FontFactory.Register(fontPath);
            BaseFont bfChinese = BaseFont.CreateFont(fontPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

            PdfContentByte contentByte = pdfWriter.DirectContent;

            contentByte.BeginText();
            contentByte.SetCMYKColorFill(cmyk_titleColor.C, cmyk_titleColor.M, cmyk_titleColor.Y, cmyk_titleColor.K);
            contentByte.SetCMYKColorStroke(cmyk_titleColor.C, cmyk_titleColor.M, cmyk_titleColor.Y, cmyk_titleColor.K);
            contentByte.SetFontAndSize(bfChinese, fontSize);
            contentByte.SetLeading(textLeading);
            contentByte.SetCharacterSpacing(textSpacing);
            contentByte.SetTextMatrix(textX, textY);


            string strOutputContent = "";
            int    linesCount       = 1;

            for (int i = 0; i < textContent.Length; i++)
            {
                if (linesCount > maxLine)
                {
                    break;
                }
                strOutputContent += textContent[i].ToString();
                float floLineContentSize = bfChinese.GetWidthPointKerned(strOutputContent, fontSize);
                if ((strOutputContent.Contains("\r") == true && textContent.Substring(i, 2).Equals("\r\n") == true) || floLineContentSize >= maxWidth)
                {
                    string nextNeedStr = "";
                    if (floLineContentSize >= maxWidth && (i + 1) < textContent.Length && textContent[i] != ' ' && IsNatural_Number(textContent[i].ToString()) && IsNatural_Number(textContent[i].ToString()))
                    {
                        if (textContent[i + 1] != ' ')
                        {
                            int lastWhiteStrIndex = strOutputContent.LastIndexOf(' ');
                            if (lastWhiteStrIndex > 20)
                            {
                                nextNeedStr      = strOutputContent.Substring(lastWhiteStrIndex);
                                strOutputContent = strOutputContent.Substring(0, lastWhiteStrIndex + 1);
                                nextNeedStr      = nextNeedStr.TrimStart(' ');
                            }
                        }
                        else
                        {
                            i = i + 1;
                        }
                    }
                    string p = @"[].,,。;;》>、!!??】}}]";
                    if (floLineContentSize >= maxWidth && (i + 1) < textContent.Length && Regex.IsMatch(textContent[i + 1].ToString(), p))
                    {
                        strOutputContent += textContent[i + 1].ToString();
                        i = i + 1;
                    }
                    strOutputContent = strOutputContent.TrimEnd('r').TrimEnd('\\');// Remove(strOutputContent.Length - 1, 1);
                    contentByte.ShowTextAligned(PdfContentByte.ALIGN_LEFT, strOutputContent, textX, textY, 0);
                    contentByte.NewlineText();
                    linesCount++;
                    strOutputContent = nextNeedStr;
                    continue;
                }
            }
            if (linesCount <= maxLine)
            {
                contentByte.ShowText(strOutputContent);
            }
            contentByte.Stroke();
            contentByte.EndText();
        }