Beispiel #1
0
        protected void ManipulatePdf(string dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
            Document    doc    = new Document(pdfDoc);

            PdfCanvas canvas        = new PdfCanvas(pdfDoc.AddNewPage());
            PdfFont   font          = PdfFontFactory.CreateFont(StandardFonts.HELVETICA);
            string    fillText      = "this is the fill text! ";
            float     fillTextWidth = font.GetWidth(fillText, 6);

            PdfPattern.Tiling tilingPattern = new PdfPattern.Tiling(fillTextWidth, 60, fillTextWidth, 60);
            PdfPatternCanvas  patternCanvas = new PdfPatternCanvas(tilingPattern, pdfDoc);

            patternCanvas.BeginText().SetFontAndSize(font, 6f);
            float x = 0;

            for (float y = 0; y < 60; y += 10)
            {
                patternCanvas.SetTextMatrix(x - fillTextWidth, y);
                patternCanvas.ShowText(fillText);
                patternCanvas.SetTextMatrix(x, y);
                patternCanvas.ShowText(fillText);
                x += (fillTextWidth / 6);
            }

            patternCanvas.EndText();

            canvas.Rectangle(10, 10, 575, 822);
            canvas.SetFillColor(new PatternColor(tilingPattern));
            canvas.Fill();

            doc.Close();
        }
Beispiel #2
0
 public PatternColor(PdfPattern.Tiling uncoloredPattern, PdfSpecialCs.UncoloredTilingPattern uncoloredTilingCS
                     , float[] colorValue)
     : base(uncoloredTilingCS, colorValue)
 {
     this.pattern         = uncoloredPattern;
     this.underlyingColor = MakeColor(uncoloredTilingCS.GetUnderlyingColorSpace(), colorValue);
 }
        public virtual void PatternColorColoredSetTwiceTest()
        {
            String    name   = "patternColorColoredSetTwiceTest.pdf";
            PdfWriter writer = new PdfWriter(destinationFolder + name);

            writer.SetCompressionLevel(CompressionConstants.NO_COMPRESSION);
            PdfDocument document = new PdfDocument(writer);
            PdfPage     page     = document.AddNewPage();
            PdfCanvas   canvas   = new PdfCanvas(page);

            PdfPattern.Tiling square = new PdfPattern.Tiling(15, 15);
            new PdfPatternCanvas(square, document).SetFillColor(new DeviceRgb(0xFF, 0xFF, 0x00)).SetStrokeColor(new DeviceRgb
                                                                                                                    (0xFF, 0x00, 0x00)).Rectangle(5, 5, 5, 5).FillStroke().Release();
            PdfPattern.Tiling ellipse = new PdfPattern.Tiling(15, 10, 20, 25);
            new PdfPatternCanvas(ellipse, document).SetFillColor(new DeviceRgb(0xFF, 0xFF, 0x00)).SetStrokeColor(new DeviceRgb
                                                                                                                     (0xFF, 0x00, 0x00)).Ellipse(2, 2, 13, 8).FillStroke().Release();
            canvas.SetFillColor(new PatternColor(square));
            canvas.Rectangle(36, 696, 126, 126).Fill();
            canvas.SetFillColor(new PatternColor(square));
            canvas.Rectangle(180, 696, 126, 126).Fill();
            canvas.SetFillColor(new PatternColor(ellipse));
            canvas.Rectangle(360, 696, 126, 126).Fill();
            byte[] pageContentStreamBytes = canvas.GetContentStream().GetBytes();
            canvas.Release();
            document.Close();
            String contentStreamString = iText.IO.Util.JavaUtil.GetStringForBytes(pageContentStreamBytes, System.Text.Encoding
                                                                                  .ASCII);
            int p1Count = CountSubstringOccurrences(contentStreamString, "/P1 scn");
            int p2Count = CountSubstringOccurrences(contentStreamString, "/P2 scn");

            NUnit.Framework.Assert.AreEqual(1, p1Count);
            NUnit.Framework.Assert.AreEqual(1, p2Count);
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(destinationFolder + name, sourceFolder +
                                                                             "cmp_" + name, destinationFolder));
        }
Beispiel #4
0
        private void ManipulatePdf(String dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
            Document    doc    = new Document(pdfDoc);

            ImageData img   = ImageDataFactory.Create(IMG);
            Image     image = new Image(img);

            PdfPattern.Tiling imgPattern = new PdfPattern.Tiling(image.GetImageScaledWidth(),
                                                                 image.GetImageScaledHeight());

            PdfPatternCanvas canvas = new PdfPatternCanvas(imgPattern, pdfDoc);

            canvas.AddImageAt(img, 0, 0, false);

            Color color = new PatternColor(imgPattern);

            Table table = new Table(UnitValue.CreatePercentArray(2)).UseAllAvailableWidth();

            table.AddCell(new Cell().Add(new Paragraph("Behold a cell with an image pattern:")));

            Cell cell = new Cell();

            cell.SetHeight(60);
            cell.SetBackgroundColor(color);
            table.AddCell(cell);

            doc.Add(table);

            doc.Close();
        }
