2011-10-06 5 views
9

में अपाचे पीओआई एक्सडब्ल्यूपीएफ के साथ .docx दस्तावेज़ में एक तस्वीर कैसे जोड़ें, मैंने जावा पीओआई 3.7 का उपयोग करके एक साधारण डॉक्क्स-दस्तावेज़ बनाया है। XWPF। फिर, मैंने विधि XWPFDocument.addpicture(byte[] arg0, int arg1) का उपयोग कर द्वारा एक तस्वीर जोड़ा।जावा

XWPFDocument docx = new XWPFDocument(); 
XWPFParagraph par = docx.createParagraph(); 
XWPFRun run = par.createRun(); 
run.setText("Hello, World. This is my first java generated docx-file. Have fun."); 
run.setFontSize(13); 

InputStream pic = new FileInputStream("logo.jpg"); 
byte [] picbytes = IOUtils.toByteArray(pic); 
docx.addPicture(picbytes, Document.PICTURE_TYPE_JPEG); 

के बाद से docx का फ़ाइल आकार बढ़ता है, तस्वीर "शारीरिक रूप से" दस्तावेज़ में जोड़ा गया था; लेकिन यह एमएस वर्ड में बिल्कुल प्रदर्शित नहीं होता है। ऐसा लगता है कि दस्तावेज़ में लापता तस्वीर का संदर्भ है।

ऐसा करने का तरीका क्या है? अपाचे पीओआई के साथ चित्र हैंडलिंग कैसे किया जाता है? और जहां वेब पर अधिक ट्यूटोरियल XWPF, जो पैराग्राफ की हैंडलिंग बताता, चलाता है के लिए सभी पर लगभग कोई दस्तावेज या ट्यूटोरियल, आदि

हैं, वहाँ है केवल एक चीज मैं इस बारे में पाया यहाँ है: https://issues.apache.org/bugzilla/show_bug.cgi?id=49765 लेकिन यदि ऐसा नहीं होता बिल्कुल मदद करो।

अग्रिम में बहुत धन्यवाद।

+0

आप POI 3.8 बीटा 4 के साथ की कोशिश की? कुछ तस्वीर कीड़े 3.7 फ़ाइनल के बाद तय की गई हैं, इसलिए यह देखने लायक है कि अपग्रेड आपके मुद्दे को हल करता है – Gagravarr

+0

हां, केवल अंतर यह है कि पीओआई 3.8 बीटा 4 में विधि का नाम XWPFDocument.addPicture (बाइट [] arg0, int arg1) बदल दिया गया है XWPFDocument.addPictureData (बाइट [] arg0, int arg1) - शायद यह रूपरेखा के लिए कि यह विधि केवल दस्तावेज़ डेटा को दस्तावेज़ में जोड़ती है, लेकिन चित्र में शब्द दिखाने के लिए चित्र को पैराग्राफ में नहीं जोड़ता है। सवाल यह है कि कैसे क्या यह अभी भी अनुत्तरित है। मुझे लगता है कि एक सीटीपीचित्र वस्तु (org.openxmlformats.schemas.drawingml.x2006.picture.CT चित्र) को एक पैराग्राफ में बनाया और जोड़ा जाना है - हालांकि, मुझे यह नहीं पता है कि यह कैसे करें। –

+0

क्या आपने [addPicture] को कॉल करने का प्रयास किया था (http://poi.apache.org/apidocs/org/apache/poi/xwpf/usermodel/XWPFRun.html#addPicture%28java.io.InputStream,%20int,%20java.lang पाठ चलाने पर .ट्रिंग,% 20int,% 20int% 2 9) जहां आप अपनी तस्वीर दिखाना चाहते हैं? – Gagravarr

उत्तर

2

बग लिंक कि आप वास्तव में प्रदान की है एक काम कर समाधान है: https://issues.apache.org/bugzilla/show_bug.cgi?id=49765#c15

यह hackery में से कुछ राशि की आवश्यकता है लेकिन मैं इसे उपयोग कर रहा हूँ और यह निश्चित रूप से काम करता है!

+0

@ अमितभ क्या आप पीओआई में मेरी मदद कर सकते हैं ?? – lucifer

0

मैंने docx4j का उपयोग किया है और इसे काम करने के लिए पाया है। मैंने पीओआई के साथ प्रयास किया लेकिन इसे काम करने में सक्षम नहीं है। कृपया docx4j का उपयोग करने के लिए कोड पाएं।

public static void main(String[] args) throws Exception { 
    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage(); 
    //wordMLPackage.getMainDocumentPart().addStyledParagraphOfText("Title", "Hello World"); 
    //wordMLPackage.getMainDocumentPart().addParagraphOfText("Text"); 
    java.io.InputStream is = new java.io.FileInputStream("path\\to\\images.jpg"); 
// commons-io.jar 
    byte[] bytes = IOUtils.toByteArray(is); 
    String filenameHint = null; 
    String altText = null; 
    int id1 = 0; 
    int id2 = 1; 
    org.docx4j.wml.P p = newImage(wordMLPackage, bytes,filenameHint, altText,id1, id2,6000 ); 
    // Now add our p to the document 
    wordMLPackage.getMainDocumentPart().addObject(p); 
    wordMLPackage.save(new java.io.File("helloworld.docx")); 
    is.close(); 
} 



public static org.docx4j.wml.P newImage(WordprocessingMLPackage wordMLPackage, 
     byte[] bytes, 
     String filenameHint, String altText, 
     int id1, int id2, long cx) throws Exception { 

     BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wordMLPackage, bytes); 

     Inline inline = imagePart.createImageInline(filenameHint, altText,id1, id2, cx,false); 

    // Now add the inline in w:p/w:r/w:drawing 
     org.docx4j.wml.ObjectFactory factory = new org.docx4j.wml.ObjectFactory(); 
     org.docx4j.wml.P p = factory.createP(); 
     org.docx4j.wml.R run = factory.createR();    
     p.getContent().add(run);  
     org.docx4j.wml.Drawing drawing = factory.createDrawing();    
     run.getContent().add(drawing);    
     drawing.getAnchorOrInline().add(inline); 

     return p; 

}

