Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
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));
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();
colorCode=Long.decode("0x000000").intValue();
return colorCode;
I created a HTML document with the following code:
<DOCTYPE! html>
<html lang="DE-CH">
<title>Testseite</title>
</head>
<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">
<title>Testseite</title>
</head>
<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.
–
–
–
I have all JAR Files in the WEB-INF/lib folder within the project's folder
Jars in that location are not accessible to direct fetch by the browser (try it by putting the full path to the Jar in the browser address bar & pressing enter) or, by extension of that, the JVM.
All JAR Files inside the lib folder are also in CLASSPATH.
What exactly is CLASSPATH
?
But those aren't the only problems.
<param name="archive" value="ConverterApplet.jar" />
The iText jars (& every jar relevant to the applet) need to be explicitly referenced in the archive
element.
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.
Writing an applet, and deploying it for general use on the internet, are problems of approximately equivalent difficulty. Or to put that another way, when 'it runs OK in the IDE, just need to deploy it', the job is about half done.
.classpath is the file in my Main root of the project
That file is only useful to your IDE. It is not accessed or accounted for by the browser (or the JVM when the applet is launched using a browser).
<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" />
I'm a bit rusty on the object
element, but AFAIR, the archive param
is used as the archive
attribute of an applet
element would be used.
Therefore the next line is where it starts to go wrong. Your applet itself might read the dom_archive
attribute, but the JM will not account for that archive to be on the run-time classpath. For those two lines it should instead be:
<object type="application/x-java-applet" width="800" height="600">
<param name="code" value="ConverterApplet" />
<param name="archive" value="~/ConverterApplet.jar ~/dom4j-1.6.1.jar .." />
Name every Jar in a space separated list in the (single) archive
element. Then they'll be put on the applet's run-time class path.
–
Java applet technology has been marked for deprecation. Many browsers disable Java applets by default. See
http://openjdk.java.net/jeps/289
https://blogs.oracle.com/java-platform-group/moving-to-a-plugin-free-web
https://blogs.oracle.com/java-platform-group/further-updates-to-moving-to-a-plugin-free-web
About a year ago we wrote a post announcing plans to deprecate the
Java browser plugin in JDK 9 due to browser vendors moving away from
the standards-based NPAPI plugin support technology required to launch
Java Applets.
Since then, the Oracle development team has published a JDK
Enhancement Proposal (JEP 289: Deprecate the Applet API) with
technical details about the planned deprecation step in JDK 9.
In addition, updated timelines for removal of standards-based plugin
support from their browsers, eliminating the possibility to embed Java
and other plugin based technologies, have been announced by the
developers of Apple's Safari and Mozilla's Firefox. In accordance with
their timeline, starting with Mozilla Firefox 52, due to release in
March 2017, the 32-bit version of Mozilla Firefox will no longer
provide the requisite APIs for standards based plugin support. The
64-bit version of Mozilla Firefox for Microsoft Windows launched
without support for most NPAPI-based plugins including Java.
My advice is that you migrate to one of the different technologies described in the Oracle or OpenJDK blogs.
–
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.