Beispiel #5
0
 public PatternColor(PdfPattern.Tiling uncoloredPattern, PdfColorSpace underlyingCS, float[] colorValue)
     : base(new PdfSpecialCs.UncoloredTilingPattern(underlyingCS), colorValue)
 {
     if (underlyingCS is PdfSpecialCs.Pattern)
     {
         throw new ArgumentException("underlyingCS");
     }
     this.pattern         = uncoloredPattern;
     this.underlyingColor = MakeColor(underlyingCS, colorValue);
 }
 public virtual void PatternColorUncoloredPatternCsUnitTest()
 {
     NUnit.Framework.Assert.That(() => {
         PdfDocument doc          = new PdfDocument(new PdfWriter(new MemoryStream()));
         PdfPattern.Tiling circle = new PdfPattern.Tiling(15, 15, 10, 20, false);
         new PdfPatternCanvas(circle, doc).Circle(7.5f, 7.5f, 2.5f).Fill().Release();
         new PatternColor(circle, new PdfSpecialCs.Pattern(), new float[0]);
     }
                                 , NUnit.Framework.Throws.InstanceOf <ArgumentException>())
     ;
 }
        private void CheckInTilingPattern(PdfObject @object)
        {
            String newContentString = PdfACheckerTestUtils.GetStreamWithValue(@object);

            byte[]     newContent = newContentString.GetBytes(System.Text.Encoding.UTF8);
            PdfPattern pattern    = new PdfPattern.Tiling(200, 200);

            ((PdfStream)pattern.GetPdfObject()).SetData(newContent);
            Color color = new PatternColor(pattern);

            pdfA1Checker.CheckColor(color, new PdfDictionary(), true, null);
        }
        public virtual void PatternColorUncoloredSetTwiceTest()
        {
            String    name   = "patternColorUncoloredSetTwiceTest.pdf";
            PdfWriter writer = new PdfWriter(destinationFolder + name);

            writer.SetCompressionLevel(CompressionConstants.NO_COMPRESSION);
            PdfDocument document = new PdfDocument(writer);
            PdfPage     page     = document.AddNewPage();
            PdfCanvas   canvas   = new PdfCanvas(page);

            PdfPattern.Tiling circle = new PdfPattern.Tiling(15, 15, 10, 20, false);
            new PdfPatternCanvas(circle, document).Circle(7.5f, 7.5f, 2.5f).Fill().Release();
            PdfPattern.Tiling line = new PdfPattern.Tiling(5, 10, false);
            new PdfPatternCanvas(line, document).SetLineWidth(1).MoveTo(3, -1).LineTo(3, 11).Stroke().Release();
            PatternColor patternColorCircle = new PatternColor(circle, ColorConstants.RED);

            float[] cyan    = new float[] { 1, 0, 0, 0 };
            float[] magenta = new float[] { 0, 1, 0, 0 };
            PdfSpecialCs.UncoloredTilingPattern uncoloredTilingCmykCs = new PdfSpecialCs.UncoloredTilingPattern(new PdfDeviceCs.Cmyk
                                                                                                                    ());
            PatternColor patternColorLine = new PatternColor(line, uncoloredTilingCmykCs, magenta);

            canvas.SetFillColor(patternColorCircle);
            canvas.Rectangle(36, 696, 126, 126).Fill();
            canvas.SetFillColor(patternColorCircle);
            canvas.Rectangle(180, 696, 126, 126).Fill();
            canvas.SetFillColor(patternColorLine);
            canvas.Rectangle(36, 576, 126, 126).Fill();
            patternColorLine.SetColorValue(cyan);
            canvas.SetFillColor(patternColorLine);
            canvas.Rectangle(180, 576, 126, 126).Fill();
            // this case will be removed when deprecated method is removed
            patternColorLine.SetPattern(circle);
            canvas.SetFillColor(patternColorLine);
            canvas.Rectangle(360, 696, 126, 126).Fill();
            byte[] pageContentStreamBytes = canvas.GetContentStream().GetBytes();
            canvas.Release();
            document.Close();
            String contentStreamString = iText.IO.Util.JavaUtil.GetStringForBytes(pageContentStreamBytes, System.Text.Encoding
                                                                                  .ASCII);
            int p1Count = CountSubstringOccurrences(contentStreamString, "/P1 scn");
            int p2Count = CountSubstringOccurrences(contentStreamString, "/P2 scn");

            NUnit.Framework.Assert.AreEqual(3, p1Count);
            NUnit.Framework.Assert.AreEqual(2, p2Count);
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(destinationFolder + name, sourceFolder +
                                                                             "cmp_" + name, destinationFolder));
        }
