List of usage examples for com.itextpdf.text.pdf PdfReader getNumberOfPages
public int getNumberOfPages()
From source file:lecteur.Interface.java
private static String ReadPDF(String pdf_url) { String[] row;/* www . j a v a 2 s . co m*/ ArrayList<Bilan> tabRubriqueBilanTotal = new ArrayList<>(); ArrayList<Bilan> tabRubriqueBilan = null; System.out.println("ReadPDF"); StringBuilder str = new StringBuilder(); try { PdfReader reader = new PdfReader(pdf_url); int nbpage = reader.getNumberOfPages(); System.out.println("Nombre de page = " + nbpage); //Recherche page BILAN - ACTIF //Recherche page BILAN - PASSIF //Recherche page //pour chaque page, lire ligne. for (int i = 22; i <= 22; i++) { //for(int i=1;i<=nbpage;i++) { String str2 = PdfTextExtractor.getTextFromPage(reader, i); //System.out.println("STR2 = " + str2); //System.out.println("==========================="); row = null; //Concatener les pages : //str.append(str2); //System.out.println("STR = " + str); //Appel fonction split chaque ligne de la page. row = splitPage(str2); System.out.println(); System.out.println("\nnb row traiter = " + row.length); //recherche de correspondance AA, AF, ect.. //Recherche deux majuscules suivis d'espaces et nombre/espace/nombre //TODO //Gerer les cas o il n'y a pas de chiffre String pattern1 = "[A-Z]{2}"; String pattern = "[A-Z]{2}\\p{Space}+\\d+\\p{Space}?\\d+"; for (int j = 0; j < row.length; j++) { System.out.println("\nLigne traiter AVANT FCT: " + row[j]); //TAB_BILAN par ligne tabRubriqueBilan = recherchebilan(row[j], pattern); for (int k = 0; k < tabRubriqueBilan.size(); k++) { tabRubriqueBilanTotal.add(tabRubriqueBilan.get(k)); } } for (int j = 0; j < tabRubriqueBilanTotal.size(); j++) { tabRubriqueBilanTotal.get(j).show(); } System.out.println("TAILLE TAB PAR PAGE = " + tabRubriqueBilanTotal.size()); } afficheResultat(tabRubriqueBilanTotal); } catch (Exception err) { err.printStackTrace(); } return String.format("%s", str); }
From source file:managedbeans.descargas.PDFConversionDemo.java
public static void main(String[] args) { try {/* w ww . j av a 2s .c om*/ //Read file using PdfReader PdfReader pdfReader = new PdfReader("HelloWorld.pdf"); //Modify file using PdfReader PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileOutputStream("HelloWorld-modified.pdf")); Image image = Image.getInstance("temp.png"); image.scaleAbsolute(100, 50); image.setAbsolutePosition(100f, 700f); for (int i = 1; i <= pdfReader.getNumberOfPages(); i++) { PdfContentByte content = pdfStamper.getUnderContent(i); content.addImage(image); } pdfStamper.close(); } catch (IOException | DocumentException e) { logger.warn("failed to ...." + e); } }
From source file:me.Aron.Heinecke.fbot.lib.Converter.java
License:Apache License
/*** * Return the amount of sites in a pdf//from w w w. j ava 2s . c om * using a deprecated (working) iText function * @param file path of the file to use * @return amount of sites */ @SuppressWarnings("deprecation") public int pdfSites(String file) { try { RandomAccessFile raf = new RandomAccessFile(new File(file), "r"); RandomAccessFileOrArray pdfFile; pdfFile = new RandomAccessFileOrArray(new RandomAccessSourceFactory().createSource(raf)); PdfReader reader = new PdfReader(pdfFile, new byte[0]); int pages = reader.getNumberOfPages(); reader.close(); return pages; } catch (InvalidPdfException e) { fbot.getLogger().severe("converter", "Invalid PDF file!: no index"); } catch (Exception e) { fbot.getLogger().exception("converter", e); } return -1; }
From source file:me.Aron.Heinecke.fbot.lib.Converter.java
License:Apache License
/*** * Add a note to the bottom of a pdf file in italic font * @param rfile file to be read from//from w w w .j av a 2 s. com * @param wfile file to be written to * @param text text to add * @return path to the resulting pdf, null if it failed */ private String addPDFNote(File rfile, File wfile, String text) { try { PdfReader pdfReader = new PdfReader(rfile.getAbsolutePath()); PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileOutputStream(wfile)); for (int i = 1; i <= pdfReader.getNumberOfPages(); i++) { PdfContentByte cb = pdfStamper.getUnderContent(i); BaseFont bf = BaseFont.createFont(); bf.setPostscriptFontName("ITALIC"); cb.beginText(); cb.setFontAndSize(bf, 12); cb.setTextMatrix(10, 20); cb.showText(text); cb.endText(); } pdfStamper.close(); return wfile.getAbsolutePath(); } catch (IOException | DocumentException e) { fbot.getLogger().exception("converter", e); return null; } }
From source file:me.Aron.Heinecke.fbot.lib.Converter.java
License:Apache License
@Deprecated public String ReadPdfFile(File file) throws IOException { StringBuilder text = new StringBuilder(); if (file.exists()) { PdfReader pdfReader = new PdfReader(file.getAbsolutePath()); for (int pageid = 1; pageid <= pdfReader.getNumberOfPages(); pageid++) { SimpleTextExtractionStrategy strategy = new SimpleTextExtractionStrategy(); String currentText = PdfTextExtractor.getTextFromPage(pdfReader, pageid, strategy); //currentText = Encoding.UTF8.GetString(ASCIIEncoding.Convert(Encoding.Default, Encoding.UTF8, Encoding.Default.GetBytes(currentText))); text.append(currentText);//from w w w .ja v a2s . c om } pdfReader.close(); } return text.toString(); }
From source file:mergevoucher.MainJFrame.java
private void deletePages(File file, String str) throws IOException, DocumentException { //open the old pdf file and open a blank new one String fullFileName = file.getPath(); //System.out.println(fullFileName); PdfReader reader = new PdfReader(fullFileName); Document document = new Document(reader.getPageSizeWithRotation(1)); String out = fullFileName.replaceFirst(".pdf", "(new).pdf"); PdfCopy copy = new PdfCopy(document, new FileOutputStream(out)); document.open();//from w ww . j av a 2 s.c o m int pdfPageNumber = reader.getNumberOfPages(); //get pdf page number //change pageNumber string to int array Boolean[] preservePages = getPages(pdfPageNumber, str); //copy pages except need delete to new pdf file for (int i = 1; i <= pdfPageNumber; i++) { //filter not preserve pages if (preservePages[i]) { //if preserve,copy;else bypass //String content = PdfTextExtractor.getTextFromPage(reader, i); //?i; copy.addPage(copy.getImportedPage(reader, i)); } } System.out.println("New pdf file is:" + fullFileName.replaceFirst(".pdf", "(new).pdf")); //close files reader.close(); document.close(); copy.close(); }
From source file:mkl.testarea.itext5.pdfcleanup.StrictPdfCleanUpProcessor.java
License:Open Source License
/** * Extracts locations from the redact annotations contained in the document. *//* www. j a va 2 s . c o m*/ private void extractLocationsFromRedactAnnots() { this.pdfCleanUpLocations = new HashMap<Integer, List<PdfCleanUpLocation>>(); PdfReader reader = pdfStamper.getReader(); for (int i = 1; i <= reader.getNumberOfPages(); ++i) { PdfDictionary pageDict = reader.getPageN(i); this.pdfCleanUpLocations.put(i, extractLocationsFromRedactAnnots(i, pageDict)); } }
From source file:net.algem.edition.PdfHandler.java
License:Open Source License
public void createPdf(String fileName, ByteArrayOutputStream out, short templateType) throws IOException, DocumentException { try {//from www.j av a 2 s . co m File tmpFile = File.createTempFile(fileName, ".pdf"); final String target = tmpFile.getPath(); PageTemplate pt = getTemplate(templateType); PdfReader reader = new com.itextpdf.text.pdf.PdfReader(out.toByteArray()); if (pt != null) { PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(target)); // PdfStamper stamper = new PdfStamper(reader, new PrintStream(new FileOutputStream(target), true, "UTF-8")); PdfReader model = new com.itextpdf.text.pdf.PdfReader(pt.getContent()); PdfImportedPage importedPage = stamper.getImportedPage(model, 1); for (int i = 1; i <= reader.getNumberOfPages(); i++) { PdfContentByte canvas = stamper.getUnderContent(i); canvas.addTemplate(importedPage, 0, 0); } stamper.getWriter().freeReader(model); model.close(); stamper.close(); } else { PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(target)); //PdfStamper stamper = new PdfStamper(reader, new PrintStream(new FileOutputStream(target), true, "UTF-8")); stamper.close(); } preview(target, null); } catch (SQLException ex) { GemLogger.logException(ex); } }
From source file:net.digitstar.vanadio.AbstractReportPdf.java
License:Apache License
protected void afterCloseDocument(Document document, PdfReader reader, PdfStamper writer) { int totalPages = reader.getNumberOfPages(); for (int pag = 1; pag <= totalPages; pag++) { writeHeaderFooter(document, reader, writer, pag, totalPages, getReportOptions()); }/*w w w . j av a 2s . c om*/ }
From source file:net.ionescu.jhocr2pdf.Jhocr2pdf.java
License:Open Source License
/** * @param args//ww w .j a v a 2 s . c o m * The arguments are: * [options] output_pdf * The name of the output PDF file with the added OCR information must be the last argument * Other * flags: * -i pdf_file PDF file containing the TIFF images * -hocr html_file hOCR file generated by Tesseract * -visible render the text above the image (mostry for debugging) * -font font_path path to the font file to use * -hocrnameformat string to use to construct the name of the hocr file depending on page number * * Usage example: * java -jar jhorc2pdf.jar -font /usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.ttf -i input.pdf -hocr input.html output.pdf * * @throws SAXException * @throws IOException * @throws DocumentException */ public static void main(String[] args) throws SAXException, IOException, DocumentException { /* * process input parameters * * -visible -font font_path inputPDF inputOCR outputPDF */ boolean visible = false; String fontPath = null; String fileNameFormat = null; String inputPDF = null; String inputHOCR = null; String outputPDF = null; for (int i = 0; i < args.length; i++) { if (args[i].startsWith("-")) { switch (args[i]) { case "-visible": visible = true; break; case "-font": fontPath = args[++i]; break; case "-hocrnameformat": fileNameFormat = args[++i]; break; case "-i": inputPDF = args[++i]; break; case "-hocr": inputHOCR = args[++i]; break; default: System.err.println("Invalid parameter: " + args[i]); System.exit(1); } } else { if (i < args.length - 1) { System.err.println("Ouput file name should be the last argument"); System.exit(1); } outputPDF = args[i]; } } log.fine("load the PDF file, initialize the Stamper"); PdfReader reader = new PdfReader(inputPDF); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(outputPDF)); Map<String, String> info = reader.getInfo(); info.put("Title", new File(outputPDF).getName()); stamper.setMoreInfo(info); Jhocr2pdf ocrStamp = new Jhocr2pdf(stamper, visible, fontPath); if (null != fileNameFormat) { log.fine("iterate through all the pages looking for separate hocr files"); for (int i = 1, maxPages = reader.getNumberOfPages() + 1; i < maxPages; i++) { String hocrFileName = String.format(fileNameFormat, i); try { ocrStamp.parse(hocrFileName, i); } catch (SAXException saxException) { System.err.println("Error processing hocr file: " + hocrFileName); throw saxException; } } } else { ocrStamp.parse(inputHOCR, -1); } stamper.close(); }