Core SVG: Support <pattern> element
With iText 7.1.14 and pdfHTML 3.0.3, we are pleased to announce that we have added full support for SVG’s <pattern> element. This element is used to define a graphics object which can be "tiled" fixed intervals in x and y to cover an area.
With full support for the <pattern> element in iText 7 Core’s SVG module, it is now possible for iText 7 Core’s SVG module as well as the pdfHTML add-on to correctly render <pattern> elements in SVG files and HTML-embedded SVG respectively.
You can consult the w3 website link: https://www.w3.org/TR/SVG11/pservers.html for more information about the SVG <pattern> element.
Example input SVG:
<svg viewBox="0 0 300 300" xmlns="http://www.w3.org/2000/svg">
<defs>
<pattern id="star" x="2" y="2" width="14" height="14"
patternContentUnits = "userSpaceOnUse" patternUnits="userSpaceOnUse">
<rect fill="none" stroke-width="0.4" stroke="orange" x="0" y="0" width="14" height="14"/>
<rect fill="none" stroke-width="0.4" stroke="blue" x="0" y="0" width="10" height="10"/>
<polygon stroke="none" fill="red" points="0,0 2,5 0,10 5,8 10,10 8,5 10,0 5,2"/>
</pattern>
</defs>
<rect x="0" y="0" width="100" height="100" fill="url(#star)" stroke-width="0.8" stroke="black"/>
</svg>
The iText 7 Core code for generating the PDF from SVG remains the same:
JAVA
public void convert(String svg, String output, PageSize size) throws IOException {
try (PdfDocument doc = new PdfDocument(new PdfWriter(output, new WriterProperties().setCompressionLevel(0)))) {
doc.addNewPage(size);
ISvgConverterProperties properties = new SvgConverterProperties().setBaseUri(svg);
SvgConverter.drawOnDocument(new FileInputStream(svg), doc, 1, properties);
}
}
C#
public virtual void Convert(String svg, String output, PageSize size)
{
using (PdfDocument doc = new PdfDocument(new PdfWriter(output, new WriterProperties().SetCompressionLevel(0))))
{
doc.AddNewPage(size);
ISvgConverterProperties properties = new SvgConverterProperties().SetBaseUri(svg);
SvgConverter.DrawOnDocument(new FileStream(svg, FileMode.Open, FileAccess.Read), doc, 1, properties);
}
}
Example input HTML:
<!DOCTYPE html>
<html>
<body>
<svg viewBox="0 0 300 300" xmlns="http://www.w3.org/2000/svg">
<defs>
<pattern id="star" x="2" y="2" width="14" height="14"
patternContentUnits="userSpaceOnUse"
patternUnits="userSpaceOnUse">
<rect fill="none" stroke-width="0.4" stroke="orange" x="0" y="0" width="14" height="14"/>
<rect fill="none" stroke-width="0.4" stroke="blue" x="0" y="0" width="10" height="10"/>
<polygon stroke="none" fill="red" points="0,0 2,5 0,10 5,8 10,10 8,5 10,0 5,2"/>
</pattern>
</defs>
<rect x="0" y="0" width="100" height="100" fill="url(#star)" stroke-width="0.8" stroke="black"/>
</svg>
</body>
</html>
The pdfHTML code for generating the PDF from HTML remains the same:
JAVA
HtmlConverter.convertToPdf(new FIleInputStream(htmlSource), new FileOutputStream(pdfDest))
C#
using (FileStream htmlSource = File.Open(“input.html”, FileMode.Open))
using (FileStream pdfDest = File.Open(“output.html”, FileMode.OpenOrCreate))
{
HtmlConverter.ConvertToPdf(htmlSource, pdfDest);
}