Beispiel #9
0
        private static Color?GetColor(PdfPattern.Tiling tiling, float alpha)
        {
            switch (tiling.GetTilingType())
            {
            case PdfPattern.Tiling.TilingType.CONSTANT_SPACING:
            case PdfPattern.Tiling.TilingType.CONSTANT_SPACING_AND_FASTER_TILING:
            case PdfPattern.Tiling.TilingType.NO_DISTORTION:
                break;
            }

            var box     = tiling.GetBBox();
            var xStep   = tiling.GetXStep();
            var yStep   = tiling.GetYStep();
            var colored = tiling.IsColored();

            return(null);
        }
Beispiel #10
0
 public virtual void CheckPatternWithoutTransparentFormResource()
 {
     using (MemoryStream bos = new MemoryStream()) {
         using (PdfWriter writer = new PdfWriter(bos)) {
             using (PdfDocument document = new PdfDocument(writer)) {
                 PdfFormXObject    formXObject    = new PdfFormXObject(new Rectangle(0f, 0f));
                 PdfPattern.Tiling tillingPattern = new PdfPattern.Tiling(0f, 0f);
                 tillingPattern.GetResources().AddForm(formXObject);
                 PdfPage      pageToCheck   = document.AddNewPage();
                 PdfResources pageResources = pageToCheck.GetResources();
                 pageResources.AddPattern(new PdfPattern.Shading(new PdfDictionary()));
                 pageResources.AddPattern(tillingPattern);
                 EnsureTransparencyObjectsNotEmpty();
                 // no assertions as we want to check that no exceptions would be thrown
                 pdfA2Checker.CheckSinglePage(pageToCheck);
             }
         }
     }
 }
        public virtual void PatternColorUncoloredLineGrayTest()
        {
            String    name   = "patternColorUncoloredLineGrayTest.pdf";
            PdfWriter writer = new PdfWriter(destinationFolder + name);

            writer.SetCompressionLevel(CompressionConstants.NO_COMPRESSION);
            PdfDocument document = new PdfDocument(writer);
            PdfPage     page     = document.AddNewPage();
            PdfCanvas   canvas   = new PdfCanvas(page);

            PdfPattern.Tiling line = new PdfPattern.Tiling(5, 10, false);
            new PdfPatternCanvas(line, document).SetLineWidth(1).MoveTo(3, -1).LineTo(3, 11).Stroke().Release();
            canvas.SetFillColor(new PatternColor(line, new DeviceGray()));
            canvas.Rectangle(30, 300, 400, 400).Fill();
            canvas.Release();
            document.Close();
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(destinationFolder + name, sourceFolder +
                                                                             "cmp_" + name, destinationFolder));
        }
            public override void Draw(DrawContext drawContext)
            {
                PdfPattern.Tiling imgPattern = new PdfPattern.Tiling(img.GetWidth(), img.GetHeight(),
                                                                     img.GetWidth(), img.GetHeight());

                PdfPatternCanvas patternCanvas = new PdfPatternCanvas(imgPattern, drawContext.GetDocument());

                patternCanvas.AddImageAt(img, 0, 0, false);

                PdfCanvas canvas = drawContext.GetCanvas();

                canvas.SaveState();

                ColorRectangle(canvas, new PatternColor(imgPattern), GetOccupiedAreaBBox().GetX(),
                               GetOccupiedAreaBBox().GetY(), GetOccupiedAreaBBox().GetWidth(), GetOccupiedAreaBBox().GetHeight());

                canvas.SetFillColor(new PatternColor(imgPattern));
                canvas.Stroke();

                canvas.RestoreState();
            }
Beispiel #13
0
        public virtual void MakePatternColorTest()
        {
            PdfWriter writer = new PdfWriter(DESTINATION_FOLDER + "makePatternColorTest.pdf");

            writer.SetCompressionLevel(CompressionConstants.NO_COMPRESSION);
            PdfDocument document = new PdfDocument(writer);
            PdfPage     page     = document.AddNewPage();
            PdfCanvas   canvas   = new PdfCanvas(page);

            PdfSpecialCs.Pattern pattern = new PdfSpecialCs.UncoloredTilingPattern(new PdfDeviceCs.Rgb());
            Color greenPattern           = Color.MakeColor(pattern, new float[] { 0, 1, 0 });

            PdfPattern.Tiling circle = new PdfPattern.Tiling(10, 10, 12, 12, false);
            new PdfPatternCanvas(circle, document).Circle(5f, 5f, 5f).Fill().Release();
            canvas.SetColor(greenPattern.GetColorSpace(), greenPattern.GetColorValue(), circle, true).Rectangle(50, 600
                                                                                                                , 50, 50).Fill();
            canvas.Release();
            document.Close();
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(DESTINATION_FOLDER + "makePatternColorTest.pdf"
                                                                             , SOURCE_FOLDER + "cmp_makePatternColorTest.pdf", DESTINATION_FOLDER, "diff_"));
        }
