2020国产工具Spire.Doc 在Java中进行各种格式转换全新攻略

Spire系列文档处理API是国产开发工具中功能可媲美Aspose的强大控件,帮助开发者轻松将文档功能集成到应用程序中。年终福利,小编为您倾情献上Spire.DOC在Java中格式转换的示例教程,记得收藏哦!

将文档从一种格式转换为另一种格式是Spire.DOC的主要功能之一。这种转换只不过是加载和保存操作的组合。因此,使用Spire.DOC可以将文档从任何受支持的加载格式转换为任何受支持的保存格式。

本文整理了包括在Java中以下文件格式的转换指南,希望对您有所帮助:

  • 将Word转换为图像
  • 将Word转换为PDF
  • 将HTML转换为PDF

>>你可以点击这里下载Spire.DOC for Java测试体验。

将Word转换为图像

//create a Document objectDocument doc = new Document();//load a Word filedoc.loadFromFile("C:\Users\Administrator\Desktop\sample.docx");//loop through the pagesfor (int i = 0; i < doc.getPageCount(); i++) {    //save the specific page to a BufferedImage    BufferedImage image= doc.saveToImages(i, ImageType.Bitmap);    //write the image data to a .png file    File file = new File( "output/" + String.format(("Img-%d.png"), i));    ImageIO.write(image, "PNG", file);}

将Word转换为PDF

import com.spire.doc.Document;import com.spire.doc.FileFormat;import com.spire.doc.ToPdfParameterList;public class WordToPDF {    public static void main(String[] args)  {        //Create Document object        Document doc = new Document();        //Load the file from disk.        doc.loadFromFile("Sample.docx");        //create an instance of ToPdfParameterList.        ToPdfParameterList ppl=new ToPdfParameterList();        //embeds full fonts by default when IsEmbeddedAllFonts is set to true.        ppl.isEmbeddedAllFonts(true);        //set setDisableLink to true to remove the hyperlink effect for the result PDF page.        //set setDisableLink to false to preserve the hyperlink effect for the result PDF page.        ppl.setDisableLink(true);        //Set the output image quality as 40% of the original image. 80% is the default setting.        doc.setJPEGQuality(40);        //Save to file.        doc.saveToFile("output/ToPDF.pdf",FileFormat.PDF);    }}

将HTML转换为PDF

import com.spire.doc.*;import com.spire.doc.documents.XHTMLValidationType;public class htmlFileToWord {    public static void main(String[] args) throws Exception {        // Load the sample HTML file        Document document = new Document();        document.loadFromFile("InputHtmlFile.html", FileFormat.Html, XHTMLValidationType.None);        //Save to file        document.saveToFile("Result.pdf",FileFormat.PDF);    }}

如果您有任何疑问或需求,请随时联系客服,我们很高兴为您提供查询和咨询
标签:

来源:慧都

声明:本站部分文章及图片转载于互联网,内容版权归原作者所有,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2020年11月20日
下一篇 2020年11月20日

相关推荐

发表回复

登录后才能评论