java - SVG 文件无法转换为 Itext7 中的预期图像

标签 java itext7

我有一个 SVG 文件,当我使用 SvgConverter.convertToImage 将其转换为图像并添加到 PDF 文档中时,输出图像不符合预期。图像笔划大小变得比 SVG 文件大得多。我的 svg 插件版本是 7.1.9。如何解决此类问题? 预期图表是什么:expected Chart pdf中的实际输出是:actual chart

SvgConverterProperties props = new SvgConverterProperties();
FontProvider provider = new FontProvider();
provider.addFont(FontProgramFactory.createFont("fontname"));
props.setFontProvider(provider);
Image pdfImage = SvgConverter.convertToImage(new FileInputStream(imagePath), pdfDoc, props);

svg 内容:

<svg version="1.1" id="Layer_1" xmlns:x="&ns_extend;" xmlns:i="&ns_ai;" xmlns:graph="&ns_graphs;"
	 xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="17px" height="21px"
	 viewBox="0 0 17 21" enable-background="new 0 0 17 21" xml:space="preserve">
<g>
			<g>
				<defs>
					<path id="SVGID_3_" d="M12.435,6.162c0,0.496-0.371,0.903-0.867,0.946l0.036,0.396c0.699-0.065,1.225-0.64,1.225-1.343H12.435z
						"/>
				</defs>
				<use xlink:href="#SVGID_3_"  overflow="visible" fill="#2156A3"/>
				<clipPath id="SVGID_4_">
					<use xlink:href="#SVGID_3_"  overflow="visible"/>
				</clipPath>

					<rect x="9.591" y="4.183" clip-path="url(#SVGID_4_)" fill="#2156A3" stroke="#518428" stroke-width="0.25" stroke-miterlimit="10" width="5.218" height="5.297"/>
				<use xlink:href="#SVGID_3_"  overflow="visible" fill="none" stroke="#2156A3" stroke-width="0.25" stroke-miterlimit="10"/>
			</g>
			<g>
				<defs>
					<path id="SVGID_5_" d="M11.17,4.509c0-0.357,0.289-0.646,0.647-0.646c0.357,0,0.647,0.289,0.647,0.646
						c0,0.356-0.29,0.648-0.647,0.648C11.459,5.157,11.17,4.866,11.17,4.509 M10.774,4.509c0,0.575,0.466,1.043,1.043,1.043
						c0.576,0,1.043-0.468,1.043-1.043c0-0.577-0.467-1.043-1.043-1.043C11.24,3.467,10.774,3.933,10.774,4.509"/>
				</defs>
				<use xlink:href="#SVGID_5_"  overflow="visible" fill="#2156A3"/>
				<clipPath id="SVGID_6_">
					<use xlink:href="#SVGID_5_"  overflow="visible"/>
				</clipPath>
					<rect x="8.796" y="1.489" clip-path="url(#SVGID_6_)" fill="#2156A3" stroke="#2156A3" stroke-width="0.25" stroke-miterlimit="10" width="6.041" height="6.043"/>
				<use xlink:href="#SVGID_5_"  overflow="visible" fill="none" stroke="#2156A3" stroke-width="0.25" stroke-miterlimit="10"/>
			</g>
</g>
</svg>

最佳答案

当前版本 (7.1.10) 中的 iText 7 进程 stroke-width如果 <use> 中指定则不正确元素(而不是从 <path> 引用的 <use> 元素中)。作为解决方法,您可以指定 stroke-width直接上path像这样的元素:

<svg version="1.1" id="Layer_1" 
	 xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="17px" height="21px"
	 viewBox="0 0 17 21" enable-background="new 0 0 17 21" xml:space="preserve">
<g>
			<g>
				<defs>
					<path stroke-width="0.25" id="SVGID_3_" d="M12.435,6.162c0,0.496-0.371,0.903-0.867,0.946l0.036,0.396c0.699-0.065,1.225-0.64,1.225-1.343H12.435z
						"/>
				</defs>
				<use xlink:href="#SVGID_3_"  overflow="visible" fill="#2156A3"/>
				<clipPath id="SVGID_4_">
					<use xlink:href="#SVGID_3_"  overflow="visible"/>
				</clipPath>

					<rect x="9.591" y="4.183" clip-path="url(#SVGID_4_)" fill="#2156A3" stroke="#518428" stroke-width="0.25" stroke-miterlimit="10" width="5.218" height="5.297"/>
				<use xlink:href="#SVGID_3_"  overflow="visible" fill="none" stroke="#2156A3"  stroke-miterlimit="10"/>
			</g>
			<g>
				<defs>
					<path stroke-width="0.25" id="SVGID_5_" d="M11.17,4.509c0-0.357,0.289-0.646,0.647-0.646c0.357,0,0.647,0.289,0.647,0.646
						c0,0.356-0.29,0.648-0.647,0.648C11.459,5.157,11.17,4.866,11.17,4.509 M10.774,4.509c0,0.575,0.466,1.043,1.043,1.043
						c0.576,0,1.043-0.468,1.043-1.043c0-0.577-0.467-1.043-1.043-1.043C11.24,3.467,10.774,3.933,10.774,4.509"/>
				</defs>
				<use xlink:href="#SVGID_5_"  overflow="visible" fill="#2156A3"/>
				<clipPath id="SVGID_6_">
					<use xlink:href="#SVGID_5_"  overflow="visible"/>
				</clipPath>
					<rect x="8.796" y="1.489" clip-path="url(#SVGID_6_)" fill="#2156A3" stroke="#2156A3" stroke-width="0.25" stroke-miterlimit="10" width="6.041" height="6.043"/>
				<use xlink:href="#SVGID_5_"  overflow="visible" fill="none" stroke="#2156A3"  stroke-miterlimit="10"/>
			</g>
</g>
</svg>

关于java - SVG 文件无法转换为 Itext7 中的预期图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60260367/

相关文章:

java - 如何在第二个点java之后找到第一个字符

java - 设置 RGB 值以减少 PNG 图像中的调色板

java - 如何用Java编写IF-ELSE语句

java - 如何使用iText7 pdfHtml向每个页面添加透明水印文本

c# - 是否可以使用 iText 创建 Excel 文件

Itextsharp 7 - 缩放并居中的图像作为水印

java - Spring Controller中调用请求 Action 时写入日志

java - 如何使用 Stripes Framework 删除文件后缀/扩展名(.jsp 和 .action)?

java - 使用 Itext 7.1.7 时,文本区域中的连续文本会剪切溢出文本的 PDF