11

मैं जानता हूँ कि इस पोस्ट बहुत पुरानी है लेकिन अभी भी मैं उत्तर पोस्ट कर रहा हूँ ताकि उन सभी ने इस जवाब के लिए खोज रहे हैं इसका इस्तेमाल कर सकते हैं। शब्द दस्तावेज़ में चित्र डालने के लिए आपको दो प्रोग्राम लिखना होगा। पहले एक है: -

package org.word.POI; 

import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 

import org.apache.poi.openxml4j.exceptions.InvalidFormatException; 
import org.apache.poi.xwpf.usermodel.Document; 
import org.apache.poi.xwpf.usermodel.XWPFDocument; 

/* 
Romesh Soni 
[email protected] 
*/ 

public class TestCustom 
{ 

    public static void main(String []a) throws FileNotFoundException, IOException, InvalidFormatException 
    { 

     CustomXWPFDocument document = new CustomXWPFDocument(new FileInputStream(new File("C:\\Users\\amitabh\\Documents\\Apache POI\\Word File\\new.doc"))); 
     FileOutputStream fos = new FileOutputStream(new File("C:\\Users\\amitabh\\Documents\\Apache POI\\Word File\\new.doc")); 

     String blipId = document.addPictureData(new FileInputStream(new File("C:\\Users\\amitabh\\Pictures\\pics\\3.jpg")), Document.PICTURE_TYPE_JPEG); 

     System.out.println(document.getNextPicNameNumber(Document.PICTURE_TYPE_JPEG)); 

     //System.out.println(document.getNextPicNameNumber(Document.PICTURE_TYPE_JPEG)); 
     document.createPicture(blipId,document.getNextPicNameNumber(Document.PICTURE_TYPE_JPEG), 500, 500); 


     document.write(fos); 
     fos.flush(); 
     fos.close(); 

    } 

} 

अब यहाँ मैं इस कोड में "CustomeXwPFDocument" का इस्तेमाल किया है और आप किसी भी जार फ़ाइल के माध्यम से किसी भी आयात नहीं मिलेगा तो आप अपने पैकेज में एक और जावा वर्ग को जोड़ने के लिए है। इस कार्यक्रम के लिए

package org.word.POI; 

import org.apache.poi.xwpf.usermodel.XWPFDocument; 
import org.apache.xmlbeans.XmlException; 
import org.apache.xmlbeans.XmlToken; 
import org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualDrawingProps; 
import org.openxmlformats.schemas.drawingml.x2006.main.CTPositiveSize2D; 
import org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing.CTInline; 

import java.io.IOException; 
import java.io.InputStream; 

public class CustomXWPFDocument extends XWPFDocument 
{ 
    public CustomXWPFDocument(InputStream in) throws IOException 
    { 
     super(in); 
    } 

