简体   繁体   English

如何在c#中使用iText 7将SVG图形添加​​到pdf文档?

[英]how to add SVG graphic to pdf document using iText 7 in c#?

I'm working on generator of stickers, using iText7 C#.我正在使用 iText7 C# 制作贴纸生成器。 The look of the final sticker is to look like this: https://drive.google.com/open?id=16q_sMP5H0eiVhq85DDRGgE-CDlX3fOB5最终贴纸的外观如下所示: https : //drive.google.com/open?id=16q_sMP5H0eiVhq85DDRGgE-CDlX3fOB5

I've problem with adding SVG graphic to pdf document.我在将 SVG 图形添加到 pdf 文档时遇到问题。 I have graphics in above link: https://drive.google.com/open?id=1bw2E5hVhKjjwYqn6aGbe_tNqPmYmXu4b https://drive.google.com/open?id=1lEqhrh2zAlOGlA1WMKfGtuhue6TBtcbc我在上面的链接中有图形: https : //drive.google.com/open ? id = 1bw2E5hVhKjjwYqn6aGbe_tNqPmYmXu4b https://drive.google.com/open?id=1lEqhrh2zAlOGlA1WMKfGtuhue6TBtcbc

I can not find any practical example on the Internet how to read an SVG file and add to a pdf document using iText7.我在互联网上找不到任何如何使用 iText7 读取 SVG 文件并添加到 pdf 文档的实际示例。

Can anyone help me with this topic?任何人都可以帮我解决这个话题吗?

Using the latest release 7.1.4, you would add an SVG to a document like this:使用最新版本 7.1.4,您可以将 SVG 添加到文档中,如下所示:

public static void Convert(Stream svg, Stream pdfOutputStream) {
        SvgConverter.CreatePdf(svg, pdfOutputStream);
}

There are many other possibilities in this class to convert to PDF, but this is the easiest method to use.此类中还有许多其他转换为 PDF 的可能性,但这是最容易使用的方法。

I use this code to add SVG graphic to pdf document:我使用此代码将 SVG 图形添加到 pdf 文档:

string enc_text = File.ReadAllText(SVG);                            

SvgConverter.DrawOnCanvas(enc_text, pdfCanvas); SvgConverter.DrawOnCanvas(enc_text, pdfCanvas);

but it's work only for simple SVG graphics like below https://www.w3schools.com/graphics/tryit.asp?filename=trysvg_ellipse3但它仅适用于像下面这样的简单 SVG 图形https://www.w3schools.com/graphics/tryit.asp?filename=trysvg_ellipse3

don't work for this SVG created and saved in CorelDraw: https://drive.google.com/file/d/1bw2E5hVhKjjwYqn6aGbe_tNqPmYmXu4b/view is that possible to draw this graphic on pdf using itext7 C#?不适用于在 CorelDraw 中创建和保存的此 SVG: https ://drive.google.com/file/d/1bw2E5hVhKjjwYqn6aGbe_tNqPmYmXu4b/view 是否可以使用 itext7 C# 在 pdf 上绘制此图形?

This is Code:这是代码:

public const String SVG = @"C:\Users\Desktop\logo.svg";

.... ....

string enc_text = File.ReadAllText(SVG);                            
SvgConverter.DrawOnCanvas(enc_text, pdfCanvas);

I tried three times and below are results:我试了三遍,结果如下:

Attempt 1 SVG https://drive.google.com/open?id=1ibg_KwvviRQ4b9suniZwBJRdBdgM02te PDF - the result is: OK: https://drive.google.com/open?id=1DGGLUowlEYpAydbWTTSJORVxP67LGrf5尝试 1 SVG https://drive.google.com/open?id=1ibg_KwvviRQ4b9suniZwBJRdBdgM02te PDF - 结果是:好的: https : //drive.google.com/open?id=1DGGLUowlEYpAydbWTTSJORVxP67LGrf5

Attempt 2 SVG: https://drive.google.com/open?id=1UHASgAxAaPONIo9fc6VZ9q4cjjK-uOzj PDF - the result is: half well https://drive.google.com/open?id=1yzLF-fQQcOQvEXVyDN-UuK0YVeHOn5B_尝试 2 SVG: https : //drive.google.com/open? id = 1UHASgAxAaPONIo9fc6VZ9q4cjjK- uOzj PDF - 结果是:一半好https://drive.google.com/open?id=1yzLF-fQQcOQvEXVyDN-UuK0YVeHon5B

Attempt 3 SVG https://drive.google.com/open?id=1ZNLDkc2x4WvHouKw-A4AgRrDuQ2pAoDE PDF - the result is: no logo https://drive.google.com/open?id=1nJVNT5oAMoI8HuUURZQv1yhu7IbFoI6H尝试 3 SVG https://drive.google.com/open?id=1ZNLDkc2x4WvHouKw-A4AgRrDuQ2pAoDE PDF - 结果是:没有标志https://drive.google.com/open?id=1nJVNT5oAMoI8HuUURZQv1yhu7IbFoI6H

SVG graphics are well displayed in browsers, but iText can not draw them properly, especially those complicated SVG 图形在浏览器中显示良好,但 iText 无法正确绘制,尤其是那些复杂的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM