标签:标签 element 保留 complete bst blank 接下来 嵌入 leader
原文标题:Adding images and layout to your Docx4j-generated word documents, part 1
原文链接:http://blog.iprofs.nl/2012/10/22/adding-images-and-layout-to-your-docx4j-generated-word-documents-part-1/
原文作者:lvdpal
发表日期:2012年10月22日
注:由于我对docx4j也不是很熟悉,所以很多专业名词不会翻译,如果文章内容使您感到困惑,请移步到原文。
在前一篇博客,我介绍了如何在docx文档中生成表格。这篇博客中我会展示一些关于图片、分页符和目录表的示例:
添加图片
本示例我们向新创建的word文档添加一张嵌入式图片。这并非很难,因为Docx4j示例包含了几种做这件事的方法。但是有几个问题我不是特别地清楚,所以我稍微重构了那个例子改为只添加一个内联图片并且添加了一些注释来解释发生了什么。
- public class AddingAnInlineImage {
-
- public static void main (String[] args) throws Exception {
- WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
-
- File file = new File("src/main/resources/iProfsLogo.png");
- byte[] bytes = convertImageToByteArray(file);
- addImageToPackage(wordMLPackage, bytes);
-
- wordMLPackage.save(new java.io.File("src/main/files/HelloWord7.docx"));
- }
-
-
- private static void addImageToPackage(WordprocessingMLPackage wordMLPackage,
- byte[] bytes) throws Exception {
- BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wordMLPackage, bytes);
-
- int docPrId = 1;
- int cNvPrId = 2;
- Inline inline = imagePart.createImageInline("Filename hint","Alternative text", docPrId, cNvPrId, false);
-
- P paragraph = addInlineImageToParagraph(inline);
-
- wordMLPackage.getMainDocumentPart().addObject(paragraph);
- }
-
-
- private static P addInlineImageToParagraph(Inline inline) {
-
- ObjectFactory factory = new ObjectFactory();
- P paragraph = factory.createP();
- R run = factory.createR();
- paragraph.getContent().add(run);
- Drawing drawing = factory.createDrawing();
- run.getContent().add(drawing);
- drawing.getAnchorOrInline().add(inline);
- return paragraph;
- }
-
-
- private static byte[] convertImageToByteArray(File file)
- throws FileNotFoundException, IOException {
- InputStream is = new FileInputStream(file );
- long length = file.length();
-
- if (length > Integer.MAX_VALUE) {
- System.out.println("File too large!!");
- }
- byte[] bytes = new byte[(int)length];
- int offset = 0;
- int numRead = 0;
- while (offset < bytes.length && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
- offset += numRead;
- }
-
- if (offset < bytes.length) {
- System.out.println("Could not completely read file " +file.getName());
- }
- is.close();
- return bytes;
- }
- }
在表格中添加图片
现在我们知道了如何在文档中添加图片,如果在我们前面博客中的表格中添加图片将会使表格更漂亮。正如你将在下面看到的代码一样,并不比我们前面例子中所做的困难很多。
- public class AddingAnInlineImageToTable {
- private static WordprocessingMLPackage wordMLPackage;
- private static ObjectFactory factory;
-
-
- public static void main (String[] args) throws Exception {
- wordMLPackage = WordprocessingMLPackage.createPackage();
- factory = Context.getWmlObjectFactory();
-
- Tbl table = factory.createTbl();
- addBorders(table);
-
- Tr tr = factory.createTr();
-
- P paragraphOfText = wordMLPackage.getMainDocumentPart().createParagraphOfText("Field 1");
- addTableCell(tr, paragraphOfText);
-
- File file = new File("src/main/resources/iProfsLogo.png");
- P paragraphWithImage = addInlineImageToParagraph(createInlineImage(file));
- addTableCell(tr, paragraphWithImage);
-
- table.getContent().add(tr);
-
- wordMLPackage.getMainDocumentPart().addObject(table);
- wordMLPackage.save(new java.io.File("src/main/files/HelloWord8.docx"));
- }
-
-
- private static void addTableCell(Tr tr, P paragraph) {
- Tc tc1 = factory.createTc();
- tc1.getContent().add(paragraph);
- tr.getContent().add(tc1);
- }
-
-
- private static P addInlineImageToParagraph(Inline inline) {
-
- ObjectFactory factory = new ObjectFactory();
- P paragraph = factory.createP();
- R run = factory.createR();
- paragraph.getContent().add(run);
- Drawing drawing = factory.createDrawing();
- run.getContent().add(drawing);
- drawing.getAnchorOrInline().add(inline);
- return paragraph;
- }
-
-
- private static Inline createInlineImage(File file) throws Exception {
- byte[] bytes = convertImageToByteArray(file);
-
- BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wordMLPackage, bytes);
-
- int docPrId = 1;
- int cNvPrId = 2;
-
- return imagePart.createImageInline("Filename hint", "Alternative text", docPrId, cNvPrId, false);
- }
-
-
- private static byte[] convertImageToByteArray(File file) throws FileNotFoundException, IOException {
- InputStream is = new FileInputStream(file );
- long length = file.length();
-
- if (length > Integer.MAX_VALUE) {
- System.out.println("File too large!!");
- }
- byte[] bytes = new byte[(int)length];
- int offset = 0;
- int numRead = 0;
- while (offset < bytes.length && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
- offset += numRead;
- }
-
- if (offset < bytes.length) {
- System.out.println("Could not completely read file "+file.getName());
- }
- is.close();
- return bytes;
- }
-
-
- private static void addBorders(Tbl table) {
- table.setTblPr(new TblPr());
- CTBorder border = new CTBorder();
- border.setColor("auto");
- border.setSz(new BigInteger("4"));
- border.setSpace(new BigInteger("0"));
- border.setVal(STBorder.SINGLE);
-
- TblBorders borders = new TblBorders();
- borders.setBottom(border);
- borders.setLeft(border);
- borders.setRight(border);
- borders.setTop(border);
- borders.setInsideH(border);
- borders.setInsideV(border);
- table.getTblPr().setTblBorders(borders);
- }
- }
添加换页符
添加换页符相当地简单。Docx4j拥有一个叫作Br的break对象,这个对象有一个type属性,这种情况下我们需要将其设置为page,type其它可选的值为column和textWrapping。这个break可以很简单地添加到段落中。
- public class AddingAPageBreak {
- private static ObjectFactory factory;
- private static WordprocessingMLPackage wordMLPackage;
-
- public static void main (String[] args) throws Docx4JException {
- wordMLPackage = WordprocessingMLPackage.createPackage();
- factory = Context.getWmlObjectFactory();
-
- wordMLPackage.getMainDocumentPart().addParagraphOfText("Hello Word!");
-
- addPageBreak();
-
- wordMLPackage.getMainDocumentPart().addParagraphOfText("This is page 2!");
- wordMLPackage.save(new java.io.File("src/main/files/HelloWord11.docx") );
- }
-
-
- private static void addPageBreak() {
- MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
-
- Br breakObj = new Br();
- breakObj.setType(STBrType.PAGE);
-
- P paragraph = factory.createP();
- paragraph.getContent().add(breakObj);
- documentPart.getJaxbElement().getBody().getContent().add(paragraph);
- }
- }
添加目录表
添加目录十分地简单。在Word中这是一个可以被添加的域,在Docx4j中是一样的。仅有一个你需要知道的小问题,就是像这样创建的一个文档,在你第一次打开它的时候,Word会给你一个此文档可能包含指向其它文件的域的提示信息,询问你是否要更新这些域。如果你点击了yes回应询问,Word将会将目录表域转换为真正的目录。如果你点击了no,你会看到这个文档没有目录。
在点击yes之后,这个文档会变成这个样子:
总结
在本篇博客中我展示了如何添加图片和word文档中布局的两个方面。在我的下一篇博客我会继续介绍更多布局相关的示例,我本来是将它们添加到了本篇博客中,但是博客有点太长了。
向Docx4j生成的word文档添加图片和布局--第一部分
标签:标签 element 保留 complete bst blank 接下来 嵌入 leader
原文地址:http://www.cnblogs.com/cxxjohnson/p/7899256.html