ItextPDF 5: Добавить SVG, не растеризованный - PullRequest
0 голосов
/ 05 июня 2019

Я использую itextpdf-5.5.6 для рисования графики SVG в моих документах PDF.

Возникла проблема:

После вставки все строки моего SVG растеризуются.

Существует код Java:

    public void testAddSvg(String svgPath) throws IOException, com.itextpdf.text.DocumentException {
    URL resourceUrl = getClass().getClassLoader().getResource(svgPath);
    File newTempDir = FileUtils.createNewTempDir("SVG_test");
    File RESULT = new File(newTempDir, System.nanoTime() + "_result.pdf");
    Document document = new Document(PageSize.A4, 10, 10, 10, 10);
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT));
    Rectangle pageSize = document.getPageSize();

    document.open();
    document.add(new Paragraph("SVG Example"));
    float width = 148 * 3.77f;
    float height = 210 * 3.77f;

    PdfTemplate template = writer.getDirectContent().createTemplate(width, height);
    ImgTemplate img = new ImgTemplate(template);
    PdfPrinterGraphics2D g2 = new PdfPrinterGraphics2D(template, pageSize.getWidth(), pageSize.getHeight(), new DefaultFontMapper(), PrinterJob.getPrinterJob());
    PrintTranscoder prm = new PrintTranscoder();
    File svgFile = new File(resourceUrl.getFile());
    TranscoderInput ti = new TranscoderInput(new FileInputStream(svgFile));
    prm.transcode(ti, null);
    PageFormat pg = new PageFormat();
    Paper pp = new Paper();
    pp.setSize(width, height);
    pp.setImageableArea(pageSize.getRight() - width, document.topMargin(), width, height);
    pg.setPaper(pp);
    prm.print(g2, pg, 0);
    g2.dispose();

    document.add(img);
    document.close();
}

Есть мой источник SVG:

<svg xmlns="http://www.w3.org/2000/svg" width="148" height="210">
<defs>
    <pattern id="pattern" patternUnits="userSpaceOnUse" width="1" height="1" patternTransform="rotate(135)">
        <line x1="0" x2="0" y2="4.5" stroke="#6B6B6B" stroke-width="0.5"/>
    </pattern>
</defs>
<rect x="137" width="11" height="210" fill="url(#pattern)" opacity="1"/>
<rect y="185" width="137" height="25" fill="url(#pattern)" opacity="1"/>
<rect y="134" width="137" height="10" fill="url(#pattern)" opacity="1"/>

Это результат:

Как я могу решить эту проблему?Я хочу нарисовать свой SVG, как в редакторе

...