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;
}