如何向 Word 文档添加自定义图形 header

How to add a custom graphic to the Word document header

我想给header加一条横线,用word自定义图形画的

1.For例子,效果如下:

未向 header 添加自定义图形: enter image description here

在 header 中添加一条水平线: enter image description here


2.Unzip 分别获取 header:

的 .xml 文件
<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&#xA;bHOk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AAPAAAAZHJzL2Rvd25yZXYueG1sTI/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>


我做了一些常识,但是我发现我总是解决不了自定义图形的id问题,希望大家能帮帮我,谢谢!

到目前为止(apache poi 5.0.0apache poi 不支持在 Word 中创建形状。仅支持插入图片。甚至不可能处理命名空间 markup-compatibility (mc)。因此,也使用底层低级别 org.openxmlformats.schemas.wordprocessingml.x2006.main.* 类 是不可能的。只有直接操作 XML 才有可能。但这太不直接了。

可以做的是使用 com.microsoft.schemas.vml.* 的旧形状定义。这些目前仍然受支持,即使它们被新的绘图层形状取代。

使用 com.microsoft.schemas.vml.* 形状是使用通过 VML 路径描述形状的形状类型创建的。这些形状放置在文档 and/or header/footer.

文本运行中的 pict 个元素中

header 中的线条形状可以这样创建:

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();

 }
}

但是线型真的有必要吗?如果只需要一条水平线(如 HTML 中的 HR),那么具有底部边框线的段落会更加直截了当。参见 Word autoformat with Apache-POI