【问题标题】:How to replace variables in the header and in tables with docx4j?如何用 docx4j 替换标题和表格中的变量?
【发布时间】:2015-10-01 08:41:43
【问题描述】:

我正在尝试替换文档标题和表格中的变量,但我不知道如何继续。我设法替换了文档正文中的变量,但此方法(使用 ${})不适用于标题和表格。

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.List;

import org.docx4j.XmlUtils;
import org.docx4j.customxml.ObjectFactory;
import org.docx4j.dml.wordprocessingDrawing.Inline;
import org.docx4j.jaxb.Context;
import org.docx4j.model.datastorage.migration.VariablePrepare;
import org.docx4j.model.structure.HeaderFooterPolicy;
import org.docx4j.model.structure.SectionWrapper;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.CustomXmlDataStoragePart;
import org.docx4j.openpackaging.parts.Part;
import org.docx4j.openpackaging.parts.PartName;
import org.docx4j.openpackaging.parts.Parts;
import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage;
import org.docx4j.openpackaging.parts.WordprocessingML.HeaderPart;
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
import org.docx4j.utils.BufferUtil;
import org.docx4j.wml.Hdr;
import org.docx4j.wml.HdrFtrRef;
import org.docx4j.wml.HeaderReference;

import java.util.Locale;

import javax.xml.bind.JAXBElement;

import java.text.DateFormat;
import org.docx4j.openpackaging.parts.WordprocessingML.HeaderPart;
import org.docx4j.wml.HdrFtrRef;

public class EditInvoice {

    private static WordprocessingMLPackage  template;
    private static ObjectFactory factory;

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

        boolean save = true;
        String outputfilepath = System.getProperty("user.dir")+ "/InvoiceEdited.docx";

        java.util.Date uDate = new java.util.Date();
        java.sql.Date sDate = new java.sql.Date(System.currentTimeMillis());
        sDate = new java.sql.Date(uDate.getTime());
        uDate = new java.util.Date(sDate.getTime());
        Locale locale = Locale.getDefault();
        DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.FULL, locale);

        //System.out.println(dateFormat.format(sDate));


        template = WordprocessingMLPackage.load(new FileInputStream(new File("invoice_template_sample.docx")));
        VariablePrepare.prepare(template);

        List<SectionWrapper> sectionWrappers = template.getDocumentModel().getSections();

        MainDocumentPart documentPart = template.getMainDocumentPart();

        HashMap<String, String> variables = new HashMap<String, String>();

        // populate doc variables
        variables.put("Name", "John Doe");
        variables.put("Phone", "(123) 456 78 90");
        variables.put("CompanyName", "BSI Business Systems Integration AG");
        variables.put("Email", "john.doe@bsiag.com");
        variables.put("CompanyAddress", "Täfernstrasse 16a, 5405 Baden");
        variables.put("InvoiceNo", "No. 2013-007");
        variables.put("InvoiceDate", dateFormat.format(sDate));
        variables.put("BillingName", "Jane Smith");
        variables.put("PayableToName", "John Doe, BSI");
        variables.put("SubTotal", "$1,530.00");
        variables.put("SalesTax", "$229.50");
        variables.put("Shipping", "$250.00");
        variables.put("Total", "$2,009.50");

        // and content for embedded table
        Object[][] orderItems = new Object[][]{
            new Object[]{"1", "Table", "$800.00", "$800.00"},
            new Object[]{"4", "Chair", "$150.00", "$600.00"},
            new Object[]{"1", "Assembling", "$130.00", "$130.00"},
        };


        try
        {

            documentPart.variableReplace(variables);
            //documentPart.addObject(orderItems);

        }
        catch (Exception e)
        {
            System.out.println(e);
        }


        if (save) {
            template.save(new java.io.File(outputfilepath) );
        } else {
            System.out.println(XmlUtils.marshaltoString(documentPart.getContents(), true, true));
        }


    }
}

【问题讨论】:

  • 发布您的代码?没有理由它不应该在标题和表格中工作。
  • docx 是一个带有 contents.xml 的 zip。使用jar:file://... URL,可以使用java 中的zip 文件系统简单地将content.xml 替换为新的。

标签: java docx4j


【解决方案1】:

要替换标题中的变量,您需要对相关的标题部分进行变量替换。在这里,您只在主文档部分执行此操作。

关于表格,变量替换的东西不是为了复制行而设计的(例如,每个发票行项目一行)。换句话说,它不会插入行。因此,如果您没有更多代码,您的 Object[][] orderItems 将无能为力。

(相比之下,docx4j 的 XML 数据绑定确实可以使用 OpenDoPE od:repeat 处理)

【讨论】:

    猜你喜欢
    • 2013-06-10
    • 2021-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-13
    • 2014-09-12
    • 2012-12-30
    相关资源
    最近更新 更多