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;}
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); } } }
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);}
import org.apache.pdfbox.util.Splitter; //導入依賴的package包/類@Overridepublic 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;}
本文由 貴州做網站公司 整理發布,部分圖文來源于互聯網,如有侵權,請聯系我們刪除,謝謝!
網絡推廣與網站優化公司(網絡優化與推廣專家)作為數字營銷領域的核心服務提供方,其價值在于通過技術手段與策略規劃幫助企業提升線上曝光度、用戶轉化率及品牌影響力。這...
在當今數字化時代,公司網站已成為企業展示形象、傳遞信息和開展業務的重要平臺。然而,對于許多公司來說,網站建設的價格是一個關鍵考量因素。本文將圍繞“公司網站建設價...
在當今的數字化時代,企業網站已成為企業展示形象、吸引客戶和開展業務的重要平臺。然而,對于許多中小企業來說,高昂的網站建設費用可能會成為其發展的瓶頸。幸運的是,隨...
如何創建csv文件?1.首先,打開excel辦公軟件,通過CTRL N新建一個工作簿..2.編輯現有工作簿中的表格,編輯后按ctrl s打開“另存為”窗口。3.指定保存路徑,更改文件名,并選擇文件類型。4.在文件類型中,選擇*。csv格式,選擇完成,然后單擊保存。5.閱讀彈出窗口中的提示并單擊是,這意味著csv文件已成功創建。超大的csv文件怎樣用excel打開???首先,創建一個新的Excel并打...
除了B站和A站,還有那些很有潛力的acg網站?櫻桃動畫不錯...
QQ游戲下載好了怎么安裝不了?這需要一點電腦知識。進入游戲廳文件夾,刪除相關游戲文件夾。如何找到這個游戲的文件夾?先打開游戲,打開游戲,再打開任務管理器。在[應用程序]中找到游戲名稱,點擊右鍵,從菜單中選擇[轉到進程]??吹竭M程名,點擊右邊選擇【屬性】,就會看到這個游戲的文件夾。QQ怎么下載各種軟件并安裝?下載軟件有兩種。1.首先,如果你有二維碼,保存它,然后進入主頁點擊右上方的加號,選擇掃描,掃...