我正在尝试使用Apache POI插入图片标题,但我对它没有任何想法。我正在使用
doc.createParagraph().createRun().addPicture(input,
Document.PICTURE_TYPE_PNG, "picture.png",
Units.pixelToEMU(603), Units.pixelToEMU(226));
插入图片,现在我想为它添加标题,以便能够创建图表。 我尝试过使用一些风格 paragraph.setStyle("传奇&#34); 但它只改变了我不能使用" Header1" (我有模板)。我已经查看了styles.xml(在我的模板中,我使用word插入了图片标题)并且有
-<w:style w:type="paragraph" w:styleId="Legenda">
<w:name w:val="caption"/>
<w:basedOn w:val="Normalny"/>
<w:next w:val="Normalny"/>
<w:unhideWhenUsed/>
<w:qFormat/>
所以看起来我只需要设置段落w:name w:val as&#34; caption&#34;。我对吗?我怎么能达到它?
答案 0 :(得分:4)
Word
中inserting a table of figures如何运作应该知道什么:
当我们add captions时,每个标题段落都包含一个序列字段{SEQ figure \* ARABIC}
。名称“figure”是序列字段的名称。然后,如果要创建一个图表,Word
会收集包含这样一个字段的所有段落,以构建这些图表。
到目前为止(apache poi
3.17版)并没有为数字添加字幕,也没有插入或创建数字表格,至少据我所知。所以我们必须使用低级别CTSimpleField来自己完成。
示例:
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.poi.xwpf.usermodel.*;
import org.apache.poi.util.Units;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSimpleField;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STOnOff;
public class CreateWordTableOfFigures {
public static void main(String[] args) throws Exception {
XWPFDocument document = new XWPFDocument();
XWPFParagraph paragraph;
XWPFRun run;
InputStream in;
CTSimpleField seq;
paragraph = document.createParagraph();
run = paragraph.createRun();
run.setText("Document containing figures");
paragraph = document.createParagraph();
run = paragraph.createRun();
run.setText("Lorem ipsum...");
//create paragraph containing figure
paragraph = document.createParagraph();
paragraph.setSpacingAfter(0); //Set spacing after to 0. So caption will follow immediately under the figure.
run = paragraph.createRun();
in = new FileInputStream("samplePict1.png");
run.addPicture(in, Document.PICTURE_TYPE_PNG, "samplePict1.png", Units.toEMU(150), Units.toEMU(100));
in.close();
paragraph = document.createParagraph(); //caption for figure
run = paragraph.createRun();
run.setText("Picture ");
seq = paragraph.getCTP().addNewFldSimple();
seq.setInstr("SEQ figure \\* ARABIC"); //This field is important for creating the table of figures then.
run = paragraph.createRun();
run.setText(": Description of sample picture 1");
paragraph = document.createParagraph();
run = paragraph.createRun();
run.setText("Lorem ipsum...");
paragraph = document.createParagraph();
paragraph.setSpacingAfter(0);
run = paragraph.createRun();
in = new FileInputStream("samplePict2.png");
run.addPicture(in, Document.PICTURE_TYPE_PNG, "samplePict1.png", Units.toEMU(150), Units.toEMU(100));
in.close();
paragraph = document.createParagraph();
run = paragraph.createRun();
run.setText("Picture ");
seq = paragraph.getCTP().addNewFldSimple();
seq.setInstr("SEQ figure \\* ARABIC");
run = paragraph.createRun();
run.setText(": Description of sample picture 2");
paragraph = document.createParagraph();
run = paragraph.createRun();
run.setText("Index of figures:");
//Create table of figures field. Word will updating that field while opening the file.
paragraph = document.createParagraph();
CTSimpleField toc = paragraph.getCTP().addNewFldSimple();
toc.setInstr("TOC \\c \"figure\" \\* MERGEFORMAT");
toc.setDirty(STOnOff.TRUE); //set dirty to forcing update
FileOutputStream out = new FileOutputStream("CreateWordTableOfFigures.docx");
document.write(out);
document.close();
}
}
注意,插入的字段{TOC \c "figure" \* MERGEFORMAT}
因为设置为脏而只强制在Word
中打开文件时更新该字段。使用apache poi
创建整个数据表而不强制Word
这样做会更加努力。
答案 1 :(得分:0)
更进一步,我需要按照标题排序来设置字幕图序列。因此对于header1 / header2,我需要标题为1.1.1,1.1.2等。然后添加另一个header2,它们将在1.2.1,1.2.2重新启动。这是代码。它也不要求你创建一个目录作为Richter的答案(我感谢他指出了我正确的方向)。
public void addCaption(String text) {
paragraph = doc.createParagraph();
paragraph.setStyle(STYLE_CAPTION);
paragraph.setSpacingAfter(0); // Set spacing after to 0. So caption will follow immediately under the figure.
run = paragraph.createRun();
run.setText("Figure ");
CTR ctr;
CTRPr ctrpr;
CTText ctText;
CTSimpleField seq = getParagraph().getCTP().addNewFldSimple();
String figureText1 = " STYLEREF 2 \\s ";
seq.setInstr(figureText1);
ctr = seq.addNewR();
ctrpr = ctr.addNewRPr();
ctrpr.addNewNoProof();
ctText = ctr.addNewT();
ctText.setStringValue("1");
ctr = paragraph.getCTP().addNewR();
ctr.addNewNoBreakHyphen();
seq = getParagraph().getCTP().addNewFldSimple();
String figureText = " SEQ Figure \\* ARABIC \\s 2 ";
seq.setInstr(figureText);
ctr = seq.addNewR();
ctrpr = ctr.addNewRPr();
ctrpr.addNewNoProof();
ctText = ctr.addNewT();
ctText.setStringValue("1");
run = paragraph.createRun(); // needed or numbers are at end, ie Figure : some text1.1
run.setText(": " + text);
}
答案 2 :(得分:0)
正确...因此,在属于@AxelRichter代码生成的文档的文档xml中,我已经手动插入了一张表格。生成的xml提取结果显示,ms word生成的文本略有不同...
axel的结果:
<w:p w:rsidR="009B13D9" w:rsidRDefault="009B13D9">
<w:pPr>
<w:pStyle w:val="TableofFigures"/>
<w:tabs>
<w:tab w:val="right" w:leader="dot" w:pos="9350"/>
</w:tabs>
<w:rPr>
<w:noProof/>
</w:rPr>
</w:pPr>
<w:r>
<w:rPr>
<w:noProof/>
</w:rPr>
<w:t>Picture 2: Description of sample picture 2</w:t>
</w:r>
<w:r>
<w:rPr>
<w:noProof/>
</w:rPr>
<w:tab/>
</w:r>
<w:r>
<w:rPr>
<w:noProof/>
</w:rPr>
<w:fldChar w:fldCharType="begin"/>
</w:r>
<w:r>
<w:rPr>
<w:noProof/>
</w:rPr>
<w:instrText xml:space="preserve"> PAGEREF _Toc521591239 \h </w:instrText>
</w:r>
<w:r>
<w:rPr>
<w:noProof/>
</w:rPr>
</w:r>
<w:r>
<w:rPr>
<w:noProof/>
</w:rPr>
<w:fldChar w:fldCharType="separate"/>
</w:r>
<w:r>
<w:rPr>
<w:noProof/>
</w:rPr>
<w:t>1</w:t>
</w:r>
<w:r>
<w:rPr>
<w:noProof/>
</w:rPr>
<w:fldChar w:fldCharType="end"/>
</w:r>
</w:p>
手动插入结果
<w:p w:rsidR="009B13D9" w:rsidRDefault="009B13D9">
<w:pPr>
<w:pStyle w:val="TableofFigures"/>
<w:tabs>
<w:tab w:val="right" w:leader="dot" w:pos="9350"/>
</w:tabs>
<w:rPr>
<w:noProof/>
</w:rPr>
</w:pPr>
<w:hyperlink w:anchor="_Toc521591266" w:history="1">
<w:r w:rsidRPr="003A5F7D">
<w:rPr>
<w:rStyle w:val="Hyperlink"/>
<w:noProof/>
</w:rPr>
<w:t>Picture 2: Description</w:t>
</w:r>
<w:bookmarkStart w:id="4" w:name="_GoBack"/>
<w:bookmarkEnd w:id="4"/>
<w:r w:rsidRPr="003A5F7D">
<w:rPr>
<w:rStyle w:val="Hyperlink"/>
<w:noProof/>
</w:rPr>
<w:t xml:space="preserve"> of sample picture 2</w:t>
</w:r>
<w:r>
<w:rPr>
<w:noProof/>
<w:webHidden/>
</w:rPr>
<w:tab/>
</w:r>
<w:r>
<w:rPr>
<w:noProof/>
<w:webHidden/>
</w:rPr>
<w:fldChar w:fldCharType="begin"/>
</w:r>
<w:r>
<w:rPr>
<w:noProof/>
<w:webHidden/>
</w:rPr>
<w:instrText xml:space="preserve"> PAGEREF _Toc521591266 \h </w:instrText>
</w:r>
<w:r>
<w:rPr>
<w:noProof/>
<w:webHidden/>
</w:rPr>
</w:r>
<w:r>
<w:rPr>
<w:noProof/>
<w:webHidden/>
</w:rPr>
<w:fldChar w:fldCharType="separate"/>
</w:r>
<w:r>
<w:rPr>
<w:noProof/>
<w:webHidden/>
</w:rPr>
<w:t>1</w:t>
</w:r>
<w:r>
<w:rPr>
<w:noProof/>
<w:webHidden/>
</w:rPr>
<w:fldChar w:fldCharType="end"/>
</w:r>
</w:hyperlink>
</w:p>
略过这件事,似乎应该将运行包裹在一个超链接对象中。 ...并且有一些细微的差别只是为了使事情变得有趣。