मैं iText द्वारा उदाहरण के एक जोड़े पाया लेखक जो पीडीएफ में एसवीजी खींचने के लिए ग्राफिक्स 2 डी एपीआई और अपाचे बटिक लाइब्रेरी का उपयोग करते हैं।
http://itextpdf.com/examples/iia.php?id=269
http://itextpdf.com/examples/iia.php?id=263
मेरी प्रयोजनों के लिए, मैं जबकि छवि के वेक्टर प्रकृति (कोई रैस्टराइज़ेशन) को बनाए रखने के लिए एक निश्चित आकार और स्थान पर एक पीडीएफ में एसवीजी के एक स्ट्रिंग लेने के लिए और आकर्षित करने के लिए है कि जरूरत ।
मैं एसएक्सजी फ़ाइल को बाईपास करना चाहता था जो SAXSVGDocumentFactory.createSVGDocument() फ़ंक्शंस में प्रचलित प्रतीत होता है। मुझे निम्न पोस्ट को एक फ्लैट फ़ाइल की बजाय एसवीजी टेक्स्ट स्ट्रिंग का उपयोग करने में मदद मिली।
http://batik.2283329.n4.nabble.com/Parse-SVG-from-String-td3539080.html
आप अपने स्ट्रिंग से एक StringReader बना सकते हैं और SAXSVGDocumentFactory # createDocument (स्ट्रिंग, रीडर) विधि है कि पारित करने के लिए की है। यूआरआई जिसे आप स्ट्रिंग के रूप में पहले पैरामीटर के रूप में पास करते हैं, एसवीजी दस्तावेज़ का मूल दस्तावेज़ यूआरआई होगा। यह केवल तभी महत्वपूर्ण होना चाहिए जब आपका एसवीजी किसी बाहरी फाइल का संदर्भ देता हो।
सादर,
डैनियल
जावा स्रोत iText उदाहरण से प्राप्त:
// SVG as a text string.
String svg = "<svg>...</svg>";
// Create the PDF document.
// rootPath is the present working directory path.
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(new File(rootPath + "svg.pdf")));
document.open();
// Add paragraphs to the document...
document.add(new Paragraph("Paragraph 1"));
document.add(new Paragraph(" "));
// Boilerplate for drawing the SVG to the PDF.
String parser = XMLResourceDescriptor.getXMLParserClassName();
SAXSVGDocumentFactory factory = new SAXSVGDocumentFactory(parser);
UserAgent userAgent = new UserAgentAdapter();
DocumentLoader loader = new DocumentLoader(userAgent);
BridgeContext ctx = new BridgeContext(userAgent, loader);
ctx.setDynamicState(BridgeContext.DYNAMIC);
GVTBuilder builder = new GVTBuilder();
PdfContentByte cb = writer.getDirectContent();
// Parse the SVG and draw it to the PDF.
Graphics2D g2d = new PdfGraphics2D(cb, 725, 400);
SVGDocument chart = factory.createSVGDocument(rootPath, new StringReader(svg));
GraphicsNode chartGfx = builder.build(ctx, chart);
chartGfx.paint(g2d);
g2d.dispose();
// Add paragraphs to the document...
document.add(new Paragraph("Paragraph 2"));
document.add(new Paragraph(" "));
document.close();
ध्यान दें कि यह पीडीएफ आप पर काम कर रहे करने के लिए एक एसवीजी आकर्षित करेगा। एसवीजी पाठ के ऊपर एक फ्लोटिंग परत के रूप में प्रकट होता है। मैं अभी भी इसे आगे बढ़ने/स्केल करने पर काम कर रहा हूं और इसे पाठ के साथ इनलाइन आराम कर रहा हूं, लेकिन उम्मीद है कि यह प्रश्न के तत्काल दायरे से बाहर है।
आशा है कि यह मदद करने में सक्षम था।
चीयर्स
संपादित करें: मैं निम्नलिखित का उपयोग कर एक इनलाइन वस्तु के रूप में मेरे svg को लागू करने में सक्षम था। पोजिशनिंग की जांच करने के लिए टिप्पणी की गई रेखाएं त्वरित सीमा जोड़ने के लिए हैं।
SAXSVGDocumentFactory factory = new SAXSVGDocumentFactory(XMLResourceDescriptor.getXMLParserClassName());
UserAgent userAgent = new UserAgentAdapter();
DocumentLoader loader = new DocumentLoader(userAgent);
BridgeContext ctx = new BridgeContext(userAgent, loader);
ctx.setDynamicState(BridgeContext.DYNAMIC);
GVTBuilder builder = new GVTBuilder();
SVGDocument svgDoc = factory.createSVGDocument(rootPath, new StringReader(svg));
PdfTemplate svgTempl = PdfTemplate.createTemplate(writer, Float.parseFloat(svgDoc.getDocumentElement().getAttribute("width")), Float.parseFloat(svgDoc.getDocumentElement().getAttribute("height")));
Graphics2D g2d = new PdfGraphics2D(svgTempl, svgTempl.getWidth(), svgTempl.getHeight());
GraphicsNode chartGfx = builder.build(ctx, svgDoc);
chartGfx.paint(g2d);
g2d.dispose();
Image svgImg = new ImgTemplate(svgTempl);
svgImg.setAlignment(Image.ALIGN_CENTER);
//svgImg.setBorder(Image.BOX);
//svgImg.setBorderColor(new BaseColor(0xff, 0x00, 0x00));
//svgImg.setBorderWidth(1);
document.add(svgImg);
जब मैंने आपके कोड की कोशिश की तो मुझे अपवाद मिलता है, यह मेरे लिए काम नहीं कर रहा है, मैंने हाल ही में पढ़ा है कि ईपीएस समर्थित नहीं है, आपको इसे WMF – Deumber