List of usage examples for org.apache.pdfbox.pdmodel PDDocument getNumberOfPages
public int getNumberOfPages()
From source file:PDF.PDFRemover.java
private void extractGoodPdf(PDDocument pdf, String auditPdfFileName, Boolean[][] statusArray) throws COSVisitorException, IOException { PDDocument auditPdf = new PDDocument(); int pageNum = pdf.getNumberOfPages(); // add reject page into rejectPdf for (int i = 0; i < pageNum; i++) { PDPage page = (PDPage) pdf.getDocumentCatalog().getAllPages().get(i); if (!statusArray[GlobalVar.VOID_BUTTON_INDEX][i]) { auditPdf.addPage(page);/*from w w w. j a v a 2s . c o m*/ } } auditPdf.save(auditPdfFileName); auditPdf.close(); }
From source file:pdf.PDFWindow.java
private void getImages(PDDocument doc) { PDFRenderer pdfRenderer = new PDFRenderer(doc); images = new ArrayList(); int numberOfPages = doc.getNumberOfPages(); Thread thread = new Thread(new Runnable() { public void run() { for (int page = 0; page < numberOfPages; ++page) { BufferedImage bim; i = page;/*from w ww. ja v a 2 s . com*/ try { bim = pdfRenderer.renderImageWithDPI(page, 300, ImageType.RGB); images.add(bim); } catch (IOException ex) { Logger.getLogger(PDFWindow.class.getName()).log(Level.SEVERE, null, ex); } displayPage(); try { java.lang.Thread.sleep(100); } catch (Exception e) { } } } }); thread.start(); }
From source file:pdf.to.info.PDF.java
/** * Creating a PDDocument object//from w w w . j av a 2s . co m * * @param filePath * @return * @throws java.io.IOException */ private PDDocument ReadPDDoc(String filePath) throws IOException { File file = new File(filePath); PDFParser parser = new PDFParser(new RandomAccessFile(file, "r")); // update for PDFBox V 2.0 parser.parse(); COSDocument cosDoc = parser.getDocument(); PDFTextStripper pdfStripper = new PDFTextStripper(); PDDocument pdDoc = new PDDocument(cosDoc); pdDoc.getNumberOfPages(); pdfStripper.setStartPage(1); pdfStripper.setEndPage(1); // for reading all pages of pdf file // pdfStripper.setEndPage(pdDoc.getNumberOfPages()); return pdDoc; }
From source file:pdfedittest.PDFEditTest.java
/** * @param args the command line arguments */// w w w . java2 s .co m public static void main(String[] args) { // TODO code application logic here PDDocument pd; BufferedWriter wr; try { String outputPath = "rayani.txt"; File input = new File("C:\\Users\\Administrator\\Desktop\\FA Feb 16.pdf"); // The PDF file from where you would like to extract File output = new File(outputPath); // The text file where you are going to store the extracted data pd = PDDocument.load(input); System.out.println(pd.getNumberOfPages()); System.out.println(pd.isEncrypted()); //pd.save("CopyOfInvoice.pdf"); // Creates a copy called "CopyOfInvoice.pdf" PDFTextStripper stripper = new PDFTextStripper(); //stripper.setStartPage(3); //Start extracting from page 3 //stripper.setEndPage(5); //Extract till page 5 //stripper.set wr = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(output))); stripper.writeText(pd, wr); if (pd != null) { pd.close(); } // I use close() to flush the stream. wr.close(); TextParser a = new TextParser(outputPath); a.getUserDetail(""); } catch (Exception e) { e.printStackTrace(); } }
From source file:pdfpositional.PdfPositional.java
/** * @param args the command line arguments *///ww w .ja va2s .c o m public static void main(String[] args) { try { // check file param if (args.length == 0) { throw new ParameterException("No file parameter specified"); } String file = args[args.length - 1]; Pattern patternFile = Pattern.compile("(?i)^[\\w,\\s-()/]+\\.pdf$"); Matcher matcherFile = patternFile.matcher(file); // check file is valid format if (!matcherFile.find()) { throw new ParameterException("File parameter invalid: " + file); } // check if file exists File input = new File(file); if (!input.exists()) { throw new ParameterException("File does not exist: " + file); } // ensure it isnt a directory if (input.isDirectory()) { throw new ParameterException("File is a directory: " + file); } PdfPositional pdfPositional = new PdfPositional(input); pdfPositional.setConversion(new Float(1.388888888889)); pdfPositional.processFileArgument(args[args.length - 1]); Pattern patternArgument = Pattern.compile("^-{2}([^=]+)[=]([\\s\\S]+)$"); Matcher matcher; for (int i = 0; i < args.length - 1; i++) { matcher = patternArgument.matcher(args[i]); while (matcher.find()) { switch (matcher.group(1)) { case "page": pdfPositional.setPageNumber(Integer.parseInt(matcher.group(2))); break; case "output": pdfPositional.setOutputFile(matcher.group(2)); break; } } } PDDocument document; document = PDDocument.load(pdfPositional.getInputFile()); // check for encrypted document if (document.isEncrypted()) { try { document.decrypt(""); } catch (CryptographyException | IOException e) { document.close(); throw new EncryptedDocumentException(); } } List allPages = document.getDocumentCatalog().getAllPages(); if (pdfPositional.hasPageNumber()) { if (document.getNumberOfPages() < pdfPositional.getPageNumber()) { throw new ParameterException("illegal page number"); } PDPage page = (PDPage) allPages.get(pdfPositional.getPageNumber() - 1); PDStream contents = page.getContents(); if (contents != null) { pdfPositional.processStream(page, page.findResources(), page.getContents().getStream()); pdfPositional.addPageDataToPdfData(); pdfPositional.writeJSONToOutputStream(); } } else { for (int i = 0; i < allPages.size(); i++) { pdfPositional.setPageNumber(i + 1); PDPage page = (PDPage) allPages.get(i); PDStream contents = page.getContents(); if (contents != null) { pdfPositional.processStream(page, page.findResources(), page.getContents().getStream()); pdfPositional.addPageDataToPdfData(); pdfPositional.writeJSONToOutputStream(); } page.clear(); } } pdfPositional.destroyOutputStream(); document.close(); System.exit(0); } catch (ParameterException ex) { System.out.println("Parameter Error: " + ex.getMessage()); System.exit(1); } catch (EncryptedDocumentException ex) { System.out.println("Encrypted Document Error"); System.exit(1); } catch (IOException | NumberFormatException ex) { System.out.println("General Error"); System.exit(1); } }
From source file:pdfsplicer.SplicerModel.java
License:Open Source License
/** * Render an image for the first page of the selected loaded PDF. * // ww w . j a va2 s . c o m * @param selectedValue the name of the selected PDF * @throws IOException if the selected PDF cannot be read */ public void showLoadedPDFPreview(String selectedValue) throws IOException { lPDFIcon = null; curLPage = -1; lNumPages = -1; if (selectedValue != null) { PDDocument pdf = pdfList.get(selectedValue); loadedRenderer = new PDFRenderer(pdf); lPDFIcon = new ImageIcon(loadedRenderer.renderImage(0)); curLPage = 1; lNumPages = pdf.getNumberOfPages(); } setChanged(); notifyObservers(); }
From source file:pln.Pln.java
static String pdftoText(String fileName) { PDFParser parser;/*w w w . ja va 2 s .c om*/ String parsedText = null; ; PDFTextStripper pdfStripper = null; PDDocument pdDoc = null; COSDocument cosDoc = null; File file = new File(fileName); if (!file.isFile()) { System.err.println("File " + fileName + " does not exist."); return null; } try { parser = new PDFParser(new FileInputStream(file)); } catch (IOException e) { System.err.println("Unable to open PDF Parser. " + e.getMessage()); return null; } try { parser.parse(); cosDoc = parser.getDocument(); pdfStripper = new PDFTextStripper(); pdDoc = new PDDocument(cosDoc); pdfStripper.setStartPage(1); pdfStripper.setEndPage(pdDoc.getNumberOfPages()); parsedText = pdfStripper.getText(pdDoc); } catch (Exception e) { System.err.println("An exception occured in parsing the PDF Document." + e.getMessage()); } finally { try { if (cosDoc != null) { cosDoc.close(); } if (pdDoc != null) { pdDoc.close(); } } catch (Exception e) { e.printStackTrace(); } } return parsedText; }
From source file:projekt.CustomRenderer.java
private void Stopka(PDPageContentStream content, PDDocument doc) throws Exception { content.beginText();//from w w w. j av a 2 s. com content.setFont(PDType1Font.HELVETICA, 12); content.moveTextPositionByAmount(10, 10); content.showText("Task Project Manager"); content.endText(); content.beginText(); content.setFont(PDType1Font.HELVETICA_BOLD, 12); content.moveTextPositionByAmount(300, 10); int nr = doc.getNumberOfPages(); content.showText(Integer.toString(nr)); content.endText(); }
From source file:Reports.DROTDataReader.java
public void singlePdfGenerator(String pdfFileNameBase, List<String> cycles) throws IOException, COSVisitorException { GlobalVar.dirMake(new File(pdfFileNameBase)); //create a folder with the same name int rowCount = 0; int pageCount = 1; PDDocument pdf = new PDDocument(); PDPage page; //default size PAGE_SIZE_A4 PDPageContentStream stream;/*from www . j a v a2 s .c om*/ //page.setRotation(90); //counterclock wise rotate 90 degree ////left hand rule // stream.setFont(PDType1Font.COURIER, FONT_SIZE); String lastUpdate = null; String lastInputSource = null; System.out.println("DROT_DATA keyset:" + DROT_DATA.keySet()); // System.out.println("Cycles empty? " + cycles.isEmpty() + cycles.size()); // System.out.println(cycles); String pdfFileName = pdfFileNameBase + "\\DROT UPDT"; for (String updateDate : DROT_DATA.keySet()) { Map<String, List<String>> thisDTData = DROT_DATA.get(updateDate); lastUpdate = updateDate; System.out.println("thisDT_DATA keyset:" + thisDTData.keySet()); pdfFileName = pdfFileName + " " + updateDate; for (String inputSource : thisDTData.keySet()) { if (cycles.isEmpty() || cycles.contains(inputSource)) { //PDDocument pdf = new PDDocument(); lastInputSource = inputSource; List<String> thisCycle = thisDTData.get(inputSource); pageCount = 1; // new cycle, restart page num page = new PDPage(); //page break stream = new PDPageContentStream(pdf, page, true, false); stream.beginText(); page.setRotation(PAGE_ROT_DEGREE); //stream.setFont(PDType1Font.COURIER, FONT_SIZE); addBigFontUpdateNumberAndCycle(updateDate, inputSource, stream); stream.setFont(PDType1Font.COURIER, FONT_SIZE); // stream.setFont(PDType1Font. int thisCycleRowCount = 0; for (String row : thisCycle) { if (thisCycleRowCount > MAX_NUM_TRANS) { //close the current page setupFootNote(stream, pageCount, inputSource, updateDate); pageCount++; stream.endText(); stream.close(); pdf.addPage(page); // start a new page page = new PDPage(); stream = new PDPageContentStream(pdf, page, true, false); page.setRotation(PAGE_ROT_DEGREE); stream.beginText(); stream.setFont(PDType1Font.COURIER, FONT_SIZE); thisCycleRowCount = 0; } stream.setTextRotation(TEXT_ROT_RAD, TRANS_X + thisCycleRowCount * INVERVAL_X, TRANS_Y); stream.drawString(row); thisCycleRowCount++; //System.out.println("Update:" + updateDate + " Cycle: " + cycle + " " + row); } setupFootNote(stream, pageCount, lastInputSource, lastUpdate); stream.endText(); stream.close(); pdf.addPage(page); //String pdfFileName = pdfFileNameBase + "\\DROT UPDT " + updateDate + " " + cycle + ".pdf"; } } } if (pdf.getNumberOfPages() > 0) { pdfFileName = pdfFileName + ".pdf"; pdf.save(pdfFileName); } pdf.close(); }
From source file:Reports.LeaveDataReader.java
public void singlePdfGenerator(String pdfFileNameBase, Set<String> inputSources) throws IOException, COSVisitorException { GlobalVar.dirMake(new File(pdfFileNameBase)); //create a folder with the same name int rowCount = 0; int pageCount = 1; System.out.println("passed in " + inputSources); PDDocument pdf = new PDDocument(); PDPage page; //default size PAGE_SIZE_A4 PDPageContentStream stream;/* ww w . j av a 2 s. c om*/ //page.setRotation(90); //counterclock wise rotate 90 degree ////left hand rule // stream.setFont(PDType1Font.COURIER, FONT_SIZE); String lastUpdate = null; String lastInputSource = null; System.out.println("LEAVE_DATA keyset:" + LEAVE_DATA.keySet()); // System.out.println("Cycles empty? " + cycles.isEmpty() + cycles.size()); // System.out.println(cycles); String pdfFileName = pdfFileNameBase + "\\LEAVE UPDT"; for (String updateDate : LEAVE_DATA.keySet()) { Map<String, List<String>> thisDTData = LEAVE_DATA.get(updateDate); lastUpdate = updateDate; System.out.println("thisDT_DATA keyset:" + thisDTData.keySet()); pdfFileName = pdfFileName + " " + updateDate; for (String inputSource : thisDTData.keySet()) { if (inputSources.isEmpty() || inputSources.contains(inputSource)) { lastInputSource = inputSource; List<String> thisCycle = thisDTData.get(inputSource); pageCount = 1; // new cycle, restart page num page = new PDPage(); //page break stream = new PDPageContentStream(pdf, page, true, false); stream.beginText(); page.setRotation(PAGE_ROT_DEGREE); //stream.setFont(PDType1Font.COURIER, FONT_SIZE); addBigFontUpdateNumberAndCycle(updateDate, lastInputSource, stream); stream.setFont(PDType1Font.COURIER, FONT_SIZE); // stream.setFont(PDType1Font. int thisCycleRowCount = 0; for (String row : thisCycle) { if (thisCycleRowCount > MAX_NUM_TRANS) { //close the current page setupFootNote(stream, pageCount, lastInputSource, updateDate); pageCount++; stream.endText(); stream.close(); pdf.addPage(page); // start a new page page = new PDPage(); stream = new PDPageContentStream(pdf, page, true, false); page.setRotation(PAGE_ROT_DEGREE); stream.beginText(); stream.setFont(PDType1Font.COURIER, FONT_SIZE); thisCycleRowCount = 0; } stream.setTextRotation(TEXT_ROT_RAD, TRANS_X + thisCycleRowCount * INVERVAL_X, TRANS_Y); stream.drawString(row); thisCycleRowCount++; //System.out.println("Update:" + updateDate + " Cycle: " + cycle + " " + row); } setupFootNote(stream, pageCount, lastInputSource, lastUpdate); stream.endText(); stream.close(); pdf.addPage(page); } } } if (pdf.getNumberOfPages() > 0) { pdfFileName = pdfFileName + ".pdf"; pdf.save(pdfFileName); } pdf.close(); }