Wen Zhao
Wen Zhao

Reputation: 11

How to add a custom graphic to the Word document header

I want to add a horizontal line to the header, which is drawn with word custom graphics.

1.For example, the following effect:

No custom graphics were added to the header: enter image description here

Add a horizontal line to the header: enter image description here


2.Unzip them separately and get the .xml file for the header:

<w:p w:rsidR="00212733" w:rsidRPr="00212733" w:rsidRDefault="00212733" w:rsidP="00212733">
    <w:pPr>
        <w:pStyle w:val="af"/>
        <w:ind w:firstLine="800"/>
        <w:jc w:val="center"/>
        <w:rPr>
            <w:sz w:val="40"/>
            <w:szCs w:val="40"/>
        </w:rPr>
    </w:pPr>
    <w:r w:rsidRPr="00212733">
        <w:rPr>
            <w:sz w:val="40"/>
            <w:szCs w:val="40"/>
        </w:rPr>
        <w:t>This is a header</w:t>
    </w:r>
</w:p>


<w:p w:rsidR="00EB798F" w:rsidRDefault="00EB798F" w:rsidP="00EB798F">
    <w:pPr>
        <w:pStyle w:val="af"/>
        <w:ind w:firstLine="800"/>
        <w:jc w:val="center"/>
        <w:rPr>
            <w:sz w:val="40"/>
            <w:szCs w:val="40"/>
        </w:rPr>
    </w:pPr>
    <w:r w:rsidRPr="00EB798F">
        <w:rPr>
            <w:rFonts w:hint="eastAsia"/>
            <w:sz w:val="40"/>
            <w:szCs w:val="40"/>
        </w:rPr>
        <w:t>T</w:t>
    </w:r>
    <w:r w:rsidRPr="00EB798F">
        <w:rPr>
            <w:sz w:val="40"/>
            <w:szCs w:val="40"/>
        </w:rPr>
        <w:t>his is a header</w:t>
    </w:r>
</w:p>


