0

I want to save the contents of a JTextPane to a word file. I don't have a problem saving but I can't currently keep some style options such as paragraph styles.

I use these libraries:

import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;

Lines of code;

System.out.println("Kaydete basıldı");
        String text = textPane.getText();
        lblNewLabel.setText(text);

        XWPFDocument document = new XWPFDocument();
        XWPFParagraph paragraph = document.createParagraph();
        XWPFRun run = paragraph.createRun();
        run.setText(text);

        try {

            FileOutputStream dosyaCikis = new FileOutputStream(
                    "sercan.docx");
            document.write(dosyaCikis);
            dosyaCikis.close();
        } catch (Exception e2) {
            e2.printStackTrace();
        }

Apache POI or another way, it does not matter, I am waiting for your help.

Charles Goodwin
  • 6,402
  • 3
  • 34
  • 63
  • 1
    Your question is confusing. Are you trying to simply copy text from a `JTextPane`, or style something in the Word document to look like a `JTextPane`? – Justin Skiles Apr 24 '14 at 12:38

3 Answers3

1

This example shows how to set various style options:(Apache POI)

SimpleDocument

Example code form the link:

   XWPFDocument doc = new XWPFDocument();
   XWPFParagraph p1 = doc.createParagraph();

        p1.setAlignment(ParagraphAlignment.CENTER);
        p1.setBorderBottom(Borders.DOUBLE);
        p1.setBorderTop(Borders.DOUBLE);    
        p1.setBorderRight(Borders.DOUBLE);
        p1.setBorderLeft(Borders.DOUBLE);
        p1.setBorderBetween(Borders.SINGLE);    
        p1.setVerticalAlignment(TextAlignment.TOP);

   XWPFRun r1 = p1.createRun();

        r1.setBold(true);
        r1.setText("The quick brown fox");
        r1.setBold(true);
        r1.setFontFamily("Courier");
        r1.setUnderline(UnderlinePatterns.DOT_DOT_DASH);
        r1.setTextPosition(100);

Other examples(styles,images .etc) can be found here:

Example Package

boxed__l
  • 1,334
  • 1
  • 10
  • 24
0

AFAIK the options for writing Word files are limited from standard Java libraries.

You probably want to use a tool that explicitly supports Word formats - the best bet is probably LibreOffice, which is Free software. The LibreOffice API supports Java and other languages.

For a fuller explanation look here:

However that answer refers to OpenOffice, of which LibreOffice is a more actively developed fork due to management issues over the years.

Community
  • 1
  • 1
Charles Goodwin
  • 6,402
  • 3
  • 34
  • 63
  • The OP mentioned he is using Apache POI, which works just fine. – Justin Skiles Apr 24 '14 at 12:38
  • Ah, right you are, I missed that. I obviously didn't read the question thoroughly albeit I'd suggest the imports could have been more obviously stated - excuses excuses! (EDIT: so I improved the question... the power of edits!) – Charles Goodwin Apr 24 '14 at 15:25
0

You could try docx_editor_kit. From the web page:

it can open docx file and reflect the content in JEditorPane (or JTextPane). Also user can create styled content and store the content in docx format.

Somewhat related is my docx4all, but it hasn't been updated recently, and it may be overkill for your purposes.

Both of these use docx4j (as opposed to POI).

JasonPlutext
  • 15,352
  • 4
  • 44
  • 84