使用docx4java将字段代码插入文档
使用Docx4J,如何将字段代码(如“INCLUDEPICTURE”)插入到文档中 我需要使用INCLUDEPICTURE的\d开关 更新:从internet插入图像时,此代码不起作用:-(使用docx4java将字段代码插入文档,java,ms-word,docx,docx4j,Java,Ms Word,Docx,Docx4j,使用Docx4J,如何将字段代码(如“INCLUDEPICTURE”)插入到文档中 我需要使用INCLUDEPICTURE的\d开关 更新:从internet插入图像时,此代码不起作用:-( boolean save=true; WordprocessingMLPackage wordMLPackage=WordprocessingMLPackage.load(文件); MainDocumentPart mdp=wordMLPackage.getMainDocumentPart(); //示例1
boolean save=true;
WordprocessingMLPackage wordMLPackage=WordprocessingMLPackage.load(文件);
MainDocumentPart mdp=wordMLPackage.getMainDocumentPart();
//示例1:以标题样式添加文本
addStyledParagraphOfText(“Title”,“全选,然后在Word中点击F9以查看图片,或者先以编程方式添加图片”);
创建文本的段落(“简单字段:”);
P=新的P();
p、 getContent()。添加(
createSimpleField(“INCLUDEPICTURE\”http://placehold.it/312x322\“\\*合并格式”)
);
mdp.getContent().add(p);
mdp.createParagraphOfText(“复杂字段:”);
p=新的p();
addComplexField(p,“INCLUDEPICTURE\”http://placehold.it/312x322\“\\*合并格式”);
mdp.getContent().add(p);
保存(文件);
私有静态CTSimpleField createSimpleField(字符串值){
CTSimpleField=新的CTSimpleField();
字段setInstr(val);
返回字段;
}
私有静态void addComplexField(P,字符串instrText){
org.docx4j.wml.ObjectFactory wmlObjectFactory=Context.getWmlObjectFactory();
//为r创建对象
R=wmlObjectFactory.createR();
p、 getContent().add(r);
//为fldChar创建对象(包装在JAXBElement中)
FldChar FldChar=wmlObjectFactory.createFldChar();
JAXBElement fldcharWrapped=wmlObjectFactory.createRFldChar(fldchar);
r、 getContent().add(fldcharWrapped);
setFldCharType(org.docx4j.wml.STFldCharType.BEGIN);
//为instrText创建对象(包装在JAXBElement中)
Text Text=wmlObjectFactory.createText();
JAXBElement textWrapped=wmlObjectFactory.createrIntrText(文本);
r、 getContent().add(textWrapped);
text.setValue(instrText);
文本。设置空间(“保留”);
//为fldChar创建对象(包装在JAXBElement中)
fldchar=wmlObjectFactory.createFldChar();
fldcharWrapped=wmlObjectFactory.createRFldChar(fldchar);
r、 getContent().add(fldcharWrapped);
setFldCharType(org.docx4j.wml.STFldCharType.END);
}
当我解压.docx并打开document.xml时,我发现:
<w:p>
<w:pPr>
<w:pStyle w:val="Title"/>
</w:pPr>
<w:r>
<w:t>Select all, then hit F9 in Word to see your pictures, or programmatically add them first</w:t>
</w:r>
</w:p>
<w:p>
<w:fldSimple w:instr=" INCLUDEPICTURE "http://placehold.it/312x322" \* MERGEFORMAT "/>
</w:p>
<w:p>
<w:r>
<w:fldChar w:fldCharType="begin"/>
<w:instrText xml:space="preserve">INCLUDEPICTURE "http://placehold.it/312x322" \* MERGEFORMAT </w:instrText>
<w:fldChar w:fldCharType="end"/>
</w:r>
</w:p>
选择“全部”,然后在Word中按F9以查看图片,或者先以编程方式添加图片
包容性描述”http://placehold.it/312x322“\*合并格式
但是,如果我使用internet URL,它不会在文档上显示为图像
我试着用MS Word插入一个字段代码,这是我得到的代码。它正确地显示为一个图像
<w:p w14:paraId="43d51bd" w14:textId="43d51bd" w:rsidR="006549FC" w:rsidRDefault="00581431">
<w:pPr>
<w15:collapsed w:val="false"/>
</w:pPr>
<w:r>
<w:fldChar w:fldCharType="begin"/>
</w:r>
<w:r>
<w:instrText xml:space="preserve">INCLUDEPICTURE "http://placehold.it/100x100" \* MERGEFORMAT </w:instrText>
</w:r>
<w:r>
<w:fldChar w:fldCharType="separate"/>
</w:r>
<w:r>
<w:pict>
<v:shapetype coordsize="21600,21600" filled="f" id="_x0000_t75" o:preferrelative="t" o:spt="75.0" path="m@4@5l@4@11@9@11@9@5xe" stroked="f">
<v:stroke joinstyle="miter"/>
<v:formulas>
<v:f eqn="if lineDrawn pixelLineWidth 0"/>
<v:f eqn="sum @0 1 0"/>
<v:f eqn="sum 0 0 @1"/>
<v:f eqn="prod @2 1 2"/>
<v:f eqn="prod @3 21600 pixelWidth"/>
<v:f eqn="prod @3 21600 pixelHeight"/>
<v:f eqn="sum @0 0 1"/>
<v:f eqn="prod @6 1 2"/>
<v:f eqn="prod @7 21600 pixelWidth"/>
<v:f eqn="sum @8 21600 0"/>
<v:f eqn="prod @7 21600 pixelHeight"/>
<v:f eqn="sum @10 21600 0"/>
</v:formulas>
<v:path gradientshapeok="t" o:connecttype="rect" o:extrusionok="f"/>
<o:lock aspectratio="t" v:ext="edit"/>
</v:shapetype>
<v:shape id="_x0000_i1025" style="width:100pt;height:100pt" type="#_x0000_t75">
<v:imagedata r:href="rId6" r:id="rId5"/>
</v:shape>
</w:pict>
</w:r>
<w:r>
<w:fldChar w:fldCharType="end"/>
</w:r>
<w:bookmarkEnd w:id="0"/>
</w:p>
包容性描述”http://placehold.it/100x100“\*合并格式
查看新示例,该示例演示如何将其添加为简单字段或复杂字段
我将\d开关留给读者,作为一个大概微不足道的练习
请注意,除非您实际添加字段结果,否则在刷新字段之前,图片不会显示在Word中。以编程方式将图片添加到字段结果与addImage示例大致相同。我还尝试以编程方式为远程链接插入图像,但如果不手动,则无法获得所需的结果更新word 2010上的链接
public class FieldINCLUDEPICTURE {
public static void main(String[] args) throws Exception {
boolean save = true;
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
MainDocumentPart mdp = wordMLPackage.getMainDocumentPart();
// Example 1: add text in Title style
mdp.addStyledParagraphOfText("Title", "Select all, then hit F9 in Word to see your pictures, or programmatically add them first");
mdp.createParagraphOfText("simple field:");
String url = "http://www.springsoftware.co.uk/spring/images/sampledata/itcg.png";
P p = new P();
// p.getContent().add(
// createSimpleField( " INCLUDEPICTURE \"file:///C:/Users/jharrop/git/plutext/docx4j/src/test/resources/images/greentick.png\" \\* MERGEFORMAT ")
// );
// mdp.getContent().add(p);
mdp.createParagraphOfText("complex field:");
//addComplexField(p, "INCLUDEPICTURE \"" + url + "\"" + " \\* MERGEFORMAT \\d");
addComplexField(p, " INCLUDEPICTURE \"" + url + "\" \\* MERGEFORMAT \\d");
mdp.getContent().add(p);
//addComplexField(p, " INCLUDEPICTURE \"" + attachmentRequestURL + "\"" + " \\* MERGEFORMAT \\d");
// Pretty print the main document part
System.out.println(
XmlUtils.marshaltoString(mdp.getJaxbElement(), true, true));
String filenameHint = null;
String altText = null;
int id1 = 0;
int id2 = 1;
// Image 1: no width specified
P p1 = newImage(wordMLPackage, url, filenameHint, altText, id1, id2);
addComplexField(p1,url);
// Optionally save it
if (save) {
String filename = System.getProperty("user.dir") + "/OUT_FieldINCLUDEPICTURE.docx";
wordMLPackage.save(new java.io.File(filename) );
System.out.println("Saved " + filename);
}
}
private static CTSimpleField createSimpleField(String val) {
CTSimpleField field = new CTSimpleField();
field.setInstr(val);
return field;
}
private static void addComplexField(P p, String instrText) {
org.docx4j.wml.ObjectFactory wmlObjectFactory = Context.getWmlObjectFactory();
// Create object for r
R r = wmlObjectFactory.createR();
p.getContent().add( r);
// Create object for fldChar (wrapped in JAXBElement)
FldChar fldchar = wmlObjectFactory.createFldChar();
JAXBElement<org.docx4j.wml.FldChar> fldcharWrapped = wmlObjectFactory.createRFldChar(fldchar);
r.getContent().add( fldcharWrapped);
fldchar.setFldCharType(org.docx4j.wml.STFldCharType.BEGIN);
// Create object for instrText (wrapped in JAXBElement)
Text text = wmlObjectFactory.createText();
JAXBElement<org.docx4j.wml.Text> textWrapped = wmlObjectFactory.createRInstrText(text);
r.getContent().add( textWrapped);
text.setValue( instrText);
text.setSpace( "preserve");
// Create object for fldChar (wrapped in JAXBElement)
fldchar = wmlObjectFactory.createFldChar();
fldcharWrapped = wmlObjectFactory.createRFldChar(fldchar);
r.getContent().add( fldcharWrapped);
fldchar.setFldCharType(org.docx4j.wml.STFldCharType.END);
}
/**
* Create image, without specifying width
*/
public static P newImage(WordprocessingMLPackage wordMLPackage, String url, String filenameHint, String altText, int id1, int id2) throws Exception {
BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createLinkedImagePart(wordMLPackage, new URL(url));
Inline inline = imagePart.createImageInline(filenameHint, altText, id1, id2, true);
// Now add the inline in w:p/w:r/w:drawing
org.docx4j.wml.ObjectFactory factory = Context.getWmlObjectFactory();
P p = factory.createP();
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;
}
}
公共类字段includepicture{
公共静态void main(字符串[]args)引发异常{
布尔保存=真;
WordprocessingMLPackage wordMLPackage=WordprocessingMLPackage.createPackage();
MainDocumentPart mdp=wordMLPackage.getMainDocumentPart();
//示例1:以标题样式添加文本
addStyledParagraphOfText(“Title”,“全选,然后在Word中点击F9以查看图片,或者先以编程方式添加图片”);
创建文本的段落(“简单字段:”);
字符串url=”http://www.springsoftware.co.uk/spring/images/sampledata/itcg.png";
P=新的P();
//p.getContent().add(
//createSimpleField(“INCLUDEPICTURE\”file:///C:/Users/jharrop/git/plutext/docx4j/src/test/resources/images/greentick.png\“\\*合并格式”)
// );
//mdp.getContent().add(p);
mdp.createParagraphOfText(“复杂字段:”);
//addComplexField(p,“INCLUDEPICTURE\\\\+url+”\\+“\\*MERGEFORMAT\\d”);
addComplexField(p,“INCLUDEPICTURE\\\\\+url+”\\\\\*MERGEFORMAT\\d”);
mdp.getContent().add(p);
//addComplexField(p,“INCLUDEPICTURE\\”“+attachmentRequestURL+”\”“+“\\\*合并格式\\d”);
//打印主文档部分
System.out.println(
marshaltoString(mdp.getJaxbElement(),true,true));
字符串filenameint=null;
字符串altText=null;
int id1=0;
int id2=1;
//图1:未指定宽度
p1=newImage(wordMLPackage,url,filenameint,altText,id1,id2);
addComplexField(p1,url);
//可以选择保存它
如果(保存){
字符串文件名=System.getProperty(“user.dir”)+“/OUT\u FieldINCLUDEPICTURE.docx”;
保存(新的java.io.File(文件名));
System.out.println(“已保存”+文件名);
}
}
私有静态CTSimpleField createSimpleField(字符串值){
CTSimpleField=新的CTSimpleField();
字段setInstr(val);
返回字段;
}
私有静态void addComplexField(P,字符串instrText){
org.docx4j.wml.ObjectFactory wmlObjectFactory=Context.getWmlObjectFactory();
//为r创建对象
public class FieldINCLUDEPICTURE {
public static void main(String[] args) throws Exception {
boolean save = true;
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
MainDocumentPart mdp = wordMLPackage.getMainDocumentPart();
// Example 1: add text in Title style
mdp.addStyledParagraphOfText("Title", "Select all, then hit F9 in Word to see your pictures, or programmatically add them first");
mdp.createParagraphOfText("simple field:");
String url = "http://www.springsoftware.co.uk/spring/images/sampledata/itcg.png";
P p = new P();
// p.getContent().add(
// createSimpleField( " INCLUDEPICTURE \"file:///C:/Users/jharrop/git/plutext/docx4j/src/test/resources/images/greentick.png\" \\* MERGEFORMAT ")
// );
// mdp.getContent().add(p);
mdp.createParagraphOfText("complex field:");
//addComplexField(p, "INCLUDEPICTURE \"" + url + "\"" + " \\* MERGEFORMAT \\d");
addComplexField(p, " INCLUDEPICTURE \"" + url + "\" \\* MERGEFORMAT \\d");
mdp.getContent().add(p);
//addComplexField(p, " INCLUDEPICTURE \"" + attachmentRequestURL + "\"" + " \\* MERGEFORMAT \\d");
// Pretty print the main document part
System.out.println(
XmlUtils.marshaltoString(mdp.getJaxbElement(), true, true));
String filenameHint = null;
String altText = null;
int id1 = 0;
int id2 = 1;
// Image 1: no width specified
P p1 = newImage(wordMLPackage, url, filenameHint, altText, id1, id2);
addComplexField(p1,url);
// Optionally save it
if (save) {
String filename = System.getProperty("user.dir") + "/OUT_FieldINCLUDEPICTURE.docx";
wordMLPackage.save(new java.io.File(filename) );
System.out.println("Saved " + filename);
}
}
private static CTSimpleField createSimpleField(String val) {
CTSimpleField field = new CTSimpleField();
field.setInstr(val);
return field;
}
private static void addComplexField(P p, String instrText) {
org.docx4j.wml.ObjectFactory wmlObjectFactory = Context.getWmlObjectFactory();
// Create object for r
R r = wmlObjectFactory.createR();
p.getContent().add( r);
// Create object for fldChar (wrapped in JAXBElement)
FldChar fldchar = wmlObjectFactory.createFldChar();
JAXBElement<org.docx4j.wml.FldChar> fldcharWrapped = wmlObjectFactory.createRFldChar(fldchar);
r.getContent().add( fldcharWrapped);
fldchar.setFldCharType(org.docx4j.wml.STFldCharType.BEGIN);
// Create object for instrText (wrapped in JAXBElement)
Text text = wmlObjectFactory.createText();
JAXBElement<org.docx4j.wml.Text> textWrapped = wmlObjectFactory.createRInstrText(text);
r.getContent().add( textWrapped);
text.setValue( instrText);
text.setSpace( "preserve");
// Create object for fldChar (wrapped in JAXBElement)
fldchar = wmlObjectFactory.createFldChar();
fldcharWrapped = wmlObjectFactory.createRFldChar(fldchar);
r.getContent().add( fldcharWrapped);
fldchar.setFldCharType(org.docx4j.wml.STFldCharType.END);
}
/**
* Create image, without specifying width
*/
public static P newImage(WordprocessingMLPackage wordMLPackage, String url, String filenameHint, String altText, int id1, int id2) throws Exception {
BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createLinkedImagePart(wordMLPackage, new URL(url));
Inline inline = imagePart.createImageInline(filenameHint, altText, id1, id2, true);
// Now add the inline in w:p/w:r/w:drawing
org.docx4j.wml.ObjectFactory factory = Context.getWmlObjectFactory();
P p = factory.createP();
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;
}
}