4

I am going to convert a Word Document (.docx) into images so I am using aspose.word.jar! The problem is that I am new to this work and using my code getting an error. My code part is:

public class NewClass {
   public static void main(String[] args){
       new NewClass().generateImages("D:\\Net Beans Work Space\\Text to Image\\Doc1.docx");
   }

   public void generateImages(final String sourcePath) {  
      try {  
           Document doc = new Document(sourcePath);  
           ImageSaveOptions options = new ImageSaveOptions(SaveFormat.JPEG);  
           options.setJpegQuality(100);  
           options.setResolution(100);  

           for (int i = 0; i < doc.getPageCount(); i++) {  
                String imageFilePath = sourcePath + "_output_" + i + ".jpeg";  
                options.setPageIndex(i);  
                doc.save(imageFilePath, options);  
           }  
      } catch (Exception e) {  
           e.printStackTrace();  
      }  
 }  
} 

well I am new to aspose so i got this code part online and edit it to my requirements! the problem is in the print stack trace! I have google it for hours and didn't get any solution can somebody done this work before! is it me doing something wrong can somebody please help me!

java.lang.NullPointerException
    at asposewobfuscated.hh.a(GdiRenderer.java:176) at asposewobfuscated.hh.a(GdiRenderer.java:176)
    at asposewobfuscated.s.a(ApsGlyphs.java:48)
    at asposewobfuscated.p.a(ApsCompositeNode.java:22)
    at asposewobfuscated.m.a(ApsCanvas.java:18)
    at asposewobfuscated.p.a(ApsCompositeNode.java:22)
    at asposewobfuscated.m.a(ApsCanvas.java:18)
    at asposewobfuscated.p.a(ApsCompositeNode.java:22)
    at asposewobfuscated.m.a(ApsCanvas.java:18)
    at asposewobfuscated.p.a(ApsCompositeNode.java:22)
    at asposewobfuscated.z.a(ApsPage.java:75)
    at asposewobfuscated.p.a(ApsCompositeNode.java:22)
    at asposewobfuscated.m.a(ApsCanvas.java:18)
    at asposewobfuscated.hh.a(GdiRenderer.java:49)
    at com.aspose.words.apd.a(SaveToImageHelper.java:129)
    at com.aspose.words.apd.a(SaveToImageHelper.java:94)
    at com.aspose.words.apd.a(SaveToImageHelper.java:71)
    at com.aspose.words.uz.F(ImagingWriter.java:79)
    at com.aspose.words.uz.a(ImagingWriter.java:38)
    at com.aspose.words.Document.a(Document.java:1345)
    at com.aspose.words.Document.save(Document.java:738)
    at NewClass.generateImages(NewClass.java:31)
    at NewClass.main(NewClass.java:18)
java.lang.NullPointerException
    at asposewobfuscated.hh.a(GdiRenderer.java:176)
    at asposewobfuscated.s.a(ApsGlyphs.java:48)
    at asposewobfuscated.p.a(ApsCompositeNode.java:22)
    at asposewobfuscated.m.a(ApsCanvas.java:18)
    at asposewobfuscated.p.a(ApsCompositeNode.java:22)
    at asposewobfuscated.m.a(ApsCanvas.java:18)
    at asposewobfuscated.p.a(ApsCompositeNode.java:22)
    at asposewobfuscated.m.a(ApsCanvas.java:18)
    at asposewobfuscated.p.a(ApsCompositeNode.java:22)
    at asposewobfuscated.z.a(ApsPage.java:75)
    at asposewobfuscated.p.a(ApsCompositeNode.java:22)
    at asposewobfuscated.m.a(ApsCanvas.java:18)
    at asposewobfuscated.hh.a(GdiRenderer.java:49)
    at com.aspose.words.apd.a(SaveToImageHelper.java:129)
    at com.aspose.words.apd.a(SaveToImageHelper.java:94)
    at com.aspose.words.apd.a(SaveToImageHelper.java:71)
    at com.aspose.words.uz.F(ImagingWriter.java:79)
    at com.aspose.words.uz.a(ImagingWriter.java:38)
    at com.aspose.words.Document.a(Document.java:1345)
    at com.aspose.words.Document.save(Document.java:738)
    at NewClass.generateImages(NewClass.java:31)
    at NewClass.main(NewClass.java:18)
