Microsoft Publisher files are widely used for flyers, brochures, newsletters, and other marketing material. When you process these files in bulk, you often need to know what’s inside them without opening Publisher. For example, you might want to check the page size, find the fonts a publication needs, or list who created each file.

In this tutorial, you’ll learn how to read PUB files in Java using Aspose.PUB for Java. You’ll get the page size, fields, fonts, colors, and document properties of a PUB file, and you’ll also learn how to read a PUB file from a stream.

Java Library to Read PUB Files

Aspose.PUB for Java is a library for reading Microsoft Publisher PUB files and converting them to PDF in Java applications. It works without Microsoft Publisher and gives you access to the content and properties of a publication through the Document class.

Add the following repository and dependency to your project’s pom.xml file:

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

You can also download the JAR from the releases section.

Read PUB File in Java

Follow these steps to read a PUB file in Java:

  1. Load the PUB file and create a parser using the PubFactory.createParser() method.
  2. Parse the file into a Document object using the parse() method.
  3. Read the page size using the getWidth() and getHeight() methods.
  4. Read the number of fields using the getFieldCount() method.
  5. Read the fonts and colors using the getFontNames() and getColors() methods.

The following Java code shows how to read a PUB file:

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)

The getWidth() and getHeight() methods return the page size in EMUs (English Metric Units). The code divides each value by 914400 to convert it to inches, which shows that the sample flyer uses the US Letter page size.

The font list includes every font referenced in the publication. Use it to check which fonts must be installed before you convert or print the file.

Read PUB Document Properties in Java

A PUB file also stores document properties, such as the title, author, and keywords. Follow these steps to read them:

  1. Load and parse the PUB file.
  2. Get the summary information using the getSummaryInfo() method and read the title, subject, author, last author, keywords, and comments.
  3. Get the document summary information using the getDocumentSummaryInfo() method and read the category, company, and language.

The following Java code shows how to read the document properties of a PUB file:

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:

The sample flyer only stores the author name, so the other properties are empty. A property is empty when it was never set in the original publication. To update these values, see Read and Edit PUB Metadata in Java.

Read PUB File from Stream in Java

You can also read a PUB file from an InputStream. This is useful when the file comes from a web upload, a database, or cloud storage instead of the local disk.

Follow these steps to read a PUB file from a stream:

  1. Open the PUB file as an InputStream.
  2. Pass the stream to the PubFactory.createParser() method.
  3. Parse the document using the parse() method and read its information.

The following Java code shows how to read a PUB file from a stream:

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

Once you’ve parsed a PUB file, you can also convert it to PDF. Learn more in Convert PUB to PDF in Java.

Get a Free License

You can request a free temporary license to evaluate Aspose.PUB for Java without limitations. Apply the license in your application before running the examples.

Read Publisher File: Free Resources

Learn more about working with Publisher files using these resources:

Conclusion

You can now read PUB files in Java using Aspose.PUB for Java. Use the Document class to get the page size, fields, fonts, colors, and document properties of a publication, from a file or a stream. If you have any questions, post them on our free support forum.

Frequently Asked Questions

How do I read a PUB file in Java?

Create a parser using PubFactory.createParser(), call parse() to get a Document object, and then read its properties, such as getWidth(), getHeight(), getFontNames(), and getColors().

In which unit are the page width and height returned?

The getWidth() and getHeight() methods return values in EMUs (English Metric Units). Divide the value by 914400 to get the size in inches.

Can I read a PUB file from a stream in Java?

Yes. Pass an InputStream to PubFactory.createParser() and call parse() to get the Document object.

How do I read the title and author of a PUB file in Java?

Call document.getSummaryInfo() and use its getTitle(), getAuthor(), getSubject(), and getKeywords() methods.

Do I need Microsoft Publisher to read PUB files?

No. Aspose.PUB for Java reads PUB files without Microsoft Publisher or any other software.

See Also