Microsoft Publisher 檔案廣泛用於傳單、手冊、電子報以及其他行銷素材。當您批量處理這些檔案時,通常需要在不開啟 Publisher 的情況下了解其內容。例如,您可能想檢查頁面尺寸、找出出版物所需的字型,或列出每個檔案的建立者。
在本教程中,您將學習如何使用 Aspose.PUB for Java 在 Java 中讀取 PUB 檔案。您將取得 PUB 檔案的頁面大小、欄位、字型、顏色和文件屬性,並且還會學習如何從串流中讀取 PUB 檔案。
用於讀取 PUB 檔案的 Java 函式庫
Aspose.PUB for Java 是一個用於在 Java 應用程式中讀取 Microsoft Publisher PUB 檔案並將其轉換為 PDF 的函式庫。它可在不安裝 Microsoft Publisher 的情況下運作,並讓您透過 Document 類別存取出版物的內容與屬性。
將以下儲存庫和相依性新增至您專案的 pom.xml 檔案中:
<repositories>
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java API</name>
<url>https://releases.aspose.com/java/repo/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-pub</artifactId>
<version>22.8</version>
</dependency>
</dependencies>
您也可以從發行區段下載 JAR。
在 Java 中讀取 PUB 檔案
遵循以下步驟在 Java 中讀取 PUB 檔案:
- 使用 PubFactory.createParser() 方法載入 PUB 檔案並建立解析器。
- 使用
parse()方法將檔案解析為Document物件。 - 使用
getWidth()與getHeight()方法讀取頁面尺寸。 - 使用
getFieldCount()方法讀取欄位數量。 - 使用
getFontNames()與getColors()方法讀取字型與顏色。
以下 Java 程式碼示範如何讀取 PUB 檔案:
import com.aspose.pub.Document;
import com.aspose.pub.IPubParser;
import com.aspose.pub.PubFactory;
import java.awt.Color;
public class ReadPubFile {
public static void main(String[] args) {
// Path to the documents directory
String dataDir = "C:\\Files\\";
// Load the PUB file and create a parser
IPubParser parser = PubFactory.createParser(dataDir + "halloween-flyer.pub");
// Parse the file into a Document object
Document document = parser.parse();
// Read the page size (1 inch = 914400 EMUs)
System.out.println("Page width: " + document.getWidth() / 914400.0 + " inches");
System.out.println("Page height: " + document.getHeight() / 914400.0 + " inches");
// Read the number of fields
System.out.println("Field count: " + document.getFieldCount());
// Read the fonts used in the document
System.out.println("Fonts used in document: " + String.join(", ", document.getFontNames()));
// Read the colors used in the document
System.out.println("Colors used in document:");
for (Color color : document.getColors()) {
System.out.println(" RGB(" + color.getRed() + ", " + color.getGreen() + ", " + color.getBlue() + ")");
}
}
}
Page width: 8.5 inches
Page height: 11.0 inches
Field count: 20
Fonts used in document: Calibri, Sylfaen, Tahoma, Mangal, Vrinda, Raavi, Shruti, Kalinga, Latha, Gautami, Tunga, Kartika, BrowalliaUPC, DokChampa, Microsoft Himalaya, Malgun Gothic, MS Mincho, PMingLiU, SimSun, Microsoft Yi Baiti, Estrangelo Edessa, MV Boli, Iskoola Pota, Nyala, Plantagenet Cherokee, Euphemia Regular CAS, DaunPenh, Mongolian Baiti, Cambria, Times New Roman, EucrosiaUPC, Batang, Symbol, skullphabet, Black Rose
Colors used in document:
RGB(0, 0, 0)
RGB(0, 0, 0)
RGB(255, 255, 255)
RGB(192, 0, 0)
getWidth() 和 getHeight() 方法返回以 EMU(英制公制單位)為單位的頁面大小。代碼將每個值除以 914400 以轉換為英寸,這表明示例傳單使用的是 US Letter 頁面大小。
字型清單包含出版物中引用的所有字型。使用它來檢查在轉換或列印檔案之前必須安裝哪些字型。
在 Java 中讀取 PUB 文件屬性
PUB 檔案也會儲存文件屬性,例如標題、作者和關鍵字。請依照以下步驟讀取它們:
- 載入並解析 PUB 檔案。
- 使用
getSummaryInfo()方法取得摘要資訊,並讀取標題、主旨、作者、最後作者、關鍵字和註釋。 - 使用
getDocumentSummaryInfo()方法取得文件摘要資訊,並讀取類別、公司和語言。
以下 Java 程式碼示範如何讀取 PUB 檔案的文件屬性:
import com.aspose.pub.DocSummaryInfo;
import com.aspose.pub.Document;
import com.aspose.pub.PubFactory;
import com.aspose.pub.SummaryInfo;
public class ReadPubProperties {
public static void main(String[] args) {
// Path to the documents directory
String dataDir = "C:\\Files\\";
// Load and parse the PUB file
Document document = PubFactory.createParser(dataDir + "halloween-flyer.pub").parse();
// Read the summary information
SummaryInfo summary = document.getSummaryInfo();
System.out.println("Title: " + summary.getTitle());
System.out.println("Subject: " + summary.getSubject());
System.out.println("Author: " + summary.getAuthor());
System.out.println("Last Author: " + summary.getLastAuthor());
System.out.println("Keywords: " + summary.getKeywords());
System.out.println("Comments: " + summary.getComments());
// Read the document summary information
DocSummaryInfo docSummary = document.getDocumentSummaryInfo();
System.out.println("Category: " + docSummary.getCategory());
System.out.println("Company: " + docSummary.getCompany());
System.out.println("Language: " + docSummary.getLanguage());
}
}
Title:
Subject:
Author: Authorized User
Last Author:
Keywords:
Comments:
Category:
Company:
Language:
示例傳單僅存儲作者名稱,其他屬性為空。當屬性在原始出版物中從未設置時,該屬性為空。要更新這些值,請參閱 在 Java 中讀取和編輯 PUB 元數據。
在 Java 中從串流讀取 PUB 檔案
您也可以從 InputStream 讀取 PUB 檔案。當檔案來自網路上傳、資料庫或雲端儲存,而非本機磁碟時,這非常有用。
按照以下步驟從流中讀取 PUB 檔案:
- 將 PUB 檔案作為
InputStream開啟。 - 將該串流傳遞給
PubFactory.createParser()方法。 - 使用
parse()方法解析文件並讀取其資訊。
以下 Java 程式碼示範如何從串流讀取 PUB 檔案:
import com.aspose.pub.Document;
import com.aspose.pub.PubFactory;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
public class ReadPubFromStream {
public static void main(String[] args) throws IOException {
// Path to the documents directory
String dataDir = "C:\\Files\\";
// Open the PUB file as a stream
try (InputStream stream = new FileInputStream(dataDir + "halloween-flyer.pub")) {
// Create a parser from the stream and parse the document
Document document = PubFactory.createParser(stream).parse();
// Read the document information
System.out.println("Page width: " + document.getWidth() / 914400.0 + " inches");
System.out.println("Page height: " + document.getHeight() / 914400.0 + " inches");
System.out.println("Author: " + document.getSummaryInfo().getAuthor());
}
}
}
Page width: 8.5 inches
Page height: 11.0 inches
Author: Authorized User
解析完 PUB 檔案後,您還可以將其轉換為 PDF。了解更多資訊,請參閱 Convert PUB to PDF in Java。
獲取免費授權
您可以請求一個免費的臨時許可證以在無限制的情況下評估 Aspose.PUB for Java。請在執行範例之前於您的應用程式中套用該許可證。
讀取 Publisher 檔案:免費資源
了解更多有關使用這些資源處理 Publisher 檔案的資訊:
結論
您現在可以使用 Aspose.PUB for Java 在 Java 中讀取 PUB 檔案。使用 Document 類別即可從檔案或串流取得出版物的頁面大小、欄位、字型、顏色以及文件屬性。如有任何問題,請在我們的 免費支援論壇 發表。
常見問題
如何在 Java 中讀取 PUB 檔案?
使用 PubFactory.createParser() 建立解析器,呼叫 parse() 取得 Document 物件,然後讀取其屬性,例如 getWidth()、getHeight()、getFontNames() 和 getColors()。
頁面寬度與高度以什麼單位返回?
getWidth() 和 getHeight() 方法返回 EMU(English Metric Units)的值。將該值除以 914400 可得到英寸尺寸。
我可以在 Java 中從串流讀取 PUB 檔案嗎?
是的。將 InputStream 傳遞給 PubFactory.createParser(),並呼叫 parse() 以取得 Document 物件。
如何在 Java 中讀取 PUB 檔案的標題和作者?
呼叫 document.getSummaryInfo() 並使用其 getTitle()、getAuthor()、getSubject() 和 getKeywords() 方法。
我需要 Microsoft Publisher 來閱讀 PUB 檔案嗎?
No. Aspose.PUB for Java 可在沒有 Microsoft Publisher 或任何其他軟體的情況下讀取 PUB 檔案。
