Creating Word File With JTextPane Style Option - java

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.

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

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:
What's a good Java API for creating Word documents?
However that answer refers to OpenOffice, of which LibreOffice is a more actively developed fork due to management issues over the years.

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).

Related

JAVA XWPFDocument script gives java.lang.Error

I am sorry, I am absolutely new to JAVA and no (until now) nothing about JAVA, and only little to coding. I wanted to learn simple document creation with the XWPFDocument Library. I got a sample code from https://www.geeksforgeeks.org/java-program-to-write-a-paragraph-in-a-word-document/ but the code does not work.
I am also sorry for not being able to format the code as a code. I made spaces with my hand, but not everywhere. I know it's horrible, but I don't know how to do to properly. A have a brand new Mac and a microsoft keyboard which drives me crazy + I also do not know how to do it the advice strg + k
I get the error:
at GFG.main(docgen_V0.1.java:17)
js#MacBook-Pro-von-Jonathan ~ % /usr/bin/env /Library/Java/JavaVirtualMachines/jdk-19.jdk/Contents/Home/bin/java --enable-preview -XX:+Show
CodeDetailsInExceptionMessages -cp /private/var/folders/fq/f_v1db_d17qc749gnnmrbch00000gn/T/vscodesws_2d1b0/jdt_ws/jdt.ls-java-project/bin G
FG
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
XWPFDocument cannot be resolved to a type
XWPFDocument cannot be resolved to a type
XWPFParagraph cannot be resolved to a type
XWPFRun cannot be resolved to a type
XWPFRun cannot be resolved to a type
XWPFRun cannot be resolved to a type
The Code is:
public class docgen_V0.2 {
// Java Programming to Write a paragraph in a Word Document
// Importing required packages
import java.io.File;
import java.io.FileOutputStream;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
public class GFG {
// Main driver method
public static void main(String[] args) throws Exception
{
// Create a blank document
XWPFDocument xwpfdocument = new XWPFDocument();
// Create a blank file at C:
File file = new File("/Users/Shared/Arbeit/Codes/Speachdoc/addParagraph.docx");
// Create a file output stream connection
FileOutputStream ostream
= new FileOutputStream(file);
/* Create a new paragraph using the document */
// CreateParagraph() method is used
// to instantiate a new paragraph
XWPFParagraph para = xwpfdocument.createParagraph();
// CreateRun method appends a new run to the
// paragraph created
XWPFRun xwpfrun = para.createRun();
// SetText sets the text to the run
// created using XWPF run
xwpfrun.setText(
"Geeks for Geeks is a computer science portal which aims "
+ "to provide all in one platform for learning and "
+ "practicing.We can learn multiple programming languages here. ");
// Write content set using XWPF classes available
xwpfdocument.write(ostream);
// Close connection
ostream.close();
}
}
}
I just tried to run it. I expect to create a document with a little paragraph that says:
" Geeks for Geeks is a computer science portal which aims to provide all in one platform for learning and practicing.We can learn multiple programming languages here."

JAVA - font path instead font name in docx

