翻譯|行業資訊|編輯:胡濤|2023-08-17 11:24:58.980|閱讀 133 次
概述:在本文中,您將學習如何使用Spire.Doc for Java將 Word 轉換為流行的圖像格式,例如JPG、PNG和SVG。歡迎查閱~
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
您可能需要將 Word 文檔轉換為圖像的原因有很多。例如,很多設備不需要任何特殊軟件就可以直接打開并顯示圖像,并且圖像在傳輸時其內容很難被篡改。在本文中,您將學習如何使用Spire.Doc for Java將 Word 轉換為流行的圖像格式,例如JPG、PNG和SVG。
Spire.Doc 是一款專門對 Word 文檔進行操作的 類庫。在于幫助開發人員無需安裝 Microsoft Word情況下,輕松快捷高效地創建、編輯、轉換和打印 Microsoft Word 文檔。擁有近10年專業開發經驗Spire系列辦公文檔開發工具,專注于創建、編輯、轉換和打印Word/PDF/Excel等格式文件處理,小巧便捷,除此之外,你也可以瀏覽E-iceblue 旗下其他不同語言的文檔開發工具~
Spire.Doc for.net下載 Spire.Doc for java下載
首先,您需要將 Spire.Doc.jar 文件添加為 Java 程序中的依賴項。可以從此鏈接下載 JAR 文件。如果您使用 Maven,則可以通過將以下代碼添加到項目的 pom.xml 文件中來輕松將 JAR 文件導入到應用程序中。
<repositories> <repository> <id>com.e-iceblue</id> <name>e-iceblue</name> <url>//repo.e-iceblue.com/nexus/content/groups/public/</url> </repository> </repositories> <dependencies> <dependency> <groupId>e-iceblue</groupId> <artifactId>spire.doc</artifactId> <version>11.8.1</version> </dependency> </dependencies>
Spire.Doc for Java 提供Document.saveToImages()方法將整個 Word 文檔轉換為單獨的BufferedImage圖像。然后,每個 BufferedImage 都可以保存為BMP、EMF、JPEG、PNG、GIF或WMF文件。以下是使用此庫將 Word 轉換為 JPG 的步驟。
import com.spire.doc.Document; 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 ConvertWordToJPG { public static void main(String[] args) throws IOException { //Create a Document object Document doc = new Document(); //Load a Word document doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\ConvertTemplate.docx"); //Convert the whole document into individual buffered images BufferedImage[] images = doc.saveToImages(ImageType.Bitmap); //Loop through the images for (int i = 0; i < images.length; i++) { //Get the specific image BufferedImage image = images[i]; //Re-write the image with a different color space BufferedImage newImg = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB); newImg.getGraphics().drawImage(image, 0, 0, null); //Write to a JPG file File file = new File("C:\\Users\\Administrator\\Desktop\\Images\\" + String.format(("Image-%d.jpg"), i)); ImageIO.write(newImg, "JPEG", file); } } }
使用 Spire.Doc for Java,您可以將 Word 文檔保存為字節數組列表。然后可以將每個字節數組寫入 SVG 文件。將Word轉換為SVG的詳細步驟如下。
import com.spire.doc.Document; import java.io.FileOutputStream; import java.io.IOException; import java.util.List; public class ConvertWordToSVG { public static void main(String[] args) throws IOException { //Create a Document object Document doc = new Document(); //Load a Word document doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\ConvertTemplate.docx"); //Save the document as a list of byte arrays List<byte[]> svgBytes = doc.saveToSVG(); //Loop through the items in the list for (int i = 0; i < svgBytes.size(); i++) { //Get a specific byte array byte[] byteArray = svgBytes.get(i); //Specify the output file name String outputFile = String.format("Image-%d.svg", i); //Write the byte array to a SVG file try (FileOutputStream stream = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\Images\\" + outputFile)) { stream.write(byteArray); } } } }
分辨率越高的圖像通常越清晰。您可以按照以下步驟在將 Word 轉換為 PNG 時自定義圖像分辨率。
import com.spire.doc.Document; 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 ConvertWordToPNG { public static void main(String[] args) throws IOException { //Create a Document object Document doc = new Document(); //Load a Word document doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\ConvertTemplate.docx"); //Convert the whole document into individual buffered images with customized resolution BufferedImage[] images = doc.saveToImages(0, doc.getPageCount(), ImageType.Bitmap, 150, 150); //Loop through the images for (int i = 0; i < images.length; i++) { //Get the specific image BufferedImage image = images[i]; //Write to a PNG file File file = new File("C:\\Users\\Administrator\\Desktop\\Images\\" + String.format(("Image-%d.png"), i)); ImageIO.write(image, "PNG", file); } } }
以上便是如何在Java中將 Word 轉換為圖像,如果您有其他問題也可以繼續瀏覽本系列文章,獲取相關教程,你還可以給我留言或者加入我們的官方技術交流群。
歡迎下載|體驗更多E-iceblue產品
獲取更多信息請咨詢 ;技術交流Q群(767755948)
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn