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 文件:

  1. 使用 PubFactory.createParser() 方法加载 PUB 文件并创建解析器。
  2. 使用 parse() 方法将文件解析为 Document 对象。
  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() 方法返回页面尺寸,单位为 EMU(English Metric Units)。代码将每个值除以 914400 以转换为英寸,这表明示例传单使用的是 US Letter 页面尺寸。

字体列表包含出版物中引用的所有字体。使用它来检查在转换或打印文件之前必须安装哪些字体。

在 Java 中读取 PUB 文档属性

A 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:

示例传单仅存储作者名称,因此其他属性为空。当属性在原始出版物中从未设置时,它们就是空的。要更新这些值,请参阅 在 Java 中读取和编辑 PUB 元数据。

在 Java 中从流读取 PUB 文件

您也可以从 InputStream 读取 PUB 文件。当文件来自网页上传、数据库或云存储而不是本地磁盘时,这非常有用。

按照以下步骤从流中读取 PUB 文件:

  1. 将 PUB 文件作为 InputStream 打开。
  2. 将流传递给 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。了解更多,请参阅 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 文件。

另请参见