I am using Apache POI to generate .docx document. I added external fonts to my project. For example:
String playfairDisplayRegular = this.getClass().getClassLoader().getResource("PlayfairDisplay-Regular.ttf").getFile();
I used playfairDisplayRegular in paragraph. When I mark text in the document in the field with the name of the font is path for example:
/C:/Users/..../Documents...
instead of the font name (the font is working). Any ideas ?
Greetings, Artur
URL.getFile() just returns the file name part (+ optional query part ?...) of the URL.
For resources (files possibly inside a jar, residing on the class path) one should rather not use File, but use an InputStream, whenever possible.
With java.awt.Font:
Font font = Font.createFont(Font.TRUETYPE_FONT,
getClass().getResourceAsStream("/PlayfairDisplay-Regular.ttf"));
In the docx you can now use font.getFamily() (for XSLFTextRun.setFontFamily) and such.
Embedding fonts in the docx:
Meanwhile apache poi might be able to embed fonts (license issue for you!), but doing it yourself should be simple: .docx is a zip format, fonts are in a /fonts/ subdirectory. You can test it in a small docx written in MSWord. Writing the file can be done by a zip file system: "jar:file:/C:/... .docx", and Files.copy.
Using java.awt.Font will be problematic for me, bacause my syntax looks like this:
printParagraph(createParagraphWithAlignment(document, ParagraphAlignment.RIGHT),
"something",
new Font(playfairDisplayRegular, 12, Boolean.TRUE, Boolean.FALSE));
And methods used:
protected XWPFRun printParagraph(XWPFParagraph paragraph, String text, Font font) {
XWPFRun run = paragraph.createRun();
run.setText(text);
run.setFontSize(font.getSize());
run.setBold(font.getBold());
run.setItalic(font.getItalic());
run.setFontFamily(font.getName());
return run;
}
protected XWPFParagraph createParagraphWithAlignment(IBody ibody, ParagraphAlignment alignment) {
XWPFParagraph paragraph = castParagraph(ibody);
paragraph.setAlignment(alignment);
return paragraph;
}

I can't import com.itextpdf.text.Document class

I'm building an android app and I want to use iText for creating pdf file, but I can't use Document class. As I seen in tutorials, there should be import com.itextpdf.text.Document for using Document class. For this app, I'm using com.itextpdf:itext-pdfa:5.5.9 library. I want to create a simple pdf file with 2 paragraphs, something like this:
try{
File pdfFolder = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOCUMENTS), "pdfdemo");
if (!pdfFolder.exists()) {
pdfFolder.mkdir();
}
Date date = new Date() ;
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(date);
File myFile = new File(pdfFolder + timeStamp + ".pdf");
OutputStream output = new FileOutputStream(myFile);
Document document = new Document();
PdfAWriter.getInstance(document, output);
document.open();
document.add(new Paragraph(mSubjectEditText.getText().toString()));
document.add(new Paragraph(mBodyEditText.getText().toString()));
document.close();
}catch (Exception e) {}
'
Could anyone help me with this problem? What am I doing wrong?
You say:
I'm using com.itextpdf:itext-pdfa:5.5.9 library
That is wrong for two reasons:
itext-pdfa is an addon to iText that is meant for writing or manipulating PDF/A documents. It requires the core iText libary. Read about the different parts of iText on the official web site: https://developers.itextpdf.com/itext-java
You say you want to use iText on Android, but you are referring to iText for Java. iText for Java contains classes that are not allowed on Android (java.awt.*, javax.nio,...). You should use the Android port for iText, which is called iTextG: https://developers.itextpdf.com/itextg-android
It's as if you're using iText without having visited the official iText web site. How is that even possible?
Just open your app level gradle file and add following code into your dependencies
implementation 'com.itextpdf:itext-pdfa:5.5.9'
It works for me

Viewing .doc file with java applet

