public virtual void PathNodeRendererMoveToTest1()
        {
            String      filename = "pathNodeRendererMoveToTest1.pdf";
            PdfDocument doc      = new PdfDocument(new PdfWriter(destinationFolder + filename));

            doc.AddNewPage();
            IDictionary <String, String> pathShapes = new Dictionary <String, String>();

            pathShapes.Put("d", "M 100 100 l300 100 L200 300 z");
            ISvgNodeRenderer pathRenderer = new PathSvgNodeRenderer();

            pathRenderer.SetAttributesAndStyles(pathShapes);
            SvgDrawContext context = new SvgDrawContext(null, null);
            PdfCanvas      cv      = new PdfCanvas(doc, 1);

            context.PushCanvas(cv);
            pathRenderer.Draw(context);
            doc.Close();
            String result = new CompareTool().CompareByContent(destinationFolder + filename, sourceFolder + "cmp_" + filename
                                                               , destinationFolder, "diff_");

            if (result != null && !result.Contains("No visual differences"))
            {
                NUnit.Framework.Assert.Fail(result);
            }
        }
Example #2
0
        /// <summary>
        /// Create an
        /// <see cref="iText.Kernel.Pdf.Xobject.PdfFormXObject"/>
        /// tied to the passed
        /// <c>PdfDocument</c>
        /// using the SVG processing result.
        /// </summary>
        /// <param name="result">Processing result containing the SVG information</param>
        /// <param name="pdfDocument">pdf that shall contain the image</param>
        /// <returns>PdfFormXObject instance</returns>
        public virtual PdfFormXObject CreateXObjectFromProcessingResult(ISvgProcessorResult result, PdfDocument pdfDocument
                                                                        )
        {
            ISvgNodeRenderer topSvgRenderer = result.GetRootRenderer();
            float            width;
            float            height;

            float[] wh = SvgConverter.ExtractWidthAndHeight(topSvgRenderer);
            width  = wh[0];
            height = wh[1];
            PdfFormXObject   pdfForm      = new PdfFormXObject(new Rectangle(0, 0, width, height));
            PdfCanvas        canvas       = new PdfCanvas(pdfForm, pdfDocument);
            ResourceResolver tempResolver = new ResourceResolver(null, resourceResolver.GetRetriever());
            // TODO DEVSIX-4107 pass the resourceResolver variable (not tempResolver variable) to the
            //  SvgDrawContext constructor so that the SVG inside the SVG is processed.
            SvgDrawContext context = new SvgDrawContext(tempResolver, result.GetFontProvider(), result.GetRootRenderer
                                                            ());

            context.AddNamedObjects(result.GetNamedObjects());
            context.PushCanvas(canvas);
            ISvgNodeRenderer root = new PdfRootSvgNodeRenderer(topSvgRenderer);

            root.Draw(context);
            return(pdfForm);
        }
Example #3
0
        public virtual void CalculateNestedViewportDifferentFromParentTest()
        {
            Rectangle      expected = new Rectangle(0, 0, 500, 500);
            SvgDrawContext context  = new SvgDrawContext(null, null);
            PdfDocument    document = new PdfDocument(new PdfWriter(new MemoryStream(), new WriterProperties().SetCompressionLevel
                                                                        (0)));

            document.AddNewPage();
            PdfFormXObject pdfForm = new PdfFormXObject(expected);
            PdfCanvas      canvas  = new PdfCanvas(pdfForm, document);

            context.PushCanvas(canvas);
            context.AddViewPort(expected);
            SvgTagSvgNodeRenderer        parent   = new SvgTagSvgNodeRenderer();
            SvgTagSvgNodeRenderer        renderer = new SvgTagSvgNodeRenderer();
            PdfRootSvgNodeRenderer       root     = new PdfRootSvgNodeRenderer(parent);
            IDictionary <String, String> styles   = new Dictionary <String, String>();

            styles.Put("width", "500");
            styles.Put("height", "500");
            renderer.SetAttributesAndStyles(styles);
            renderer.SetParent(parent);
            Rectangle actual = root.CalculateViewPort(context);

            NUnit.Framework.Assert.IsTrue(expected.EqualsWithEpsilon(actual));
        }
