Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537
可能需要將 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 Java中文資源,最新版免費下載,在線文檔,視頻教程,技術支持,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>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()方法將整個 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 轉換為圖像,如果您有其他問題也可以繼續瀏覽本系列文章,獲取相關教程~
ava是在1990年由James Gosling創建的,其初衷是作為智能設備(如交互式電視、無所不能的烤箱、時間旅行的終結者、奴役人類的SkyNet軍用衛星等)的大腦。Gosling對其使用C++編寫的程序感到失望,他靈機一動,決定躲在辦公室開發一種更適合其需求的新語言。
當時交互式電視已成為一個具有數百萬美元產值的行業,該語言成為公司進入該行業的發展策略的一部分。可到今天這些都沒成為現實(盡管Netflix、Hulu以及其他公司都在向游戲領域進軍),但Gosling發明的新語言卻發生了重大變化。
正當Oak開發小組即將被放棄之時,Web開始流行起來。正是陰差陽錯,使Gosling發明的語言適合家用電器的特性也使其適用于Web。Gosling的團隊設計了能夠讓程序在Web頁面安全運行的方法,而且為了與該語言的新用途相匹配,為之選了一個引人注目的新名字:Java。
Java語言的最初目標之一就是要比C++容易掌握,James Gosling于20世紀90年代在他的智能家電項目中使用的就是C++。Java的很大一部分都是基于C++的,因此學習過C++語言編程的人學習起Java來也不困難。然而,C++中有些難以學習和難以正確使用的內容已經從Java中刪除。
Android成為Java語言使用最為廣闊的一個領域。在掌握了Java語言之后,你可以使用Android軟件開發包(SDK)開發自己的app。Android SDK是一款可以在Windows、Mac OS和Linux上運行的免費編程套件。
下載地址(下載時要用郵箱申請一個帳號):
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來編譯和運行java程序了。
編譯.java使用javac.exe,編譯后生成同名.class(跨平臺),用java.exe運行,兩個程序如下安裝路徑:
"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,擴展名為.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徽標鍵+R,運行CMD,輸入:
"C:\Program Files\Java\jdk1.8.0_321\bin\javac.exe" "f:\java\HelloWorldApp.java"
路徑有空格,加上雙引號。
在.java相同路徑下生成同名.class文件。
2.3 run .class file
再在CMD輸入:cd \d f:\java
使用.java所在路徑為CMD當前路徑,再輸入:
"C:\Program Files\Java\jre1.8.0_321\bin\java.exe" "HelloWorldApp"
即可運行.class文件。
2.4 配置環境變量(簡化路徑輸入)
以上運行java.exe和javac.exe老是要在前面加一串路徑字符串,不方便,這就要配置環境變量了,這樣在CMD直接輸入java和javac時,CMD就會在環境變量指示的路徑下搜索java和javac的路徑,以確定是不是內部變量和外部程序。
2.4.1 將javac.exe所在路徑配置到path
2.4.2 將java.exe所在路徑配置為JAVA
如果是Vista、Win7、Win8系統,使用鼠標右擊“計算機”->屬性->左側高級系統設置->高級->環境變量
系統變量->新建->變量名:JAVA_HOME 變量值:JDK安裝目錄
系統變量->新建->變量名:CLASSPATH 變量值:.;%JAVA_HOME%\lib
系統變量->編輯->變量名:Path 在變量值的最前面加上:%JAVA_HOME%\bin;
至于exe文件?為什么要用java來做exe文件?java的核心是它的跨平臺,而exe只是運行在win環境下。
盡管可以只使用Java開發工具包(JDK)和文本編輯器來開發Java 程序,但是如果使用IDE,由此帶來的編程體驗會更好。
NetBeans是Oracle為Java程序員提供的一款免費IDE,它可以更容易地組織、編寫、編譯和測試使用Java開發的軟件。NetBeans集成了編輯器、圖形界面編譯、調試、運行等工具,包含一個項目和文件管理器、圖形用戶界面設計器,以及許多其他工具。它的一個殺手級特性是用戶在輸入代碼時,其代碼編輯器能夠自動檢測到Java語法錯誤。
3.1 下載
https://pan.baidu.com/s/1qYZFRre#list/path=%2F
netbeans-8.2-windows.exe:231M
3.2 安裝
安裝時會檢查是否安裝了JDK,需要先安裝JDK。
NetBeans對3個版本的Java語言都提供支持,這3個版本是:JavaStandard Edition(JSE)、Java Enterprise Edition(JEE)和JavaMobile Edition(JME)。它還支持Web應用開發、Web服務和JavaBeans。
Java Enterprise Edition是Java Standard Edition的一個擴展,它包含了支持Enterprise JavaBeans、XML處理和servlet開發的包,其中servlet是運行在Web服務器上的Java程序。Java EE還包含一個應用服務器,這是一個復雜的環境,用來執行專門為企業和其他大型組織開發的具有大量計算需求的Java軟件。
篩選后可以減少安裝大小:
安裝后文件夾大小:
4.1 新建項目:
輸入項目名稱:
自動生成框架代碼:
項目文件和文件夾:
源文件:
4.2 編譯、及編譯后的項目和文件夾:
編譯文件:
4.3 運行
https://www.onlinegdb.com/
https://tool.lu/coderunner/
https://www.bejson.com/runcode/java/
demo(體會與C++語法的區別):
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) { // 一個程序需要某個類有main方法
// 任何Java程序都是一個類,擁有main的稱為主類
// 主類,程序名與類名一致時,
// 其它類,可有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的整數,signed char
boolean b=true; // bool
final int FIELDGOAL=3;// const
int bit=0b01011; //
System.out.println(str + "def" + bit + i + b);// 隱式轉換,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);
// 數組
int[] arr=new int[25]; // int *arr=new int[25];
// 對象與類型轉換
// Java中的所有對象都是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); // 初始容量和負載因子
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將類聲明為支持一個或多個監聽接口 支持Runnable接口
// class LoadStocks implements Runnable {} //
// C++ virtual function override, implement abstract class
// 通過調用Thread構造函數創建線程化類的對象;
// 通過調用start()方法啟動線程。
附Java簡史:
1996年1月,Sun公司發布了Java的第一個開發工具包(JDK 1.0)。
1998年12月8日,第二代Java平臺的企業版J2EE發布(Enterprise Edition)。
2004年9月30日,J2SE1.5發布,成為Java語言發展史上的又一里程碑。為了表示該版本的重要性,J2SE 1.5更名為Java SE 5.0(內部版本號1.5.0),代號為“Tiger”,Tiger包含了從1996年發布1.0版本以來的最重大的更新,其中包括泛型支持、基本類型的自動裝箱、改進的循環、枚舉類型、格式化I/O及可變參數。
2009年,甲骨文公司宣布收購Sun。
2014年,甲骨文公司發布了Java8正式版。新增lambda表達式、提供函數式接口。
2017年9月22 日,Java 9正式發布,帶來了很多新特性,其中最主要的變化是已經實現的模塊化系統。此后,半年一次版本迭代。
Java SE 10 2018-03-14
Java SE 11 2018-09-26,長期支持版本(LTS, Long-Term-Support),將會獲得 5 年的技術支持。
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,長期支持版本,將會獲得 8 年的技術支持。
附關鍵字:
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 從版本號4.8.7開始支持轉換PDF到OFD。目前,Spire.Doc for Java的最新版本4.12.1支持將Word轉換成OFD。本文將演示如何實現上述操作。
首先,需要在Java程序中添加Spire.Doc.jar文件作為依賴項。你可以從E-iceblue中文官網下載 JAR 文件;如果是使用Maven,則可以在pom.xml文件中添加如下代碼導入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轉換成OFD。以下是具體實現步驟:
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
public class ToOFD {
public static void main(String[] args) {
//創建Document實例
Document document=new Document();
//加載Word示例文檔
document.loadFromFile("C:\\Users\\Test1\\Desktop\\sample.docx");
//保存為OFD格式
document.saveToFile("output/ToOFD.ofd", FileFormat.OFD);
}
}
轉換前后對比:
*請認真填寫需求信息,我們會在24小時內與您取得聯系。