将svg图表转换为pdf并更改图表位置

时间:2017-05-18 14:48:48

标签: svg charts jfreechart pdfbox batik

我想创建一个Object Refinery JFreeChart的图表。之后,我想用Apache PDFBox创建一个pdf,在那里我使用JFreeChart中的图表。当我将图表保存为png或jpg时,图表的质量非常糟糕。所以我将图表保存为svg并将其与Apache Batik转换为pdf。 pdf的质量很好,但我无法改变PDF格式中图表的位置。

如何更改图表的位置? 有没有更好的解决方案来生成图表作为s​​vg并将其放在pdf中?

以下是我将svg转换为pdf的代码:

    public void convertSVGtoPDF() throws TranscoderException, IOException{
     //Step -1: We read the input SVG document into Transcoder Input
     String svg_URI_input = 
     Paths.get("output_pie_chart.svg").toUri().toURL().toString();
     System.out.println("Path="+svg_URI_input);
     TranscoderInput input_svg_image = new TranscoderInput(svg_URI_input); 

     //Step-2: Define OutputStream to PDF file and attach to TranscoderOutput
     OutputStream pdf_ostream = new FileOutputStream("FinalPDF.pdf");
     TranscoderOutput output_pdf_file = new TranscoderOutput(pdf_ostream);

     // Step-3: Create a PDF Transcoder and define hints
     Transcoder transcoder = new PDFTranscoder();
     transcoder.addTranscodingHint(SVGAbstractTranscoder.KEY_HEIGHT, new 
     Float(800)); transcoder.addTranscodingHint(SVGAbstractTranscoder.KEY_WIDTH, new Float(800));
    transcoder.addTranscodingHint(PrintTranscoder.KEY_MARGIN_TOP,new Float (80));


    // Step-4: Write output to PDF format
    transcoder.transcode(input_svg_image, output_pdf_file);

    // Step 5- close / flush Output Stream
    pdf_ostream.flush();
    pdf_ostream.close();  

    }

}

0 个答案:

没有答案