2009-01-02 18 views
10

हम जावा से पीडीएफ उत्पन्न करने के लिए iText का उपयोग करते हैं (आंशिक रूप से इस साइट पर अनुशंसाओं पर आधारित)। हालांकि, जीआईएफ परिणामों जैसे छवि प्रारूप में हमारे लोगो की एक प्रति एम्बेड करना थोड़ा अजीब लग रहा है क्योंकि लोग ज़ूम इन और आउट करते हैं।iText पीडीएफ में वेक्टर ग्राफिक्स

आदर्श रूप से हम छवि को वेक्टर प्रारूप में एम्बेड करना चाहते हैं, जैसे ईपीएस, एसवीजी या सिर्फ एक पीडीएफ टेम्पलेट। वेबसाइट का दावा है कि ईपीएस समर्थन गिरा दिया गया है, पीडीएफ या पीएस को पीडीएफ में एम्बेड करने से त्रुटियों में परिणाम हो सकता है, और इसमें एसवीजी का भी उल्लेख नहीं है।

हमारा कोड सीधे iText की बजाय ग्राफिक्स 2 डी एपीआई का उपयोग करता है, लेकिन हम एडब्ल्यूटी मोड से बाहर निकलने के लिए तैयार होंगे और परिणाम प्राप्त करने पर iText का उपयोग स्वयं करेंगे। यह कैसे किया जा सकता है?

उत्तर

8

documentation iText के अनुसार निम्न छवि स्वरूपों का समर्थन करता। मैं अगर यह किसी भी मदद की हो सकता है पता नहीं है, लेकिन मैं सफलतापूर्वक iTextSharp का इस्तेमाल किया है एक pdf फ़ाइल में एम्बेड करने के लिए वेक्टर WMF छवि:

सी #:

using System; 
using System.IO; 
using iTextSharp.text; 
using iTextSharp.text.pdf; 

public class Program 
{ 

    public static void Main() 
    { 
     Document document = new Document(); 
     using (Stream outputPdfStream = new FileStream("output.pdf", FileMode.Create, FileAccess.Write, FileShare.None)) 
     using (Stream imageStream = new FileStream("test.wmf", FileMode.Open, FileAccess.Read, FileShare.Read)) 
     { 
      PdfWriter.GetInstance(document, outputPdfStream); 
      Image wmf = Image.GetInstance(imageStream); 
      document.Open(); 
      document.Add(wmf); 
      document.Close(); 
     } 
    } 
} 
+1

जब मैंने आपके कोड की कोशिश की तो मुझे अपवाद मिलता है, यह मेरे लिए काम नहीं कर रहा है, मैंने हाल ही में पढ़ा है कि ईपीएस समर्थित नहीं है, आपको इसे WMF – Deumber

3

मैंने हाल ही में सीखा है कि आप सीधे अपने ग्राफिक्स 2 डी ऑब्जेक्ट को iText पर भेज सकते हैं, और परिणामी पीडीएफ फाइलें स्केलेबल वेक्टर ग्राफिक्स जितनी अच्छी हैं। आपकी पोस्ट से, ऐसा लगता है कि इससे आपकी समस्या हल हो सकती है। जेपीईजी, GIF, PNG, TIFF, BMP, WMF और ईपीएस:

Document document = new Document(PageSize.LETTER); 
PdfWriter writer = null; 
try { 
    writer = PdfWriter.getInstance(document, new FileOutputStream(your file name)); 
} catch (Exception e) { 
    // do something with exception 
} 

document.open(); 

PdfContentByte cb = writer.getDirectContent(); 
PdfTemplate tp = cb.createTemplate(width, height); 
Graphics2D g2 = tp.createGraphics(width, height, new DefaultFontMapper()); 

// Create your graphics here - draw on the g2 Graphics object 

g2.dispose(); 
cb.addTemplate(tp, 0, 100); // 0, 100 = x,y positioning of graphics in PDF page 
document.close(); 
+0

