使用Java替换MS Word Templete(Docx)中的文本

时间:2018-08-27 07:35:49

标签: java ms-word apache-poi docx

我正在尝试在docx中搜索字符串,并使用java apache poi替换为其他文本,但是它是随机替换的 在第

行中作为arrayIndexoutofbound异常获取错误

“声明名称空间w ='http://schemas.openxmlformats.org/wordprocessingml/2006/main'.// w:ffData / w:name / @ w:val”)[0];

public class WordReplaceTextInFormFields {

private static void replaceFormFieldText(XWPFDocument document, String ffname, String text) {
    boolean foundformfield = false;
    for (XWPFParagraph paragraph : document.getParagraphs()) {
        for (XWPFRun run : paragraph.getRuns()) {
            XmlCursor cursor = run.getCTR().newCursor();
            cursor.selectPath(
                    "declare namespace w='http://schemas.openxmlformats.org/wordprocessingml/2006/main' .//w:fldChar/@w:fldCharType");
            while (cursor.hasNextSelection()) {
                cursor.toNextSelection();
                XmlObject obj = cursor.getObject();
                if ("begin".equals(((SimpleValue) obj).getStringValue())) {
                    cursor.toParent();
                    obj = cursor.getObject();
                    obj = obj.selectPath(
                            "declare namespace w='http://schemas.openxmlformats.org/wordprocessingml/2006/main' .//w:ffData/w:name/@w:val")[0];
                    if (ffname.equals(((SimpleValue) obj).getStringValue())) {
                        foundformfield = true;
                    } else {
                        foundformfield = false;
                    }
                } else if ("end".equals(((SimpleValue) obj).getStringValue())) {
                    if (foundformfield)
                        return;
                    foundformfield = false;
                }
            }
            if (foundformfield && run.getCTR().getTList().size() > 0) {
                run.getCTR().getTList().get(0).setStringValue(text);
                // System.out.println(run.getCTR());
            }
        }
    }
}

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

    XWPFDocument document = new XWPFDocument(new FileInputStream("WordTemplate.docx"));

    replaceFormFieldText(document, "Text1", "Моя Компания");
    replaceFormFieldText(document, "Text2", "Аксель Джоачимович Рихтер");
    replaceFormFieldText(document, "Text3", "Доверенность");

    document.write(new FileOutputStream("WordReplaceTextInFormFields.docx"));
    document.close();
}
}

它遗漏了一些字符串,不能替换整个文档。请提供示例代码帮助

1 个答案:

答案 0 :(得分:0)

我在https://github.com/centic9/poi-mail-merge的项目中进行了类似的操作,该项目提供了基于POI的常规邮件合并功能。它使用的功能与XmlBeans略有不同,该功能可以替换文档完整XML内容中的字符串,而不是分别替换每个段落。

#include <opencv2/highgui.hpp>
#include <iostream>
#include <opencv2/ximgproc/slic.hpp>
#include <opencv2/ml.hpp>
#include <opencv2/imgproc.hpp>

using namespace cv;
using namespace cv::ml;
using namespace std;


void observe_labels_and_means(const Mat& labels, const Mat& means, int h, int w){

int dimension = 3;

Mat rgb_image(h, w, CV_8UC3);
MatIterator_<Vec3b> rgb_first = rgb_image.begin<Vec3b>();
MatIterator_<Vec3b> rgb_last = rgb_image.end<Vec3b>();
MatConstIterator_<int> label_first = labels.begin<int>();

Mat means_u8;
means.convertTo(means_u8, CV_8UC1, 255.0);
Mat means_u8c3 = means_u8.reshape(dimension);

while(rgb_first != rgb_last){
    const Vec3b& rgb = means_u8c3.ptr<Vec3b>(*label_first)[0];
    *rgb_first = rgb;
    ++rgb_first;
    ++label_first;  
}

imshow("tmp", rgb_image);
waitKey();
}


int main(int argc, char** argv) {

Mat image = imread("Teddy_L.png");
const int image_rows = image.rows;
const int image_cols = image.cols;
int dimension = 3;

//VAR SUPERPIXEL
Mat labels, contour, mask;
int number_sp;
Ptr<cv::ximgproc::SuperpixelSLIC> slic = cv::ximgproc::createSuperpixelSLIC(image);

//SLIC
slic->iterate();
slic->getLabels(labels);
number_sp = slic->getNumberOfSuperpixels();

//TRY ON A SINGLE SUPERPIXEL
mask = (labels==30);
Mat temp(image_rows, image_cols, CV_64FC4);
image.copyTo(temp, mask);
imshow("superpixel", temp);

//INPUT FOR TRAINING
Mat reshaped_temp = temp.reshape(1, image_cols*image_rows);
Mat samples;
reshaped_temp.convertTo(samples, CV_64FC1, 1.0/255.0);
Mat labels_em, probs, log_likelihoods;

//EM
Ptr<EM> em_model = EM::create();
em_model->setClustersNumber(2);
em_model->setCovarianceMatrixType(EM::COV_MAT_DIAGONAL);
    em_model->setTermCriteria(TermCriteria(TermCriteria::COUNT+TermCriteria::EPS,        EM::DEFAULT_MAX_ITERS, 1e-6));

//TRAINING
em_model->trainEM(samples, log_likelihoods, labels, probs);
Mat means = em_model->getMeans();

//RESULT
observe_labels_and_means(labels, means, image_rows, image_cols);

  waitKey();
  return 0;
}

请参见line 132 in MailMerge.java