Example #4
0
        public virtual void LineRendererTest()
        {
            String      filename = "lineSvgRendererTest.pdf";
            PdfDocument doc      = new PdfDocument(new PdfWriter(destinationFolder + filename));

            doc.AddNewPage();
            IDictionary <String, String> lineProperties = new Dictionary <String, String>();

            lineProperties.Put("x1", "100");
            lineProperties.Put("y1", "800");
            lineProperties.Put("x2", "300");
            lineProperties.Put("y2", "800");
            lineProperties.Put("stroke", "green");
            lineProperties.Put("stroke-width", "25");
            LineSvgNodeRenderer root = new LineSvgNodeRenderer();

            root.SetAttributesAndStyles(lineProperties);
            SvgDrawContext context = new SvgDrawContext(null, null);
            PdfCanvas      cv      = new PdfCanvas(doc, 1);

            context.PushCanvas(cv);
            root.Draw(context);
            doc.Close();
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(destinationFolder + filename, sourceFolder
                                                                             + "cmp_" + filename, destinationFolder, "diff_"));
        }
Example #5
0
 /// <summary>
 /// Method that will set properties to be inherited by this branch renderer's
 /// children and will iterate over all children in order to draw them.
 /// </summary>
 /// <param name="context">
 /// the object that knows the place to draw this element and
 /// maintains its state
 /// </param>
 protected internal override void DoDraw(SvgDrawContext context)
 {
     if (GetChildren().Count > 0)
     {
         // if branch has no children, don't do anything
         PdfStream stream = new PdfStream();
         stream.Put(PdfName.Type, PdfName.XObject);
         stream.Put(PdfName.Subtype, PdfName.Form);
         PdfFormXObject xObject   = (PdfFormXObject)PdfXObject.MakeXObject(stream);
         PdfCanvas      newCanvas = new PdfCanvas(xObject, context.GetCurrentCanvas().GetDocument());
         ApplyViewBox(context);
         //Bounding box needs to be written after viewbox calculations to account for pdf syntax interaction
         stream.Put(PdfName.BBox, new PdfArray(context.GetCurrentViewPort()));
         context.PushCanvas(newCanvas);
         ApplyViewportClip(context);
         ApplyViewportTranslationCorrection(context);
         foreach (ISvgNodeRenderer child in GetChildren())
         {
             newCanvas.SaveState();
             child.Draw(context);
             newCanvas.RestoreState();
         }
         CleanUp(context);
         context.GetCurrentCanvas().AddXObject(xObject, 0, 0);
     }
 }
        public virtual void PolyPointCheckerTest()
        {
            PdfDocument doc = new PdfDocument(new PdfWriter(new MemoryStream()));

            doc.AddNewPage();
            ISvgNodeRenderer             root = new PolylineSvgNodeRenderer();
            IDictionary <String, String> polyLineAttributes = new Dictionary <String, String>();

            polyLineAttributes.Put(SvgConstants.Attributes.POINTS, "0,0 100,100 200,200 300,300");
            root.SetAttributesAndStyles(polyLineAttributes);
            SvgDrawContext context = new SvgDrawContext(null, null);
            PdfCanvas      cv      = new PdfCanvas(doc, 1);

            context.PushCanvas(cv);
            root.Draw(context);
            IList <Point> expectedPoints = new List <Point>();

            expectedPoints.Add(new Point(0, 0));
            expectedPoints.Add(new Point(75, 75));
            expectedPoints.Add(new Point(150, 150));
            expectedPoints.Add(new Point(225, 225));
            IList <Point> attributePoints = ((PolylineSvgNodeRenderer)root).GetPoints();

            NUnit.Framework.Assert.AreEqual(expectedPoints.Count, attributePoints.Count);
            for (int x = 0; x < attributePoints.Count; x++)
            {
                NUnit.Framework.Assert.AreEqual(expectedPoints[x], attributePoints[x]);
            }
        }
Example #7
0
        public virtual void ParseParametersAndCalculateCoordinatesWithBetterPrecisionEllipseTest()
        {
            String      filename = "parseParametersAndCalculateCoordinatesWithBetterPrecisionEllipseTest.pdf";
            PdfDocument doc      = new PdfDocument(new PdfWriter(destinationFolder + filename));

            doc.AddNewPage();
            EllipseSvgNodeRenderer ellipseRenderer = new EllipseSvgNodeRenderer();

            ellipseRenderer.SetAttribute(SvgConstants.Attributes.CX, "170.3");
            ellipseRenderer.SetAttribute(SvgConstants.Attributes.CY, "339.5");
            ellipseRenderer.SetAttribute(SvgConstants.Attributes.RX, "6");
            ellipseRenderer.SetAttribute(SvgConstants.Attributes.RY, "6");
            // Parse parameters with better precision (in double type) in the method CssUtils#parseAbsoluteLength
            ellipseRenderer.SetParameters();
            SvgDrawContext context = new SvgDrawContext(null, null);
            PdfCanvas      cv      = new PdfCanvas(doc, 1);

            context.PushCanvas(cv);
            // Calculate coordinates with better precision (in double type) in the method EllipseSvgNodeRenderer#doDraw
            ellipseRenderer.Draw(context);
            String pageContentBytes = iText.IO.Util.JavaUtil.GetStringForBytes(doc.GetPage(1).GetContentBytes(), System.Text.Encoding
                                                                               .UTF8);

            doc.Close();
            String expectedResult = "132.22 254.63 m\n" + "132.22 257.11 130.21 259.13 127.72 259.13 c\n" + "125.24 259.13 123.22 257.11 123.22 254.63 c\n"
                                    + "123.22 252.14 125.24 250.13 127.72 250.13 c\n" + "130.21 250.13 132.22 252.14 132.22 254.63 c";

            NUnit.Framework.Assert.IsTrue(pageContentBytes.Contains(expectedResult));
        }
        public virtual void ConnectPointsWithSameYCoordinateTest()
        {
            PdfDocument doc = new PdfDocument(new PdfWriter(new MemoryStream()));

            doc.AddNewPage();
            ISvgNodeRenderer             root = new PolygonSvgNodeRenderer();
            IDictionary <String, String> polyLineAttributes = new Dictionary <String, String>();

            polyLineAttributes.Put(SvgConstants.Attributes.POINTS, "100,100 100,200 150,200 150,100");
            polyLineAttributes.Put(SvgConstants.Attributes.FILL, "none");
            polyLineAttributes.Put(SvgConstants.Attributes.STROKE, "black");
            root.SetAttributesAndStyles(polyLineAttributes);
            SvgDrawContext context = new SvgDrawContext(null, null);
            PdfCanvas      cv      = new PdfCanvas(doc, 1);

            context.PushCanvas(cv);
            root.Draw(context);
            doc.Close();
            IList <Point> expectedPoints = new List <Point>();

            expectedPoints.Add(new Point(75, 75));
            expectedPoints.Add(new Point(75, 150));
            expectedPoints.Add(new Point(112.5, 150));
            expectedPoints.Add(new Point(112.5, 75));
            expectedPoints.Add(new Point(75, 75));
            IList <Point> attributePoints = ((PolygonSvgNodeRenderer)root).GetPoints();

            NUnit.Framework.Assert.AreEqual(expectedPoints.Count, attributePoints.Count);
            for (int x = 0; x < attributePoints.Count; x++)
            {
                NUnit.Framework.Assert.AreEqual(expectedPoints[x], attributePoints[x]);
            }
        }
        public virtual void SetupDrawContextAndCanvas()
        {
            sdc = new SvgDrawContext(new ResourceResolver(""), new FontProvider());
            // set compression to none, in case you want to write to disk and inspect the created document
            PdfWriter   writer = new PdfWriter(new MemoryStream(), new WriterProperties().SetCompressionLevel(0));
            PdfDocument doc    = new PdfDocument(writer);

            cv = new PdfCanvas(doc.AddNewPage());
            sdc.PushCanvas(cv);
        }
