0

Can somebody help me to integrate some MS Word document to another. I can open, edit and save, but only with one MS Word document.

My simple code only creates, edits and saves .docx:

import java.io.FileOutputStream;
import org.apache.poi.xwpf.usermodel.*;

public class SimpleDocument {

public  void SimpleDocument() throws Exception {
    XWPFDocument doc = new XWPFDocument();

    XWPFParagraph p1 = doc.createParagraph();
    p1.setAlignment(ParagraphAlignment.CENTER);
    p1.setAlignment(ParagraphAlignment.LEFT);//setVerticalAlignment(TextAlignment.TOP);

    XWPFRun r1 = p1.createRun();
    r1.setBold(true);
    r1.setText("The quick brown fox");
    r1.setFontFamily("Courier");
    r1.setUnderline(UnderlinePatterns.DOT_DOT_DASH);

    XWPFParagraph p2 = doc.createParagraph();
    p2.setAlignment(ParagraphAlignment.RIGHT);

    XWPFRun r2 = p2.createRun();
    r2.setText("jumped over the lazy dog");

    FileOutputStream out = new FileOutputStream("C:/simple.docx");
    doc.write(out);
    out.close();

}
}

How to combine two pieces of formatted text (RANGE, PARAGRAPH) ?

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
user3090771
  • 9
  • 1
  • 2
  • Are you saying you want to take text from one document and place it into another? – Levenal Dec 11 '13 at 12:00
  • I want to take text from two documents and place it into one. I dont know how to combine formatted text in code. – user3090771 Dec 11 '13 at 12:19
  • What sort of 'formatting' are you on about? Is it just plain text from two different documents or is their something special about it? – Levenal Dec 11 '13 at 14:06
  • Formatting, like: text color, text font, text size, alignment, tables, headers, footers, images, etc. – user3090771 Dec 12 '13 at 05:09
  • I am trying to merge two pieces of formatted text without making it parsing – user3090771 Dec 12 '13 at 05:13
  • The only way I know of doing that would be to get the text from the documents and reformat it upon entering it into the new one, I am unaware of any way of keeping the formatting when extracting text. The other option is to pick the document with the least formatting, extract that and reformat it into the other doc, then save that as a new one. – Levenal Dec 12 '13 at 08:08
  • Have to take the input text without formatting(extract the text ), and continue to work with it.thanks for joining – user3090771 Dec 12 '13 at 13:05

1 Answers1

0

try the following code:

import java.io.*;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.*;

public class test {
    public static void main(String[] args) throws Exception {
        // POI apparently can't create a document from scratch,
        // so we need an existing empty dummy document
        HWPFDocument doc = new HWPFDocument(new FileInputStream("D:\\src.doc"));
        Range range = doc.getRange();
        CharacterRun run = range
                .insertAfter("Text After copied file contents!");
        run.setBold(true);
        OutputStream out = new FileOutputStream("D:\\result.doc");
        doc.write(out);
        out.flush();
        out.close();

    }
}