List of usage examples for com.itextpdf.text.pdf PdfReader PdfReader
public PdfReader(final PdfReader reader)
From source file:be.roots.taconic.pricingguide.service.PDFServiceImpl.java
License:Open Source License
private byte[] enableLinkToWebsite(byte[] pdf, Toc tableOfContents) throws IOException, DocumentException { try (final ByteArrayOutputStream bos = new ByteArrayOutputStream()) { final PdfReader reader = new PdfReader(pdf); final PdfStamper stamper = new PdfStamper(reader, bos); for (int i = tableOfContents.getFirstPageOfToc(); i <= tableOfContents .getLastPageNumberOfModelPages(); i++) { final Chunk websiteChunk = new Chunk(".................."); websiteChunk.setAction(new PdfAction(websiteLink)); ColumnText ct = new ColumnText(stamper.getUnderContent(i)); ct.setSimpleColumn(335, 10, 400, 35); ct.addText(new Phrase(websiteChunk)); ct.go();/*from w ww. j a v a 2 s . c o m*/ final Chunk emailChunk = new Chunk("........................................."); emailChunk.setAction(new PdfAction(emailLink)); ct = new ColumnText(stamper.getUnderContent(i)); ct.setSimpleColumn(240, 10, 330, 35); ct.addText(new Phrase(emailChunk)); ct.go(); } stamper.close(); reader.close(); return bos.toByteArray(); } }
From source file:be.roots.taconic.pricingguide.util.iTextUtil.java
License:Open Source License
public static byte[] merge(byte[]... pdfAsBytes) throws DocumentException, IOException { try (final ByteArrayOutputStream copyBaos = new ByteArrayOutputStream()) { final Document doc = new Document(); final PdfCopy copy = new PdfSmartCopy(doc, copyBaos); doc.open();/* ww w. j a va2 s . c om*/ int numberOfPages = 0; final java.util.List<HashMap<String, Object>> bookmarks = new ArrayList<>(); PdfReader pdf = null; for (byte[] pdfAsByte : pdfAsBytes) { if (pdfAsByte != null && pdfAsByte.length > 0) { pdf = new PdfReader(pdfAsByte); pdf.consolidateNamedDestinations(); final List<HashMap<String, Object>> pdfBookmarks = SimpleBookmark.getBookmark(pdf); if (!CollectionUtils.isEmpty(pdfBookmarks)) { SimpleBookmark.shiftPageNumbers(pdfBookmarks, numberOfPages, null); bookmarks.addAll(pdfBookmarks); } for (int i = 1; i <= pdf.getNumberOfPages(); i++) { copy.addPage(copy.getImportedPage(pdf, i)); } numberOfPages += pdf.getNumberOfPages(); } } if (pdf != null) { SimpleNamedDestination.getNamedDestination(pdf, false); } if (!CollectionUtils.isEmpty(bookmarks)) { copy.setOutlines(bookmarks); } copy.close(); return copyBaos.toByteArray(); } }
From source file:be.roots.taconic.pricingguide.util.iTextUtil.java
License:Open Source License
public static int numberOfPages(byte[] fileToPrint) throws IOException { if (fileToPrint == null) { return 0; }//from w w w . j a v a 2 s. c o m return new PdfReader(fileToPrint.clone()).getNumberOfPages(); }
From source file:be.roots.taconic.pricingguide.util.iTextUtil.java
License:Open Source License
public static byte[] setPageNumbers(byte[] pdfDocument) throws IOException, DocumentException { final int numberOfPages = numberOfPages(pdfDocument); if (numberOfPages > 1) { try (final ByteArrayOutputStream baos = new ByteArrayOutputStream()) { final PdfReader reader = new PdfReader(pdfDocument); final PdfStamper stamper = new PdfStamper(reader, baos); for (int pageNumber = 2; pageNumber <= numberOfPages; pageNumber++) { // get the first page final PdfContentByte canvas = stamper.getOverContent(pageNumber); // stamp the footer on the page final ColumnText ct = new ColumnText(canvas); ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER, new Phrase(pageNumber + "", getFontPageNumber()), 550, 22, 0); ct.go();//from www . j a v a 2 s. com } // close out stamper.close(); reader.close(); return baos.toByteArray(); } } return pdfDocument; }
From source file:be.roots.taconic.pricingguide.util.iTextUtil.java
License:Open Source License
public static byte[] organize(byte[] pdf, Toc tableOfContents) throws IOException, DocumentException { try (final ByteArrayOutputStream copyBaos = new ByteArrayOutputStream()) { final Document doc = new Document(); final PdfCopy copy = new PdfSmartCopy(doc, copyBaos); final PdfReader reader = new PdfReader(pdf); reader.selectPages(tableOfContents.getPageSequence()); doc.open();/* w w w . j a v a 2 s . co m*/ for (int i = 1; i <= reader.getNumberOfPages(); i++) { copy.addPage(copy.getImportedPage(reader, i)); } reader.close(); copy.close(); return copyBaos.toByteArray(); } }
From source file:be.roots.taconic.pricingguide.util.iTextUtil.java
License:Open Source License
public static Image getImageFromPdf(byte[] pdf) throws IOException, BadElementException { try (final ByteArrayOutputStream bos = new ByteArrayOutputStream()) { final PdfReader reader = new PdfReader(pdf); final PdfReaderContentParser parser = new PdfReaderContentParser(reader); final ImageRenderListener listener = new ImageRenderListener(bos); parser.processContent(1, listener); reader.close();/*from www . j a v a2 s.c om*/ return Image.getInstance(bos.toByteArray()); } }
From source file:be.roots.taconic.pricingguide.util.iTextUtil.java
License:Open Source License
public static byte[] embedFont(byte[] pdf, String fontFileName, String fontName) throws IOException, DocumentException { try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { // the font file RandomAccessFile raf = new RandomAccessFile(fontFileName, "r"); byte fontfile[] = new byte[(int) raf.length()]; raf.readFully(fontfile);/*w w w . jav a 2s.c o m*/ raf.close(); // create a new stream for the font file PdfStream stream = new PdfStream(fontfile); stream.flateCompress(); stream.put(PdfName.LENGTH1, new PdfNumber(fontfile.length)); // create a reader object PdfReader reader = new PdfReader(pdf); int n = reader.getXrefSize(); PdfObject object; PdfDictionary font; PdfStamper stamper = new PdfStamper(reader, baos); PdfName fontname = new PdfName(fontName); for (int i = 0; i < n; i++) { object = reader.getPdfObject(i); if (object == null || !object.isDictionary()) continue; font = (PdfDictionary) object; if (PdfName.FONTDESCRIPTOR.equals(font.get(PdfName.TYPE1)) && fontname.equals(font.get(PdfName.FONTNAME))) { PdfIndirectObject objref = stamper.getWriter().addToBody(stream); font.put(PdfName.FONTFILE2, objref.getIndirectReference()); } } stamper.close(); reader.close(); return baos.toByteArray(); } }
From source file:bflows.FattureManagement.java
public void processPDF() { // Document pdf = null; BufferedWriter writer = null; consumi = new ArrayList<Consumo>(); lines = new ArrayList<String>(); try {/*from w w w . ja v a2s .co m*/ // Salvo file temporaneo per debugging //outputFile = new File("C:\\Users\\nklma\\Documents\\NetBeansProjects\\temp", "temp.txt"); //writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputFile))); //FileOutputStream fileOutputStream = new FileOutputStream("extracted.txt"); // iText Library PdfReader pdfReader = new PdfReader(inputStream); for (int page = 1; page <= pdfReader.getNumberOfPages(); page++) { SimpleTextExtractionStrategy strategy = new SimpleTextExtractionStrategy(); String currentText = PdfTextExtractor.getTextFromPage(pdfReader, page, strategy); String[] l = currentText.split("\n"); for (int i = 0; i < l.length; i++) lines.add(l[i]); } pdfReader.close(); //for(String line : lines) //{ // writer.write(line); // writer.newLine(); //} //writer.close(); boolean startPointFound = false; boolean dateFound = false; boolean totaleFound = false; boolean contributiFound = false; boolean prodottiFound = false; boolean altriFound = false; boolean ivaFound = false; Consumo consumo = null; for (String line : lines) { //recupero data fattura if (!dateFound) { if (line.contains("Emessa")) { String cleanLine = line.replaceAll("\\s+", " "); String[] splitted = cleanLine.split(" "); for (String s : splitted) { if (s.contains("/")) { s = s.replace("/", "-"); data = s; } } dateFound = true; } } //recupero totale fattura con iva if (!totaleFound) { if (line.contains("IMPORTO")) { String cleanLine = line.replaceAll("\\s+", " ").replaceAll("_", ""); String importo = cleanLine.replace("IMPORTO: ", "").replace("Euro", "").trim(); totale = Double.parseDouble(importo.replace(".", "").replace(",", ".")); totaleFound = true; } } //recupero importo contributi e abbonamenti if (!contributiFound) { if (line.contains("CONTRIBUTI E ABBONAMENTI")) { String cleanLine = line.replaceAll("\\s+", " "); String importo = cleanLine.replace("CONTRIBUTI E ABBONAMENTI ", ""); contributi = Double.parseDouble(importo.replace(".", "").replace(",", ".")); contributiFound = true; } } //recupero importo prodotti (noleggi) // SOLO PER 2017+ if (data != null && Integer.parseInt(data.split("-")[2]) >= 2017 && !prodottiFound) { if (line.contains("PRODOTTI")) { String cleanLine = line.replaceAll("\\s+", " "); String importo = cleanLine.replace("PRODOTTI ", ""); prodotti = Double.parseDouble(importo.replace(".", "").replace(",", ".")); prodottiFound = true; } } //recupero importo altri addebiti e accrediti if (!altriFound) { if (line.contains("ALTRI ADDEBITI E ACCREDITI")) { String cleanLine = line.replaceAll("\\s+", " "); String importo = cleanLine.replace("ALTRI ADDEBITI E ACCREDITI ", ""); altri = Double.parseDouble(importo.replace(".", "").replace(",", ".")); altriFound = true; } } //recupero importo IVA if ((contributiFound || altriFound) && !ivaFound) // in questo modo si evitano match con "partita iva" ecc { if (line.contains("IVA")) { String cleanLine = line.replaceAll("\\s+", " "); String importo = cleanLine.replace("IVA ", ""); iva = Double.parseDouble(importo.replace(".", "").replace(",", ".")); ivaFound = true; } } //Il primo RIEPILOGO PER UTENZA segna l'inizio della tabella dei consumi da analizzare if (!startPointFound && line.contains("RIEPILOGO PER UTENZA")) startPointFound = !startPointFound; if (!startPointFound) continue; //SERVIZI OPZIONALI segna la fine della tabella if (line.matches("SERVIZI OPZIONALI")) { consumi.add(consumo); return; } if (Integer.parseInt(data.split("-")[2]) >= 2017) { // ------------------------------ // PER FATTURE SUCCESSIVE AL 2017 // ------------------------------ ArrayList<String> splitted; splitted = StringMatcher.matches(line, "\\bLinea\\b\\s((\\d{10}))"); //if(line.matches("(?:(?:Linea)\\s)(\\d{10})")) if (!splitted.isEmpty()) { // Nuovo consumo if (consumo != null) //salvo la precedente { consumi.add(consumo); } //creo un nuovo consumo consumo = new Consumo(); StringBuilder str = new StringBuilder(splitted.get(0)); str.insert(3, "-"); consumo.Telefono = str.toString(); } splitted = StringMatcher.matches(line, "((?:\\w+\\s|\\w+-\\w+\\s)+)(?:\\d{2}\\/\\d{2}\\/\\d{4}\\s)((?:\\D+\\s)+)(?:\\d{2}\\/\\d{2}-\\d{2}\\/\\d{2}\\s)((\\d+,\\d+))$"); //if(line.matches("(?:(?:(?:\w+\s)+\w+\-)?(?:\w+\s)+)(?:\d{2}\/\d{2}\/\d{4})\s((?:\w+\s)+)(?:\d{2}\/\d{2}\-\d{2}\/\d{2})\s(\d+,\d+)")) if (!splitted.isEmpty()) { // Contributi o abbonamenti if (consumo != null) { String lel = splitted.get(1); if (splitted.get(1).contains("Contributi")) consumo.CRB += Double.parseDouble(splitted.get(2).replace(",", ".")); else if (splitted.get(1).contains("Abbonamenti")) consumo.ABB += Double.parseDouble(splitted.get(2).replace(",", ".")); } } splitted = StringMatcher.matches(line, "\\bRicariche\\b(?:\\s\\w+)+(?:\\s\\d\\s)((\\d+,\\d+))$"); if (!splitted.isEmpty()) //if(line.matches("(Ricariche(?:\\s\\w+)+)(\\s\\d\\s)(\\d+,\\d+)")) { // Ricariche //splitted = SplitLine.splitNewRicarica(line); if (consumo != null) { consumo.AAA += Double.parseDouble(splitted.get(0).replace(",", ".")); } } splitted = StringMatcher.matches(line, "\\bTotale\\b\\s((\\d+,\\d+))$"); //if(line.matches("(Totale\\s+)(\\d+,\\d+)")) if (!splitted.isEmpty()) { // Totale //splitted = SplitLine.splitNewTotale(line); if (consumo != null) { consumo.Totale += Double.parseDouble(splitted.get(0).replace(",", ".")); } } } else { // ------------------------------ // PER FATTURE PRECEDENTI AL 2017 // ------------------------------ // Linea e consumo if (line.matches("(\\d{3}(\\s+)?-(\\s+)?\\d{7})((?:\\s+)(?:\\w+\\s+)+)(\\d+,\\d+)")) // (3 digits)(optional whitespaces)-(optional whitespaces)(7 digits) // (any number of whitespaces)(any number of words followed by whitespace)(1+ digits),(1+digits) { //elimino gli spazi nel numero di telefono line = line.replace(" - ", "-"); //se entro qui significa che inizia un consumo ArrayList<String> splitted = SplitLine.splitConsumo1(line); //esiste un consumo con lo stesso numero quindi i dati vanno aggiunti if (consumo != null && splitted.get(0).replaceAll("\\s+", "").equals(consumo.Telefono)) // il continuo del precedente { if (splitted.get(1).contains("Contributi")) consumo.CRB = Double.parseDouble(splitted.get(2).replace(",", ".")); else if (splitted.get(1).contains("Altri")) consumo.AAA = Double.parseDouble(splitted.get(2).replace(",", ".")); else if (splitted.get(1).contains("Abbonamenti")) consumo.ABB = Double.parseDouble(splitted.get(2).replace(",", ".")); } else { //non esiste un consumo con il numero letto if (consumo != null) //salvo la precedente { consumi.add(consumo); } //creo un nuovo consumo consumo = new Consumo(); consumo.Telefono = splitted.get(0); if (splitted.get(1).contains("Contributi")) consumo.CRB = Double.parseDouble(splitted.get(2).replace(",", ".")); else if (splitted.get(1).contains("Altri")) consumo.AAA = Double.parseDouble(splitted.get(2).replace(",", ".")); else if (splitted.get(1).contains("Abbonamenti")) consumo.ABB = Double.parseDouble(splitted.get(2).replace(",", ".")); } } if (line.matches("((?:\\w+\\s+)+)(\\d+,\\d+)"))//(any number of words followed by whitespaces)(1+ digits),(1+ digits) { //continua la fattura precedente ArrayList<String> splitted = SplitLine.splitConsumo2(line); if (consumo != null) { if (splitted.get(0).contains("Contributi")) consumo.CRB = Double.parseDouble(splitted.get(1).replace(",", ".")); else if (splitted.get(0).contains("Altri")) consumo.AAA = Double.parseDouble(splitted.get(1).replace(",", ".")); else if (splitted.get(0).contains("Abbonamenti")) consumo.ABB = Double.parseDouble(splitted.get(1).replace(",", ".")); else if (splitted.get(0).contains("Totale")) consumo.Totale = Double.parseDouble(splitted.get(1).replace(",", ".")); } } } } //outputFile.delete(); } catch (IOException ex) { EService.logAndRecover(ex); setResult(EService.UNRECOVERABLE_ERROR); setErrorMessage("FattureManagement.ProcessPDF(): " + ex.getMessage()); } catch (NumberFormatException ex) { EService.logAndRecover((FatalError) ex); setResult(EService.UNRECOVERABLE_ERROR); setErrorMessage("FattureManagement.ProcessPDF(): " + ex.getMessage()); } }
From source file:book.pdftemplates.FillTemplateHelper.java
public FillTemplateHelper(String stationery) throws IOException, DocumentException { reader = new PdfReader(stationery); AcroFields fields = reader.getAcroFields(); pageSize = reader.getPageSize(1);// ww w . ja v a 2s. c o m body = fields.getFieldPositions("body").get(0).position; mLeft = body.getLeft() - pageSize.getLeft(); mRight = pageSize.getRight() - body.getRight(); mTop = pageSize.getTop() - body.getTop(); mBottom = body.getBottom() - pageSize.getBottom(); to = fields.getFieldPositions("to").get(0).position; from = fields.getFieldPositions("from").get(0).position; date = fields.getFieldPositions("date").get(0).position; footer = fields.getFieldPositions("footer").get(0).position; basefont = BaseFont.createFont(); font = new Font(basefont, 12); }
From source file:bpmlab.invioscript.ConstruirQualis.java
public static List<String> primeiraValidacao() { try {/*from w w w . j av a 2s.co m*/ PdfReader pdfReader = new PdfReader( "/home/bpmlab/NetBeansProjects/InvioScript/src/main/java/bpmlab/invioscript/Consulta_Webqualis.pdf"); String[] linha; String novaLinha = null; List<String> qualis = new ArrayList<>(); int total = 0; int invalidos = 0; for (int i = 1; i <= pdfReader.getNumberOfPages(); i++) { linha = PdfTextExtractor.getTextFromPage(pdfReader, i).split("\n"); for (int j = 1; j < linha.length; j++) { total++; try { if (linha[j].contains("Friday 06 March 2015") || linha[j].contains("TURISMO") || linha[j].contains("INTERNACIONAIS") || linha[j].contains("DEMOGRAFIA") || linha[j].contains("Lado C") || linha[j].contains("y TA Journal of Food C") || linha[j].contains("www.siicsalud.com C NUTRIO Atualizado") || linha[j].contains("ISSN T?TULO ESTRATO ?REA DE AVALIAO STATUS")) { throw new Exception(); } if (!linha[j].contains("Atualizado")) { throw new Exception(); } int indexFinal = linha[j].indexOf("Atualizado"); if (linha[j].contains(" A1 ")) { novaLinha = linha[j].substring(linha[j].indexOf(" A1 ") + 4, indexFinal); } else if (linha[j].contains(" A2 ")) { novaLinha = linha[j].substring(linha[j].indexOf(" A2 ") + 4, indexFinal); } else if (linha[j].contains(" B1 ")) { novaLinha = linha[j].substring(linha[j].indexOf(" B1 ") + 4, indexFinal); } else if (linha[j].contains(" B2 ")) { novaLinha = linha[j].substring(linha[j].indexOf(" B2 ") + 4, indexFinal); } else if (linha[j].contains(" B3 ")) { novaLinha = linha[j].substring(linha[j].indexOf(" B3 ") + 4, indexFinal); } else if (linha[j].contains(" B4 ")) { novaLinha = linha[j].substring(linha[j].indexOf(" B4 ") + 4, indexFinal); } else if (linha[j].contains(" B5 ")) { novaLinha = linha[j].substring(linha[j].indexOf(" B5 ") + 4, indexFinal); } else if (linha[j].contains(" C ")) { novaLinha = linha[j].substring(linha[j].indexOf(" C ") + 3, indexFinal); } else { throw new Exception(); } if (!linha[j].substring(0, 9).matches("\\w\\w\\w\\w-\\w\\w\\w\\w") || linha[j].substring(0, 12).matches("\\w\\w\\w\\w-\\w\\w\\w\\w A1") || linha[j].substring(0, 12).matches("\\w\\w\\w\\w-\\w\\w\\w\\w A2") || linha[j].substring(0, 12).matches("\\w\\w\\w\\w-\\w\\w\\w\\w B1") || linha[j].substring(0, 12).matches("\\w\\w\\w\\w-\\w\\w\\w\\w B2") || linha[j].substring(0, 12).matches("\\w\\w\\w\\w-\\w\\w\\w\\w B3") || linha[j].substring(0, 12).matches("\\w\\w\\w\\w-\\w\\w\\w\\w B4") || linha[j].substring(0, 12).matches("\\w\\w\\w\\w-\\w\\w\\w\\w B5") || linha[j].substring(0, 12).matches("\\w\\w\\w\\w-\\w\\w\\w\\w C ")) { throw new Exception(); } if (novaLinha != null) { qualis.add(linha[j]); } novaLinha = null; } catch (Exception e) { StringBuilder construirLinha; switch (linha[j]) { case "ADMINISTRAO, CINCIAS CONT?BEIS E": construirLinha = new StringBuilder(linha[j + 1]); construirLinha.insert(linha[j + 1].indexOf("Atualizado") - 1, " " + linha[j] + " " + linha[j + 2]); qualis.add(construirLinha.toString()); break; case "CINCIA POL?TICA E RELAES": construirLinha = new StringBuilder(linha[j + 1]); construirLinha.insert(linha[j + 1].indexOf("Atualizado") - 1, " " + linha[j] + " " + linha[j + 2]); qualis.add(construirLinha.toString()); break; case "PLANEJAMENTO URBANO E REGIONAL /": construirLinha = new StringBuilder(linha[j + 1]); construirLinha.insert(linha[j + 1].indexOf("Atualizado") - 1, " " + linha[j] + " " + linha[j + 2]); qualis.add(construirLinha.toString()); break; case "American Journal of Physiology. Regulatory, Integrative and Comparative Physiology": construirLinha = new StringBuilder(linha[j + 1]); construirLinha.insert(9, " " + linha[j]); qualis.add(construirLinha.toString()); break; case "Proceedings of the National Academy of Sciences of the United States of America": construirLinha = new StringBuilder(linha[j + 1]); construirLinha.insert(9, " " + linha[j] + linha[j + 2]); qualis.add(construirLinha.toString()); break; case "Revista de Clnica e Pesquisa Odontolgica (Impresso) / Journal of Dental Clinical and": construirLinha = new StringBuilder(linha[j + 1]); construirLinha.insert(9, " " + linha[j] + " " + linha[j + 2]); qualis.add(construirLinha.toString()); break; default: invalidos++; if (!(linha[j].contains("Friday 06 March") || linha[j].contains("TURISMO") || linha[j].contains("(Online)") || linha[j].contains("Research") || linha[j].contains("INTERNACIONAIS") || linha[j].contains("DEMOGRAFIA"))) { // System.out.println(linha[j]); } break; } } } } for (String q : qualis) { System.out.println(q); } System.out.println("TOTAL: " + total); System.out.println("VALIDOS: " + qualis.size() + ";" + ((float) qualis.size() * 100 / total) + "%"); System.out.println("INVALIDOS: " + invalidos + ";" + ((float) invalidos * 100 / total) + "%"); System.out.println(qualis.size() + invalidos); return qualis; } catch (IOException ex) { return null; } }