I have a web application. I've generated MS Word document in xml format (Word 2003 XML Document) on server side. I need to show this document to a user on a client side using some kind of viewer. So, question is: what libraries I can use to solve this problem? I need an API to view word document on client side using java.
You cannot reliably display a Word document in a web page using Java (or any other simple technology for that matter). There are several commercial libraries out there to render Word, but you will not find these to be easy, cheap or reliable solutions.
What you should do is the following:
(1) Open the Word engine on the server using a .NET program
(2) Convert the document to Rich Text using the Word engine
(3) Display the rich text either using the RTF Swing widget, or convert to HTML:
String rtf = [your document rich text];
BufferedReader input = new BufferedReader(new StringReader(rtf));
RTFEditorKit rtfKit = new RTFEditorKit();
StyledDocument doc = (StyledDocument) rtfKit.createDefaultDocument();
rtfEdtrKt.read( input, doc, 0 );
input.close();
HTMLEditorKit htmlKit = new HTMLEditorKit();
StringWriter output = new StringWriter();
htmlKit.write( output, doc, 0, doc.getLength());
String html = output.toString();
The main risk in this approach is that the Word engine will either crash or have a memory leak. For this reason you have to have a mechanism for restarting it periodically and testing it to make sure it is functional and not hogging memory.
docx4all is a Swing-based applet which does Word 2007 XML (ie not Word 2003 XML), which we wrote several years ago.
Get it from svn.
That's a possible approach for editing. If all you want is a viewer, which not convert to HTML or PDF? You can use docx4j for that. (Disclosure: "my" project).
You might have a look at the Apache POI - Java API to Handle Microsoft Word Files which is able to read all kinds of word documents (OLE2 and OOXML formats, .doc and .docx extensions respectively).
Reading a doc file can be easy as:
import java.io.*;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.extractor.WordExtractor;
public class ReadDocFile {
public static void main(String[] args) {
File file = null;
WordExtractor extractor = null ;
try {
file = new File("c:\\New.doc");
FileInputStream fis=new FileInputStream(file.getAbsolutePath());
HWPFDocument document=new HWPFDocument(fis);
extractor = new WordExtractor(document);
String [] fileData = extractor.getParagraphText();
for(int i=0;i<fileData.length;i++){
if(fileData[i] != null)
System.out.println(fileData[i]);
}
}
catch(Exception exep){}
}
}
You can find more at: HWPF Quick-Guide (specifically HWPF unit tests)
Note that, according to the POI site:
HWPF is still in early development.
I'd suggest looking at the openoffice source code and implement that.
It's supposed to be written in java.

Apache POI - Docx output issue

I am evaluating apache poi as an option to write docx files. The specific thing I am looking for is to generate content in the docx file in different languages (hindi/marathi to be specific). I am facing the following issue:
When the docx file gets written the "Hindi/Marathi" text is visible as square boxes even though the font "Arial Unicode MS" supports it. The point is that when we check the boxes MS Word displays the font as "Cailbri", even though i have explicitly set the font to "Arial Unicode MS". If i select the boxes in MS Word and then change the font to "Arial Unicode MS" the hindi/marathi words are visible correctly. Any idea why this happens? Please note I am using a development version of POI as the previous stable version did not support setting of font families. Here is the source:
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
public class CreateDocumentFromScratch
{
public static void main(String[] args)
{
XWPFDocument document = new XWPFDocument();
XWPFParagraph paragraphTwo = document.createParagraph();
XWPFRun paragraphTwoRunOne = paragraphTwo.createRun();
paragraphTwoRunOne.setFontFamily("Arial Unicode MS");
paragraphTwoRunOne.setText("नसल्यास");
XWPFParagraph paragraphThree = document.createParagraph();
XWPFRun paragraphThreeRunOne = paragraphThree.createRun();
paragraphThreeRunOne.setFontFamily("Arial Unicode MS");
paragraphThreeRunOne.setText("This is nice");
FileOutputStream outStream = null;
try {
outStream = new FileOutputStream("c:/will/First.doc");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
document.write(outStream);
outStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Any help will be appreciated.
To resurrect a very old post; can the OP confirm the version of MS Office that being used? The problem appears to be with MS Office 2003 running on Windows XP. But then it could be on a higher OS version, too.
It would appear that MS Word applies the Mangal font for Hindi script [Encoding standard: Indic: Hindi ISCII 57002 (Devanagari)]. The following link explains this:
https://support.office.com/en-ca/article/Choose-text-encoding-when-you-open-and-save-files-60d59c21-88b5-4006-831c-d536d42fd861
Suggested workaround:
From Windows XP Control Panel, select Regional and Language Options. Select Languages. Check the box "Install files for complex script and right-to-left languages (including Thai).
Restart PC.
However, no such problem was observed when opening the file using LibreOffice versions 4.3.5.2 on Windows, and LibreOffice 4.2.7.2 on Linux (Ubuntu).
Used the following libraries:
poi-3.10-FINAL-20140208.jar, poi-ooxml-3.10-FINAL-20140208.jar,
poi-ooxml-schemas-3.10-FINAL-20140208.jar, xmlbeans-2.3.0.jar,
dom4j-1.6.1.jar, stax-api-1.0.1.jar

Categories