コード例 #1
0
        private byte[] CreatePdfWithNegativeCharSpacing(String str1, float charSpacing, String str2)
        {
            MemoryStream baos   = new MemoryStream();
            Document     doc    = new Document();
            PdfWriter    writer = PdfWriter.GetInstance(doc, baos);

            writer.CompressionLevel = 0;
            doc.Open();

            PdfContentByte canvas = writer.DirectContent;

            canvas.BeginText();
            canvas.SetFontAndSize(BaseFont.CreateFont(), 12);
            canvas.MoveText(45, doc.PageSize.Height - 45);
            PdfTextArray ta = new PdfTextArray();

            ta.Add(str1);
            ta.Add(charSpacing);
            ta.Add(str2);
            canvas.ShowText(ta);
            canvas.EndText();

            doc.Close();

            return(baos.ToArray());
        }
コード例 #2
0
        public virtual void TestWordSpacingCausedByExplicitGlyphPositioning2()
        {
            PdfTextArray textArray = new PdfTextArray();

            textArray.Add(new PdfString("S"));
            textArray.Add(3.2f);
            textArray.Add(new PdfString("an"));
            textArray.Add(-255);
            textArray.Add(new PdfString("D"));
            textArray.Add(13);
            textArray.Add(new PdfString("i"));
            textArray.Add(8.3f);
            textArray.Add(new PdfString("e"));
            textArray.Add(-10.1f);
            textArray.Add(new PdfString("g"));
            textArray.Add(1.6f);
            textArray.Add(new PdfString("o"));
            textArray.Add(-247.5f);
            textArray.Add(new PdfString("C"));
            textArray.Add(2.4f);
            textArray.Add(new PdfString("h"));
            textArray.Add(5.8f);
            textArray.Add(new PdfString("ap"));
            textArray.Add(3);
            textArray.Add(new PdfString("t"));
            textArray.Add(10.7f);
            textArray.Add(new PdfString("er"));
            byte[] bytes = CreatePdfWithArrayText(textArray);
            NUnit.Framework.Assert.AreEqual("San Diego Chapter", PdfTextExtractor.GetTextFromPage(new PdfDocument(new
                                                                                                                  PdfReader(new MemoryStream(bytes))).GetPage(1), CreateRenderListenerForTest()));
        }
コード例 #3
0
        /// <exception cref="System.IO.IOException"/>
        private byte[] CreatePdfWithLittleFontSize()
        {
            MemoryStream byteStream = new MemoryStream();
            PdfDocument  document   = new PdfDocument(new PdfWriter(byteStream));
            PdfPage      page1      = document.AddNewPage();
            PdfCanvas    canvas     = new PdfCanvas(page1);
            PdfTextArray textArray  = new PdfTextArray();
            PdfFont      font       = PdfFontFactory.CreateFont();

            textArray.Add(font.ConvertToBytes("P"));
            textArray.Add(1);
            textArray.Add(font.ConvertToBytes("r"));
            textArray.Add(1);
            textArray.Add(font.ConvertToBytes("e"));
            textArray.Add(1);
            textArray.Add(font.ConvertToBytes("f"));
            textArray.Add(1);
            textArray.Add(font.ConvertToBytes("a"));
            textArray.Add(1);
            textArray.Add(font.ConvertToBytes("c"));
            textArray.Add(1);
            textArray.Add(font.ConvertToBytes("e"));
            canvas.SaveState().BeginText().SetFontAndSize(font, 0.2f).ShowText(textArray).EndText().RestoreState();
            canvas.Release();
            document.Close();
            return(byteStream.ToArray());
        }
