在 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.");
}
}安装和设置
将 Aspose.Page for Java 添加到您的 Maven:
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 是一个原生、独立的 Java 库,可直接构建、渲染和操作 PS/EPS 文档结构,而无需依赖外部打印机驱动程序或原生软件。
2. 我可以直接将创建的 PostScript (PS) 文档转换为 PDF 或光栅图像吗?
可以。您可以将 PsDocument 实例传递给 Aspose.Page 中的 PdfDevice 或 ImageDevice,将生成的 PostScript 文件直接转换为 PDF、PNG、JPEG 或 TIFF 格式。
3. 如何在 PsDocument 中定义坐标和测量单位?
Aspose.Page 使用标准 PostScript 点,其中 1 英寸 = 72 点。坐标是相对于页面表面的原点进行测量的。