ไฟล์ Microsoft Publisher ถูกใช้กันอย่างแพร่หลายสำหรับใบปลิว โบรชัวร์ จดหมายข่าว และสื่อการตลาดอื่น ๆ เมื่อคุณประมวลผลไฟล์เหล่านี้เป็นจำนวนมาก คุณมักต้องการทราบข้อมูลภายในโดยไม่ต้องเปิด Publisher ตัวอย่างเช่น คุณอาจต้องการตรวจสอบขนาดหน้า ค้นหาแบบอักษรที่ต้องใช้ในเอกสาร หรือแสดงรายชื่อผู้สร้างแต่ละไฟล์
ในบทเรียนนี้ คุณจะได้เรียนรู้วิธี อ่านไฟล์ PUB ใน Java โดยใช้ Aspose.PUB for Java. คุณจะได้รับขนาดหน้า, ฟิลด์, ฟอนต์, สี, และคุณสมบัติของเอกสารในไฟล์ PUB, และคุณยังจะได้เรียนรู้วิธีอ่านไฟล์ PUB จากสตรีมด้วย.
ไลบรารี Java เพื่ออ่านไฟล์ PUB
Aspose.PUB for Java เป็นไลบรารีสำหรับอ่านไฟล์ Microsoft Publisher PUB และแปลงเป็น PDF ในแอปพลิเคชัน Java มันทำงานได้โดยไม่ต้องใช้ Microsoft Publisher และให้คุณเข้าถึงเนื้อหาและคุณสมบัติของการเผยแพร่ผ่านคลาส Document .
เพิ่ม repository และ dependency ต่อไปนี้ลงในไฟล์ 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 จากส่วนของการปล่อยได้.
อ่านไฟล์ PUB ใน Java
ทำตามขั้นตอนต่อไปนี้เพื่ออ่านไฟล์ PUB ใน Java:
- โหลดไฟล์ PUB และสร้าง parser โดยใช้เมธอด PubFactory.createParser()
- แปลงไฟล์เป็นอ็อบเจ็กต์
Documentโดยใช้เมธอดparse() - อ่านขนาดหน้าโดยใช้เมธอด
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 (English Metric Units) โค้ดจะหารค่าทั้งสองด้วย 914400 เพื่อแปลงเป็นนิ้ว ซึ่งแสดงว่าตัวอย่างแผ่นพับใช้ขนาดหน้ากระดาษ US Letter.
รายการฟอนต์รวมถึงฟอนต์ทุกตัวที่อ้างอิงในเอกสาร ใช้เพื่อตรวจสอบว่าฟอนต์ใดต้องติดตั้งก่อนที่คุณจะทำการแปลงหรือพิมพ์ไฟล์.
อ่านคุณสมบัติของเอกสาร PUB ใน Java
ไฟล์ 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:
ตัวอย่างใบปลิวนี้จะเก็บเฉพาะชื่อผู้เขียนเท่านั้น ดังนั้นคุณสมบัติอื่น ๆ จะว่างเปล่า คุณสมบัติจะว่างเมื่อไม่เคยตั้งค่าไว้ในงานตีพิมพ์ต้นฉบับ เพื่ออัปเดตค่าดังกล่าว ดูที่ อ่านและแก้ไขเมตาดาต้า PUB ใน Java.
อ่านไฟล์ PUB จากสตรีมใน Java
คุณยังสามารถอ่านไฟล์ PUB จาก InputStream ได้อีกด้วย นี่เป็นประโยชน์เมื่อไฟล์มาจากการอัปโหลดผ่านเว็บ ฐานข้อมูล หรือที่เก็บข้อมูลบนคลาวด์แทนที่จะเป็นจากดิสก์ในเครื่อง
ทำตามขั้นตอนต่อไปนี้เพื่ออ่านไฟล์ 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 ได้อีกด้วย เรียนรู้เพิ่มเติมใน แปลง PUB เป็น PDF ด้วย Java.
รับใบอนุญาตฟรี
คุณสามารถขอรับ ใบอนุญาตชั่วคราวฟรี เพื่อประเมิน Aspose.PUB for Java โดยไม่มีข้อจำกัด ใส่ใบอนุญาตในแอปพลิเคชันของคุณก่อนรันตัวอย่าง
อ่านไฟล์ Publisher: แหล่งข้อมูลฟรี
เรียนรู้เพิ่มเติมเกี่ยวกับการทำงานกับไฟล์ Publisher ด้วยทรัพยากรเหล่านี้:
สรุป
คุณสามารถอ่านไฟล์ PUB ใน Java ได้แล้วโดยใช้ Aspose.PUB for Java. ใช้คลาส Document เพื่อรับขนาดหน้า, ฟิลด์, ฟอนต์, สี, และคุณสมบัติของเอกสารของการเผยแพร่, จากไฟล์หรือสตรีม. หากคุณมีคำถามใด ๆ, โปรดโพสต์ใน ฟอรั่มสนับสนุนฟรี ของเรา.
คำถามที่พบบ่อย
ฉันจะอ่านไฟล์ PUB ใน Java อย่างไร?
สร้างตัวแยกวิเคราะห์โดยใช้ PubFactory.createParser() เรียก parse() เพื่อรับอ็อบเจกต์ Document จากนั้นอ่านคุณสมบัติต่าง ๆ เช่น getWidth() , getHeight() , getFontNames() และ getColors()
หน่วยใดที่ความกว้างและความสูงของหน้าได้รับการส่งคืน?
getWidth() และ getHeight() เมธอดจะคืนค่าที่เป็นหน่วย EMUs (English Metric Units) แบ่งค่าด้วย 914400 เพื่อให้ได้ขนาดเป็นนิ้ว.
ฉันสามารถอ่านไฟล์ PUB จากสตรีมใน Java ได้หรือไม่?
ใช่. ส่ง InputStream ไปยัง PubFactory.createParser() และเรียก parse() เพื่อรับอ็อบเจกต์ Document.
ฉันจะอ่านชื่อเรื่องและผู้เขียนของไฟล์ PUB ใน Java อย่างไร?
Call document.getSummaryInfo() และใช้เมธอด getTitle(), getAuthor(), getSubject(), และ getKeywords() ของมัน.
ฉันต้องใช้ Microsoft Publisher เพื่ออ่านไฟล์ PUB หรือไม่?
เลขที่ Aspose.PUB for Java สามารถอ่านไฟล์ PUB ได้โดยไม่ต้องใช้ Microsoft Publisher หรือซอฟต์แวร์อื่นใด
