Microsoft Publisher 파일은 전단지, 브로셔, 뉴스레터 및 기타 마케팅 자료에 널리 사용됩니다. 이러한 파일을 대량으로 처리할 때는 Publisher를 열지 않고도 내부 내용을 파악해야 할 경우가 많습니다. 예를 들어 페이지 크기를 확인하거나, 출판물에 필요한 글꼴을 찾거나, 각 파일을 만든 사람을 목록화하고 싶을 수 있습니다.

이 튜토리얼에서는 Aspose.PUB for Java를 사용하여 Java에서 PUB 파일을 읽는 방법을 배웁니다. PUB 파일의 페이지 크기, 필드, 폰트, 색상 및 문서 속성을 가져올 수 있으며, 스트림에서 PUB 파일을 읽는 방법도 배울 수 있습니다.

PUB 파일을 읽는 Java 라이브러리

Aspose.PUB for Java 은 Microsoft Publisher PUB 파일을 읽고 Java 애플리케이션에서 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. PUB 파일을 로드하고 PubFactory.createParser() 메서드를 사용하여 파서를 생성합니다.
  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 문서 속성 읽기

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로 변환할 수도 있습니다. 자세한 내용은 Java에서 PUB를 PDF로 변환하기를 참조하세요.

무료 라이선스 받기

제한 없이 Aspose.PUB for Java을 평가하기 위해 무료 임시 라이선스를 요청할 수 있습니다. 예제를 실행하기 전에 애플리케이션에 라이선스를 적용하십시오.

게시자 파일 읽기: 무료 리소스

이러한 리소스를 사용하여 Publisher 파일 작업에 대해 자세히 알아보세요:

결론

이제 Java에서 Aspose.PUB for Java를 사용하여 PUB 파일을 읽을 수 있습니다. Document 클래스를 사용하여 파일이나 스트림에서 출판물의 페이지 크기, 필드, 글꼴, 색상 및 문서 속성을 가져올 수 있습니다. 질문이 있으면 무료 지원 포럼 에 게시하세요.

자주 묻는 질문

Java에서 PUB 파일을 읽으려면 어떻게 해야 하나요?

PubFactory.createParser()를 사용하여 파서를 생성하고, parse()를 호출하여 Document 객체를 얻은 다음, getWidth(), getHeight(), getFontNames(), getColors()와 같은 속성을 읽습니다.

페이지 너비와 높이는 어떤 단위로 반환됩니까?

getWidth() 및 getHeight() 메서드는 EMU(English Metric Units) 단위의 값을 반환합니다. 값을 914400으로 나누면 인치 단위 크기를 얻을 수 있습니다.

Java에서 스트림으로 PUB 파일을 읽을 수 있나요?

예. PubFactory.createParser()에 InputStream을 전달하고 parse()를 호출하여 Document 객체를 가져옵니다.

Java에서 PUB 파일의 제목과 저자를 어떻게 읽나요?

document.getSummaryInfo()를 호출하고, 그 안의 getTitle(), getAuthor(), getSubject(), getKeywords() 메서드를 사용합니다.

PUB 파일을 읽기 위해 Microsoft Publisher가 필요합니까?

No. Aspose.PUB for Java는 Microsoft Publisher나 다른 소프트웨어 없이도 PUB 파일을 읽습니다.

참고