I'm trying to code a Word-To-PDF Converter and build the Java Code into a HTML Website in form of a Java Applet.
I used the Java Code from http://java.worldbestlearningcenter.com/2013/07/word-to-pdf-converter.html. The code itself seems to work perfectly fine, when I run the program in Eclipse, I can indeed convert a word document to a PDF. Now I'd like to add that code to my HTML file and make it work.
To do so, I made my class herit from JApplet and wrote the method paint(). The code I have by now looks like the snippet below:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Iterator;
import java.util.List;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFPicture;
import org.apache.poi.xwpf.usermodel.XWPFPictureData;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.PageSize;
import java.applet.*;
import java.awt.*;
public class ConverterApplet extends Applet {
private static final long serialVersionUID = 1L;
public void paint(Graphics g) {
g.drawString("Hello", 60, 40);
selectFiles();
}
public static void selectFiles(){
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("Microsoft Word 2007+", "docx");
chooser.setFileFilter(filter);
chooser.setMultiSelectionEnabled(true);
int returnVal = chooser.showOpenDialog(null);
if(returnVal == JFileChooser.APPROVE_OPTION) {
File[] Files=chooser.getSelectedFiles();
System.out.println("Konvertierung gestartet ...");
for( int i=0;i<Files.length;i++){
String wordfile=Files[i].toString();
convertWordToPdf(wordfile,wordfile.substring(0,wordfile.indexOf('.'))+".pdf");
}
System.out.println("Konvertierung abgeschlossen!");
}
}
public static void convertWordToPdf(String src, String desc){
try {
//create file inputstream object to read data from file
FileInputStream fs=new FileInputStream(src);
//create document object to wrap the file inputstream object
XWPFDocument doc=new XWPFDocument(fs);
//72 units=1 inch
Document pdfdoc=new Document(PageSize.A4,72,72,72,72);
//create a pdf writer object to write text to mypdf.pdf file
PdfWriter pwriter=PdfWriter.getInstance(pdfdoc, new FileOutputStream(desc));
//specify the vertical space between the lines of text
pwriter.setInitialLeading(20);
//get all paragraphs from word docx
List<XWPFParagraph> plist=doc.getParagraphs();
//open pdf document for writing
pdfdoc.open();
for (int i = 0; i < plist.size(); i++) {
//read through the list of paragraphs
XWPFParagraph pa = plist.get(i);
//get all run objects from each paragraph
List<XWPFRun> runs = pa.getRuns();
//read through the run objects
for (int j = 0; j < runs.size(); j++) {
XWPFRun run=runs.get(j);
//get pictures from the run and add them to the pdf document
List<XWPFPicture> piclist=run.getEmbeddedPictures();
//traverse through the list and write each image to a file
Iterator<XWPFPicture> iterator=piclist.iterator();
while(iterator.hasNext()){
XWPFPicture pic=iterator.next();
XWPFPictureData picdata=pic.getPictureData();
byte[] bytepic=picdata.getData();
Image imag=Image.getInstance(bytepic);
pdfdoc.add(imag);
}
//get color code
int color=getCode(run.getColor());
//construct font object
Font f=null;
if(run.isBold() && run.isItalic())
f=FontFactory.getFont(FontFactory.TIMES_ROMAN,run.getFontSize(),Font.BOLDITALIC, new BaseColor(color));
else if(run.isBold())
f=FontFactory.getFont(FontFactory.TIMES_ROMAN,run.getFontSize(),Font.BOLD, new BaseColor(color));
else if(run.isItalic())
f=FontFactory.getFont(FontFactory.TIMES_ROMAN,run.getFontSize(),Font.ITALIC, new BaseColor(color));
else if(run.isStrike())
f=FontFactory.getFont(FontFactory.TIMES_ROMAN,run.getFontSize(),Font.STRIKETHRU, new BaseColor(color));
else
f=FontFactory.getFont(FontFactory.TIMES_ROMAN,run.getFontSize(),Font.NORMAL, new BaseColor(color));
//construct unicode string
String text=run.getText(-1);
byte[] bs;
if (text!=null){
bs=text.getBytes();
String str=new String(bs,"UTF-8");
//add string to the pdf document
Chunk chObj1=new Chunk(str,f);
pdfdoc.add(chObj1);
}
}
//output new line
pdfdoc.add(new Chunk(Chunk.NEWLINE));
}
//close pdf document
pdfdoc.close();
} catch(Exception e){
e.printStackTrace();
}
}
public static int getCode(String code){
int colorCode;
if(code!=null)
colorCode=Long.decode("0x"+code).intValue();
else
colorCode=Long.decode("0x000000").intValue();
return colorCode;
}
}
I created a HTML document with the following code:
<DOCTYPE! html>
<html lang="DE-CH">
<head>
<title>Testseite</title>
</head>
<body>
<h1>Titel</h1>
<object type="application/x-java-applet" width="800" height="600">
<param name="code" value="ConverterApplet" />
<param name="archive" value="ConverterApplet.jar" />
Applet failed to run. No Java plug-in was found.
</object>
</body>
</html>
When trying to open that HTML page in Internet Explorer (Chrome and Firefox seem not to work and even IE needs a special permission), I get the error "java.lang.noClassDefFoundError: com/itextpdf/text/element".
What I can also tell already:
- I only have one JAR File for itextpdf. Its the version 5.1.0, because 2.1.7 does not include all packages that specific code needs. There is no other JAR File similar to itextpdf.
- I have all JAR Files in the WEB-INF/lib folder within the project's folder
- All JAR Files inside the lib folder are also in CLASSPATH.
- I have an updated version of my code exported as "ConverterApplet.jar", also have that file in the project's root folder and also in CLASSPATH.
- I tried the browsers Chrome, Firefox, Opera, Edge and Internet Explorer. Only the last one even gave me the possibility to see the (not working) applet, all others said "no plugin found".
I couldn't find any other help on the web apart the bullet points I listed above. I'm really clueless what else could be the problem to get such an error, especially because the Java code itself works inside Eclipse, but not outside. Any help would really be apperciated.
== EDIT == .classpath File:
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="lib" path="C:/Users/silve/Documents/Eclipse_Launch/Converter/ConverterApplet.jar"/>
<classpathentry kind="lib" path="C:/Users/silve/Documents/Eclipse_Launch/Converter/dom4j-1.6.1.jar"/>
<classpathentry kind="lib" path="C:/Users/silve/Documents/Eclipse_Launch/Converter/openxml4j-1.0-beta.jar"/>
<classpathentry kind="lib" path="C:/Users/silve/Documents/Eclipse_Launch/Converter/poi-3.9.jar"/>
<classpathentry kind="lib" path="C:/Users/silve/Documents/Eclipse_Launch/Converter/poi-ooxml-3.8.jar"/>
<classpathentry kind="lib" path="C:/Users/silve/Documents/Eclipse_Launch/Converter/poi-ooxml-schemas-3.9.jar"/>
<classpathentry kind="lib" path="C:/Users/silve/Documents/Eclipse_Launch/Converter/xmlbeans-2.5.0.jar"/>
<classpathentry kind="lib" path="C:/Users/silve/Documents/Eclipse_Launch/Converter/itextpdf-5.5.12.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
New HTML File:
<DOCTYPE! html>
<html lang="DE-CH">
<head>
<title>Testseite</title>
</head>
<body>
<h1>Titel</h1>
<object type="application/x-java-applet" width="800" height="600">
<param name="code" value="ConverterApplet" />
<param name="archive" value="~/ConverterApplet.jar" />
<param name="dom_archive" value="~/dom4j-1.6.1.jar" />
<param name="itext_archive" value="~/itextpdf-5.5.12.jar" />
<param name="openxml_archive" value="~/openxml4j-1.0-beta.jar" />
<param name="poi_archive" value="~/poi-3.9.jar" />
<param name="ooxml_archive" value="~/poi-ooxml-3.8.jar" />
<param name="schemas_archive" value="~/poi-ooxml-schemas-3.9.jar" />
<param name="mxmlbeans_archive" value="~/xmlbeans-2.5.0" />
Applet failed to run. No Java plug-in was found.
</object>
</body>
</html>
Right now I'm getting the error "NoClassFound" by IE.