Beispiel #14
0
 public virtual void CheckPatternWithTransparentFormResource()
 {
     using (MemoryStream bos = new MemoryStream()) {
         using (PdfWriter writer = new PdfWriter(bos)) {
             using (PdfDocument document = new PdfDocument(writer)) {
                 PdfFormXObject formXObject = new PdfFormXObject(new Rectangle(0f, 0f));
                 formXObject.SetGroup(new PdfTransparencyGroup());
                 PdfPattern.Tiling tillingPattern = new PdfPattern.Tiling(0f, 0f);
                 tillingPattern.GetResources().AddForm(formXObject);
                 PdfPage      pageToCheck   = document.AddNewPage();
                 PdfResources pageResources = pageToCheck.GetResources();
                 pageResources.AddPattern(new PdfPattern.Shading(new PdfDictionary()));
                 pageResources.AddPattern(tillingPattern);
                 NUnit.Framework.Assert.That(() => {
                     pdfA2Checker.CheckSinglePage(pageToCheck);
                 }
                                             , NUnit.Framework.Throws.InstanceOf <PdfAConformanceException>().With.Message.EqualTo(PdfAConformanceException.THE_DOCUMENT_DOES_NOT_CONTAIN_A_PDFA_OUTPUTINTENT_BUT_PAGE_CONTAINS_TRANSPARENCY_AND_DOES_NOT_CONTAIN_BLENDING_COLOR_SPACE))
                 ;
             }
         }
     }
 }
        public virtual void PatternColorUncoloredCircleRgbTest()
        {
            String    name   = "patternColorUncoloredCircleRgbTest.pdf";
            PdfWriter writer = new PdfWriter(destinationFolder + name);

            writer.SetCompressionLevel(CompressionConstants.NO_COMPRESSION);
            PdfDocument document = new PdfDocument(writer);
            PdfPage     page     = document.AddNewPage();
            PdfCanvas   canvas   = new PdfCanvas(page);

            PdfPattern.Tiling circle = new PdfPattern.Tiling(15, 15, 10, 20, false);
            new PdfPatternCanvas(circle, document).Circle(7.5f, 7.5f, 2.5f).Fill().Release();
            PdfSpecialCs.UncoloredTilingPattern uncoloredRgbCs = new PdfSpecialCs.UncoloredTilingPattern(new PdfDeviceCs.Rgb
                                                                                                             ());
            float[] green = new float[] { 0, 1, 0 };
            canvas.SetFillColor(new PatternColor(circle, uncoloredRgbCs, green));
            canvas.Rectangle(30, 300, 400, 400).Fill();
            canvas.Release();
            document.Close();
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(destinationFolder + name, sourceFolder +
                                                                             "cmp_" + name, destinationFolder));
        }
Beispiel #16
0
 public PatternColor(PdfPattern.Tiling uncoloredPattern, PdfColorSpace underlyingCS, float[] colorValue)
     : this(uncoloredPattern, new PdfSpecialCs.UncoloredTilingPattern(EnsureNotPatternCs(underlyingCS)), colorValue
            )
 {
 }
Beispiel #17
0
 public PatternColor(PdfPattern.Tiling uncoloredPattern, Color color)
     : this(uncoloredPattern, color.GetColorSpace(), color.GetColorValue())
 {
 }
 /// <summary>Creates PdfPatternCanvas for a document from a provided Tiling pattern</summary>
 /// <param name="pattern">The Tiling pattern must be colored</param>
 /// <param name="document">The document that the resulting content stream will be written to</param>
 public PdfPatternCanvas(PdfPattern.Tiling pattern, PdfDocument document)
     : base((PdfStream)pattern.GetPdfObject(), pattern.GetResources(), document)
 {
     this.tilingPattern = pattern;
 }
 /// <summary>Creates PdfPatternCanvas from content stream of page, form XObject, pattern etc.</summary>
 /// <param name="contentStream">The content stream</param>
 /// <param name="resources">The resources, a specialized dictionary that can be used by PDF instructions in the content stream
 ///     </param>
 /// <param name="document">The document that the resulting content stream will be written to</param>
 public PdfPatternCanvas(PdfStream contentStream, PdfResources resources, PdfDocument document)
     : base(contentStream, resources, document)
 {
     this.tilingPattern = new PdfPattern.Tiling(contentStream);
 }