将SVG添加到PDF时例外

发布于 2025-01-18 18:35:39 字数 2808 浏览 1 评论 0原文

我正在尝试将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

飘过的浮云 2025-01-25 18:35:39

看起来Itext可能有一个错误。 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" />

具有很小的幅度,以科学符号表示:-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,

<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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文