我如何添加图片作为使用Apache POI我的Word文档中的页眉(How i can add an

2019-10-21 11:31发布

我试图创建使用Apache POI的word文档,将含有作为其头和一些信息,即第一个图片作为其footer.I我能够同款创建页眉页脚而已。不过我需要将图像添加到页眉我无法管理,我张贴我的代码在这里被赋予正确的结果作为页眉页脚都与paragraph.Somebody请大家帮我实现这个,

public class CreateWordDoc {

public static void main (String[] args) throws Exception {


    //XWPFDocument document = new XWPFDocument();
    CustomXWPFDocument document = new CustomXWPFDocument(new FileInputStream(new File("D:\\test.docx")));
    CTP ctp = CTP.Factory.newInstance();
    CTR ctr = ctp.addNewR();
    CTRPr rpr = ctr.addNewRPr();
    CTText textt = ctr.addNewT();
    textt.setStringValue( " Client Service Contact:Tomas.Layrisse@mshgroupconsulting.com" );
    XWPFParagraph codePara = new XWPFParagraph( ctp, document );
    XWPFParagraph imagePara = new XWPFParagraph(ctp, document);

    XWPFParagraph[] newparagraphs = new XWPFParagraph[1];
    newparagraphs[0] = codePara;
    XWPFParagraph[] imaheparagraphs = new XWPFParagraph[1];
    imaheparagraphs[0]=imagePara;
    String blipId = document.addPictureData(new FileInputStream(new File("D:\\msh.jpg")), Document.PICTURE_TYPE_JPEG);
    CTSectPr sectPr = document.getDocument().getBody().addNewSectPr();
    XWPFHeaderFooterPolicy headerFooterPolicy = new  XWPFHeaderFooterPolicy( document, sectPr );

    document.createPicture(blipId,document.getNextPicNameNumber(Document.PICTURE_TYPE_JPEG), 400, 129);


    headerFooterPolicy.createHeader( STHdrFtr.FIRST,imaheparagraphs);
    //headerFooterPolicy.createFooter( STHdrFtr.DEFAULT, newparagraphs );
    FileOutputStream out = new FileOutputStream("D:\\test.docx");
    document.write(out);
    System.out.println("Doc Created");

   }
}

我modofications

XWPFParagraph[] imaheparagraphs = new XWPFParagraph[1];
    r.addPicture(new FileInputStream(new File("D:\\msh.jpg")), Document.PICTURE_TYPE_JPEG, "D:\\msh.jpg", 21, 32);
    r=imagePara.createRun();
    imaheparagraphs[0]=imagePara;

    String blipId = document.addPictureData(new FileInputStream(new File("D:\\msh.jpg")), Document.PICTURE_TYPE_JPEG);
    CTSectPr sectPr = document.getDocument().getBody().addNewSectPr();
    XWPFHeaderFooterPolicy headerFooterPolicy = new  XWPFHeaderFooterPolicy( document, sectPr );

    document.createPicture(blipId,document.getNextPicNameNumber(Document.PICTURE_TYPE_JPEG), 400, 129);


    headerFooterPolicy.createHeader( STHdrFtr.DEFAULT,imaheparagraphs);

.................................................. ...................................

public class CustomXWPFDocument extends XWPFDocument{

public CustomXWPFDocument(FileInputStream 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");
   }
}

截至目前头在doc的身体来了,请帮忙,谢谢你是进步。

文章来源: How i can add an Image as my header in a word document using Apache POI