Example #10
0
        public virtual void ReferenceNotFoundTest()
        {
            DummySvgNodeRenderer renderer    = new DummySvgNodeRenderer();
            SvgDrawContext       context     = new SvgDrawContext(null, null);
            PdfDocument          pdfDocument = new PdfDocument(new PdfWriter(new ByteArrayOutputStream()));
            PdfPage page = pdfDocument.AddNewPage();

            context.PushCanvas(new PdfCanvas(page));
            ISvgNodeRenderer use = new UseSvgNodeRenderer();

            use.SetAttribute(SvgConstants.Attributes.HREF, "dummy");
            use.Draw(context);
            pdfDocument.Close();
            NUnit.Framework.Assert.IsFalse(renderer.IsDrawn());
        }
Example #11
0
 /// <summary>
 /// Method that will set properties to be inherited by this branch renderer's
 /// children and will iterate over all children in order to draw them.
 /// </summary>
 /// <param name="context">
 /// the object that knows the place to draw this element and
 /// maintains its state
 /// </param>
 protected internal override void DoDraw(SvgDrawContext context)
 {
     // if branch has no children, don't do anything
     if (GetChildren().Count > 0)
     {
         PdfStream stream = new PdfStream();
         stream.Put(PdfName.Type, PdfName.XObject);
         stream.Put(PdfName.Subtype, PdfName.Form);
         PdfFormXObject xObject   = (PdfFormXObject)PdfXObject.MakeXObject(stream);
         PdfCanvas      newCanvas = new PdfCanvas(xObject, context.GetCurrentCanvas().GetDocument());
         ApplyViewBox(context);
         bool overflowVisible = IsOverflowVisible(this);
         // TODO (DEVSIX-3482) Currently overflow logic works only for markers.  Update this code after the ticket will be finished.
         if (this is MarkerSvgNodeRenderer && overflowVisible)
         {
             WriteBBoxAccordingToVisibleOverflow(context, stream);
         }
         else
         {
             Rectangle bbBox = context.GetCurrentViewPort().Clone();
             stream.Put(PdfName.BBox, new PdfArray(bbBox));
         }
         if (this is MarkerSvgNodeRenderer)
         {
             ((MarkerSvgNodeRenderer)this).ApplyMarkerAttributes(context);
         }
         context.PushCanvas(newCanvas);
         // TODO (DEVSIX-3482) Currently overflow logic works only for markers. Update this code after the ticket will be finished.
         if (!(this is MarkerSvgNodeRenderer) || !overflowVisible)
         {
             ApplyViewportClip(context);
         }
         ApplyViewportTranslationCorrection(context);
         foreach (ISvgNodeRenderer child in GetChildren())
         {
             if (!(child is MarkerSvgNodeRenderer))
             {
                 newCanvas.SaveState();
                 child.Draw(context);
                 newCanvas.RestoreState();
             }
         }
         CleanUp(context);
         // transformation already happened in AbstractSvgNodeRenderer, so no need to do a transformation here
         context.GetCurrentCanvas().AddXObject(xObject, 0, 0);
     }
 }
 public virtual void PolyLineInvalidAttributeTest01()
 {
     NUnit.Framework.Assert.That(() => {
         PdfDocument doc = new PdfDocument(new PdfWriter(new MemoryStream()));
         doc.AddNewPage();
         ISvgNodeRenderer root = new PolylineSvgNodeRenderer();
         IDictionary <String, String> polyLineAttributes = new Dictionary <String, String>();
         polyLineAttributes.Put(SvgConstants.Attributes.POINTS, "0,0 notAnum,alsoNotANum");
         root.SetAttributesAndStyles(polyLineAttributes);
         SvgDrawContext context = new SvgDrawContext(null, null);
         PdfCanvas cv           = new PdfCanvas(doc, 1);
         context.PushCanvas(cv);
         root.Draw(context);
     }
                                 , NUnit.Framework.Throws.InstanceOf <StyledXMLParserException>())
     ;
 }
