将Docx文件转换为图像

时间:2014-08-14 13:34:31

标签: java doc aspose aspose.words

我要将Word文档(.docx)转换为图像,所以我使用的是aspose.word.jar! 问题是我不熟悉这项工作并使用我的代码获取错误。我的代码部分是:

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();  
      }  
 }  
} 

我是aspose的新手,所以我在网上得到了这个代码部分并根据我的要求进行编辑! 问题出在打印堆栈跟踪中! 我已经谷歌了几个小时,并没有得到任何解决方案可以有人完成这项工作! 是不是有人可以帮帮我呢?

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)

3 个答案:

答案 0 :(得分:1)

我测试了你的代码,它对我来说很好。请确保您使用的是最新版本的Aspose.Words for Java

如果问题仍然存在,请分享您要转换的word文档。您还可以使用我们的support forums安全上传文件。

披露:我是Aspose的开发人员。

答案 1 :(得分:0)

由于docx只是一种zip格式,您可以使用Zip文件系统将图像文件复制到外部。

    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);
            }
        });
    }

这会复制zip文件夹/word/media中的所有媒体文件。

当然,您没有元数据,也没有关联图像文件的上下文。但它是文件的一个很好的用法。

答案 2 :(得分:0)

代码很好  但是如何从用户输入.docx文件。

代码本身具有即将转换的doc文件的预定义路径

应该对代码进行哪些更改,以便应用程序可以从用户获取文档文件,然后生成其图像

我的班级:

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();
        }
    }
}