Class SvgConverter

java.lang.Object
com.itextpdf.svg.converter.SvgConverter

public final class SvgConverter extends Object
This is the main container class for static methods that do high-level conversion operations from input to PDF, either by drawing on a canvas or by returning an XObject, which can then be used by the calling class for further processing and drawing operations.
  • Method Details

    • drawOnDocument

      public static void drawOnDocument (String content, PdfDocument document, int pageNo)
      Draws a String containing valid SVG to a document, on a given page number at the origin of the page.
      Parameters:
      content - the String value containing valid SVG content
      document - the PdfDocument instance to draw on
      pageNo - the page to draw on
    • drawOnDocument

      public static void drawOnDocument (String content, PdfDocument document, int pageNo, float x, float y)
      Draws a String containing valid SVG to a document, on a given page number on the provided x and y coordinate.
      Parameters:
      content - the String value containing valid SVG content
      document - the PdfDocument instance to draw on
      pageNo - the page to draw on
      x - x-coordinate of the location to draw at
      y - y-coordinate of the location to draw at
    • drawOnDocument

      public static void drawOnDocument (String content, PdfDocument document, int pageNo, ISvgConverterProperties props)
      Draws a String containing valid SVG to a document, on a given page number on the provided x and y coordinate.
      Parameters:
      content - the String value containing valid SVG content
      document - the PdfDocument instance to draw on
      pageNo - the page to draw on
      props - a container for extra properties that customize the behavior
    • drawOnDocument

      public static void drawOnDocument (String content, PdfDocument document, int pageNo, float x, float y, ISvgConverterProperties props)
      Draws a String containing valid SVG to a document, on a given page number on the provided x and y coordinate.
      Parameters:
      content - the String value containing valid SVG content
      document - the PdfDocument instance to draw on
      pageNo - the page to draw on
      x - x-coordinate of the location to draw at
      y - y-coordinate of the location to draw at
      props - a container for extra properties that customize the behavior
    • drawOnDocument

      public static void drawOnDocument (InputStream stream, PdfDocument document, int pageNo) throws IOException
      Draws a Stream containing valid SVG to a document, on a given page number ate the origni of the page.
      Parameters:
      stream - the Stream containing valid SVG content
      document - the PdfDocument instance to draw on
      pageNo - the page to draw on
      Throws:
      IOException - when the Stream cannot be read correctly
    • drawOnDocument

      public static void drawOnDocument (InputStream stream, PdfDocument document, int pageNo, float x, float y) throws IOException
      Draws a Stream containing valid SVG to a document, on a given page number on the provided x and y coordinate.
      Parameters:
      stream - the Stream containing valid SVG content
      document - the PdfDocument instance to draw on
      pageNo - the page to draw on
      x - x-coordinate of the location to draw at
      y - y-coordinate of the location to draw at
      Throws:
      IOException - when the Stream cannot be read correctly
    • drawOnDocument

      public static void drawOnDocument (InputStream stream, PdfDocument document, int pageNo, ISvgConverterProperties props) throws IOException
      Draws a Stream containing valid SVG to a document, on a given page number on the provided x and y coordinate.
      Parameters:
      stream - the Stream containing valid SVG content
      document - the PdfDocument instance to draw on
      pageNo - the page to draw on
      props - a container for extra properties that customize the behavior
      Throws:
      IOException - when the Stream cannot be read correctly
    • drawOnDocument

      public static void drawOnDocument (InputStream stream, PdfDocument document, int pageNo, float x, float y, ISvgConverterProperties props) throws IOException
      Draws a Stream containing valid SVG to a document, on a given page number on the provided x and y coordinate.
      Parameters:
      stream - the Stream containing valid SVG content
      document - the PdfDocument instance to draw on
      pageNo - the page to draw on
      x - x-coordinate of the location to draw at
      y - y-coordinate of the location to draw at
      props - a container for extra properties that customize the behavior
      Throws:
      IOException - when the Stream cannot be read correctly
    • drawOnPage

      public static void drawOnPage (String content, PdfPage page)
      Draws a String containing valid SVG to a given page at the origin of the page.
      Parameters:
      content - the String value containing valid SVG content
      page - the PdfPage instance to draw on
    • drawOnPage

      public static void drawOnPage (String content, PdfPage page, float x, float y)
      Draws a String containing valid SVG to a given page on the provided x and y coordinate.
      Parameters:
      content - the String value containing valid SVG content
      page - the PdfPage instance to draw on
      x - x-coordinate of the location to draw at
      y - y-coordinate of the location to draw at
    • drawOnPage

      public static void drawOnPage (String content, PdfPage page, ISvgConverterProperties props)
      Draws a String containing valid SVG to a given page on the provided x and y coordinate.
      Parameters:
      content - the String value containing valid SVG content
      page - the PdfPage instance to draw on
      props - a container for extra properties that customize the behavior
    • drawOnPage

      public static void drawOnPage (String content, PdfPage page, float x, float y, ISvgConverterProperties props)
      Draws a String containing valid SVG to a given page on the provided x and y coordinate.
      Parameters:
      content - the String value containing valid SVG content
      page - the PdfPage instance to draw on
      x - x-coordinate of the location to draw at
      y - y-coordinate of the location to draw at
      props - a container for extra properties that customize the behavior
    • drawOnPage

      public static void drawOnPage (InputStream stream, PdfPage page) throws IOException
      Draws a Stream containing valid SVG to a given page at coordinate 0,0.
      Parameters:
      stream - the Stream object containing valid SVG content
      page - the PdfPage instance to draw on
      Throws:
      IOException - when the Stream cannot be read correctly
    • drawOnPage

      public static void drawOnPage (InputStream stream, PdfPage page, float x, float y) throws IOException
      Draws a Stream containing valid SVG to a given page, at a given location.
      Parameters:
      stream - the Stream object containing valid SVG content
      page - the PdfPage instance to draw on
      x - x-coordinate of the location to draw at
      y - y-coordinate of the location to draw at
      Throws:
      IOException - when the Stream cannot be read correctly
    • drawOnPage

      public static void drawOnPage (InputStream stream, PdfPage page, ISvgConverterProperties props) throws IOException
      Draws a Stream containing valid SVG to a given page at a given location.
      Parameters:
      stream - the Stream object containing valid SVG content
      page - the PdfPage instance to draw on
      props - a container for extra properties that customize the behavior
      Throws:
      IOException - when the Stream cannot be read correctly
    • drawOnPage

      public static void drawOnPage (InputStream stream, PdfPage page, float x, float y, ISvgConverterProperties props) throws IOException
      Draws a Stream containing valid SVG to a given page at a given location.
      Parameters:
      stream - the Stream object containing valid SVG content
      page - the PdfPage instance to draw on
      x - x-coordinate of the location to draw at
      y - y-coordinate of the location to draw at
      props - a container for extra properties that customize the behavior
      Throws:
      IOException - when the Stream cannot be read correctly
    • drawOnCanvas

      public static void drawOnCanvas (String content, PdfCanvas canvas)
      Draws a String containing valid SVG to a pre-made canvas object.
      Parameters:
      content - the String value containing valid SVG content
      canvas - the PdfCanvas instance to draw on
    • drawOnCanvas

      public static void drawOnCanvas (String content, PdfCanvas canvas, float x, float y)
      Draws a String containing valid SVG to a pre-made canvas object.
      Parameters:
      content - the String value containing valid SVG content
      canvas - the PdfCanvas instance to draw on
      x - x-coordinate of the location to draw at
      y - y-coordinate of the location to draw at
    • drawOnCanvas

      public static void drawOnCanvas (String content, PdfCanvas canvas, ISvgConverterProperties props)
      Draws a String containing valid SVG to a pre-made canvas object.
      Parameters:
      content - the String value containing valid SVG content
      canvas - the PdfCanvas instance to draw on
      props - a container for extra properties that customize the behavior
    • drawOnCanvas

      public static void drawOnCanvas (String content, PdfCanvas canvas, float x, float y, ISvgConverterProperties props)
      draws a String containing valid SVG to a pre-made canvas object, at a specified location.
      Parameters:
      content - the String value containing valid SVG content
      canvas - the PdfCanvas instance to draw on
      x - x-coordinate of the location to draw at
      y - y-coordinate of the location to draw at
      props - a container for extra properties that customize the behavior
    • drawOnCanvas

      public static void drawOnCanvas (InputStream stream, PdfCanvas canvas) throws IOException
      Draws a Stream containing valid SVG to a pre-made canvas object.
      Parameters:
      stream - the Stream object containing valid SVG content
      canvas - the PdfCanvas instance to draw on
      Throws:
      IOException - when the Stream cannot be read correctly
    • drawOnCanvas

      public static void drawOnCanvas (InputStream stream, PdfCanvas canvas, float x, float y) throws IOException
      Draws a Stream containing valid SVG to a pre-made canvas object, to a specified location.
      Parameters:
      stream - the Stream object containing valid SVG content
      canvas - the PdfCanvas instance to draw on
      x - x-coordinate of the location to draw at
      y - y-coordinate of the location to draw at
      Throws:
      IOException - when the Stream cannot be read correctly
    • drawOnCanvas

      public static void drawOnCanvas (InputStream stream, PdfCanvas canvas, ISvgConverterProperties props) throws IOException
      Draws a Stream containing valid SVG to a pre-made canvas object.
      Parameters:
      stream - the Stream object containing valid SVG content
      canvas - the PdfCanvas instance to draw on
      props - a container for extra properties that customize the behavior
      Throws:
      IOException - when the Stream cannot be read correctly
    • drawOnCanvas

      public static void drawOnCanvas (InputStream stream, PdfCanvas canvas, float x, float y, ISvgConverterProperties props) throws IOException
      Draws a String containing valid SVG to a pre-made canvas object, at a specified position on the canvas.
      Parameters:
      stream - the Stream object containing valid SVG content
      canvas - the PdfCanvas instance to draw on
      x - x-coordinate of the location to draw at
      y - y-coordinate of the location to draw at
      props - a container for extra properties that customize the behavior
      Throws:
      IOException - when the Stream cannot be read correctly
    • createPdf

      public static void createPdf (File svgFile, File pdfFile) throws IOException
      Converts SVG stored in a File to a PDF File.
      Parameters:
      svgFile - the File containing the source SVG
      pdfFile - the File containing the resulting PDF
      Throws:
      IOException - Signals that an I/O exception has occurred.
    • createPdf

      public static void createPdf (File svgFile, File pdfFile, ISvgConverterProperties props) throws IOException
      Converts SVG stored in a File to a PDF File, using specific ISvgConverterProperties.
      Parameters:
      svgFile - the File containing the source SVG
      pdfFile - the File containing the resulting PDF
      props - a ISvgConverterProperties an instance for extra properties to customize the behavior
      Throws:
      IOException - Signals that an I/O exception has occurred.
    • createPdf

      public static void createPdf (File svgFile, File pdfFile, WriterProperties writerProps) throws IOException
      Converts SVG stored in a File to a PDF File, using WriterProperties
      Parameters:
      svgFile - the File containing the source SVG
      pdfFile - the File containing the resulting PDF
      writerProps - theWriterProperties for the pdf document
      Throws:
      IOException - Signals that an I/O exception has occurred.
    • createPdf

      public static void createPdf (File svgFile, File pdfFile, ISvgConverterProperties props, WriterProperties writerProps) throws IOException
      Converts SVG stored in a File to a PDF File, using specific ISvgConverterProperties and WriterProperties.
      Parameters:
      svgFile - the File containing the source SVG
      pdfFile - the File containing the resulting PDF
      props - a ISvgConverterProperties an instance for extra properties to customize the behavior
      writerProps - a WriterProperties for the pdf document
      Throws:
      IOException - Signals that an I/O exception has occurred.
    • createPdf

      public static void createPdf (InputStream svgStream, OutputStream pdfDest) throws IOException
      Create a single page pdf containing the SVG on its page using the default processing and drawing logic
      Parameters:
      svgStream - Stream containing the SVG
      pdfDest - PDF destination outputStream
      Throws:
      IOException - when the one of the streams cannot be read correctly
    • createPdf

      public static void createPdf (InputStream svgStream, OutputStream pdfDest, WriterProperties writerProps) throws IOException
      Create a single page pdf containing the SVG on its page using the default processing and drawing logic
      Parameters:
      svgStream - Stream containing the SVG
      pdfDest - PDF destination outputStream
      writerProps - writer properties for the pdf document
      Throws:
      IOException - when the one of the streams cannot be read correctly
    • createPdf

      public static void createPdf (InputStream svgStream, OutputStream pdfDest, ISvgConverterProperties props) throws IOException
      Create a single page pdf containing the SVG on its page using the default processing and drawing logic
      Parameters:
      svgStream - Stream containing the SVG
      pdfDest - PDF destination outputStream
      props - ISvgConverterProperties an instance for extra properties to customize the behavior
      Throws:
      IOException - when the one of the streams cannot be read correctly
    • createPdf

      public static void createPdf (InputStream svgStream, OutputStream pdfDest, ISvgConverterProperties props, WriterProperties writerProps) throws IOException
      Create a single page pdf containing the SVG on its page using the default processing and drawing logic
      Parameters:
      svgStream - Stream containing the SVG
      pdfDest - PDF destination outputStream
      props - ISvgConverterProperties an instance for extra properties to customize the behavior
      writerProps - WriterProperties for the pdf document
      Throws:
      IOException - when the one of the streams cannot be read correctly
    • convertToXObject

      public static PdfFormXObject convertToXObject (String content, PdfDocument document)
      Converts a String containing valid SVG content to an XObject that can then be used on the passed PdfDocument. This method does NOT manipulate the PdfDocument in any way.

      This method (or its overloads) is the best method to use if you want to reuse the same SVG image multiple times on the same PdfDocument.

      If you want to reuse this object on other PdfDocument instances, please either use any of the process(com.itextpdf.styledxmlparser.node.INode, com.itextpdf.svg.processors.ISvgConverterProperties) overloads in this same class and convert its result to an XObject with convertToXObject(ISvgNodeRenderer, PdfDocument) , or look into using PdfObject.copyTo(PdfDocument).

      Parameters:
      content - the String value containing valid SVG content
      document - the PdfDocument instance to draw on
      Returns:
      a XObject containing the PDF instructions corresponding to the passed SVG content
    • convertToXObject

      public static PdfFormXObject convertToXObject (String content, PdfDocument document, ISvgConverterProperties props)
      Converts a String containing valid SVG content to an XObject that can then be used on the passed PdfDocument. This method does NOT manipulate the PdfDocument in any way.

      This method (or its overloads) is the best method to use if you want to reuse the same SVG image multiple times on the same PdfDocument.

      If you want to reuse this object on other PdfDocument instances, please either use any of the process(com.itextpdf.styledxmlparser.node.INode, com.itextpdf.svg.processors.ISvgConverterProperties) overloads in this same class and convert its result to an XObject with convertToXObject(ISvgNodeRenderer, PdfDocument) , or look into using PdfObject.copyTo(PdfDocument).

      Parameters:
      content - the String value containing valid SVG content
      document - the PdfDocument instance to draw on
      props - ISvgConverterProperties an instance for extra properties to customize the behavior
      Returns:
      a XObject containing the PDF instructions corresponding to the passed SVG content
    • convertToXObject

      public static PdfFormXObject convertToXObject (InputStream stream, PdfDocument document, ISvgConverterProperties props) throws IOException
      Converts a String containing valid SVG content to an XObject that can then be used on the passed PdfDocument. This method does NOT manipulate the PdfDocument in any way.

      This method (or its overloads) is the best method to use if you want to reuse the same SVG image multiple times on the same PdfDocument.

      If you want to reuse this object on other PdfDocument instances, please either use any of the process(com.itextpdf.styledxmlparser.node.INode, com.itextpdf.svg.processors.ISvgConverterProperties) overloads in this same class and convert its result to an XObject with convertToXObject(ISvgNodeRenderer, PdfDocument) , or look into using PdfObject.copyTo(PdfDocument).

      Parameters:
      stream - the Stream containing valid SVG content
      document - the PdfDocument instance to draw on
      props - ISvgConverterProperties an instance for extra properties to customize the behavior
      Returns:
      a XObject containing the PDF instructions corresponding to the passed SVG content
      Throws:
      IOException - when the stream cannot be read correctly
    • convertToXObject

      public static PdfFormXObject convertToXObject (InputStream stream, PdfDocument document) throws IOException
      Converts a String containing valid SVG content to an XObject that can then be used on the passed PdfDocument. This method does NOT manipulate the PdfDocument in any way.

      This method (or its overloads) is the best method to use if you want to reuse the same SVG image multiple times on the same PdfDocument.

      If you want to reuse this object on other PdfDocument instances, please either use any of the process(com.itextpdf.styledxmlparser.node.INode, com.itextpdf.svg.processors.ISvgConverterProperties) overloads in this same class and convert its result to an XObject with convertToXObject(ISvgNodeRenderer, PdfDocument) , or look into using PdfObject.copyTo(PdfDocument).

      Parameters:
      stream - the Stream containing valid SVG content
      document - the PdfDocument instance to draw on
      Returns:
      a XObject containing the PDF instructions corresponding to the passed SVG content
      Throws:
      IOException - when the Stream cannot be read correctly
    • convertToImage

      public static Image convertToImage (InputStream stream, PdfDocument document) throws IOException
      Converts a String containing valid SVG content to an XObject that can then be used on the passed PdfDocument. This method does NOT manipulate the PdfDocument in any way.

      This method (or its overloads) is the best method to use if you want to reuse the same SVG image multiple times on the same PdfDocument.

      If you want to reuse this object on other PdfDocument instances, please either use any of the process(com.itextpdf.styledxmlparser.node.INode, com.itextpdf.svg.processors.ISvgConverterProperties) overloads in this same class and convert its result to an XObject with convertToXObject(ISvgNodeRenderer, PdfDocument) , or look into using PdfObject.copyTo(PdfDocument).

      Parameters:
      stream - the Stream containing valid SVG content
      document - the PdfDocument instance to draw on
      Returns:
      a Image containing the PDF instructions corresponding to the passed SVG content
      Throws:
      IOException - when the Stream cannot be read correctly
    • convertToImage

      public static Image convertToImage (InputStream stream, PdfDocument document, ISvgConverterProperties props) throws IOException
      Converts a String containing valid SVG content to an image that can then be used on the passed PdfDocument. This method does NOT manipulate the PdfDocument in any way.

      This method (or its overloads) is the best method to use if you want to reuse the same SVG image multiple times on the same PdfDocument.

      If you want to reuse this object on other PdfDocument instances, please either use any of the process(com.itextpdf.styledxmlparser.node.INode, com.itextpdf.svg.processors.ISvgConverterProperties) overloads in this same class and convert its result to an XObject with convertToXObject(ISvgNodeRenderer, PdfDocument) , or look into using PdfObject.copyTo(PdfDocument).

      Parameters:
      stream - the Stream containing valid SVG content
      document - the PdfDocument instance to draw on
      props - ISvgConverterProperties an instance for extra properties to customize the behavior
      Returns:
      a Image containing the PDF instructions corresponding to the passed SVG content
      Throws:
      IOException - when the Stream cannot be read correctly
    • convertToXObject

      public static PdfFormXObject convertToXObject (ISvgNodeRenderer topSvgRenderer, PdfDocument document)
      This method draws a NodeRenderer tree to a canvas that is tied to the passed document.

      This method (or its overloads) is the best method to use if you want to reuse the same SVG image multiple times on the same PdfDocument.

      If you want to reuse this object on other PdfDocument instances, please either use any of the process(com.itextpdf.styledxmlparser.node.INode, com.itextpdf.svg.processors.ISvgConverterProperties) overloads in this same class and convert its result to an XObject with this method, or look into using PdfObject.copyTo(PdfDocument).

      Parameters:
      topSvgRenderer - the ISvgNodeRenderer instance that contains the renderer tree
      document - the document that the returned XObject can be drawn on (on any given page coordinates)
      Returns:
      an XObjectcontaining the PDF instructions corresponding to the passed node renderer tree.
    • parseAndProcess

      public static ISvgProcessorResult parseAndProcess (InputStream svgStream)
      Parse and process an Inputstream containing an SVG, using the default Svg processor (DefaultSvgProcessor) The parsing of the stream is done using UTF-8 as the default charset. The properties used by the processor are the SvgConverterProperties
      Parameters:
      svgStream - Stream containing the SVG to parse and process
      Returns:
      ISvgProcessorResult containing the root renderer and metadata of the svg
    • parseAndProcess

      public static ISvgProcessorResult parseAndProcess (InputStream svgStream, ISvgConverterProperties props)
      Parse and process an Inputstream containing an SVG, using the default Svg processor (DefaultSvgProcessor)
      Parameters:
      svgStream - Stream containing the SVG to parse and process
      props - ISvgConverterProperties an instance for extra properties to customize the behavior
      Returns:
      ISvgProcessorResult containing the root renderer and metadata of the svg
    • process

      public static ISvgProcessorResult process (INode root, ISvgConverterProperties props)
      Use the default implementation of ISvgProcessor to convert an XML DOM tree to a node renderer tree. The passed properties can modify the default behaviour
      Parameters:
      root - the XML DOM tree
      props - ISvgConverterProperties an instance for extra properties to customize the behavior
      Returns:
      a node renderer tree corresponding to the passed XML DOM tree
    • parse

      public static INode parse (String content)
      Parse a String containing valid SVG into an XML DOM node, using the default JSoup XML parser.
      Parameters:
      content - the String value containing valid SVG content
      Returns:
      an XML DOM tree corresponding to the passed String input
    • parse

      public static INode parse (InputStream stream) throws IOException
      Parse a Stream containing valid SVG into an XML DOM node, using the default JSoup XML parser. This method will assume that the encoding of the Stream is UTF-8.
      Parameters:
      stream - the Stream containing valid SVG content
      Returns:
      an XML DOM tree corresponding to the passed String input
      Throws:
      IOException - when the Stream cannot be read correctly
    • parse

      public static INode parse (InputStream stream, ISvgConverterProperties props) throws IOException
      Parse a Stream containing valid SVG into an XML DOM node, using the default JSoup XML parser. This method will assume that the encoding of the Stream is UTF-8, unless specified otherwise by the method ISvgConverterProperties.getCharset() of the props parameter.
      Parameters:
      stream - the Stream containing valid SVG content
      props - ISvgConverterProperties an instance for extra properties to customize the behavior
      Returns:
      an XML DOM tree corresponding to the passed String input
      Throws:
      IOException - when the Stream cannot be read correctly
    • extractWidthAndHeight

      public static float[] extractWidthAndHeight (ISvgNodeRenderer topSvgRenderer)
      Extract width and height of the passed SVGNodeRenderer, defaulting to respective viewbox values if either one is not present or to browser default if viewbox is missing as well
      Parameters:
      topSvgRenderer - the ISvgNodeRenderer instance that contains the renderer tree
      Returns:
      float[2], width is in position 0, height in position 1