Example #13
0
 public virtual void NoBoundingBoxOnXObjectTest()
 {
     NUnit.Framework.Assert.That(() => {
         PdfDocument document = new PdfDocument(new PdfWriter(new MemoryStream(), new WriterProperties().SetCompressionLevel
                                                                  (0)));
         document.AddNewPage();
         ISvgNodeRenderer processed  = SvgConverter.Process(SvgConverter.Parse("<svg />")).GetRootRenderer();
         PdfRootSvgNodeRenderer root = new PdfRootSvgNodeRenderer(processed);
         PdfFormXObject pdfForm      = new PdfFormXObject(new PdfStream());
         PdfCanvas canvas            = new PdfCanvas(pdfForm, document);
         SvgDrawContext context      = new SvgDrawContext(null, null);
         context.PushCanvas(canvas);
         root.Draw(context);
     }
                                 , NUnit.Framework.Throws.InstanceOf <SvgProcessingException>().With.Message.EqualTo(SvgLogMessageConstant.ROOT_SVG_NO_BBOX))
     ;
 }
Example #14
0
        public virtual void CalculateOutermostViewportWithDifferentXYTest()
        {
            Rectangle      expected = new Rectangle(10, 20, 600, 600);
            SvgDrawContext context  = new SvgDrawContext(null, null);
            PdfDocument    document = new PdfDocument(new PdfWriter(new MemoryStream(), new WriterProperties().SetCompressionLevel
                                                                        (0)));

            document.AddNewPage();
            PdfFormXObject pdfForm = new PdfFormXObject(expected);
            PdfCanvas      canvas  = new PdfCanvas(pdfForm, document);

            context.PushCanvas(canvas);
            SvgTagSvgNodeRenderer  renderer = new SvgTagSvgNodeRenderer();
            PdfRootSvgNodeRenderer root     = new PdfRootSvgNodeRenderer(renderer);
            Rectangle actual = root.CalculateViewPort(context);

            NUnit.Framework.Assert.IsTrue(expected.EqualsWithEpsilon(actual));
        }
        public virtual void SmoothCurveTest3()
        {
            String      filename = "smoothCurveTest3.pdf";
            PdfDocument doc      = new PdfDocument(new PdfWriter(destinationFolder + filename));

            doc.AddNewPage();
            String                 svgFilename = "smoothCurveTest3.svg";
            Stream                 xmlStream   = new FileStream(sourceFolder + svgFilename, FileMode.Open, FileAccess.Read);
            IElementNode           rootTag     = new JsoupXmlParser().Parse(xmlStream, "ISO-8859-1");
            DefaultSvgProcessor    processor   = new DefaultSvgProcessor();
            IBranchSvgNodeRenderer root        = (IBranchSvgNodeRenderer)processor.Process(rootTag, null).GetRootRenderer();
            SvgDrawContext         context     = new SvgDrawContext(null, null);
            PdfCanvas              cv          = new PdfCanvas(doc, 1);

            context.PushCanvas(cv);
            NUnit.Framework.Assert.IsTrue(root.GetChildren()[0] is PathSvgNodeRenderer);
            root.GetChildren()[0].Draw(context);
            doc.Close();
        }
        public virtual void PolylineRendererTest()
        {
            String      filename = "polylineRendererTest.pdf";
            PdfDocument doc      = new PdfDocument(new PdfWriter(destinationFolder + filename));

            doc.AddNewPage();
            ISvgNodeRenderer             root = new PolylineSvgNodeRenderer();
            IDictionary <String, String> polyLineAttributes = new Dictionary <String, String>();

            polyLineAttributes.Put(SvgConstants.Attributes.POINTS, "0,40 40,40 40,80 80,80 80,120 120,120 120,160");
            root.SetAttributesAndStyles(polyLineAttributes);
            SvgDrawContext context = new SvgDrawContext(null, null);
            PdfCanvas      cv      = new PdfCanvas(doc, 1);

            context.PushCanvas(cv);
            root.Draw(context);
            doc.Close();
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareVisually(destinationFolder + filename, sourceFolder
                                                                            + "cmp_" + filename, destinationFolder, "diff_"));
        }
