有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java SVG文件无法转换为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>

共 (1) 个答案

  1. # 1 楼答案

    当前版本(7.1.10)中的iText 7如果在<use>元素(而不是从该<use>引用的<path>元素)中指定stroke-width,则会错误地处理stroke-width。作为一种解决方法,您可以直接在path元素上指定stroke-width,如下所示:

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