コード例 #4
0
        /// <exception cref="System.IO.IOException"/>
        private byte[] CreatePdfWithFontSpacingEqualsCharSpacing()
        {
            MemoryStream byteStream = new MemoryStream();
            PdfDocument  document   = new PdfDocument(new PdfWriter(byteStream));
            PdfPage      page1      = document.AddNewPage();
            PdfCanvas    canvas     = new PdfCanvas(page1);
            PdfFont      font       = PdfFontFactory.CreateFont();
            PdfTextArray textArray  = new PdfTextArray();

            textArray.Add(font.ConvertToBytes("P"));
            textArray.Add(-226.2f);
            textArray.Add(font.ConvertToBytes("r"));
            textArray.Add(-231.8f);
            textArray.Add(font.ConvertToBytes("e"));
            textArray.Add(-230.8f);
            textArray.Add(font.ConvertToBytes("f"));
            textArray.Add(-238);
            textArray.Add(font.ConvertToBytes("a"));
            textArray.Add(-238.9f);
            textArray.Add(font.ConvertToBytes("c"));
            textArray.Add(-228.9f);
            textArray.Add(font.ConvertToBytes("e"));
            float charSpace = font.GetWidth(' ', 12);

            canvas.SaveState().BeginText().SetFontAndSize(font, 12).SetCharacterSpacing(-charSpace).ShowText(textArray
                                                                                                             ).EndText().RestoreState();
            canvas.Release();
            document.Close();
            return(byteStream.ToArray());
        }
コード例 #5
0
        /// <exception cref="System.Exception"/>
        private static byte[] CreatePdfWithArrayText(String text1, String text2, int spaceInPoints)
        {
            PdfTextArray textArray = new PdfTextArray();

            textArray.Add(new PdfString(text1));
            textArray.Add(-spaceInPoints);
            textArray.Add(new PdfString(text2));
            return(CreatePdfWithArrayText(textArray));
        }
コード例 #6
0
        /// <exception cref="System.Exception"/>
        private static byte[] CreatePdfWithArrayText(PdfTextArray textArray)
        {
            MemoryStream byteStream = new MemoryStream();
            PdfDocument  document   = new PdfDocument(new PdfWriter(byteStream));

            document.SetDefaultPageSize(new PageSize(612, 792));
            PdfCanvas canvas = new PdfCanvas(document.AddNewPage());
            PdfFont   font   = PdfFontFactory.CreateFont(StandardFonts.HELVETICA);

            canvas.BeginText().SetFontAndSize(font, 12);
            canvas.ShowText(textArray);
            canvas.EndText();
            document.Close();
            return(byteStream.ToArray());
        }
コード例 #7
0
        private void WriteText(String @operator, IList <PdfObject> operands, PdfArray cleanedText, PdfCanvas canvas
                               )
        {
            CanvasGraphicsState canvasGs = canvas.GetGraphicsState();
            bool newLineShowText         = "'".Equals(@operator) || "\"".Equals(@operator);

            if (newLineShowText)
            {
                if (canvasGs.GetLeading() != currLeading)
                {
                    canvas.SetLeading((float)currLeading);
                }
                // after new line operator, removed text shift doesn't matter
                removedTextShift = null;
            }
            PdfTextArray tjShiftArray = null;

            if (removedTextShift != null)
            {
                float tjShift = (float)removedTextShift * 1000 / (canvasGs.GetFontSize() * canvasGs.GetHorizontalScaling()
                                                                  / 100);
                tjShiftArray = new PdfTextArray();
                tjShiftArray.Add(new PdfNumber(tjShift));
            }
            if (cleanedText != null)
            {
                if (newLineShowText)
                {
                    // char spacing and word spacing are set via writeNotAppliedTextStateParams() method
                    canvas.NewlineText();
                }
                if (removedTextShift != null)
                {
                    tjShiftArray.AddAll(cleanedText);
                    cleanedText = tjShiftArray;
                }
                canvas.ShowText(cleanedText);
            }
            else
            {
                if (removedTextShift != null)
                {
                    canvas.ShowText(tjShiftArray);
                }
                PdfCleanUpProcessor.WriteOperands(canvas, operands);
            }
        }
