Java Splitter类使用实例
时间:2022-05-20
本文章向大家介绍Java Splitter类代码示例,你可以查看下面代码实例来了解Java Splitter类的使用方法及注意事项。文章结合实例形式分析了其使用技巧,希望通过本文能帮助到大家理解应用这部分内容。
实例1: splitToExamPapersWithPDFStreams
import org.apache.pdfbox.util.Splitter; //导入依赖的package包/类
/**
* Splits a PDF-document into exam papers.
*
* @param pdfStream PDF-file as InputStream
* @return A List of ExamPapers.
* @throws IOException If InputStream can not be read or stream doesn't
* contain a PDF-format file.
* @throws DocumentException If document contains odd number of pages.
* @throws PdfException If a document is not in the right format or error
* occurs while loading or splitting or document has an odd number of pages.
* @throws COSVisitorException if something goes wrong when visiting a PDF
* object.
*/
public List<ExamPaper> splitToExamPapersWithPDFStreams(InputStream pdfStream) throws IOException, DocumentException, PdfException, COSVisitorException {
PDDocument allPdfDocument = PDDocument.load(pdfStream);
if (allPdfDocument.getNumberOfPages() % 2 != 0) {
throw new DocumentException("Odd number of pages");
}
Splitter splitter = new Splitter();
splitter.setSplitAtPage(2);
List<PDDocument> pdfDocuments = splitter.split(allPdfDocument);
ArrayList<ExamPaper> examPapers = new ArrayList<>();
for (PDDocument pdfDocument : pdfDocuments) {
ExamPaper paper = new ExamPaper();
ByteArrayOutputStream out = new ByteArrayOutputStream();
pdfDocument.save(out);
byte[] data = out.toByteArray();
paper.setPdf(data);
examPapers.add(paper);
pdfDocument.close();
}
allPdfDocument.close();
return examPapers;
}
实例2: main
import org.apache.pdfbox.util.Splitter; //导入依赖的package包/类
public static void main(String[] args) throws IOException {
// Load the PDF. The PDDocument throws IOException
PDDocument document = new PDDocument();
document = PDDocument.load("C:\\Main.pdf");
// Create a Splitter object
Splitter splitter = new Splitter();
// We need this as split method returns a list
List<PDDocument> listOfSplitPages;
// We are receiving the split pages as a list of PDFs
listOfSplitPages = splitter.split(document);
// We need an iterator to iterate through them
Iterator<PDDocument> iterator = listOfSplitPages.listIterator();
// I am using variable i to denote page numbers.
int i = 1;
while(iterator.hasNext()){
PDDocument pd = iterator.next();
try{
// Saving each page with its assumed page no.
pd.save("C:\\Page " + i++ + ".pdf");
} catch (COSVisitorException anException){
// Something went wrong with a PDF object
System.out.println("Something went wrong with page " + (i-1) + "\n Here is the error message" + anException);
}
}
}
实例3: splitDocument
import org.apache.pdfbox.util.Splitter; //导入依赖的package包/类
private PDDocumentContainer splitDocument(PDDocument sDocument, Integer sStartPage, Integer sEndPage, Integer sSplitAtPage) throws IOException {
Splitter aSplitter = new Splitter();
int aNumberOfPages = sDocument.getNumberOfPages();
boolean aStartEndPageSet = false;
if (sStartPage != null) {
aSplitter.setStartPage(sStartPage);
aStartEndPageSet = true;
if (sSplitAtPage == null) {
aSplitter.setSplitAtPage(aNumberOfPages);
}
}
if (sEndPage != null) {
aSplitter.setEndPage(sEndPage);
aStartEndPageSet = true;
if (sSplitAtPage == null) {
aSplitter.setSplitAtPage(sEndPage);
}
}
if (sSplitAtPage != null) {
aSplitter.setSplitAtPage(sSplitAtPage);
} else if (!aStartEndPageSet) {
aSplitter.setSplitAtPage(1);
}
List<PDDocument> aParts = aSplitter.split(sDocument);
return new BasicPDDocumentContainer(sDocument, aParts);
}
实例4: split
import org.apache.pdfbox.util.Splitter; //导入依赖的package包/类
@Override
public List<String> split(final String inputUri, final String outputUri, final Integer pages)
throws IOException, COSVisitorException {
final List<String> result = new ArrayList<String>();
if (StringUtils.isNotBlank(inputUri) && StringUtils.isNotBlank(outputUri) && pages != null) {
final PDDocument doc = PDDocument.load(inputUri);
final Splitter splitter = new Splitter();
splitter.setSplitAtPage(pages);
final List<PDDocument> splittedDocs = splitter.split(doc);
Integer subIndex = 1;
for (final PDDocument document : splittedDocs) {
final String extension = this.converterUtils.addSubIndexBeforeExtension(outputUri,
subIndex++);
document.save(extension);
result.add(extension);
document.close();
}
doc.close();
} else {
throw new IllegalArgumentException(Constants.ILLEGAL_ARGUMENT_EXCEPTION_MESSAGE);
}
return result;
}
- Web 前端利器Emmet 的CSS 用法总结
- 响应式 HTML 邮件制作之三个实例
- java知识点归纳
- EaseMobile 主题导航菜单设置小图标的方法(图文+视频教程)
- NEC css规范
- DW Mobile Switcher:移动设备识别切换主题插件
- 揭秘技术大国以色列
- mix-blend-mode 混合模式 background-blend-mode 背景混合模式 isolation:isolate 隔离
- wp_nav_menu 函数经Walker_Nav_Menu 类自定义导航菜单HTML
- css3 RGBA 红色R+绿色G+蓝色B+Alpha通道
- css3 gradient 渐变
- TwentyTwenty:一个图片特效Jquery 插件
- 何为实在
- css3 动画应用 animations 和transtions transform在加上JavaScript 可以实现硬件加速动画。
- java教程
- Java快速入门
- Java 开发环境配置
- Java基本语法
- Java 对象和类
- Java 基本数据类型
- Java 变量类型
- Java 修饰符
- Java 运算符
- Java 循环结构
- Java 分支结构
- Java Number类
- Java Character类
- Java String类
- Java StringBuffer和StringBuilder类
- Java 数组
- Java 日期时间
- Java 正则表达式
- Java 方法
- Java 流(Stream)、文件(File)和IO
- Java 异常处理
- Java 继承
- Java 重写(Override)与重载(Overload)
- Java 多态
- Java 抽象类
- Java 封装
- Java 接口
- Java 包(package)
- Java 数据结构
- Java 集合框架
- Java 泛型
- Java 序列化
- Java 网络编程
- Java 发送邮件
- Java 多线程编程
- Java Applet基础
- Java 文档注释
- 基于linux命令提取文件夹内特定文件路径
- Ubuntu20.04修改ip地址的方法示例
- Linux 逻辑卷管理(LVM)使用方法总结
- Linux 下载安装VSCode 使用编程输出当前时间的方法
- 详解Linux获取线程的PID(TID、LWP)的几种方式
- Linux文件基本属性知识点总结
- Linux MySQL忘记root密码解决方案
- 如何使用iostat查看linux硬盘IO性能
- linux搭建NFS文件共享服务器的步骤详解
- Windows和Linux实现远程桌面连接
- Ubuntu19.10开启ssh服务(详细过程)
- Ubuntu 18.04更换apt-get源的方法
- 浅谈Linux环境变量文件介绍
- linux创建线程之pthread_create的具体使用
- Ubuntu20.04安装搜狗输入法的详细步骤