Java 将Docx文件转换为图像

Java 将Docx文件转换为图像,java,doc,aspose,aspose.words,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"); } publ

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

我已经测试了你的代码,对我来说效果很好。请确保您使用的是最新版本的

如果问题仍然存在,请共享您尝试转换的word文档。您还可以使用我们的


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

因为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);
            }
        });
    }
URI docxUri=file.toURI(); docxUri=新URI(“jar:+docxUri.toString());//“罐子:file://..." 最终路径targetDirPath=Path.get(“C:/test”); Files.createDirectories(targetDirPath); Map zipProperties=new HashMap(); ZipProperty.put(“编码”、“UTF-8”); try(FileSystem zipFS=FileSystems.newFileSystem(docxUri, (不动产){ Path mediaPath=zipFS.getPath(“/word/media”); walkFileTree(mediaPath,新的SimpleFileVisitor(){ @凌驾 公共文件VisitResult visitFile(路径, BasicFileAttributes)引发IOException{ 字符串名称=path.getFileName().toString(); Path imgPath=Path.get(targetDirPath.toString(),name); 复制(路径、imgPath、StandardCopyOption.REPLACE_EXISTING); 返回super.visitFile(路径、属性); } }); } 这将复制zip文件夹
/word/media
中的所有媒体文件

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

代码很好 但是如何将.docx文件作为用户的输入

代码本身具有要转换的文档文件的预定义路径

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

我的班级:

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();
        }
    }
}
导入com.aspose.words.Document;
导入com.aspose.words.ImageSaveOptions;
导入com.aspose.words.SaveFormat;
/**
*由怀特创作
*/
公共课程{
公共静态void main(字符串args[])引发异常{
新建程序().generateImages(“E:\\pro2\\Document.docx”);
}
公共void generateImages(最终字符串源路径){
试一试{
文档文档=新文档(sourcePath);
ImageSaveOptions=newImageSaveOptions(SaveFormat.JPEG);
选项。设置质量(100);
选项。setResolution(100);
对于(int i=0;i
您可以查看ApachePOI()将docx转换为PDF。然后,您可以使用apache PDF box将PDF转换为图像:但是如何将表格和图形转换为图像?从PDF转换为图像,您可以使用ImageWriter:好的,我正在尝试使用ithats for the idea@Athanorso,在那里我可以获得aspose.words.jar文件我想我的jar文件有一些问题请访问下载最新版本。这里是一个直接链接。saqib它说评估的目的只是它的意思是什么?如果你不申请许可证,Aspose.Words for Java将在评估模式下运行。您可以选择或请求。检查如何申请许可证。我认为docx扩展代表MS Word 2007,那么这是用于将MS Word文档转换为图像的代码吗?docx是MS Word的“开放格式”。docx和xlsx基本上都是zip存档,其中包含用于内容和样式的XML,以及用于图像的图像文件。如果您将docx重命名为.zip,您将看到您自己。所以我想提到一个使用纯标准java的解决方案。zip文件系统也很棒。不反对阿斯波塞。
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();
        }
    }
}