如何使用java从Microsoft文档创建预览图像
目前,我正在处理Microsoft文档:Word(doc,docx)、Powerpoint(ppt,pptx)和Excel(xls,xlsx) 我想从它的第一页创建一个预览图像 Apache poi库只能完成PowerPoint文档 但我找不到其他类型的解决方案 我有一个想法,将文档转换为pdf(1)和转换为图像(2) 对于步骤2(将pdf转换为图像),有许多免费的java库,例如PDFBox。它可以很好地处理我的虚拟pdf文件 但是,我在步骤1中遇到了一个问题 在我的文档中,它可能包含具有多种样式、表格、图像或对象的文本。word文档第一页的示例图像: 哪个开源java库可以完成此任务 我已尝试使用以下库实现: JODConverter-输出看起来不错,但需要OpenOffice docx4j-我不确定它是否可以与非ooxml格式(doc、xls、ppt)一起使用,而且它真的是免费的吗? 以下是示例代码:如何使用java从Microsoft文档创建预览图像,java,image,pdf-generation,ms-office,Java,Image,Pdf Generation,Ms Office,目前,我正在处理Microsoft文档:Word(doc,docx)、Powerpoint(ppt,pptx)和Excel(xls,xlsx) 我想从它的第一页创建一个预览图像 Apache poi库只能完成PowerPoint文档 但我找不到其他类型的解决方案 我有一个想法,将文档转换为pdf(1)和转换为图像(2) 对于步骤2(将pdf转换为图像),有许多免费的java库,例如PDFBox。它可以很好地处理我的虚拟pdf文件 但是,我在步骤1中遇到了一个问题 在我的文档中,它可能包含具有多种
String inputWordPath = "C:\\Users\\test\\Desktop\\TestPDF\\Docx.docx";
String outputPDFPath = "C:\\Users\\test\\Desktop\\TestPDF\\OutDocx4j.pdf";
try {
InputStream is = new FileInputStream(new File(inputWordPath));
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(is);
Mapper fontMapper = new IdentityPlusMapper();
wordMLPackage.setFontMapper(fontMapper);
Docx4J.toPDF(wordMLPackage, new FileOutputStream(new File(outputPDFPath)));
} catch (Exception e) {
e.printStackTrace();
}
输出看起来正常,但在生成的pdf中包含“###评估仅使用##”
xdocreport-生成的pdf不包含图像
String inputWordPath = "C:\\Users\\test\\Desktop\\TestPDF\\Docx.docx";
String outputPDFPath = "C:\\Users\\test\\Desktop\\TestPDF\\OutXDOCReport.pdf";
InputStream is = new FileInputStream(new File(inputWordPath));
XWPFDocument document = new XWPFDocument(is);
PdfOptions options = PdfOptions.create();
OutputStream out = new FileOutputStream(new File(outputPDFPath));
PdfConverter.getInstance().convert(document, out, options);
我找不到适合这项任务的图书馆
- 你有什么建议吗
- 我可以直接将文档(docx、doc、xlsx、xls)转换为图像吗
- docx4j真的免费提供转换功能吗
- 如何从生成的pdf(docx4j)中删除“###评估仅使用###”
- docx4j可以处理非ooxml文档吗
- 我可以只将第一页转换为pdf吗
- 我可以设置pdf的大小以适应转换后的文档内容吗
- 是否有将文档转换为pdf或将文档转换为图像的库和示例代码
// Create an office manager using the default configuration.
// The default port is 2002. Note that when an office manager
// is installed, it will be the one used by default when
// a converter is created.
final LocalOfficeManager officeManager = LocalOfficeManager.install();
try {
// Start an office process and connect to the started instance (on port 2002).
officeManager.start();
final File inputFile = new File("document.docx");
final File outputFile = new File("document.png");
// Create a page selector filter in order to
// convert only the first page.
final PageSelectorFilter selectorFilter = new PageSelectorFilter(1);
LocalConverter
.builder()
.filterChain(selectorFilter)
.build()
.convert(inputFile)
.to(outputFile)
.execute();
} finally {
// Stop the office process
LocalOfficeUtils.stopQuietly(officeManager);
}
至于你的问题
我可以设置pdf的大小以适应转换后的文档内容吗
如果您可以使用LibreOffice或apacheopenoffice而不使用JODConverter,那么您可以使用JODConverter。您只需找出如何以编程方式完成,然后创建一个用于JODConverter的过滤器
我不会在这里详细说明,因为您可以选择另一种方式,但如果您需要进一步的帮助,只需询问项目的详细信息。您可以试试,它的免费套餐计划每月提供50个免费学分。它支持所有公共文件的转换
DOCX到图像流转换代码示例:
// Get App Key and App SID from https://dashboard.groupdocs.cloud/
ConvertApi apiInstance = new ConvertApi(AppSID,AppKey);
try {
ConvertSettings settings = new ConvertSettings();
settings.setStorageName(Utils.MYStorage);
settings.setFilePath("conversions\\password-protected.docx");
settings.setFormat("jpeg");
DocxLoadOptions loadOptions = new DocxLoadOptions();
loadOptions.setPassword("password");
loadOptions.setHideWordTrackedChanges(true);
loadOptions.setDefaultFont("Arial");
settings.setLoadOptions(loadOptions);
JpegConvertOptions convertOptions = new JpegConvertOptions();
convertOptions.setFromPage(1);
convertOptions.setPagesCount(1);
convertOptions.setGrayscale(false);
convertOptions.setHeight(1024);
convertOptions.setQuality(100);
convertOptions.setRotateAngle(90);
convertOptions.setUsePdf(false);
settings.setConvertOptions(convertOptions);
// set OutputPath as empty will result the output as document IOStream
settings.setOutputPath("");
// convert to specified format
File response = apiInstance.convertDocumentDownload(new ConvertDocumentRequest(settings));
System.out.println("Document converted successfully: " + response.length());
} catch (ApiException e) {
System.err.println("Exception while calling ConvertApi:");
e.printStackTrace();
}
我是Aspose的开发者传道者。是的,Libre Office有无JODConverter都是不错的选择。您必须接受安装Libre Office,但它是免费的。您好,我正在尝试通过反复试验来了解该库,在上面的示例代码(我最初从您的github wiki获得)中,我无法理解OfficeUtils类是什么。很抱歉,我刚才没有看到您的评论。这篇文章是在新的jodconverter在线模块之前发布的,当我更新文章时错过了这一部分。类OfficeUtils已重命名为LocalOfficeUtils。