При работе с документом Word нам часто приходится конвертировать его из doc/docx в другие форматы файлов для удовлетворения потребностей различных рабочих ситуаций. В этой статье будет рассказано, как конвертировать документы Word в PDF, PNG, SVG, RTF, XPS, XML, ТЕКСТ с помощью бесплатного Spire. Документ для Java.
Установка Способ 1: Скачайте Бесплатный шпиль. Документ для Java и распакуйте его. Затем добавьте Spire.Doc.jar файл в ваше Java-приложение в качестве зависимости.
Метод 2: Вы также можете добавить зависимость jar в проект maven, добавив следующие конфигурации в pom.xml .
com.e-iceblue e-iceblue http://repo.e-iceblue.com/nexus/content/groups/public/ e-iceblue spire.doc.free 2.7.3
【Пример 1】Преобразование 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) { //Load the Word Document Document doc = new Document(); doc.loadFromFile("test1.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("ToPDF.pdf",FileFormat.PDF); } }
【Пример 2】Преобразование Word в другие форматы
import com.spire.doc.Document; import com.spire.doc.FileFormat; import com.spire.doc.documents.ImageType; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; public class ConvertWordToOtherFormats { public static void main(String[] args) throws IOException { //Load the Word Document Document doc = new Document(); doc.loadFromFile("test1.docx"); //Save the first page to a BufferedImage BufferedImage image= doc.saveToImages(0, ImageType.Bitmap); //Write the image data to a .png file File file= new File("out/ToPNG.png"); ImageIO.write(image, "PNG", file); //Save the Word document as SVG format doc.saveToFile("out/ToSVG.svg",FileFormat.SVG); //Save the Word document as RTF format doc.saveToFile("out/ToRTF.rtf",FileFormat.Rtf); //Save the Word document as XPS format doc.saveToFile("out/ToXPS.xps",FileFormat.XPS); //Save the Word document as XML format doc.saveToFile("out/ToXML.xml",FileFormat.Xml); //Save the Word document as TXT format doc.saveToFile("out/ToTXT.txt",FileFormat.Txt); } }
Оригинал: “https://dev.to/jazzzzz/converting-word-to-pdf-png-svg-rtf-xps-xml-in-java-application-3870”