List of usage examples for org.apache.pdfbox.pdmodel PDDocument close
@Override public void close() throws IOException
From source file:SanerePdf.java
License:Apache License
public static String txtFromPdf(String filename) throws IOException { PDDocument doc = PDDocument.load(filename); PDFTextStripper stripper = new PDFTextStripper(); stripper.setPageSeparator("\f"); // form feed //stripper.setSortByPosition(true); //stripper.setShouldSeparateByBeads(false); String text = stripper.getText(doc); doc.close(); return text;/*from www . j av a2 s . c o m*/ }
From source file:PDFTextExtract.java
License:Apache License
/** * This will parse the documents data.//from ww w . j a v a2 s .c o m * * @throws IOException If there is an error parsing the document. */ public void process() throws IOException { PDDocument document = null; String res = null; OutputStream os = null; try { // Target PDF file. document = PDDocument.load(new File(this.PDFFilePath)); // Extract Text from PDF ordered by page number. for (int i = 1; i <= document.getNumberOfPages(); i++) { System.out.println("processing page " + i + "..."); _tmp.clear(); PDFTextExtract stripper = new PDFTextExtract(); // Tell PDFBox to sort the text positions. stripper.setSortByPosition(true); // Extract only one page. stripper.setStartPage(i); stripper.setEndPage(i); // Convert class `textPositions` into `Text`. // Save conversion result in `_tmp`. Writer dummy = new OutputStreamWriter(new ByteArrayOutputStream()); stripper.writeText(document, dummy); // Skip if nothing converted. if (_tmp.isEmpty()) continue; // Concate result into string. for (Text now : _tmp) { res += now.unicode; } } // Write result to file. os = new FileOutputStream(OutputFilepath); os.write(res.getBytes()); System.out.println("processing completed."); } finally { if (document != null) { document.close(); } if (os != null) { os.close(); } } }
From source file:mail.java
private void ImprimirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ImprimirActionPerformed try {//from w ww . j a va 2 s .co m // TODO add your handling code here: PDDocument documento = new PDDocument(); PDPage paginablanco = new PDPage(); documento.addPage(paginablanco); PDPageContentStream content; try { content = new PDPageContentStream(documento, paginablanco); content.beginText(); content.setFont(PDType1Font.HELVETICA, 9); content.newLineAtOffset(50, 780); content.showText("Consejo Profesional de Abogacia"); content.endText(); content.beginText(); content.setFont(PDType1Font.HELVETICA, 9); content.newLineAtOffset(450, 780); Locale espanol = new Locale("es", "ES"); SimpleDateFormat dateFormat = new SimpleDateFormat("EEEE MMMM d HH:mm:ss z yyyy", espanol); String fecha = dateFormat.format(new Date()); content.showText(fecha); content.endText(); content.beginText(); content.setFont(PDType1Font.HELVETICA, 6); content.newLineAtOffset(50, 770); content.showText("Direccin: San Martin 457 - Formosa"); content.endText(); content.beginText(); content.setFont(PDType1Font.HELVETICA, 10); content.newLineAtOffset(200, 750); content.showText("Estado de Cuenta de Matricula"); content.endText(); content.beginText(); content.setFont(PDType1Font.HELVETICA, 10); content.newLineAtOffset(50, 735); content.showText("Matricula N " + idmatricula + " Nombre: " + nombre + "," + apellido);// content.endText(); content.beginText(); content.setFont(PDType1Font.HELVETICA, 10); content.newLineAtOffset(100, 705); content.showText( "Items Periodo Vencimiento Importe"); content.endText(); content.addRect(50, 10, 400, 700); int j = 0; int renglon = 685; System.out.println(itemsList.size()); for (int i = 1; i < itemsList.size(); i++) { content.beginText(); content.setFont(PDType1Font.HELVETICA, 10); j++; content.newLineAtOffset(100, renglon); content.showText(" " + i + " " + itemsList.get(i).getVencimiento() + " " + itemsList.get(i).getPeriodo() + " " + String.valueOf(itemsList.get(i).getImporte())); renglon = renglon - 13; content.endText(); } content.beginText(); content.newLineAtOffset(100, renglon - 20); DecimalFormatSymbols simbolos = new DecimalFormatSymbols(); simbolos.setDecimalSeparator('.'); DecimalFormat decim = new DecimalFormat("0.00", simbolos); content.showText("Total Adeudado :$ " + String.valueOf(decim.format(total))); content.endText(); content.close(); documento.save("matricula_" + idmatricula + ".pdf"); documento.close(); System.out.println("guardo archivo matricula_" + idmatricula + ".pdf"); EnviarMail.setEnabled(true); } catch (IOException ex) { EnviarMail.setEnabled(false); Logger.getLogger(mail.class.getName()).log(Level.SEVERE, null, ex); } bonos(); } catch (SQLException ex) { Logger.getLogger(mail.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:mail.java
private void bonos() throws SQLException { //**//from w ww. j av a2s . co m double total = 0; ListaBono.clear(); if (idmatricula != "0") { Connection conn; try { conn = Conector.Connect(); System.out.println("Bonos"); PreparedStatement resultado; PreparedStatement bono; String sql1 = "select * from bonos b left join juzgados j on b.ID_JUZGADO=j.id_juzgado where ID_MATRICULA=? ORDER BY ANO "; bono = conn.prepareStatement(sql1); bono.setString(1, idmatricula); ResultSet ds = bono.executeQuery(); DecimalFormatSymbols simbolos = new DecimalFormatSymbols(); SimpleDateFormat formatofecha = new SimpleDateFormat("dd-MM-yyyy"); simbolos.setDecimalSeparator('.'); DecimalFormat decim = new DecimalFormat("0.00", simbolos); if (ds.first()) { ds.beforeFirst();//regresa el puntero al primer registro while (ds.next()) { int numero_expediente = ds.getInt("NUMERO_EXPTE"); String ano = ds.getString("ANO"); String caratula = ds.getString("cara"); String fecha_actuacion = ds.getString("FECHA_ACTUACION"); String juzgado = ds.getString("descripcion"); double importe = ds.getDouble("MONTOBONO"); Double monto_bono = Double.valueOf(decim.format(importe)); caratula = caratula.replaceAll("\r\n", " "); caratula = caratula.replaceAll("\n", " "); caratula = caratula.replaceAll("\\\\\\\\", ""); // Date per =formatofecha.parse(fecha_pa); // String fecha_pago = formatofecha.format(per); System.out.println(idmatricula); System.out.format("%s,%s,%s,%s,%s,%s\n", numero_expediente, ano, caratula.toLowerCase(), juzgado.toLowerCase(), fecha_actuacion, importe); ListaBono.add( new Bonos(numero_expediente, ano, caratula, juzgado, fecha_actuacion, monto_bono)); total = total + monto_bono; botones(true); } ds.close(); System.out.println(total); System.out.println(ListaBono.size()); } } catch (SQLException ex) { Logger.getLogger(mail.class.getName()).log(Level.SEVERE, null, ex); } } //** int alto = 595; int ancho = 842; PDDocument documento = new PDDocument(); PDPage paginablanco = new PDPage(new PDRectangle(ancho, alto)); documento.addPage(paginablanco); PDPageContentStream content; System.out.println(paginablanco.getMediaBox().getHeight() + "--" + paginablanco.getMediaBox().getWidth()); try { content = new PDPageContentStream(documento, paginablanco); content.beginText(); content.setFont(PDType1Font.HELVETICA, 9); content.newLineAtOffset(50, alto - 20); content.showText("Consejo Profesional de Abogacia"); content.endText(); content.beginText(); content.setFont(PDType1Font.HELVETICA, 9); content.newLineAtOffset(450, alto - 20); Locale espanol = new Locale("es", "ES"); SimpleDateFormat dateFormat = new SimpleDateFormat("EEEE MMMM d HH:mm:ss z yyyy", espanol); String fecha = dateFormat.format(new Date()); content.showText(fecha); content.endText(); content.beginText(); content.setFont(PDType1Font.HELVETICA, 6); content.newLineAtOffset(50, alto - 28); content.showText("Direccin: San Martin 457 - Formosa"); content.endText(); content.beginText(); content.setFont(PDType1Font.HELVETICA, 10); content.newLineAtOffset(200, alto - 38); content.showText("BONOS DE ACCCIN LETRADA"); content.endText(); content.beginText(); content.setFont(PDType1Font.HELVETICA, 8); content.newLineAtOffset(200, alto - 45); content.showText("Estado de Gestin de bonos del Profesional"); content.endText(); content.beginText(); content.setFont(PDType1Font.HELVETICA, 9); content.newLineAtOffset(50, alto - 60); // para que no tenga errores las tabla de mysql cotejamiento en utf8_bin al cargar en la PDF da error si esta con el tema de las content.showText("Matricula N: " + idmatricula + " Nombre: " + nombre + "," + apellido);// content.endText(); content.beginText(); content.setFont(PDType1Font.HELVETICA, 8); content.newLineAtOffset(50, alto - 75); content.showText("Item"); content.endText(); content.beginText(); content.setFont(PDType1Font.HELVETICA, 8); content.newLineAtOffset(70, alto - 75); content.showText("Expte"); content.endText(); content.beginText(); content.setFont(PDType1Font.HELVETICA, 8); content.newLineAtOffset(100, alto - 75); content.showText("Ao"); content.endText(); content.beginText(); content.setFont(PDType1Font.HELVETICA, 8); content.newLineAtOffset(130, alto - 75); content.showText("Caratula"); content.endText(); content.beginText(); content.setFont(PDType1Font.HELVETICA, 8); content.newLineAtOffset(500, alto - 75); content.showText("Juzgado"); content.endText(); content.beginText(); content.setFont(PDType1Font.HELVETICA, 8); content.newLineAtOffset(650, alto - 75); content.showText("Monto"); content.endText(); content.beginText(); content.setFont(PDType1Font.HELVETICA, 8); content.newLineAtOffset(680, alto - 75); content.showText("Vencimiento"); content.endText(); content.setNonStrokingColor(Color.DARK_GRAY); content.addRect(25, 45, 700, 400); // content.fill(); int j = 0; int renglon = alto - 100; for (int i = 0; i < ListaBono.size(); i++) { content.beginText(); content.setFont(PDType1Font.HELVETICA, 8); j++; content.newLineAtOffset(50, renglon); content.showText(String.valueOf(i + 1)); content.endText(); content.beginText(); content.setFont(PDType1Font.HELVETICA, 8); content.newLineAtOffset(70, renglon); content.showText(String.valueOf(ListaBono.get(i).getNumero_expediente())); content.endText(); content.beginText(); content.setFont(PDType1Font.HELVETICA, 8); content.newLineAtOffset(100, renglon); content.showText(ListaBono.get(i).getAno()); content.endText(); content.beginText(); content.setFont(PDType1Font.HELVETICA, 8); content.newLineAtOffset(130, renglon); content.showText(String.valueOf(ListaBono.get(i).getCaratula())); content.endText(); content.setFont(PDType1Font.COURIER, 8); content.beginText(); content.setFont(PDType1Font.HELVETICA, 8); content.newLineAtOffset(500, renglon); content.showText(ListaBono.get(i).getJuzgado()); content.endText(); content.beginText(); content.setFont(PDType1Font.HELVETICA, 8); content.newLineAtOffset(650, renglon); content.showText(String.valueOf(ListaBono.get(i).getMonto_bono())); content.endText(); content.beginText(); content.setFont(PDType1Font.HELVETICA, 8); content.newLineAtOffset(680, renglon); content.showText(ListaBono.get(i).getFecha_pago()); content.endText(); renglon = renglon - 13; } content.beginText(); content.newLineAtOffset(100, renglon - 20); DecimalFormatSymbols simbolos = new DecimalFormatSymbols(); simbolos.setDecimalSeparator('.'); DecimalFormat decim = new DecimalFormat("0.00", simbolos); content.showText("Total Bonos Adeudados :$ " + String.valueOf(decim.format(total))); content.endText(); content.close(); documento.save("matricula_" + idmatricula + "_bonos.pdf"); documento.close(); System.out.println("guardo archivo matricula_" + idmatricula + "_bonos.pdf"); // System.out.println(String.valueOf(decim.format(total))); EnviarMail.setEnabled(true); } catch (IOException ex) { EnviarMail.setEnabled(false); Logger.getLogger(mail.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:FormFiller.java
private static void fillPdf(HashMap dealerTrackData, String inputFileName, String outputDir, String outputFormType) {/*from www. j a v a 2s .co m*/ try { PDDocument pdfTemplate = PDDocument.load(new File(inputFileName)); PDDocumentCatalog docCatalog = pdfTemplate.getDocumentCatalog(); PDAcroForm acroForm = docCatalog.getAcroForm(); List<PDField> fieldList = acroForm.getFields(); String[] fieldArray = new String[fieldList.size()]; int i = 0; for (PDField sField : fieldList) { fieldArray[i] = sField.getFullyQualifiedName(); i++; } for (String f : fieldArray) { PDField field = acroForm.getField(f); String value = (String) dealerTrackData.get(f); if (value != null) { try { field.setValue(value); } catch (IllegalArgumentException e) { System.err.println("Could not insert: " + f + "."); } } } pdfTemplate.save(outputDir + "/" + dealerTrackData.get("fullName") + " " + outputFormType + ".pdf"); // printing - need to look into the long form stuff! if (print && !inputFileName.contains("Title Guarantee")) printPdf(pdfTemplate, dealerTrackData, inputFileName, inputFileName.contains("Purchase Contract") ? 2 : 1); pdfTemplate.close(); } catch (Exception e) { e.printStackTrace(); } }
From source file:FormFiller.java
private static void getChaseCreditInfo(JavascriptExecutor jse, HashMap dealerTrackData, WebDriver driver) throws InterruptedException, IOException { // Home Phone String homePhone0 = (String) jse.executeScript("return document.getElementById('iFrm')." + "contentWindow.document.body.childNodes[2].contentDocument.getElementById('app_home_phone1').value"); String homePhone1 = (String) jse.executeScript("return document.getElementById('iFrm')." + "contentWindow.document.body.childNodes[2].contentDocument.getElementById('app_home_phone2').value"); String homePhone2 = (String) jse.executeScript("return document.getElementById('iFrm')." + "contentWindow.document.body.childNodes[2].contentDocument.getElementById('app_home_phone3').value"); dealerTrackData.put("Home (or business) Phone Number", "(" + homePhone0 + ")" + " " + homePhone1 + "-" + homePhone2); // Business Phone String workPhone0 = (String) jse.executeScript("return document.getElementById('iFrm')." + "contentWindow.document.body.childNodes[2].contentDocument.getElementById('app_bus_phone1').value"); String workPhone1 = (String) jse.executeScript("return document.getElementById('iFrm')." + "contentWindow.document.body.childNodes[2].contentDocument.getElementById('app_bus_phone2').value"); String workPhone2 = (String) jse.executeScript("return document.getElementById('iFrm')." + "contentWindow.document.body.childNodes[2].contentDocument.getElementById('app_bus_phone3').value"); dealerTrackData.put("workPhone", "(" + workPhone0 + ")" + " " + workPhone1 + "-" + workPhone2); jse.executeScript(/* w ww . java2 s. c o m*/ "document.getElementById('iFrm').contentWindow.document.body.childNodes[2].contentDocument." + "getElementsByName('cmdPrint')[0].click()"); Thread.sleep(4000); File chaseCreditApplication; String downloadDir = System.getProperty("os.name").toLowerCase().contains("win") ? "C:/Users/" + System.getProperty("user.name") + "/Downloads/" : "/Users/" + System.getProperty("user.name") + "/Downloads/"; do { chaseCreditApplication = getLatestFileFromDir(downloadDir); Thread.sleep(1000); } while (chaseCreditApplication == null || System.currentTimeMillis() > chaseCreditApplication.lastModified() + 4000); PDDocument creditAppTemplate = PDDocument.load(chaseCreditApplication); outputDir = makeOutputDir(dealerTrackData); creditAppTemplate.save(outputDir + "/" + dealerTrackData.get("fullName") + " " + "Chase Credit App.pdf"); if (print) printPdf(creditAppTemplate, dealerTrackData, "Chase Credit Application", 1); creditAppTemplate.close(); }
From source file:PrintImageLocations.java
License:Apache License
/** * This will print the documents data./*from ww w .j av a 2s. co m*/ * * @param args The command line arguments. * * @throws Exception If there is an error parsing the document. */ public static void main2() throws Exception { if (flag) { usage(); } else { PDDocument document = null; try { document = PDDocument.load(PrintTextLocations.INPUTFILE); if (document.isEncrypted()) { try { document.decrypt(""); } catch (InvalidPasswordException e) { System.err.println("Error: Document is encrypted with a password."); System.exit(1); } } PrintImageLocations printer = new PrintImageLocations(); List allPages = document.getDocumentCatalog().getAllPages(); for (int i = 0; i < allPages.size(); i++) { PDPage page = (PDPage) allPages.get(i); System.out.println("Processing page: " + i); printer.processStream(page, page.findResources(), page.getContents().getStream()); } } finally { if (document != null) { document.close(); } } } }
From source file:PrintUtil.java
License:Apache License
void print() { PDDocument doc = null; try {// w w w . j av a 2 s . co m doc = PDDocument.load(new File(path)); } catch (IOException e) { e.printStackTrace(); } job.setPrintable(new PDFPrintable(doc, Scaling.SCALE_TO_FIT)); PrintRequestAttributeSet attr = new HashPrintRequestAttributeSet(); attr.add(Sides.DUPLEX); attr.add(MediaSizeName.ISO_A4); System.out.println("Printing =>" + path); try { job.print(attr); } catch (PrinterException e) { e.printStackTrace(); } finally { if (doc != null) { try { doc.close(); } catch (IOException ignored) { } } } }
From source file:PDFConverter.java
License:Apache License
/** * Implementation is informed by PDFBox authors. * * @param doc// w w w . j a v a 2 s . co m * @return * @throws IOException */ @Override public synchronized ConvertedDocument convert(java.io.File doc) throws IOException { /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /** * Adapted from LucenePDFDocument.java from PDFBox lucene project * * This class is used to create a document for the lucene search engine. * This should easily plug into the IndexHTML or IndexFiles that comes * with the lucene project. This class will populate the following * fields. * <table> <tr> <th>Lucene Field Name</th> <th>Description</th> </tr> * <tr> * <td>path</td> <td>File system path if loaded from a file</td> </tr> * <tr> * <td>url</td> <td>URL to PDF document</td> </tr> <tr> * <td>contents</td> * <td>Entire contents of PDF document, indexed but not stored</td> * </tr> * <tr> <td>summary</td> <td>First 500 characters of content</td> </tr> * <tr> * <td>modified</td> <td>The modified date/time according to the url or * path</td> </tr> <tr> <td>uid</td> <td>A unique identifier for the * Lucene document.</td> </tr> <tr> <td>CreationDate</td> <td>From PDF * meta-data if available</td> </tr> <tr> <td>Creator</td> <td>From PDF * meta-data if available</td> </tr> <tr> <td>Keywords</td> <td>From PDF * meta-data if available</td> </tr> <tr> <td>ModificationDate</td> * <td>From PDF meta-data if available</td> </tr> <tr> <td>Producer</td> * <td>From PDF meta-data if available</td> </tr> <tr> <td>Subject</td> * <td>From PDF meta-data if available</td> </tr> <tr> <td>Trapped</td> * <td>From PDF meta-data if available</td> </tr> <tr> * <td>Encrypted</td> <td>From PDF meta-data if available</td> </tr> * </table> * * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a> * @version $Revision: 1.23 $ * * @throws IOException If there is an error parsing the document. */ PDDocument pdfDocument = null; ConvertedDocument textdoc = new ConvertedDocument(doc); try { pdfDocument = PDDocument.load(doc); if (pdfDocument.isEncrypted()) { //Just try using the default password and move on // Even if the doc is encrypted, apparently you can try. Throw exception if it fails. textdoc.addProperty("encrypted", "YES"); } //create a writer where to append the text content. StringWriter writer = new StringWriter(); stripper.resetEngine(); stripper.writeText(pdfDocument, writer); PDDocumentInformation info = pdfDocument.getDocumentInformation(); if (info != null) { textdoc.addAuthor(info.getAuthor()); try { textdoc.addCreateDate(info.getCreationDate()); } catch (IOException io) { //ignore, bad date but continue with indexing } textdoc.addProperty("creator_tool", info.getCreator()); textdoc.addProperty("keywords", info.getKeywords()); /* try { metadata.add("ModificationDate", info.getModificationDate()); } catch (IOException io) { //ignore, bad date but continue with indexing } */ //metadata.add("Producer", info.getProducer()); textdoc.addProperty("subject", info.getSubject()); String ttl = info.getTitle(); if (ttl == null || "untitled".equalsIgnoreCase(ttl)) { ttl = textdoc.filename; } textdoc.addTitle(ttl); // metadata.add("Trapped", info.getTrapped()); // TODO: Character set is what? textdoc.setEncoding("UTF-8"); } // Note: the buffer to string operation is costless; // the char array value of the writer buffer and the content string // is shared as long as the buffer content is not modified, which will // not occur here. textdoc.setText(writer.getBuffer().toString()); return textdoc; } finally { if (pdfDocument != null) { pdfDocument.close(); } } }
From source file:PDFExtractMetadata.java
License:Apache License
/** * This is the main method./*from www.j a v a 2 s . co m*/ * * @param args The command line arguments. * * @throws IOException If there is an error parsing the document. * @throws XmpParsingException */ public static void main(String[] args) throws IOException, XmpParsingException { if (args.length != 1) { usage(); System.exit(1); } else { PDDocument document = null; try { document = PDDocument.load(new File(args[0])); PDDocumentCatalog catalog = document.getDocumentCatalog(); PDMetadata meta = catalog.getMetadata(); if (meta != null) { DomXmpParser xmpParser = new DomXmpParser(); try { XMPMetadata metadata = xmpParser.parse(meta.createInputStream()); DublinCoreSchema dc = metadata.getDublinCoreSchema(); if (dc != null) { display("Title:", dc.getTitle()); display("Description:", dc.getDescription()); listString("Creators: ", dc.getCreators()); listCalendar("Dates:", dc.getDates()); listString("Subjects:", dc.getSubjects()); } AdobePDFSchema pdf = metadata.getAdobePDFSchema(); if (pdf != null) { display("Keywords:", pdf.getKeywords()); display("PDF Version:", pdf.getPDFVersion()); display("PDF Producer:", pdf.getProducer()); } XMPBasicSchema basic = metadata.getXMPBasicSchema(); if (basic != null) { display("Create Date:", basic.getCreateDate()); display("Modify Date:", basic.getModifyDate()); display("Creator Tool:", basic.getCreatorTool()); } } catch (XmpParsingException e) { System.err.println("An error ouccred when parsing the meta data: " + e.getMessage()); } } else { // The pdf doesn't contain any metadata, try to use the // document information instead PDDocumentInformation information = document.getDocumentInformation(); if (information != null) { showDocumentInformation(information); } } } finally { if (document != null) { document.close(); } } } }