BUILD SUCCESSFUL (total time: 9 seconds)
Java Nerd
  • 958
  • 3
  • 19
  • 51

3 Answers3

1

I have tested your code and it worked fine for me. Please make sure that you are using the latest version of Aspose.Words for Java.

If the issue persists, kindly share the word document that you are trying to convert. You can also upload your documents securely using our support forums.

Disclosure: I am a developer at Aspose.

Saqib
  • 91
  • 6
  • so where i can get the aspose.words.jar file i think my jar file has some issues – Java Nerd Aug 18 '14 at 07:18
  • 1
    Visit [Aspose.Words for Java](http://www.aspose.com/java/word-component.aspx) to download the latest version. Here is a direct link http://maven.aspose.com/repository/repo/com/aspose/aspose-words/14.7.0/aspose-words-14.7.0-jdk16.jar. – Saqib Aug 18 '14 at 08:33
  • saqib it says that evaluation purpose only what does it mean? – Java Nerd Aug 19 '14 at 07:41
  • If you don't apply a license, Aspose.Words for Java will run in evaluation mode. You can [buy](http://www.aspose.com/purchase/) or request a [free temporary license](http://www.aspose.com/corporate/purchase/temporary-license.aspx). Check [our documentation](http://www.aspose.com/docs/display/wordsjava/Applying+a+License) for how to apply a license. – Saqib Aug 19 '14 at 07:54
  • @Saqib is this opensource of free to use in commercial purpose? – Juliyanage Silva Aug 12 '23 at 18:33
0

As docx is just a zip format you might use a Zip FileSystem to copy the image files to the outside.

    URI docxUri = file.toURI();
    docxUri = new URI("jar:" + docxUri.toString()); // "jar:file://..."

    final Path targetDirPath = Paths.get("C:/test");
    Files.createDirectories(targetDirPath);

    Map<String, String> zipProperties = new HashMap<>();
    zipProperties.put("encoding", "UTF-8");
    try (FileSystem zipFS = FileSystems.newFileSystem(docxUri,
            zipProperties)) {
        Path mediaPath = zipFS.getPath("/word/media");
        Files.walkFileTree(mediaPath, new SimpleFileVisitor<Path>() {

            @Override
            public FileVisitResult visitFile(Path path,
                    BasicFileAttributes attributes) throws IOException {
                String name = path.getFileName().toString();
                Path imgPath = Paths.get(targetDirPath.toString(), name);
                Files.copy(path, imgPath, StandardCopyOption.REPLACE_EXISTING);
                return super.visitFile(path, attributes);
            }
        });
    }

This copies all media files, found in zip folder /word/media.

Of course you have no metadata, no context to relate the image files. But it is a nice usage of Files.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • i think the docx extension stands for MS Word 2007 so is this code for converting the MS Word Document to Images? – Java Nerd Aug 18 '14 at 07:19
  • 1
    docx is an "open format" of MS Word. docx, xlsx are basically zip archives with XML for content and styles, and image files for images. If you rename the docx to .zip you'll see yourself. So I wanted to mention a solution with pure standard java. And the zip filesystem is wonderful too. _Nothing against Aspose._ – Joop Eggen Aug 18 '14 at 07:28
0

the code is fine but how to take the .docx files an input from user.

the code itself has predefined path of the doc file which is about to be converted

what changes in the code should be done so that the application can take the document file from user and then generates its image

My Class:

import com.aspose.words.Document;
import com.aspose.words.ImageSaveOptions;
import com.aspose.words.SaveFormat;

/**
 * Created by white
 */
public class Program {

    public static void main(String args[]) throws Exception {


        new Program().generateImages("E:\\pro2\\Document.docx");

    }

    public void generateImages(final String sourcePath) {
        try {
            Document doc = new Document(sourcePath);
            ImageSaveOptions options = new ImageSaveOptions(SaveFormat.JPEG);
            options.setJpegQuality(100);
            options.setResolution(100);

            for (int i = 0; i < doc.getPageCount(); i++) {
                String imageFilePath = sourcePath + "_output_" + i + ".jpg";

                options.setPageIndex(i);

                doc.save(imageFilePath, options);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
duffy356
  • 3,678
  • 3
  • 32
  • 47
whhite
  • 1
  • 1