यही वह है जो हम पहले से कर रहे हैं - पृष्ठ पर आकर्षित करने के लिए ग्राफिक्स 2 डी का उपयोग कर। हमें एक वेक्टर प्रारूप में एक छवि जोड़ने की जरूरत है। –

4

मैं 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); 
+1

में परिवर्तित करना होगा हैलो, मैं कुछ ऐसा करने का तरीका ढूंढने की कोशिश कर रहा हूं, एक पीडीएफ दस्तावेज में एसवीजी फाइलें प्रस्तुत करें। Itext का कौन सा संस्करण आप उपयोग कर रहे हैं? मैंने देखा है कि रचनाकार वर्ग com.lowagie.text.pdf.PdfGraphics2D के लिए निजी हैं, कम से कम iText 2.1.2 में, और मुझे IText 5.5.4 में ऐसी श्रेणी नहीं मिली। धन्यवाद! - जोस टेपेडिनो 6 मिनट पहले –

+0

@ जोसेटेपेडिनो मुझे खेद है, लेकिन मैंने वर्षों में इसे छुआ नहीं है (क्योंकि मैंने जवाब लिखा था)। मैं कल्पना करता हूं कि मैं एक संस्करण का उपयोग कर रहा था जिसे सितंबर 2012 के आसपास जारी किया गया था, इसलिए शायद iText 5.3.2 होगा जिसे अगस्त 2012 में उनके गिटहब रिलीज़ के अनुसार जारी किया गया था - https://github.com/itext/itextpdf/releases /tag/5.3.2। उम्मीद है की वो मदद करदे। – clayzermk1

+0

आपके संदेश के लिए धन्यवाद! मैंने एक एपीआई पेज ढूंढकर थोड़ा प्रगति की है, जिसमें यह बताते हुए कि पैकेज com.itextpdf.awt से PdfGraphics2D क्लास iText 5.0.2 के बाद मौजूद है: http://developers.itextpdf.com/reference/com.itextpdf.awt.PdfGraphics2D । मेरे पास अभी भी कवर करने के लिए कुछ जमीन है, लेकिन उपयोग करने के लिए सही संस्करण का पता लगाना एक अच्छी शुरुआत है। धन्यवाद! ' org.apache.xmlgraphics बाटिक-GVT 1,7 ': कोड में –

6

यह मैं क्या पोस्ट और सरकारी उदाहरण मैं यहां पाया से ली गई:

/** 
* Reads an SVG Image file into an com.itextpdf.text.Image instance to embed it into a PDF 
* @param svgPath SVG filepath 
* @param writer PdfWriter instance 
* @return Instance of com.itextpdf.text.Image holding the SVG file 
* @throws IOException 
* @throws BadElementException 
*/ 
private static Image getSVGImage(String svgPath, PdfWriter writer) throws IOException, BadElementException { 
    SVGDocument svgDoc = new SAXSVGDocumentFactory(null).createSVGDocument(null, new FileReader(svgPath)); 

    // Try to read embedded height and width 
    float svgWidth = Float.parseFloat(svgDoc.getDocumentElement().getAttribute("width").replaceAll("[^0-9.,]","")); 
    float svgHeight = Float.parseFloat(svgDoc.getDocumentElement().getAttribute("height").replaceAll("[^0-9.,]","")); 

    PdfTemplate svgTempl = PdfTemplate.createTemplate(writer, svgWidth, svgHeight); 
    Graphics2D g2d = new PdfGraphics2D(svgTempl, svgTempl.getWidth(), svgTempl.getHeight()); 
    GraphicsNode chartGfx = (new GVTBuilder()).build(new BridgeContext(new UserAgentAdapter()), svgDoc); 
    chartGfx.paint(g2d); 
    g2d.dispose(); 

    return new ImgTemplate(svgTempl); 
} 

छवि उदाहरण (एक हस्ताक्षर के रूप में मेरे मामले में) पीडीएफ करने के लिए आसानी से जोड़ा जा सकता है।

+1

वर्ग से ऊपर अगले Maven निर्भरता का उपयोग कर आयात किया जा सकता है –