可能需要將 Word 文檔轉(zhuǎn)換為圖像的原因有很多。例如,很多設(shè)備不需要任何特殊軟件就可以直接打開并顯示圖像,并且圖像在傳輸時(shí)其內(nèi)容很難被篡改。在本文中,您將學(xué)習(xí)如何使用Spire.Doc for Java將 Word 轉(zhuǎn)換為流行的圖像格式,例如JPG、PNG和SVG。
Spire.Doc 是一款專門對(duì) Word 文檔進(jìn)行操作的 類庫。在于幫助開發(fā)人員無需安裝 Microsoft Word情況下,輕松快捷高效地創(chuàng)建、編輯、轉(zhuǎn)換和打印 Microsoft Word 文檔。擁有近10年專業(yè)開發(fā)經(jīng)驗(yàn)Spire系列辦公文檔開發(fā)工具,專注于創(chuàng)建、編輯、轉(zhuǎn)換和打印Word/PDF/Excel等格式文件處理,小巧便捷,除此之外,你也可以瀏覽E-iceblue 旗下其他不同語言的文檔開發(fā)工具~
Spire.Doc for Java中文資源,最新版免費(fèi)下載,在線文檔,視頻教程,技術(shù)支持,Spire.Doc for Java正版購買-慧都網(wǎng)
首先,您需要將 Spire.Doc.jar 文件添加為 Java 程序中的依賴項(xiàng)。可以從此鏈接下載 JAR 文件。如果您使用 Maven,則可以通過將以下代碼添加到項(xiàng)目的 pom.xml 文件中來輕松將 JAR 文件導(dǎo)入到應(yīng)用程序中。
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://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()方法將整個(gè) Word 文檔轉(zhuǎn)換為單獨(dú)的BufferedImage圖像。然后,每個(gè) BufferedImage 都可以保存為BMP、EMF、JPEG、PNG、GIF或WMF文件。以下是使用此庫將 Word 轉(zhuǎn)換為 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 文檔保存為字節(jié)數(shù)組列表。然后可以將每個(gè)字節(jié)數(shù)組寫入 SVG 文件。將Word轉(zhuǎn)換為SVG的詳細(xì)步驟如下。
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 轉(zhuǎn)換為 PNG 時(shí)自定義圖像分辨率。
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 轉(zhuǎn)換為圖像,如果您有其他問題也可以繼續(xù)瀏覽本系列文章,獲取相關(guān)教程~
ava是在1990年由James Gosling創(chuàng)建的,其初衷是作為智能設(shè)備(如交互式電視、無所不能的烤箱、時(shí)間旅行的終結(jié)者、奴役人類的SkyNet軍用衛(wèi)星等)的大腦。Gosling對(duì)其使用C++編寫的程序感到失望,他靈機(jī)一動(dòng),決定躲在辦公室開發(fā)一種更適合其需求的新語言。
當(dāng)時(shí)交互式電視已成為一個(gè)具有數(shù)百萬美元產(chǎn)值的行業(yè),該語言成為公司進(jìn)入該行業(yè)的發(fā)展策略的一部分。可到今天這些都沒成為現(xiàn)實(shí)(盡管Netflix、Hulu以及其他公司都在向游戲領(lǐng)域進(jìn)軍),但Gosling發(fā)明的新語言卻發(fā)生了重大變化。
正當(dāng)Oak開發(fā)小組即將被放棄之時(shí),Web開始流行起來。正是陰差陽錯(cuò),使Gosling發(fā)明的語言適合家用電器的特性也使其適用于Web。Gosling的團(tuán)隊(duì)設(shè)計(jì)了能夠讓程序在Web頁面安全運(yùn)行的方法,而且為了與該語言的新用途相匹配,為之選了一個(gè)引人注目的新名字:Java。
Java語言的最初目標(biāo)之一就是要比C++容易掌握,James Gosling于20世紀(jì)90年代在他的智能家電項(xiàng)目中使用的就是C++。Java的很大一部分都是基于C++的,因此學(xué)習(xí)過C++語言編程的人學(xué)習(xí)起Java來也不困難。然而,C++中有些難以學(xué)習(xí)和難以正確使用的內(nèi)容已經(jīng)從Java中刪除。
Android成為Java語言使用最為廣闊的一個(gè)領(lǐng)域。在掌握了Java語言之后,你可以使用Android軟件開發(fā)包(SDK)開發(fā)自己的app。Android SDK是一款可以在Windows、Mac OS和Linux上運(yùn)行的免費(fèi)編程套件。
下載地址(下載時(shí)要用郵箱申請(qǐng)一個(gè)帳號(hào)):
https://www.oracle.com/java/technologies/downloads/#java8-windows
jdk-8u321-windows-i586.exe 162M
安裝:
1.1 可選組件
1.2 可在安裝完成后訪問教程:
https://docs.oracle.com/javase/8/docs/
安裝后文件夾大小:
安裝好JDK后就可以用cmd來編譯和運(yùn)行java程序了。
編譯.java使用javac.exe,編譯后生成同名.class(跨平臺(tái)),用java.exe運(yùn)行,兩個(gè)程序如下安裝路徑:
"C:\Program Files\Java\jdk1.8.0_321\bin\javac.exe"
"C:\Program Files\Java\jre1.8.0_321\bin\java.exe"
ref:
https://docs.oracle.com/javase/tutorial/getStarted/cupojava/win32.html
2.1 Create a Source File,擴(kuò)展名為.java
class HelloWorldApp {
public static void main(String[] args) {
System.out.println("Hello World!"); // Display the string.
}
}
2.2 Compile the Source File into a .class File
windows徽標(biāo)鍵+R,運(yùn)行CMD,輸入:
"C:\Program Files\Java\jdk1.8.0_321\bin\javac.exe" "f:\java\HelloWorldApp.java"
路徑有空格,加上雙引號(hào)。
在.java相同路徑下生成同名.class文件。
2.3 run .class file
再在CMD輸入:cd \d f:\java
使用.java所在路徑為CMD當(dāng)前路徑,再輸入:
"C:\Program Files\Java\jre1.8.0_321\bin\java.exe" "HelloWorldApp"
即可運(yùn)行.class文件。
2.4 配置環(huán)境變量(簡化路徑輸入)
以上運(yùn)行java.exe和javac.exe老是要在前面加一串路徑字符串,不方便,這就要配置環(huán)境變量了,這樣在CMD直接輸入java和javac時(shí),CMD就會(huì)在環(huán)境變量指示的路徑下搜索java和javac的路徑,以確定是不是內(nèi)部變量和外部程序。
2.4.1 將javac.exe所在路徑配置到path
2.4.2 將java.exe所在路徑配置為JAVA
如果是Vista、Win7、Win8系統(tǒng),使用鼠標(biāo)右擊“計(jì)算機(jī)”->屬性->左側(cè)高級(jí)系統(tǒng)設(shè)置->高級(jí)->環(huán)境變量
系統(tǒng)變量->新建->變量名:JAVA_HOME 變量值:JDK安裝目錄
系統(tǒng)變量->新建->變量名:CLASSPATH 變量值:.;%JAVA_HOME%\lib
系統(tǒng)變量->編輯->變量名:Path 在變量值的最前面加上:%JAVA_HOME%\bin;
至于exe文件?為什么要用java來做exe文件?java的核心是它的跨平臺(tái),而exe只是運(yùn)行在win環(huán)境下。
盡管可以只使用Java開發(fā)工具包(JDK)和文本編輯器來開發(fā)Java 程序,但是如果使用IDE,由此帶來的編程體驗(yàn)會(huì)更好。
NetBeans是Oracle為Java程序員提供的一款免費(fèi)IDE,它可以更容易地組織、編寫、編譯和測試使用Java開發(fā)的軟件。NetBeans集成了編輯器、圖形界面編譯、調(diào)試、運(yùn)行等工具,包含一個(gè)項(xiàng)目和文件管理器、圖形用戶界面設(shè)計(jì)器,以及許多其他工具。它的一個(gè)殺手級(jí)特性是用戶在輸入代碼時(shí),其代碼編輯器能夠自動(dòng)檢測到Java語法錯(cuò)誤。
3.1 下載
https://pan.baidu.com/s/1qYZFRre#list/path=%2F
netbeans-8.2-windows.exe:231M
3.2 安裝
安裝時(shí)會(huì)檢查是否安裝了JDK,需要先安裝JDK。
NetBeans對(duì)3個(gè)版本的Java語言都提供支持,這3個(gè)版本是:JavaStandard Edition(JSE)、Java Enterprise Edition(JEE)和JavaMobile Edition(JME)。它還支持Web應(yīng)用開發(fā)、Web服務(wù)和JavaBeans。
Java Enterprise Edition是Java Standard Edition的一個(gè)擴(kuò)展,它包含了支持Enterprise JavaBeans、XML處理和servlet開發(fā)的包,其中servlet是運(yùn)行在Web服務(wù)器上的Java程序。Java EE還包含一個(gè)應(yīng)用服務(wù)器,這是一個(gè)復(fù)雜的環(huán)境,用來執(zhí)行專門為企業(yè)和其他大型組織開發(fā)的具有大量計(jì)算需求的Java軟件。
篩選后可以減少安裝大小:
安裝后文件夾大小:
4.1 新建項(xiàng)目:
輸入項(xiàng)目名稱:
自動(dòng)生成框架代碼:
項(xiàng)目文件和文件夾:
源文件:
4.2 編譯、及編譯后的項(xiàng)目和文件夾:
編譯文件:
4.3 運(yùn)行
https://www.onlinegdb.com/
https://tool.lu/coderunner/
https://www.bejson.com/runcode/java/
demo(體會(huì)與C++語法的區(qū)別):
package first; // 用包組織類
// to group classes in a collection called a package.
// The standard Java library is distributed over a number of packages,
import java.util.*; // #include+using namespace+others
import java.io.*;
public class First { // C++類是全局的,無修飾
// Everything in a Java program must be inside a class.
// every class in Java extends Object.
// A source file can only contain one public class,
// and the names of the file and the public class must match.
private String city;
int ivar; // default scope: package
public static void main(String[] args) { // 一個(gè)程序需要某個(gè)類有main方法
// 任何Java程序都是一個(gè)類,擁有main的稱為主類
// 主類,程序名與類名一致時(shí),
// 其它類,可有main,用于單元測試,
//System.out.println(args[0]);// bound checking, not program itself
int $var=3; // C++變量只能以字母和下劃線開頭
String str="abc"; // C++ string是模板類的typedef
byte i=3; // byte,取值范圍為?128~127的整數(shù),signed char
boolean b=true; // bool
final int FIELDGOAL=3;// const
int bit=0b01011; //
System.out.println(str + "def" + bit + i + b);// 隱式轉(zhuǎn)換,C++ printf()
System.out.printf("%8.5f\n",1/0.618);
String declar; // only declaration
declar=new String("abc");
System.out.println(declar);
// switch不只是整型
String command="BUY";
int balance=550;
int quantity=42;
switch (command) {
case "BUY":
quantity +=5;
balance -=20;
break;
case "SELL":
quantity -=5;
balance +=15;
}
// A constant expression of type char, byte, short, or int
// An enumerated constant
// Starting with Java SE 7, a string literal
System.out.println("Balance: " + balance + "\n"
+ "Quantity: " + quantity);
// time
long startTime=System.currentTimeMillis();
System.out.println(startTime);
// c++: time_t first=time(NULL);
// 數(shù)組
int[] arr=new int[25]; // int *arr=new int[25];
// 對(duì)象與類型轉(zhuǎn)換
// Java中的所有對(duì)象都是Object類的子類
String[] names={"Spanky", "Alfalfa", "Buckwheat", "Daria",
"Stymie", "Marianne", "Scotty", "Tommy", "Chubby"};
ArrayList<String> list=new ArrayList<String>(); // vector<int> arr[25];
// the angle brackets cannot be a primitive type
for (i=0; i < names.length; i++) {
list.add(names[i]);
}
Collections.sort(list);
for (String name : list){
System.out.println(name);
}
int[][] magicSquare={
{16, 3, 2, 13},
{5, 10, 11, 8},
{9, 6, 7, 12},
{4, 15, 14, 1}};
for (i=0; i < magicSquare.length; i++)
{
for (int j=0; j < magicSquare[i].length; j++) {
System.out.printf("%d ",magicSquare[i][j]);
}
System.out.printf("\n");
}
//System.out.printf("%d ",magicSquare[8][8]);// bounding checking
// 哈希類
HashMap phonebook2=new HashMap(30, 0.7F); // 初始容量和負(fù)載因子
HashMap<String, Integer> phonebook=new HashMap<>(); // 指明鍵和值的類
phonebook.put("Butterball Turkey Line", 80028883);
int number=phonebook.get("Butterball Turkey Line");
File cookie=new File("cookie.web");
try (FileInputStream stream=new FileInputStream(cookie)) {
System.out.println("Length of file: " + cookie.length());
} catch (IOException ioe) {
System.out.println("Could not read file.");
}
// lambda
Comparator<String> comp
=(first, second) // Same as (String first, String second)
-> first.length() - second.length();
// object
// java data type
Integer xx=17; // Autoboxing (int -> Integer)
int aa=xx; // Unboxing (Integer -> int)
Moden md=new Moden(80);// All Java objects live on the heap
System.out.println(md.speed);
Demo.staticFunc();
Demo de=new Demo(); // return a object reference
de.instanceFunc();
/*
1 primitive types: boolean, byte, char, double, float, int, long, short;
2 reference types: A class type, interface type, or array type, such as String,
Charge, Comparable, or int[]. A variable of a reference type stores
an object reference, not the data-type value itself.
3 wrapper types: A reference type corresponding to one of the primitive types,
such as Boolean, Btye, Character(char), Double, Float, int(Integer), Long, Short.
Java automatically converts between an object from a wrapper type
and the corresponding primitive data-type value—in assignment statements,
method arguments, and arithmetic/logic expressions.
pass by value: Java's style of passing arguments to methods—
either as a data-type value (for primitive types)
or as an object reference (for reference types).
*/
// interface instance, lambda
double res=integrate(new GaussianPDF(), -1, 1, 1000);
System.out.println(res);
res=integrate(x -> x*x, 0, 10, 1000); // lambda expression
System.out.println(res);
}
public static double integrate(Function f,
double a, double b, int n) {
double delta=(b - a) / n;
double sum=0.0;
for (int i=0; i < n; i++) {
sum +=delta * f.evaluate(a + delta * (i + 0.5));
}
return sum;
}
}
// 繼承
class Moden {
int speed;
public Moden(int i) {
speed=i;
}
}
class cableModen extends Moden { // :public, All inheritance in Java is public inheritance
public int size;
public cableModen(int speed,int size){ // :Moden(speed){this.size=size;}
super(speed);
this.size=size;
}
}
abstract class Comparable2
{
private String datafiled;
public abstract int compareTo(Object other); //
}
// an abstract method is called a pure virtual function and is tagged with a trailing=0
interface Comparable
{
int compareTo(Object other); // All methods of an interface are automatically public.
default int compareTo2(Object other) { return 0; }
// By default, all elements are the same
}
class Demo{
public static void staticFunc(){
System.out.println("class method(static, class call)!");
}
public void instanceFunc(){
System.out.println("Instance method(object call)!");
}
}
interface Function
{
public abstract double evaluate(double x);
}
class Square implements Function
{
public double evaluate(double x)
{ return x*x; }
}
class GaussianPDF implements Function
{
public double evaluate(double x)
{ return Math.exp(-x*x/2) / Math.sqrt(2 * Math.PI); }
}
class LinkedStackOfStrings {
private Node first;
private class Node {
private String item;
private Node next;
}
public boolean isEmpty()
{ return (first==null); }
public void push(String item)
{ // Insert a new node at the beginning of the list.
Node oldFirst=first;
first=new Node();
first.item=item;
first.next=oldFirst;
}
public String pop() { // Remove the first node from the list and return item.
String item=first.item;
first=first.next;
return item;
}
}
/* Supplying instance fields and methods that operate on them
is the job of the classes that implement the interface.
being similar to an abstract class with no instance fields.
*/
/* Why can’t Comparable simply be an abstract class?
A class can only extend a single class(not support multiple inheritance.
But each class can implement as many interfaces.
C++ has multiple inheritance, similar to a Java class with a single superclass
and additional interfaces.
*/
// dynamic binding is the default behavior;
// if you do not want a method to be virtual, you tag it as final.
// Unlike C++, Java has no programmable operator overloading.
// Since Java does automatic garbage collection,
// manual memory reclamation is not needed, so Java does not support destructors.
// implements將類聲明為支持一個(gè)或多個(gè)監(jiān)聽接口 支持Runnable接口
// class LoadStocks implements Runnable {} //
// C++ virtual function override, implement abstract class
// 通過調(diào)用Thread構(gòu)造函數(shù)創(chuàng)建線程化類的對(duì)象;
// 通過調(diào)用start()方法啟動(dòng)線程。
附Java簡史:
1996年1月,Sun公司發(fā)布了Java的第一個(gè)開發(fā)工具包(JDK 1.0)。
1998年12月8日,第二代Java平臺(tái)的企業(yè)版J2EE發(fā)布(Enterprise Edition)。
2004年9月30日,J2SE1.5發(fā)布,成為Java語言發(fā)展史上的又一里程碑。為了表示該版本的重要性,J2SE 1.5更名為Java SE 5.0(內(nèi)部版本號(hào)1.5.0),代號(hào)為“Tiger”,Tiger包含了從1996年發(fā)布1.0版本以來的最重大的更新,其中包括泛型支持、基本類型的自動(dòng)裝箱、改進(jìn)的循環(huán)、枚舉類型、格式化I/O及可變參數(shù)。
2009年,甲骨文公司宣布收購Sun。
2014年,甲骨文公司發(fā)布了Java8正式版。新增lambda表達(dá)式、提供函數(shù)式接口。
2017年9月22 日,Java 9正式發(fā)布,帶來了很多新特性,其中最主要的變化是已經(jīng)實(shí)現(xiàn)的模塊化系統(tǒng)。此后,半年一次版本迭代。
Java SE 10 2018-03-14
Java SE 11 2018-09-26,長期支持版本(LTS, Long-Term-Support),將會(huì)獲得 5 年的技術(shù)支持。
Java SE 12 2019-03-20
Java SE 13 2019-09-17
Java SE 14 2020-03-17
Java SE 15 2020-09
Java SE 16 2021-03
Java SE 17 2021-09,長期支持版本,將會(huì)獲得 8 年的技術(shù)支持。
附關(guān)鍵字:
ref:
https://www.oracle.com/java/technologies/
https://jingyan.baidu.com/article/1612d500968640e20e1eeebb.html
https://baike.baidu.com/item/jdk/1011
-End-
前文中,我曾介紹過Spire.PDF for Java 從版本號(hào)4.8.7開始支持轉(zhuǎn)換PDF到OFD。目前,Spire.Doc for Java的最新版本4.12.1支持將Word轉(zhuǎn)換成OFD。本文將演示如何實(shí)現(xiàn)上述操作。
首先,需要在Java程序中添加Spire.Doc.jar文件作為依賴項(xiàng)。你可以從E-iceblue中文官網(wǎng)下載 JAR 文件;如果是使用Maven,則可以在pom.xml文件中添加如下代碼導(dǎo)入JAR文件:
<repositories>
<repository>
<id>com.e-iceblue</id>
<url>https://repo.e-iceblue.cn/repository/maven-public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.doc</artifactId>
<version>4.12.1</version>
</dependency>
</dependencies>
Spire.Doc for Java提供的Document.saveToFile()方法支持將Word轉(zhuǎn)換成OFD。以下是具體實(shí)現(xiàn)步驟:
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
public class ToOFD {
public static void main(String[] args) {
//創(chuàng)建Document實(shí)例
Document document=new Document();
//加載Word示例文檔
document.loadFromFile("C:\\Users\\Test1\\Desktop\\sample.docx");
//保存為OFD格式
document.saveToFile("output/ToOFD.ofd", FileFormat.OFD);
}
}
轉(zhuǎn)換前后對(duì)比:
*請(qǐng)認(rèn)真填寫需求信息,我們會(huì)在24小時(shí)內(nèi)與您取得聯(lián)系。