JAVA XWPFDocument script gives java.lang.Error - java

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

Related

Use pdfbox with Notepad ++ and windows cmd?

I don't want to use large programs like Netbeans Eclipse and so on and should only do a small thing with pdfbox (https://pdfbox.apache.org/) but it is not working. Do I have to use maven etc?
I have downloaded pdfbox-2.0.20.jar and pdfbox-app-2.0.20.jar
Added PATH to environment variable in Windows.
Created this (Document_Creation.java) in Notepad++
import java.io.IOException;
import org.apache.pdfbox.pdmodel.PDDocument;
public class Document_Creation {
public static void main (String args[]) throws IOException {
//Creating PDF document object
PDDocument document = new PDDocument();
//Saving the document
document.save("C:/folder/my_doc.pdf");
System.out.println("PDF created");
//Closing the document
document.close();
}
}
Open Windows CDM and type: javac Document_Creation.java
But get this error
Document_Creation.java:2: error: package org.apache.pdfbox.pdmodel does not exist
import org.apache.pdfbox.pdmodel.PDDocument;
^
Document_Creation.java:9: error: cannot find symbol
PDDocument document = new PDDocument();
^
symbol: class PDDocument
location: class Document_Creation
Document_Creation.java:9: error: cannot find symbol
PDDocument document = new PDDocument();
^
symbol: class PDDocument
location: class Document_Creation
3 errors
Looks like I can't access pdfbox with import org.apache.pdfbox.pdmodel.PDDocument;
How can I make this code work with latest java (working, tested with println without pdfbox), Notepad++ and Windows CMD?
As noted here, the solution is in the syntax order, requiring classpath items before the class:
java -cp .;"C:\Program Files\Java\pdfBox\pdfbox-app-2.0.24.jar" Document_Creation
I also put in the full path where I have pdfBox jar located.
---TLDR---
I've used pdfbox years ago as command line tool format with success, but wanted to step up to using it in a java application. After cleaning up stuff (deleting old java installations [to get java and javac on same version], cleaning up paths, figuring out how to create a macro in notepad++ that allowed for compilation and running of the application, and restarting notepad++) to get to the point of this question, I was almost ready to give up until my search result pulled up the solution. Now I feel ready to dive deeper and hopefully get to use pdfBox for my actual application.

Creating Word File With JTextPane Style Option

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

How do I resolve code relating to "instantiate"?

So, I am writing a code in which I imported a class with class name "Workbook" and function "createWorkBook". I asked the same question earlier but I wanted to add changes so I removed that before anyone could reply.
Anyways,
I am new to Java , interfaces and importing class.
I imported a package named "jxl" and I am using it. Here is my FULL code so far.
import java.io.File;
import java.io.IOException;
import jxl.*;
import jxl.write.*;
import jxl.write.Number;
public class WriteExcel {
public static void main (String args[]) throws IOException , WriteException
{
try{
Workbook wb = new Workbook();
}
catch(WriteException e)
{
System.out.println("Sorry, failed! Keep on trying harder! :)");
}
}
}
ALL I am trying for past half an hour is trying to make an object "wb" in class "workbook" .
I followed bit of instructions from https://www.youtube.com/watch?v=A9866lBdmKo (importing of class).
I am getting an error for the link Workbook wb= new Workbook();
Cannot instantiate the Workbook type. I did some research turns out it is relating to some "interface". But the video didn't even talk about interface. I am new and would like some guidance. I just want to create one object.
So I got the file, thanks for ANY sort of input!
Workbook is an Anonymous Inner Type java class, whenever you want to instantiate that class, we'll need to override several methods. You will have to implement those methods. You can find more about Anonymous Inner Type java class here
http://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html
Having a look at the javadoc, it seems that Workbook is an abstract class, so you can't instanciate it, but it seems that it provides some staticmethods called createWorkbook() that you can use, like that :
Workbook wb = Workbook.createWorkbook(new File("/path/to/the/workbook/file"));
As workbook is an abstract class. The only way to create a workbook object is
String fileName = "file.xls";
WritableWorkbook workbook = Workbook.createWorkbook(new File(fileName));
Creates a writable workbook with the given file name.
Also please refer to the below link that represent the workbook class and its methods
http://jexcelapi.sourceforge.net/resources/javadocs/2_6_10/docs/jxl/Workbook.html

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