    public void createPicture(String blipId,int id, int width, int height) 
    { 
     final int EMU = 9525; 
     width *= EMU; 
     height *= EMU; 
     //String blipId = getAllPictures().get(id).getPackageRelationship().getId(); 


     CTInline inline = createParagraph().createRun().getCTR().addNewDrawing().addNewInline(); 

     String picXml = "" + 
       "<a:graphic xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\">" + 
       " <a:graphicData uri=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">" + 
       "  <pic:pic xmlns:pic=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">" + 
       "   <pic:nvPicPr>" + 
       "   <pic:cNvPr id=\"" + id + "\" name=\"Generated\"/>" + 
       "   <pic:cNvPicPr/>" + 
       "   </pic:nvPicPr>" + 
       "   <pic:blipFill>" + 
       "   <a:blip r:embed=\"" + blipId + "\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\"/>" + 
       "   <a:stretch>" + 
       "    <a:fillRect/>" + 
       "   </a:stretch>" + 
       "   </pic:blipFill>" + 
       "   <pic:spPr>" + 
       "   <a:xfrm>" + 
       "    <a:off x=\"0\" y=\"0\"/>" + 
       "    <a:ext cx=\"" + width + "\" cy=\"" + height + "\"/>" + 
       "   </a:xfrm>" + 
       "   <a:prstGeom prst=\"rect\">" + 
       "    <a:avLst/>" + 
       "   </a:prstGeom>" + 
       "   </pic:spPr>" + 
       "  </pic:pic>" + 
       " </a:graphicData>" + 
       "</a:graphic>"; 

     //CTGraphicalObjectData graphicData = inline.addNewGraphic().addNewGraphicData(); 
     XmlToken xmlToken = null; 
     try 
     { 
      xmlToken = XmlToken.Factory.parse(picXml); 
     } 
     catch(XmlException xe) 
     { 
      xe.printStackTrace(); 
     } 
     inline.set(xmlToken); 
     //graphicData.set(xmlToken); 

     inline.setDistT(0); 
     inline.setDistB(0); 
     inline.setDistL(0); 
     inline.setDistR(0); 

     CTPositiveSize2D extent = inline.addNewExtent(); 
     extent.setCx(width); 
     extent.setCy(height); 

     CTNonVisualDrawingProps docPr = inline.addNewDocPr(); 
     docPr.setId(id); 
     docPr.setName("Picture " + id); 
     docPr.setDescr("Generated"); 
    } 
} 

उपयोग POI 3.9 जार -: "CustomXWPFDocument" वर्ग के लिए कोड इस तरह चला जाता है। सबसे अच्छा यूआरएल है: - http://www.apache.org/dyn/closer.cgi/poi/release/bin/poi-bin-3.9-20121203.zip

अब आप उड़ने के लिए तैयार हैं। शुभकामनाएँ।

+0

धन्यवाद आदमी, इससे बहुत मदद मिली। हालांकि, आपको दो कार्यक्रम बनाने की आवश्यकता नहीं है। आप आसानी से 'createPicture' विधि से कोड का उपयोग कर सकते हैं जैसा कहीं भी आप चाहते हैं। –

+0

@ अमिताभ राजन: क्या किसी विशेष पृष्ठ और रेखा में और केंद्र संरेखण के साथ तस्वीर जोड़ना संभव है? – softmage99

+0

@MageshBabu: - दोस्त मुझे लगता है कि यह संभव है लेकिन मुझे खेद है कि मैं इस पर आपकी मदद करने में सक्षम नहीं हूं। मैं पिछले साल पीओआई पर बहुत कम असाइनमेंट पर काम कर रहा था लेकिन मुझे लगता है कि आप जो कह रहे हैं वह संभव है। कृपया कोड के साथ खेलें और मुझे लगता है कि आपको जवाब मिल जाएगा। मुझे आपकी मदद करने में सक्षम होने के लिए वास्तव में खेद है। –

4

सुरुचिपूर्ण समाधान आ रहा है, बाहर की जाँच: https://issues.apache.org/bugzilla/show_bug.cgi?id=55476

