将SVG添加到PDF时例外
我正在尝试将 SVG 插入 PDF 文档,但我不断收到此异常:
Nullable object must have a value.
堆栈跟踪显示:
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
这是我的 SVG 的内容:
https://drive.google.com/file/d/1xT941Ps_ulIkv4VUQ1v8iDlE2r6VOUT0/view?usp=sharing
这是我尝试生成图片的方法:
var img = SvgConverter.ConvertToImage(planStream, pdf);
奇怪的是,对于某些 SVG,它可以工作,但大多数人都会抛出该异常。
-我应该尝试什么? -我做错了什么?
我还尝试将 SVG 解析为 XObject,但遇到了相同的异常。 如果我获取 SVG 的内容,它会在浏览器中正确显示。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来Itext可能有一个错误。 SVG中的最后一条路径
具有很小的幅度,以科学符号表示:
-4.5474735e-13
。踏上调试器,将其解析为x:-4.5474735 e ,y:-13
至于为什么它在某些方面工作,我猜他们只是不工作在任何地方都有那个超级小的值。
Looks like there might be a bug with iText. The very last path in the SVG,
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
As for why it works on some, I'm guessing they just don't have that super-small value anywhere.