在Java中如何与内容流运营商工作
这个指南显示如何在Aspose.PDF FOSS 中解析和构建页面内容流. 对于Java.你会学到阅读原始页面字节,分析它们的 ContentStreamParser, 及使用 ContentStreamBuilder 创建新的内容.
访问内容流
Each Page 在一个 Document 包含其图形内容的内容流. 包含一个定义文本定位,绘图命令的PDF操作符序列; 资源引用. 从页面中读取原始内容流字节:
try (Document doc = new Document("input.pdf")) {
PageCollection pages = doc.getPages();
Page page = pages.get(1);
byte[] contentBytes = page.getContents().toByteArray();
System.out.println("Content stream size: " + contentBytes.length + " bytes");
}使用ContentStreamParser进行解析
ContentStreamParser 将内容流转换为运营商对象,并分析其;, 允许检查流中的每个绘图命令:
try (Document doc = new Document("input.pdf")) {
PageCollection pages = doc.getPages();
Page page = pages.get(1);
byte[] contentBytes = page.getContents().toByteArray();
ContentStreamParser parser = new ContentStreamParser(contentBytes);
// Iterate over parsed operators
}使用 ContentStreamBuilder 构建
ContentStreamBuilder 创建新的内容流,用于嵌入一个 Page. 使用情况 ContentStreamBuilder 在构建页面内容时, 程序化而不是 构建后,将其分配到现有流. 页面并保存文件.
运营商参考
文件内容流操作员定义在ISO 32000-1:2008表A.1.常见的操作人员包括:
- 文字运算符:
BT,ET,Tf,Tj - 图形状态操作符:
q,Q,cm - 路径运营商:
m,l,h,f,S