JavaでPostScript(PS)ドキュメントを操作する
Aspose.Page for Javaを使用して、PostScript(PS)ドキュメントをプログラムで作成、変更、管理できます。複数ページのPSドキュメントをゼロから簡単に作成し、ページを追加したり、ベクターグラフィックスやテキスト要素を挿入したり、複数のPostScriptファイルを結合したり、Adobe AcrobatやDistillerをインストールすることなくドキュメント構造を調整したりできます。
Aspose.Page for Javaは、PsDocumentクラスを中心とした高レベルのスタンドアロンオブジェクトモデルを提供し、PostScriptドキュメントの作成とページ操作を制御できます。
単一ページまたは複数ページの新しいPostScript印刷ストリームを作成するには、PsDocument(outputStream, options)を使用します
- PostScriptストリーム内の個々のページをプログラムで構成するには、
PsDocument.openPage()/closePage()メソッドを使用します。 - ベクターグラフィックス、図形、テキスト、ページをディスク上のファイルまたはストリームに保存するには、
PsDocument.save()メソッドを使用します。 - ページの余白、カスタムサイズ(A4、Letter)、印刷方向を設定するには、
PsSaveOptionsクラスを使用します。
Javaで複数ページのPostScriptドキュメントを作成する
保存オプションを設定し、ドキュメントストリーム内でページを明示的に開いたり閉じたりすることで、複数ページのPSドキュメントをゼロから作成します。
import com.aspose.page.eps.PsDocument;
import com.aspose.page.eps.device.PsSaveOptions;
import java.io.FileOutputStream;
public class CreateMultiPagePSDocument {
public static void main(String[] args) throws Exception {
// Initialize output stream for the new PS file
FileOutputStream outStream = new FileOutputStream("C:/PSProject/Output/multipage_document.ps");
// Set save options (Default page size A4, 72 DPI)
PsSaveOptions options = new PsSaveOptions();
// Create a multi-page PS document instance
PsDocument document = new PsDocument(outStream, options, 2); // Explicitly specifying 2 pages
// --- PAGE 1 ---
document.openPage("Page 1");
// Add content to Page 1 (e.g., text or graphics)
document.closePage();
// --- PAGE 2 ---
document.openPage("Page 2");
// Add content to Page 2
document.closePage();
// Save and flush the document
document.save();
outStream.close();
System.out.println("Multi-page PostScript document created successfully.");
}
}既存のドキュメントビルダーにページとグラフィックスを追加する
PostScriptドキュメントのページに、ベクター図形、テキスト要素、変換を追加します。
import com.aspose.page.eps.PsDocument;
import com.aspose.page.eps.device.PsSaveOptions;
import java.awt.Color;
import java.awt.Font;
import java.io.FileOutputStream;
public class AddPageContentToPS {
public static void main(String[] args) throws Exception {
FileOutputStream outStream = new FileOutputStream("C:/PSProject/Output/styled_document.ps");
PsSaveOptions options = new PsSaveOptions();
PsDocument document = new PsDocument(outStream, options, 1);
// Open page
document.openPage(null);
// Add text element
Font font = new Font("Arial", Font.BOLD, 24);
document.fillText("Hello World from Aspose.Page Java!", font, 100, 100, Color.BLACK);
// Draw a vector rectangle
document.setPaint(Color.RED);
document.drawRect(100, 150, 300, 200);
// Close page and save
document.closePage();
document.save();
outStream.close();
System.out.println("Styled PS document with vector content saved.");
}
}カスタムページサイズと余白を設定する
PsSaveOptionsを使用して、正確なページサイズ(Letter、Legal、またはカスタムサイズなど)を指定します。
import com.aspose.page.eps.PsDocument;
import com.aspose.page.eps.device.PsSaveOptions;
import java.io.FileOutputStream;
public class CustomPageSizePS {
public static void main(String[] args) throws Exception {
FileOutputStream outStream = new FileOutputStream("C:/PSProject/Output/custom_size.ps");
// Create options and set custom size (e.g., Letter dimensions in points)
PsSaveOptions options = new PsSaveOptions();
options.setPageSize(new java.awt.Dimension(612, 792)); // 8.5 x 11 inches in points
PsDocument document = new PsDocument(outStream, options, 1);
document.openPage("Main Page");
// Page drawing commands...
document.closePage();
document.save();
outStream.close();
System.out.println("PS document with custom page size saved.");
}
}インストールとセットアップ
MavenにAspose.Page for Javaを追加します:
Package Manager Console Command
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java API</name>
<url>https://repository.aspose.com/repo/</url>
</repository>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-page</artifactId>
<version>Latest</version>
</dependency>
FAQ
1. Adobe Acrobat または PostScript ドライバー ユーティリティをインストールする必要がありますか?
いいえ。Aspose.Page for Java は、外部のプリンター ドライバーやネイティブ ソフトウェアに依存せず、PS/EPS ドキュメント構造を直接構築、レンダリング、操作できる、ネイティブでスタンドアロンの Java ライブラリです。
2. 作成した PostScript (PS) ドキュメントを PDF またはラスター画像に直接変換できますか?
はい。Aspose.Page の PdfDevice または ImageDevice に PsDocument インスタンスを渡すことで、生成した PostScript ファイルを PDF、PNG、JPEG、または TIFF 形式に直接変換できます。
3. PsDocument では座標と測定単位はどのように定義されますか?
Aspose.Page は標準の PostScript ポイントを使用し、1 インチ = 72 ポイント となります。座標はページ サーフェスの原点を基準として測定されます。