<w:p w:rsidR="0039120D" w:rsidRPr="00EB798F" w:rsidRDefault="0039120D" w:rsidP="00EB798F">
    <w:pPr>
        <w:pStyle w:val="af"/>
        <w:ind w:firstLine="400"/>
        <w:jc w:val="center"/>
        <w:rPr>
            <w:sz w:val="40"/>
            <w:szCs w:val="40"/>
        </w:rPr>
    </w:pPr>
    <w:bookmarkStart w:id="0" w:name="_GoBack"/>
    <w:r>
        <w:rPr>
            <w:rFonts w:ascii="仿宋_GB2312"/>
            <w:noProof/>
            <w:color w:val="FF0000"/>
            <w:sz w:val="20"/>
        </w:rPr>
        <mc:AlternateContent>
            <mc:Choice Requires="wps">
                <w:drawing>
                    <wp:anchor distT="0" distB="0" distL="114300" distR="114300" simplePos="0" relativeHeight="251659264" behindDoc="0" locked="0" layoutInCell="1" allowOverlap="1" wp14:anchorId="07DD5BF2" wp14:editId="521D0E7B">
                    <wp:simplePos x="0" y="0"/>
                    <wp:positionH relativeFrom="column">
                        <wp:posOffset>0</wp:posOffset>
                    </wp:positionH>
                    <wp:positionV relativeFrom="paragraph">
                        <wp:posOffset>10795</wp:posOffset>
                    </wp:positionV>
                    <wp:extent cx="5963920" cy="1905"/>
                    <wp:effectExtent l="5715" t="10795" r="12065" b="6350"/>
                    <wp:wrapNone/>
                    <wp:docPr id="5" name="直线 15"/>
                    <wp:cNvGraphicFramePr><a:graphicFrameLocks xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"/>
                    </wp:cNvGraphicFramePr>
                    <a:graphic xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">
                        <a:graphicData uri="http://schemas.microsoft.com/office/word/2010/wordprocessingShape">
                            <wps:wsp>
                                <wps:cNvCnPr>
                                    <a:cxnSpLocks noChangeShapeType="1"/>
                                </wps:cNvCnPr>
                                <wps:spPr bwMode="auto">
                                    <a:xfrm>
                                        <a:off x="0" y="0"/>
                                        <a:ext cx="5963920" cy="1905"/>
                                    </a:xfrm>
                                    <a:prstGeom prst="line">
                                        <a:avLst/>
                                    </a:prstGeom>
                                    <a:noFill/>
                                    <a:ln w="9525">
                                        <a:solidFill>
                                            <a:srgbClr val="000000"/>
                                        </a:solidFill>
                                        <a:round/>
                                        <a:headEnd/>
                                        <a:tailEnd/>
                                    </a:ln>
                                    <a:extLst>
                                        <a:ext uri="{909E8E84-426E-40DD-AFC4-6F175D3DCCD1}">
                                        <a14:hiddenFill xmlns:a14="http://schemas.microsoft.com/office/drawing/2010/main">
                                            <a:noFill/>
                                        </a14:hiddenFill>
                                        </a:ext>
                                    </a:extLst>
                                </wps:spPr>
                                <wps:bodyPr/>
                            </wps:wsp>
                        </a:graphicData>
                    </a:graphic>
                    <wp14:sizeRelH relativeFrom="page">
                        <wp14:pctWidth>0</wp14:pctWidth>
                    </wp14:sizeRelH>
                    <wp14:sizeRelV relativeFrom="page">
                        <wp14:pctHeight>0</wp14:pctHeight>
                    </wp14:sizeRelV>
                    </wp:anchor>
                </w:drawing>
            </mc:Choice>
            <mc:Fallback>
                <w:pict>
                    <v:line w14:anchorId="0A64B4CD" id="直线 15" o:spid="_x0000_s1026" style="position:absolute;left:0;text-align:left;z-index:251659264;visibility:visible;mso-wrap-style:square;mso-width-percent:0;mso-height-percent:0;mso-wrap-distance-left:9pt;mso-wrap-distance-top:0;mso-wrap-distance-right:9pt;mso-wrap-distance-bottom:0;mso-position-horizontal:absolute;mso-position-horizontal-relative:text;mso-position-vertical:absolute;mso-position-vertical-relative:text;mso-width-percent:0;mso-height-percent:0;mso-width-relative:page;mso-height-relative:page" from="0,.85pt" to="469.6pt,1pt" o:gfxdata="UEsDBBQABgAIAAAAIQC2gziS/gAAAOEBAAATAAAAW0NvbnRlbnRfVHlwZXNdLnhtbJSRQU7DMBBF&#xA;90jcwfIWJU67QAgl6YK0S0CoHGBkTxKLZGx5TGhvj5O2G0SRWNoz/78nu9wcxkFMGNg6quQqL6RA&#xA;0s5Y6ir5vt9lD1JwBDIwOMJKHpHlpr69KfdHjyxSmriSfYz+USnWPY7AufNIadK6MEJMx9ApD/oD&#xA;OlTrorhX2lFEilmcO2RdNtjC5xDF9pCuTyYBB5bi6bQ4syoJ3g9WQ0ymaiLzg5KdCXlKLjvcW893&#xA;SUOqXwnz5DrgnHtJTxOsQfEKIT7DmDSUCaxw7Rqn8787ZsmRM9e2VmPeBN4uqYvTtW7jvijg9N/y&#xA;JsXecLq0q+WD6m8AAAD//wMAUEsDBBQABgAIAAAAIQA4/SH/1gAAAJQBAAALAAAAX3JlbHMvLnJl&#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AAAAQBAAAPAAAAZHJzL2Rvd25yZXYueG1sTI/BTsMwEETvSPyDtUhcqtYmlYCGOBUCcuNC&#xA;AfW6jZckIl6nsdsGvp7lBMedGc28LdaT79WRxtgFtnC1MKCI6+A6biy8vVbzW1AxITvsA5OFL4qw&#xA;Ls/PCsxdOPELHTepUVLCMUcLbUpDrnWsW/IYF2EgFu8jjB6TnGOj3YgnKfe9zoy51h47loUWB3po&#xA;qf7cHLyFWL3Tvvqe1TOzXTaBsv3j8xNae3kx3d+BSjSlvzD84gs6lMK0Cwd2UfUW5JEk6g0oMVfL&#xA;VQZqZyEzoMtC/4cvfwAAAP//AwBQSwECLQAUAAYACAAAACEAtoM4kv4AAADhAQAAEwAAAAAAAAAA&#xA;AAAAAAAAAAAAW0NvbnRlbnRfVHlwZXNdLnhtbFBLAQItABQABgAIAAAAIQA4/SH/1gAAAJQBAAAL&#xA;AAAAAAAAAAAAAAAAAC8BAABfcmVscy8ucmVsc1BLAQItABQABgAIAAAAIQAVc5gT0gEAAG8DAAAO&#xA;AAAAAAAAAAAAAAAAAC4CAABkcnMvZTJvRG9jLnhtbFBLAQItABQABgAIAAAAIQB6dbAA2gAAAAQB&#xA;AAAPAAAAAAAAAAAAAAAAACwEAABkcnMvZG93bnJldi54bWxQSwUGAAAAAAQABADzAAAAMwUAAAAA&#xA;"/>
                </w:pict>
            </mc:Fallback>
        </mc:AlternateContent>
    </w:r>
    <w:bookmarkEnd w:id="0"/>
</w:p>


I did some common sense, but I found that I always can't solve the id problem of custom graphics, I hope you can help me, thank you!

Upvotes: 0

Views: 294

Answers (1)

Axel Richter
Axel Richter

Reputation: 61985

Until now (apache poi 5.0.0) creation of shapes in Word is not supported by apache poi. Only inserting of pictures is supported. There is not even a possibility to handle the namespace markup-compatibility (mc). So also using the underlying low level org.openxmlformats.schemas.wordprocessingml.x2006.main.* classes it is not possible. Only directly manipulating the XML wold be possible. But this is too less straight forward.

What one could do is using the old shape definitions of com.microsoft.schemas.vml.*. These are currently still supported, even if they get replaced by the new drawing layer shapes.

Using com.microsoft.schemas.vml.* shapes are created using shape types which describe the shape by VML paths. Those shapes are placed in pict elements in text runs of document and/or header/footer.

A line shape in a header could be created as so:

import java.io.FileOutputStream;

import org.apache.poi.xwpf.usermodel.*;
import org.apache.poi.wp.usermodel.HeaderFooterType;

import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPicture;

import com.microsoft.schemas.vml.CTGroup;
import com.microsoft.schemas.vml.CTShape;
import com.microsoft.schemas.vml.CTShapetype;

import org.w3c.dom.Node;

public class CreateWordLineInHeader {

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

  XWPFDocument doc= new XWPFDocument();

  // the body content
  XWPFParagraph paragraph = doc.createParagraph();
  XWPFRun run=paragraph.createRun();  
  run.setText("The Body:");

  paragraph = doc.createParagraph();
  run=paragraph.createRun();  
  run.setText("Lorem ipsum....");

  // create header start
  XWPFHeader header = doc.createHeader(HeaderFooterType.DEFAULT);

  paragraph = header.getParagraphArray(0);
  if (paragraph == null) paragraph = header.createParagraph();
  paragraph.setAlignment(ParagraphAlignment.CENTER);
  run = paragraph.createRun(); 
  run.setText("This is a header");

  // paragraph for the line
  paragraph = header.createParagraph();
  paragraph.setAlignment(ParagraphAlignment.CENTER);
  run = paragraph.createRun(); 

  // create line shape in run
  CTGroup ctGroup = CTGroup.Factory.newInstance();

  String shapeTypeId = "_x0000_t123";
  CTShapetype shapetype = ctGroup.addNewShapetype();
  shapetype.setId(shapeTypeId);
  shapetype.setCoordsize("21600,21600");
  shapetype.setSpt(123);
  shapetype.setPath2("m,l21600,21600e");

  CTShape ctShape = ctGroup.addNewShape();
  ctShape.setType("#"+shapeTypeId);
  ctShape.setStyle("position:absolute;left:100pt;width:280pt;height:0");

  Node ctGroupNode = ctGroup.getDomNode(); 
  CTPicture ctPicture = CTPicture.Factory.parse(ctGroupNode);
  CTR cTR = run.getCTR();
  cTR.addNewPict();
  cTR.setPictArray(0, ctPicture);

  // create header end

  // create footer start
  XWPFFooter footer = doc.createFooter(HeaderFooterType.DEFAULT);

  paragraph = footer.getParagraphArray(0);
  if (paragraph == null) paragraph = footer.createParagraph();
  paragraph.setAlignment(ParagraphAlignment.CENTER);

  run = paragraph.createRun();  
  run.setText("The Footer");

  // create footer end

  FileOutputStream out = new FileOutputStream("CreateWordLineInHeader.docx");
  doc.write(out);
  out.close();
  doc.close();

 }
}

But is the line shape really necessary? If only a horizontal rule (like a HR in HTML) is needed, then a paragraph having a bottom border line would be much more straigt forward. See Word autoformat with Apache-POI.

Upvotes: 1

Related Questions