Example #17
0
        public virtual void CalculateOutermostTransformation()
        {
            AffineTransform expected = new AffineTransform(1d, 0d, 0d, -1d, 0d, 600d);
            SvgDrawContext  context  = new SvgDrawContext(null, null);
            PdfDocument     document = new PdfDocument(new PdfWriter(new MemoryStream(), new WriterProperties().SetCompressionLevel
                                                                         (0)));

            document.AddNewPage();
            PdfFormXObject pdfForm = new PdfFormXObject(new Rectangle(0, 0, 600, 600));
            PdfCanvas      canvas  = new PdfCanvas(pdfForm, document);

            context.PushCanvas(canvas);
            SvgTagSvgNodeRenderer  renderer = new SvgTagSvgNodeRenderer();
            PdfRootSvgNodeRenderer root     = new PdfRootSvgNodeRenderer(renderer);

            context.AddViewPort(root.CalculateViewPort(context));
            AffineTransform actual = root.CalculateTransformation(context);

            NUnit.Framework.Assert.AreEqual(expected, actual);
        }
Example #18
0
 public virtual void InvalidAttributeTest01()
 {
     NUnit.Framework.Assert.That(() => {
         PdfDocument doc = new PdfDocument(new PdfWriter(new MemoryStream()));
         doc.AddNewPage();
         ISvgNodeRenderer root = new LineSvgNodeRenderer();
         IDictionary <String, String> lineProperties = new Dictionary <String, String>();
         lineProperties.Put("x1", "1");
         lineProperties.Put("y1", "800");
         lineProperties.Put("x2", "notAnum");
         lineProperties.Put("y2", "alsoNotANum");
         root.SetAttributesAndStyles(lineProperties);
         SvgDrawContext context = new SvgDrawContext(null, null);
         PdfCanvas cv           = new PdfCanvas(doc, 1);
         context.PushCanvas(cv);
         root.Draw(context);
     }
                                 , NUnit.Framework.Throws.InstanceOf <StyledXMLParserException>().With.Message.EqualTo(MessageFormatUtil.Format(iText.StyledXmlParser.LogMessageConstant.NAN, "notAnum")))
     ;
 }
Example #19
0
        /// <summary>
        /// Create an
        /// <see cref="iText.Kernel.Pdf.Xobject.PdfFormXObject"/>
        /// tied to the passed
        /// <c>PdfDocument</c>
        /// using the SVG processing result.
        /// </summary>
        /// <param name="result">Processing result containing the SVG information</param>
        /// <param name="pdfDocument">pdf that shall contain the image</param>
        /// <returns>PdfFormXObject instance</returns>
        public virtual PdfFormXObject CreateXObjectFromProcessingResult(ISvgProcessorResult result, PdfDocument pdfDocument
                                                                        )
        {
            ISvgNodeRenderer topSvgRenderer = result.GetRootRenderer();
            float            width;
            float            height;

            float[] wh = SvgConverter.ExtractWidthAndHeight(topSvgRenderer);
            width  = wh[0];
            height = wh[1];
            PdfFormXObject pdfForm = new PdfFormXObject(new Rectangle(0, 0, width, height));
            PdfCanvas      canvas  = new PdfCanvas(pdfForm, pdfDocument);
            SvgDrawContext context = new SvgDrawContext(null, result.GetFontProvider());

            context.AddNamedObjects(result.GetNamedObjects());
            context.PushCanvas(canvas);
            ISvgNodeRenderer root = new PdfRootSvgNodeRenderer(topSvgRenderer);

            root.Draw(context);
            return(pdfForm);
        }
        public virtual void PolyLineEmptyPointsListTest()
        {
            String      filename = "polyLineEmptyPointsListTest.pdf";
            PdfDocument doc      = new PdfDocument(new PdfWriter(destinationFolder + filename));

            doc.AddNewPage();
            ISvgNodeRenderer             root = new PolylineSvgNodeRenderer();
            IDictionary <String, String> polyLineAttributes = new Dictionary <String, String>();

            root.SetAttributesAndStyles(polyLineAttributes);
            SvgDrawContext context = new SvgDrawContext(null, null);
            PdfCanvas      cv      = new PdfCanvas(doc, 1);

            context.PushCanvas(cv);
            root.Draw(context);
            doc.Close();
            int numPoints = ((PolylineSvgNodeRenderer)root).GetPoints().Count;

            NUnit.Framework.Assert.AreEqual(numPoints, 0);
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareVisually(destinationFolder + filename, sourceFolder
                                                                            + "cmp_" + filename, destinationFolder, "diff_"));
        }