コード例 #8
0
        private byte[] CreatePdfWithFontSpacingEqualsCharSpacing()
        {
            MemoryStream baos   = new MemoryStream();
            Document     doc    = new Document();
            PdfWriter    writer = PdfWriter.GetInstance(doc, baos);

            doc.Open();

            BaseFont font      = BaseFont.CreateFont();
            int      fontSize  = 12;
            float    charSpace = font.GetWidth(' ') / 1000.0f;

            PdfContentByte canvas = writer.DirectContent;

            canvas.BeginText();
            canvas.SetFontAndSize(font, fontSize);
            canvas.MoveText(45, doc.PageSize.Height - 45);
            canvas.SetCharacterSpacing(-charSpace * fontSize);

            PdfTextArray textArray = new PdfTextArray();

            textArray.Add("P");
            textArray.Add(-226.2f);
            textArray.Add("r");
            textArray.Add(-231.8f);
            textArray.Add("e");
            textArray.Add(-230.8f);
            textArray.Add("f");
            textArray.Add(-238);
            textArray.Add("a");
            textArray.Add(-238.9f);
            textArray.Add("c");
            textArray.Add(-228.9f);
            textArray.Add("e");

            canvas.ShowText(textArray);
            canvas.EndText();

            doc.Close();

            byte[] pdfBytes = baos.ToArray();

            return(pdfBytes);
        }
コード例 #9
0
        /// <exception cref="System.Exception"/>
        private byte[] CreatePdfWithNegativeCharSpacing(String str1, float charSpacing, String str2)
        {
            MemoryStream baos        = new MemoryStream();
            PdfDocument  pdfDocument = new PdfDocument(new PdfWriter(baos).SetCompressionLevel(0));
            PdfCanvas    canvas      = new PdfCanvas(pdfDocument.AddNewPage());

            canvas.BeginText();
            canvas.SetFontAndSize(PdfFontFactory.CreateFont(StandardFonts.HELVETICA), 12);
            canvas.MoveText(45, pdfDocument.GetDefaultPageSize().GetHeight() - 45);
            PdfTextArray ta = new PdfTextArray();

            ta.Add(new PdfString(str1));
            ta.Add(charSpacing);
            ta.Add(new PdfString(str2));
            canvas.ShowText(ta);
            canvas.EndText();
            pdfDocument.Close();
            return(baos.ToArray());
        }
コード例 #10
0
        /// <summary>Filter a TextRenderInfo object</summary>
        /// <param name="text">the TextRenderInfo to be filtered</param>
        public virtual PdfCleanUpFilter.FilterResult <PdfArray> FilterText(TextRenderInfo text)
        {
            PdfTextArray textArray = new PdfTextArray();

            if (IsTextNotToBeCleaned(text))
            {
                return(new PdfCleanUpFilter.FilterResult <PdfArray>(false, new PdfArray(text.GetPdfString())));
            }
            foreach (TextRenderInfo ri in text.GetCharacterRenderInfos())
            {
                if (IsTextNotToBeCleaned(ri))
                {
                    textArray.Add(ri.GetPdfString());
                }
                else
                {
                    textArray.Add(new PdfNumber(-ri.GetUnscaledWidth() * 1000f / (text.GetFontSize() * text.GetHorizontalScaling
                                                                                      () / 100)));
                }
            }
            return(new PdfCleanUpFilter.FilterResult <PdfArray>(true, textArray));
        }
コード例 #11
0
        private byte[] CreatePdfWithLittleFontSize()
        {
            MemoryStream baos   = new MemoryStream();
            Document     doc    = new Document();
            PdfWriter    writer = PdfWriter.GetInstance(doc, baos);

            doc.Open();

            BaseFont       font   = BaseFont.CreateFont();
            PdfContentByte canvas = writer.DirectContent;

            canvas.BeginText();
            canvas.SetFontAndSize(font, 0.2f);
            canvas.MoveText(45, doc.PageSize.Height - 45);

            PdfTextArray textArray = new PdfTextArray();

            textArray.Add("P");
            textArray.Add("r");
            textArray.Add("e");
            textArray.Add("f");
            textArray.Add("a");
            textArray.Add("c");
            textArray.Add("e");
            textArray.Add(" ");

            canvas.ShowText(textArray);
            canvas.SetFontAndSize(font, 10);
            canvas.ShowText(textArray);

            canvas.EndText();

            doc.Close();

            byte[] pdfBytes = baos.ToArray();

            return(pdfBytes);
        }
コード例 #12
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();
            }
        }