Results 1 to 5 of 5
Like Tree2Likes
  • 1 Post By nivar89
  • 1 Post By javastuden

Thread: Add image to WORD in header doc or docx

  1. #1
    dfloresgonz is offline Member
    Join Date
    Oct 2014
    Posts
    1
    Rep Power
    0

    Post Add image to WORD in header doc or docx

    Hi everyone
    I have the following 2 classes which add an image to the body of a word document.
    Now I need to add an image but to the header.
    Please provide me with the solution.
    Thanks all!
    I have attached the image of the libraries I used apache poi 3.10
    And the image of the error when I open the word document when I try to add the image
    Class 1:
    Java Code:
    
    import java.io.*;
    
    import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
    import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;
    import org.apache.poi.xwpf.usermodel.*;
    
    import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
    import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR;
    import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText;
    
    public class ImagesDoc {
        public static void main(String[] args) throws IOException,
                                                      InvalidFormatException {
            String raiz = "C:\\Users\\diflores\\Desktop\\";
            CustomXWPFDocument document = new CustomXWPFDocument(new FileInputStream(new File(raiz+"doc1.docx")));
            FileOutputStream fos = new FileOutputStream(new File(raiz+"doc2.doc"));
            
          XWPFHeaderFooterPolicy policy = document.getHeaderFooterPolicy();
          if (policy.getDefaultHeader() == null && policy.getFirstPageHeader() == null
                 && policy.getDefaultFooter() == null) {
             // Need to create some new headers
             // The easy way, gives a single empty paragraph
             XWPFHeader headerD = policy.createHeader(policy.DEFAULT);
             //headerD.getParagraphs().get(0).createRun().setText("Hello Header World!");//this works I NEED to ADD AN IMAGE
             /*headerD.getParagraphs().get(0).createRun().addPicture(new FileInputStream(new File(raiz+"Captura.png")), 
                                                                   Document.PICTURE_TYPE_PNG, "Captura.png", 426, 389);*///ERROR HERE
             // Or the full control way
              CTP ctP1 = CTP.Factory.newInstance();
              CTR ctR1 = ctP1.addNewR();
              CTText t = ctR1.addNewT();
              t.setStringValue("Paragraph in header");
    
              XWPFParagraph p1 = new XWPFParagraph(ctP1, document);
              XWPFParagraph[] pars = new XWPFParagraph[1];
              pars[0] = p1;
    
              policy.createHeader(policy.FIRST, pars);
          } else {
             // Already has a header, change it
          }
          
            String id = document.addPictureData(new FileInputStream(new File(raiz+"Captura.png")),
                                                Document.PICTURE_TYPE_PNG);
            document.createPicture(id,document.getNextPicNameNumber(Document.PICTURE_TYPE_PNG),
                                   426, 389);//W,H
            document.write(fos);
            fos.flush();
            fos.close();
        }
    }
    
    
    //second class
    
    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");
        }
    }
    Attached Thumbnails Attached Thumbnails Add image to WORD in header doc or docx-error.png   Add image to WORD in header doc or docx-libs.png  

  2. #2
    nivar89 is offline Member
    Join Date
    Jul 2015
    Posts
    3
    Rep Power
    0

    Default Re: Add image to WORD in header doc or docx

    i have same problem, anyone know how to fix it?
    javastuden likes this.

  3. #3
    javastuden's Avatar
    javastuden is offline Senior Member
    Join Date
    Nov 2009
    Posts
    180
    Rep Power
    10

    Default Re: Add image to WORD in header doc or docx

    its due to you data,please check is there any issue in data

  4. #4
    nivar89 is offline Member
    Join Date
    Jul 2015
    Posts
    3
    Rep Power
    0

    Default Re: Add image to WORD in header doc or docx

    Quote Originally Posted by javastuden View Post
    its due to you data,please check is there any issue in data
    What do you mean about "data"?

  5. #5
    javastuden's Avatar
    javastuden is offline Senior Member
    Join Date
    Nov 2009
    Posts
    180
    Rep Power
    10

    Default Re: Add image to WORD in header doc or docx

    hi,
    data means the value you are passing will have some error,please check the same.
    EvanChrispie likes this.

Similar Threads

  1. Replies: 0
    Last Post: 11-01-2013, 07:17 AM
  2. how i set header and footer for word document using poi
    By Andavar Perumal in forum Apache POI
    Replies: 1
    Last Post: 03-14-2012, 04:10 PM
  3. How to read the word character in image file.
    By sureshsoftengg in forum Java Software
    Replies: 1
    Last Post: 09-21-2010, 03:37 PM
  4. read the word character in image file
    By sureshsoftengg in forum Advanced Java
    Replies: 2
    Last Post: 09-21-2010, 03:36 PM
  5. read tiff image header
    By mr_empty in forum Java 2D
    Replies: 0
    Last Post: 12-23-2009, 04:08 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •