Exception when adding SVG to PDF

Viewed 193

I'm trying to insert a SVG into a PDF document, but I keep getting this exception:

Nullable object must have a value.

The stack trace shows:

   at System.ThrowHelper.ThrowInvalidOperationException_InvalidOperation_NoValue()
   at iText.Svg.Renderers.Path.Impl.AbstractPathShape.CreatePoint(String coordX, String coordY)
   at iText.Svg.Renderers.Impl.PathSvgNodeRenderer.AddMoveToShapes(IPathShape pathShape, String[] pathProperties)
   at iText.Svg.Renderers.Impl.PathSvgNodeRenderer.ProcessPathOperator(String[] pathProperties, IPathShape previousShape)
   at iText.Svg.Renderers.Impl.PathSvgNodeRenderer.GetShapes()
   at iText.Svg.Renderers.Impl.PathSvgNodeRenderer.DoDraw(SvgDrawContext context)
   at iText.Svg.Renderers.Impl.AbstractSvgNodeRenderer.Draw(SvgDrawContext context)
   at iText.Svg.Renderers.Impl.GroupSvgNodeRenderer.DoDraw(SvgDrawContext context)
   at iText.Svg.Renderers.Impl.ClipPathSvgNodeRenderer.DoDraw(SvgDrawContext context)
   at iText.Svg.Renderers.Impl.AbstractSvgNodeRenderer.Draw(SvgDrawContext context)
   at iText.Svg.Renderers.Impl.AbstractSvgNodeRenderer.DrawInClipPath(SvgDrawContext context)
   at iText.Svg.Renderers.Impl.AbstractSvgNodeRenderer.Draw(SvgDrawContext context)
   at iText.Svg.Renderers.Impl.AbstractBranchSvgNodeRenderer.DoDraw(SvgDrawContext context)
   at iText.Svg.Renderers.Impl.SvgTagSvgNodeRenderer.DoDraw(SvgDrawContext context)
   at iText.Svg.Renderers.Impl.AbstractSvgNodeRenderer.Draw(SvgDrawContext context)
   at iText.Svg.Renderers.Impl.PdfRootSvgNodeRenderer.Draw(SvgDrawContext context)
   at iText.Svg.Converter.SvgConverter.ConvertToXObject(ISvgNodeRenderer topSvgRenderer, PdfDocument document, SvgDrawContext context)
   at iText.Svg.Converter.SvgConverter.ConvertToXObject(ISvgProcessorResult processorResult, PdfDocument document, ISvgConverterProperties props)
   at iText.Svg.Converter.SvgConverter.ConvertToXObject(Stream stream, PdfDocument document, ISvgConverterProperties props)
   at iText.Svg.Converter.SvgConverter.ConvertToXObject(Stream stream, PdfDocument document)
   at iText.Svg.Converter.SvgConverter.ConvertToImage(Stream stream, PdfDocument document)
   at HPT.Files.Infrastructure.Pdf.PdfProcessingService.GeneratePdfFile(List`1 customPlanStreams, PdfContent pdfContent, ICompressor compressor) in C:\--omited--\PdfProcessingService.cs:line 438

Here is the content of my SVG:

https://drive.google.com/file/d/1xT941Ps_ulIkv4VUQ1v8iDlE2r6VOUT0/view?usp=sharing

Here is how I'm trying to generate the picture:

var img = SvgConverter.ConvertToImage(planStream, pdf);

the weird thing is, with some SVGs it works, but the majority of them are throwing that exception.

-what should I try? -what am I doing wrong?

I also tried parsing the SVG to an XObject but I get the same exception. If I take the contents of the SVG it displays correctly in a browser.

1 Answers

Looks like there might be a bug with iText. The very last path in the SVG,

<path d="M-4.5474735E-13 527.02695L698.51578 527.02695L698.51578 1.364242E-12L-4.5474735E-13 1.364242E-12L-4.5474735E-13 527.02695" stroke="#FF0000" stroke-width="0.068529764" stroke-linecap="round" fill="none" />

has points with a very small magnitude, expressed in scientific notation: -4.5474735E-13.

Stepping through the debugger, this is being parsed as x: -4.5474735E, y:-13 bad parse

As for why it works on some, I'm guessing they just don't have that super-small value anywhere.

Related