如何创建一个模板或现有文档与Java的Word文档? [英] how create a Word document from a template or existing document with Java?

查看:256
本文介绍了如何创建一个模板或现有文档与Java的Word文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文档模板,其中某些字段是静态的,人是动态的。我需要更换一些数据(名字,姓氏,薪水),并生成新文件。你推荐什么库来做到这一点?是POI合适?
我与春天的Java EE6和Oracle合作。

I have a document template where some fields are static and others are dynamic. I need to replace some data (name, last name, salary) and generate the new file. What library do you recommend to do this? Is POI appropriate? I am working with Spring, Java EE6 and Oracle.

推荐答案

您可以给Apache的POI的尝试,但这些都要求处理Word文件都非常复杂使用POI的HWPF和XWPF部分 - 你需要有在至少有很好的理解一个字的文件是如何构成的!

You can give Apache POI a try but the HWPF and XWPF part of POI which are required to manipulate word files are really complicated to use - you need to have at least a good understanding how a word file is structured!

利用iText和PDF的解决方案

Solution using iText and PDF

我与PDF(这可能是你的一个选项)

I did something similar with PDF (this might be an option for you)

1)可以使用的LibreOffice文档中创建字段(如在的Acrobat Pro)

1) You can use LibreOffice to create fields in the document (like in Acrobat Pro)


  • 创建一个文件的.odt和风格它

  • 或使用MS Word或LibreOffice的作家你的模板转换成它

  • 然后去查看 - > Toolsbars - >表单设计,并设置设计模式开/关

  • 现在,您可以字段添加到您的文件(双击它会打开字段属性)

  • 当你完成:文件 - >导出为PDF

2),现在你可以使用的iText填写创建的字段

2) Now you can use iText to fill in the created fields

下面就是例子code:

The following is just example code:

    public byte[] getDocumentAsByteArray(Object dataBean, String pdfTemplateName) throws KkmsException {

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    PdfStamper stamp = null;
    InputStream templateInputStream = null;

    Locale local = new Locale(language);

    try {
        templateInputStream = // get the file input stream of the pdf
        PdfReader reader = new PdfReader(templateInputStream);

        // Create a stamper that will copy the document to a new file
        stamp = new PdfStamper(reader, outputStream);

        AcroFields form = stamp.getAcroFields();

        // form fields are normal text in the end
        stamp.setFormFlattening(true);
        Map<String, AcroFields.Item> map = (Map<String, AcroFields.Item>)form.getFields();
        if (map != null) {
            if (map.size() == 0) {
                logger.debug("There are no fields in this PDF layout");
            }
            for (Entry<String, AcroFields.Item> e : map.entrySet()) {
                logger.debug("PDF fieldname = " + e.getKey());

                // at the moment we only handle text fields
                if (AcroFields.FIELD_TYPE_TEXT == form.getFieldType(e.getKey())) {
                    fillForm(dataBean, form, e.getKey(), local);
                } else {
                    logger.warn("Field type is not supported: "+form.getFieldType(e.getKey()));
                }
            }
        }

        stamp.close();
    } catch (Exception e) {
        logger.warn("Failed to create PDF document", e);
        throw new KkmsException("Failed to create PDF document: "+e.getMessage());
    } finally {
        if (templateInputStream != null) {
            try {
                templateInputStream.close();
            } catch (IOException e) {
                throw new KkmsException("Failed to close InputStream of PDF document", e);
            }
        }
    }
    return outputStream.toByteArray();
}

在你得到一个PDF到底! - >希望这可以帮助你至少有点

In the end you get a PDF -> hope this helps you at least a little bit!

另一种快速和肮脏的解决方案

可能是使用ODT电源或DOCX - >转换您的文档,以DOCX或ODT - >它只是一个zip文件 - >所以将它解压缩 - >你会看到一个文件的content.xml在zip的根 - >还有就是在那里的所有文档内容
现在,你可以添加一些魔法标记(例如$$$)在这里其可之后你的程序来代替

Could be to use the power of odt or docx -> convert your doc to docx or odt -> its just a zip file -> so unzip it -> you will see a content.xml file in the root of the zip -> there is all the document content in there Now you could add some magic tags (e.g. $$$) here which can later be replaced by your program

<text:p text:style-name="P3">SAP Customer Number:</text:p>

<text:p text:style-name="P3">SAP Customer Number: $$$sapCustomerNumber$$$</text:p>

现在创建一个程序,解压的ODT / DOCX文件 - >替换标签 - >再次呼啸而过文件

Now create a program which unzips the odt/docx file -> replaces the tags -> zips the file again

这篇关于如何创建一个模板或现有文档与Java的Word文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