आप इंतजार कर सकते हैं या तो जब तक मेरी पैच स्वीकार कर लिया और जारी किया जाता है या नवीनतम POI SVN चेकआउट (http://poi.apache.org/subversion.html पर पैच लागू करने का प्रयास करें, काम करेंगे केवल पहुँच लिंक पढ़) और स्रोतों से पीओआई जार का निर्माण।

दृष्टिकोण tsd.tom ने सुझाव दिया काम करेगा:

XWPFParagraph par = docx.createParagraph(); 
XWPFRun run = par.createRun(); 
run.addPicture(pic, XWPFDocument.PICTURE_TYPE_JPEG, "logo.JPG",300,300); 
+0

स्टैक ओवरव्लो में आपका स्वागत है। आप उन्हें 4 रिक्त स्थान के साथ इंडेंट करके कोड ब्लॉक बना सकते हैं (या संपूर्ण ब्लॉक का चयन करें और '{} 'आइकन पर क्लिक करें)। – Sumurai8

+0

सुरुचिपूर्ण समाधान अभी भी यहां नहीं है, feb 2016 – Exceptyon

0
@Pradeep Your code 

    public static void main(String[] args) throws Exception { 
    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage(); 
    //wordMLPackage.getMainDocumentPart().addStyledParagraphOfText("Title", "Hello World"); 
    //wordMLPackage.getMainDocumentPart().addParagraphOfText("Text"); 
    java.io.InputStream is = new java.io.FileInputStream("path\\to\\images.jpg"); 
// commons-io.jar 
    byte[] bytes = IOUtils.toByteArray(is); 
    String filenameHint = null; 
    String altText = null; 
    int id1 = 0; 
    int id2 = 1; 
    org.docx4j.wml.P p = newImage(wordMLPackage, bytes,filenameHint, altText,id1, id2,6000 ); 
    // Now add our p to the document 
    wordMLPackage.getMainDocumentPart().addObject(p); 
    wordMLPackage.save(new java.io.File("helloworld.docx")); 
    is.close(); 
} 



public static org.docx4j.wml.P newImage(WordprocessingMLPackage wordMLPackage, 
     byte[] bytes, 
     String filenameHint, String altText, 
     int id1, int id2, long cx) throws Exception { 

     BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wordMLPackage, bytes); 

     Inline inline = imagePart.createImageInline(filenameHint, altText,id1, id2, cx,false); 

    // Now add the inline in w:p/w:r/w:drawing 
     org.docx4j.wml.ObjectFactory factory = new org.docx4j.wml.ObjectFactory(); 
     org.docx4j.wml.P p = factory.createP(); 
     org.docx4j.wml.R run = factory.createR();    
     p.getContent().add(run);  
     org.docx4j.wml.Drawing drawing = factory.createDrawing();    
     run.getContent().add(drawing);    
     drawing.getAnchorOrInline().add(inline); 

     return p; 

works , but all the above codes does not work, i tried it personally 

thanks, a lot. 
-2

क्योंकि XWPFRun।addPicture (यह क्षति xdoc फ़ाइल) काम नहीं करता है मैं इस तरह तस्वीर के प्रबंधन:

private void addPicture(final String fileName, final int id, int width, int height, final XWPFRun run) 
{ 
    FileInputStream fileInputStream = null; 

    try 
    { 
     fileInputStream = new FileInputStream("/home/maq/ebworkspace/TeamForge_Tools/" + fileName); 
     final String blipId = run.getDocument().addPictureData(fileInputStream, Document.PICTURE_TYPE_JPEG); 

     final int EMU = 9525; 
     width *= EMU; 
     height *= EMU; 
     //String blipId = getAllPictures().get(id).getPackageRelationship().getId(); 

     final CTInline inline = run.getCTR().addNewDrawing().addNewInline(); 

     final String picXml = "" + "<a:graphic xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\">" 
       + " <a:graphicData uri=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">" + "  <pic:pic xmlns:pic=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">" 
       + "   <pic:nvPicPr>" + "   <pic:cNvPr id=\"" 
       + id 
       + "\" name=\"Generated\"/>" 
       + "   <pic:cNvPicPr/>" 
       + "   </pic:nvPicPr>" 
       + "   <pic:blipFill>" 
       + "   <a:blip r:embed=\"" 
       + blipId 
       + "\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\"/>" 
       + "   <a:stretch>" 
       + "    <a:fillRect/>" 
       + "   </a:stretch>" 
       + "   </pic:blipFill>" 
       + "   <pic:spPr>" 
       + "   <a:xfrm>" 
       + "    <a:off x=\"0\" y=\"0\"/>" 
       + "    <a:ext cx=\"" 
       + width 
       + "\" cy=\"" 
       + height 
       + "\"/>" 
       + "   </a:xfrm>" 
       + "   <a:prstGeom prst=\"rect\">" 
       + "    <a:avLst/>" 
       + "   </a:prstGeom>" 
       + "   </pic:spPr>" 
       + "  </pic:pic>" + " </a:graphicData>" + "</a:graphic>"; 

     //CTGraphicalObjectData graphicData = inline.addNewGraphic().addNewGraphicData(); 
     XmlToken xmlToken = null; 
     xmlToken = XmlToken.Factory.parse(picXml); 
     inline.set(xmlToken); 
     //graphicData.set(xmlToken); 

     inline.setDistT(0); 
     inline.setDistB(0); 
     inline.setDistL(0); 
     inline.setDistR(0); 

     final CTPositiveSize2D extent = inline.addNewExtent(); 
     extent.setCx(width); 
     extent.setCy(height); 

     final CTNonVisualDrawingProps docPr = inline.addNewDocPr(); 
     docPr.setId(id); 
     docPr.setName("Picture " + id); 
     docPr.setDescr("Generated"); 
    } 
    catch (final Exception e) 
    { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    finally 
    { 
     //close streams 
     if (fileInputStream != null) 
     { 
      try 
      { 
       fileInputStream.close(); 
      } 
      catch (final IOException ioEx) 
      { 
       //can be ignored 
      } 
     } 
    } 

} 

और चल रहा है उदाहरण:

addPicture(filePath, xdoc.getNextPicNameNumber(Document.PICTURE_TYPE_JPEG), 100, 50, xRun);