itext 7 SVG 到 PDF 转换 - 添加字体

问题描述 投票:0回答:1

我正在使用发布在https://itextpdf.com/demos/convert-svg-to-pdf-free-online的SVG到PDF示例,但在一个简单的测试中,我发现SVG文档中的字体未嵌入。

在寻求帮助时,我发现 Itext 在 PDF 中嵌入字体 作为一个潜在的解决方案,但所讨论的解决方案适用于相当旧的 iText 版本。

如果我要向 PDF 添加文本段落,我可以在 How to use Fonts in iText PDFhttps://kb.itextpdf.com/home/it7kb/examples/itext-7-building-blocks- 找到示例Chapter-1-pdffont-examples,但我还没有成功地将它们适应 SVG-PDF 转换。

虽然 iText 提供的示例代码适用于 .NET,但我确实将其改编为 PowerShell:

        [PdfWriter]$pdfWriter = [PdfWriter]::new($Output, [WriterProperties]::new().SetCompressionLevel(0))
        [PdfDocument]$doc = [PdfDocument]::new($pdfWriter)

        $doc.AddNewPage([PageSize]::LETTER.Rotate())

        [SvgConverterProperties]$ConverterProps = [SvgConverterProperties]::new().SetBaseUri($InputFile)

        [System.IO.FileStream]$FileStream = [System.IO.FileStream]::new($InputFile, [System.IO.FileMode]::Open, [System.IO.FileAccess]::Read, [System.IO.FileShare]::Read)
        [SvgConverter]::DrawOnDocument($FileStream, $doc, 1, $ConverterProps)

        $doc.Close()

我期待类似的输出:

我当前的输出是:

pdf svg fonts itext
1个回答
0
投票

我不确定你用什么语言编写,但你可以参考我的C#代码。我认为它会起作用,因为我和你有同样的问题。这是我的代码

/* set font for using in SVG */
                FontProvider provider = new();
                FontProgram fontProgram;
                SvgConverterProperties props;
                /* createFont */
                fontProgram = FontProgramFactory.CreateFont(PATH + "lucon.ttf");
                provider.AddFont(fontProgram);
    
                /* set font for using in SVG */
                props = new SvgConverterProperties();
                props.SetFontProvider(provider);
    
                FileStream svgPath = File.Open("test.svg", FileMode.Open);
                FileStream pdfPath = File.Create("out.pdf");
                /* convert SVG to PDF and give the set properties*/
                SvgConverter.CreatePdf(svgPath, pdfPath, props);
© www.soinside.com 2019 - 2024. All rights reserved.