קבצי 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 class.

הוסף את המאגר והתלות הבאים לקובץ 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:

  1. טען את קובץ ה‑PUB וצור מפענח באמצעות המתודה PubFactory.createParser() .
  2. פענח את הקובץ לאובייקט Document באמצעות המתודה parse() .
  3. קרא את גודל העמוד באמצעות המתודות getWidth() ו‑getHeight() .
  4. קרא את מספר השדות באמצעות המתודה getFieldCount() .
  5. קרא את הגופנים והצבעים באמצעות המתודות 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() מחזירים את גודל העמוד ב‑EMUs (English Metric Units). הקוד מחלק כל ערך ב‑914400 כדי להמיר אינצ’ים, מה שמראה שהדף לדוגמה משתמש בגודל העמוד US Letter.

רשימת הגופנים כוללת כל גופן שמופיע בפרסום. השתמש בה כדי לבדוק אילו גופנים חייבים להיות מותקנים לפני שאתה ממיר או מדפיס את הקובץ.

קריאת מאפייני מסמך PUB ב-Java

קובץ PUB גם מאחסן מאפייני מסמך, כגון הכותרת, המחבר והמילות מפתח. פעל לפי הצעדים הבאים כדי לקרוא אותם:

  1. טען ופרש את קובץ ה-PUB.
  2. קבל את מידע הסיכום באמצעות המתודה getSummaryInfo() וקרא את הכותרת, הנושא, המחבר, המחבר האחרון, מילות המפתח וההערות.
  3. קבל את מידע סיכום המסמך באמצעות המתודה 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 מזרם:

  1. פתח את קובץ ה‑PUB כ‑InputStream.
  2. העבר את ה‑stream למתודה PubFactory.createParser().
  3. נתח את המסמך באמצעות המתודה 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?

צור parser באמצעות PubFactory.createParser(), קרא parse() כדי לקבל אובייקט Document, ולאחר מכן קרא את המאפיינים שלו, כגון getWidth(), getHeight(), getFontNames(), ו‑getColors().

באיזו יחידה מוחזרים רוחב וגובה העמוד?

המתודות getWidth() ו‑getHeight() מחזירות ערכים ב‑EMUs (English Metric Units). חלקו את הערך ב‑914400 כדי לקבל את הגודל באינצ’ים.

האם ניתן לקרוא קובץ PUB מזרם ב-Java?

כן. העבר InputStream ל‑PubFactory.createParser() וקרא parse() כדי לקבל את האובייקט Document.

איך אני קורא את הכותרת והמחבר של קובץ PUB ב-Java?

הקרא document.getSummaryInfo() והשתמש במתודות getTitle(), getAuthor(), getSubject() ו-getKeywords() שלה.

האם אני צריך Microsoft Publisher כדי לקרוא קבצי PUB?

לא. Aspose.PUB for Java קורא קבצי PUB ללא Microsoft Publisher או כל תוכנה אחרת.

ראה גם