Example #21
0
        public virtual void InvalidAttributeTest02()
        {
            IDictionary <String, String> lineProperties = new Dictionary <String, String>();

            lineProperties.Put("x1", "100");
            lineProperties.Put("y1", "800");
            lineProperties.Put("x2", "1 0");
            lineProperties.Put("y2", "0 2 0");
            lineProperties.Put("stroke", "orange");
            String      filename = "invalidAttributes02.pdf";
            PdfDocument doc      = new PdfDocument(new PdfWriter(destinationFolder + filename));

            doc.AddNewPage();
            LineSvgNodeRenderer root = new LineSvgNodeRenderer();

            root.SetAttributesAndStyles(lineProperties);
            SvgDrawContext context = new SvgDrawContext(null, null);
            PdfCanvas      cv      = new PdfCanvas(doc, 1);

            context.PushCanvas(cv);
            root.Draw(context);
            doc.Close();
        }
Example #22
0
        /// <summary>
        /// When in the svg element
        /// <c>overflow</c>
        /// is
        /// <c>visible</c>
        /// the corresponding formXObject
        /// should have a BBox (form XObject’s bounding box; see PDF 32000-1:2008 - 8.10.2 Form Dictionaries)
        /// that should cover the entire svg space (page in pdf) in order to be able to show parts of the element which are outside the current element viewPort.
        /// </summary>
        /// <remarks>
        /// When in the svg element
        /// <c>overflow</c>
        /// is
        /// <c>visible</c>
        /// the corresponding formXObject
        /// should have a BBox (form XObject’s bounding box; see PDF 32000-1:2008 - 8.10.2 Form Dictionaries)
        /// that should cover the entire svg space (page in pdf) in order to be able to show parts of the element which are outside the current element viewPort.
        /// To do this, we get the inverse matrix of all the current transformation matrix changes and apply it to the root viewPort.
        /// This allows you to get the root rectangle in the final coordinate system.
        /// </remarks>
        /// <param name="context">current context to get canvases and view ports</param>
        /// <param name="stream">stream to write a BBox</param>
        private static void WriteBBoxAccordingToVisibleOverflow(SvgDrawContext context, PdfStream stream)
        {
            IList <PdfCanvas> canvases = new List <PdfCanvas>();
            int canvasesSize           = context.Size();

            for (int i = 0; i < canvasesSize; i++)
            {
                canvases.Add(context.PopCanvas());
            }
            AffineTransform transform = new AffineTransform();

            for (int i = canvases.Count - 1; i >= 0; i--)
            {
                PdfCanvas canvas = canvases[i];
                Matrix    matrix = canvas.GetGraphicsState().GetCtm();
                transform.Concatenate(new AffineTransform(matrix.Get(0), matrix.Get(1), matrix.Get(3), matrix.Get(4), matrix
                                                          .Get(6), matrix.Get(7)));
                context.PushCanvas(canvas);
            }
            try {
                transform = transform.CreateInverse();
            }
            catch (NoninvertibleTransformException) {
                // Case with zero determiner (see PDF 32000-1:2008 - 8.3.4 Transformation Matrices - NOTE 3)
                // for example with a, b, c, d in cm equal to 0
                stream.Put(PdfName.BBox, new PdfArray(new Rectangle(0, 0, 0, 0)));
                ILog logger = LogManager.GetLogger(typeof(AbstractBranchSvgNodeRenderer));
                logger.Warn(SvgLogMessageConstant.UNABLE_TO_GET_INVERSE_MATRIX_DUE_TO_ZERO_DETERMINANT);
                return;
            }
            Point[] points = context.GetRootViewPort().ToPointsArray();
            transform.Transform(points, 0, points, 0, points.Length);
            Rectangle bbox = Rectangle.CalculateBBox(JavaUtil.ArraysAsList(points));

            stream.Put(PdfName.BBox, new PdfArray(bbox));
        }