List of usage examples for org.apache.pdfbox.pdmodel PDDocument save
public void save(OutputStream output) throws IOException
From source file:Tools.PostProcessing.java
private String pdfNumbering(String processedPdfFileName) throws IOException, COSVisitorException { PDDocument cedmsPdf = PDDocument.load(processedPdfFileName); List pages = cedmsPdf.getDocumentCatalog().getAllPages(); PDFTextStripper pdfStripper = new PDFTextStripper(); String res = pdfStripper.getText(cedmsPdf); //System.out.println(res); Boolean isPreProcessed = res.contains(GlobalVar.PRE_PROC_KEY_SYMBOL); // check if the file is pre-processed. Boolean isNumbered = res.contains("/0"); Iterator<PDPage> iter = pages.iterator(); int sequenceNum = 1; // start from 0001 if (isPreProcessed && isNumbered) { GlobalVar.updateSeqNum(cedmsPdf, CYCLE); // update the sequence number } else if (isPreProcessed) { // first time int pageNumber = 1; while (iter.hasNext()) { PDPage page = iter.next();/* w w w . ja v a2 s .co m*/ pdfStripper.setStartPage(pageNumber); pdfStripper.setEndPage(pageNumber); res = pdfStripper.getText(cedmsPdf); // == numbering if (res.contains(GlobalVar.PRE_PROC_KEY_SYMBOL)) { String[] data = res.split(GlobalVar.PRE_PROC_KEY_SYMBOL); if (VERIFY_WITH_LISTING) { //verify the sequence number with the number on 80/80 listing String ssn = data[0].substring(data[0].length() - GlobalVar.SSN_LEN, data[0].length()); String ctrlNum = data[1].substring(0, -GlobalVar.MAX_CTRL_NUM_LEN); String seqNum = LEGIT_LV_MAP_FOR_COLOR_LV_LOG.get(ssn).get(ctrlNum); int seqNumberFrom8080Listing = Integer.parseInt(seqNum); if (seqNumberFrom8080Listing != sequenceNum) { JOptionPane.showMessageDialog(null, ssn + " " + ctrlNum + " seq num: " + seqNum + " do not match 80/80 listing"); } } PDPageContentStream stream = new PDPageContentStream(cedmsPdf, page, true, false); stream.beginText(); stream.setFont(PDType1Font.HELVETICA, GlobalVar.SEQ_NUM_FONT_SIZE); stream.moveTextPositionByAmount(GlobalVar.SEQ_NUM_TEXT_X_POSITION, GlobalVar.SEQ_NUM_TEXT_Y_POSITION); stream.setTextRotation(3.14 / 2, GlobalVar.SEQ_NUM_TEXT_X_POSITION, GlobalVar.SEQ_NUM_TEXT_Y_POSITION); // rotate text 90 degree at x = 600, y = 400 //stream.drawString(CYCLE + "/" + seqNum); stream.drawString(CYCLE + "/" + GlobalVar.globalCountGenerator5Digit(sequenceNum)); sequenceNum++; stream.endText(); stream.close(); } pageNumber++; // end of numbering } } // String suffix = "_" + CYCLE +" Numbered.pdf"; // String fileName = DTL_PDF_FILE_NAME.replace(".pdf", suffix); String fileName = processedPdfFileName.replaceAll(".pdf", GlobalVar.NUMBERED_PDF); cedmsPdf.save(fileName); cedmsPdf.close(); return fileName; }
From source file:Tools.PreProcessing.java
private void generatePDFFile(String pdfFileName, String xlsxFileName, Boolean[][] statusArray) throws IOException, COSVisitorException { List<String> textList = readXlsxFile(xlsxFileName); // System.out.println("text list: " + textList); //Iterator<String> it = textList.iterator(); PDDocument pdf = PDDocument.load(pdfFileName); List pages = pdf.getDocumentCatalog().getAllPages(); Iterator<PDPage> iter = pages.iterator(); int pageNum = 0; // 0 based int index = 0; while (iter.hasNext()) { PDPage page = iter.next();/* www . j a va 2 s . c o m*/ // PDPage pageBlank = new PDPage(); PDPageContentStream stream = new PDPageContentStream(pdf, page, true, false); if (statusArray[GlobalVar.SELECT_BUTTON_INDEX][pageNum]) { if (index < textList.size()) { String text = textList.get(index); // zero based //System.out.println(text); pageWrite(stream, text, index); } else { JOptionPane.showMessageDialog(null, "Preprocessing is inaccurate. XLSX list is shorter than the pdf file."); break; } index++; } stream.close(); pageNum++; } if (index > textList.size()) { JOptionPane.showMessageDialog(null, "Preproc might be inaccurate. XLSX list is longer than the pdf file."); } // out put two pdf files: one is template for printer print hardcopies, the other is digital copy String suffix = "_" + "_pre_processed.pdf"; pdfFileName = pdfFileName.replace(".pdf", suffix); pdf.save(pdfFileName); pdf.close(); }
From source file:uia.pdf.PDFMakerTest.java
License:Apache License
@Test public void justTest() throws Exception { PDDocument doc = new PDDocument(); try {/*from ww w . j av a2 s. c o m*/ PDPage page = new PDPage(); doc.addPage(page); PDPageContentStream contents = new PDPageContentStream(doc, page, AppendMode.APPEND, false, false); contents.moveTo(20, 650); contents.lineTo(500, 650); contents.stroke(); contents.close(); PDImageXObject pdImage = PDImageXObject .createFromFile(PDFMakerTest.class.getResource("sample.jpg").getFile(), doc); contents = new PDPageContentStream(doc, page, AppendMode.APPEND, false, false); contents.drawImage(pdImage, 20, 700); contents.close(); contents = new PDPageContentStream(doc, page, AppendMode.APPEND, false, false); contents.moveTo(20, 550); contents.lineTo(500, 550); contents.stroke(); contents.close(); doc.save("C:\\TEMP\\IMAGE.PDF"); } finally { doc.close(); } }
From source file:Utilities.BatchInDJMSHelper.java
public void generateProcessedAndRejectPDFs(String preProcPdfFileName) throws IOException, COSVisitorException { PDDocument pdf = PDDocument.load(preProcPdfFileName); PDDocument rejectPdf = new PDDocument(); PDDocument auditPdf = new PDDocument(); String rejectPdfFileName = preProcPdfFileName.replace(".pdf", "_forReject.pdf"); String auditPdfFileName = preProcPdfFileName.replace(".pdf", "_forAudit.pdf"); int pageNum = pdf.getNumberOfPages(); // add reject page into rejectPdf PDFTextStripper pdfStripper = new PDFTextStripper(); boolean isLastReject = true; // last page status for (int i = 0; i < pageNum; i++) { PDPage page = (PDPage) pdf.getDocumentCatalog().getAllPages().get(i); int pageIndex = i + 1; pdfStripper.setStartPage(pageIndex); pdfStripper.setEndPage(pageIndex); String res = pdfStripper.getText(pdf); System.out.println(res);//from w ww . jav a2 s . c om if (res.contains(GlobalVar.PRE_PROC_KEY_SYMBOL)) { String[] data = GlobalVar.getCtrlNumAndfullSSN(res); String ctrlNum = data[0]; String fullSSN = data[1]; System.out.println("full ssn:" + fullSSN + ". ctrl num:" + ctrlNum); if (LEGIT_LV_MAP.containsKey(fullSSN)) { System.out.println("ctrl num: " + LEGIT_LV_MAP.get(fullSSN)); } if (LEGIT_LV_MAP.containsKey(fullSSN) && LEGIT_LV_MAP.get(fullSSN).containsKey(ctrlNum)) { System.out.println("Good leave"); auditPdf.addPage(page); isLastReject = false; } else { rejectPdf.addPage(page); drawComments(rejectPdf, page, fullSSN, ctrlNum); isLastReject = true; } } else { // add the supporting documents to the last pdf file if (isLastReject) { rejectPdf.addPage(page); } else { auditPdf.addPage(page); } } } if (rejectPdf.getNumberOfPages() > 0 && auditPdf.getNumberOfPages() > 0) { auditPdf.save(auditPdfFileName); rejectPdf.save(rejectPdfFileName); JOptionPane.showMessageDialog(null, "The ready-for-aduit and the rejected leave forms are saved in *_forAudit.pdf and *_forReject.pdf, respectively."); numberPDFFile(auditPdfFileName); } else if (rejectPdf.getNumberOfPages() > 0) { rejectPdf.save(rejectPdfFileName); JOptionPane.showMessageDialog(null, "The rejected leave forms are saved in *_forReject.pdf."); } else if (auditPdf.getNumberOfPages() > 0) { auditPdf.save(auditPdfFileName); JOptionPane.showMessageDialog(null, "The ready-for-aduit leave forms are saved in *_forAduit.pdf."); numberPDFFile(auditPdfFileName); } rejectPdf.close(); auditPdf.close(); pdf.close(); }
From source file:Utilities.BatchInDJMSHelper.java
public void generateReadyForAuditPDF(String preProcPdfFileName) throws IOException, COSVisitorException { PDDocument pdf = PDDocument.load(preProcPdfFileName); PDDocument auditPdf = new PDDocument(); String auditPdfFileName = preProcPdfFileName.replace(".pdf", "_forAudit.pdf"); int pageNum = pdf.getNumberOfPages(); // add reject page into rejectPdf PDFTextStripper pdfStripper = new PDFTextStripper(); for (int i = 0; i < pageNum; i++) { PDPage page = (PDPage) pdf.getDocumentCatalog().getAllPages().get(i); int pageIndex = i + 1; pdfStripper.setStartPage(pageIndex); pdfStripper.setEndPage(pageIndex); String res = pdfStripper.getText(pdf); System.out.println(res);//from w w w. j a v a2 s . c om boolean isLastReject = true; // last page status if (res.contains(GlobalVar.PRE_PROC_KEY_SYMBOL)) { String[] data = GlobalVar.getCtrlNumAndfullSSN(res); String ctrlNum = data[0]; String fullSSN = data[1]; System.out.println("full ssn:" + fullSSN + ". ctrl num:" + ctrlNum); if (LEGIT_LV_MAP.containsKey(fullSSN)) { System.out.println("ctrl num: " + LEGIT_LV_MAP.get(fullSSN)); } if (LEGIT_LV_MAP.containsKey(fullSSN) && LEGIT_LV_MAP.get(fullSSN).containsKey(ctrlNum)) { System.out.println("Good leave"); auditPdf.addPage(page); isLastReject = false; } } else { // add the supporting documents to the last pdf file if (!isLastReject) { auditPdf.addPage(page); } } } if (auditPdf.getNumberOfPages() > 0) { auditPdf.save(auditPdfFileName); JOptionPane.showMessageDialog(null, "The ready-for-aduit leave forms are saved in *_forAduit.pdf."); numberPDFFile(auditPdfFileName); } auditPdf.close(); pdf.close(); }
From source file:Utils.PDF.java
public static void print(String nFactura, Date fecha, String empleado, ClienteVO cliente, List<ProductosCanasta> productos, BigDecimal subtotal) throws IOException { DateFormat dateAnio = new SimpleDateFormat("yyyy"); DateFormat dateMes = new SimpleDateFormat("MM"); DateFormat dateDia = new SimpleDateFormat("dd"); String direccion = "facturas" + "/" + dateAnio.format(fecha) + "/" + dateMes.format(fecha) + "/" + dateDia.format(fecha);/*from w w w .j av a2 s .c o m*/ File dir = new File(direccion); if (dir.exists()) { System.out.println("Ya exitiste la carpeta"); } else { dir.mkdirs(); } DateFormat dateF = new SimpleDateFormat("kk-mm-ss_dd-MM-yyyy"); String fileName = direccion + "/" + dateF.format(fecha) + ".pdf"; String imagem = "bill-512.png"; System.out.println("Se creo su factura"); PDDocument doc = new PDDocument(); PDPage page = new PDPage(PDRectangle.A4); PDPageContentStream content = new PDPageContentStream(doc, page); doc.addPage(page); PDImageXObject pdImage = null; try { pdImage = PDImageXObject.createFromFile(imagem, doc); } catch (IOException e1) { System.out.println("ERROR no cargo logo factura"); } float scale = 0.2f; // Titulo factura content.beginText(); content.setFont(PDType1Font.HELVETICA, 26); content.setNonStrokingColor(Color.BLUE); content.newLineAtOffset(250, 785); content.showText("Factura Bazar A&J"); content.endText(); // Logo content.drawImage(pdImage, 30, 725, pdImage.getWidth() * scale, pdImage.getHeight() * scale); //Cuadro info empresa content.setNonStrokingColor(Color.BLACK); content.addRect(25, 700, 280, 1); content.setNonStrokingColor(Color.BLACK); content.addRect(304, 700, 1, -65); content.setNonStrokingColor(Color.BLACK); content.addRect(25, 634, 280, 1); content.setNonStrokingColor(Color.BLACK); content.addRect(25, 700, 1, -65); content.fill(); // Texto info empresa content.beginText(); content.setLeading(15); // da el salto de pagina content.setFont(PDType1Font.COURIER, 10); content.newLineAtOffset(30, 685); content.showText("Direccion: " + "Coop. Universitaria Mz 258 Solar 9"); content.newLine(); content.showText("Ciudad : " + "Guayaquil, Ecuador"); content.newLine(); content.showText("Telefono : " + "042937914"); content.newLine(); content.showText("RUC : " + "1710034065"); content.endText(); //Cuadro datos factura content.setNonStrokingColor(Color.BLACK); //arriba content.addRect(380, 700, 200, 1); content.setNonStrokingColor(Color.BLACK); // derecha content.addRect(579, 700, 1, -65); content.setNonStrokingColor(Color.BLACK); // abajo content.addRect(380, 634, 200, 1); content.setNonStrokingColor(Color.BLACK); // izquierda content.addRect(380, 700, 1, -65); content.fill(); DateFormat dateFormat = new SimpleDateFormat("dd - MM - yyyy"); DateFormat dateFormath = new SimpleDateFormat("h:mm a"); // Texto datos factura content.beginText(); content.setLeading(15); // da el salto de pagina content.setFont(PDType1Font.COURIER, 10); content.newLineAtOffset(390, 685); content.showText("Factura N: " + nFactura); content.newLine(); content.showText("Fecha : " + dateFormat.format(fecha)); content.newLine(); content.showText("Hora : " + dateFormath.format(fecha)); content.newLine(); content.showText("Empleado : " + empleado); content.endText(); // Linea Divisoria content.setNonStrokingColor(Color.BLUE); // abajo content.addRect(0, 620, 595, 1); content.fill(); // Datos Cliente content.beginText(); content.setLeading(18); // da el salto de pagina content.setNonStrokingColor(Color.BLACK); content.setFont(PDType1Font.COURIER, 13); content.newLineAtOffset(30, 600); content.showText("Nombre : " + cliente.getNombre_C() + cliente.getApellido_C()); content.newLine(); content.showText("Direcion : " + cliente.getDireccion_C()); content.newLine(); content.showText("Telefono : " + cliente.getConvencional_C()); content.endText(); content.setNonStrokingColor(Color.BLUE); // abajo content.addRect(0, 550, 595, 1); content.fill(); content.setNonStrokingColor(Color.BLACK); // numero content.addRect(30, 510, 50, 15); content.fill(); content.beginText(); content.setLeading(0); content.setNonStrokingColor(Color.WHITE); content.setFont(PDType1Font.COURIER_BOLD, 13); content.newLineAtOffset(45, 515); content.showText("N."); content.endText(); content.setNonStrokingColor(Color.BLACK); // cantidad content.addRect(90, 510, 40, 15); content.fill(); content.beginText(); content.setLeading(18); // da el salto de pagina content.setNonStrokingColor(Color.WHITE); content.setFont(PDType1Font.COURIER_BOLD, 13); content.newLineAtOffset(94, 515); content.showText("Cant"); content.endText(); content.setNonStrokingColor(Color.BLACK); // Descripcion content.addRect(140, 510, 270, 15); content.fill(); content.beginText(); content.setLeading(18); // da el salto de pagina content.setNonStrokingColor(Color.WHITE); content.setFont(PDType1Font.COURIER_BOLD, 13); content.newLineAtOffset(230, 515); content.showText("Descripcion"); content.endText(); content.setNonStrokingColor(Color.BLACK); // cantidad content.addRect(430, 510, 70, 15); content.fill(); content.beginText(); content.setLeading(18); // da el salto de pagina content.setNonStrokingColor(Color.WHITE); content.setFont(PDType1Font.COURIER_BOLD, 13); content.newLineAtOffset(440, 515); content.showText("P.Unit"); content.endText(); content.setNonStrokingColor(Color.BLACK); // importe content.addRect(510, 510, 70, 15); content.fill(); content.beginText(); content.setLeading(18); // da el salto de pagina content.setNonStrokingColor(Color.WHITE); content.setFont(PDType1Font.COURIER_BOLD, 13); content.newLineAtOffset(515, 515); content.showText("Importe"); content.endText(); int cont = 1; int vertical = 490; // Productos for (ProductosCanasta p : productos) { content.beginText(); content.setNonStrokingColor(Color.BLACK); content.setFont(PDType1Font.COURIER, 13); content.newLineAtOffset(45, vertical); content.showText("" + cont); content.endText(); content.beginText(); content.setNonStrokingColor(Color.BLACK); content.setFont(PDType1Font.COURIER, 13); content.newLineAtOffset(105, vertical); content.showText("" + p.getCantidad()); content.endText(); content.beginText(); content.setNonStrokingColor(Color.BLACK); content.setFont(PDType1Font.COURIER, 11); content.newLineAtOffset(145, vertical); content.showText("" + p.getNombre()); content.endText(); content.beginText(); content.setNonStrokingColor(Color.BLACK); content.setFont(PDType1Font.COURIER, 13); content.newLineAtOffset(440, vertical); content.showText("" + p.getPrecio_venta()); content.endText(); content.beginText(); content.setNonStrokingColor(Color.BLACK); content.setFont(PDType1Font.COURIER, 13); content.newLineAtOffset(520, vertical); content.showText("" + p.getPrecio_venta().multiply(new BigDecimal(p.getCantidad()))); content.endText(); cont++; vertical -= 20; } // max x pagina 595 x 841.8898 content.setNonStrokingColor(Color.BLUE); // total content.addRect(0, 60, 595, 1); content.fill(); /// TOTAL content.beginText(); content.setNonStrokingColor(Color.BLACK); content.setFont(PDType1Font.COURIER_BOLD, 20); content.newLineAtOffset(500, 35); content.showText(subtotal.multiply(new BigDecimal(0.14)).add(subtotal).setScale(2, 3).toString()); content.endText(); content.beginText(); content.newLineAtOffset(510, 20); content.setFont(PDType1Font.COURIER_BOLD, 15); content.showText("TOTAL"); content.endText(); // IVA content.beginText(); content.setNonStrokingColor(Color.BLACK); content.setFont(PDType1Font.COURIER_BOLD, 20); content.newLineAtOffset(430, 35); content.showText("" + 14 + "%"); content.endText(); content.beginText(); content.newLineAtOffset(430, 20); content.setFont(PDType1Font.COURIER_BOLD, 15); content.showText("IVA"); content.endText(); // Subtotal content.beginText(); content.setNonStrokingColor(Color.BLACK); content.setFont(PDType1Font.COURIER_BOLD, 20); content.newLineAtOffset(300, 35); content.showText(subtotal.toString()); content.endText(); content.beginText(); content.newLineAtOffset(300, 20); content.setFont(PDType1Font.COURIER_BOLD, 15); content.showText("SUBTOTAL"); content.endText(); content.close(); doc.save(fileName); doc.close(); Process p = Runtime.getRuntime().exec(new String[] { "xpdf", fileName }); System.out.println("your file created in : " + System.getProperty("user.dir")); }
From source file:vortext.TextHighlight.java
License:Apache License
public static void main(final String args[]) throws Exception { if (args.length != 3) { usage();/*from w w w. j av a 2 s . com*/ } PDDocument pdDoc = null; final File file = new File(args[0]); if (!file.isFile()) { System.err.println("File " + args[0] + " does not exist."); return; } final PDFParser parser = new PDFParser(new FileInputStream(file)); parser.parse(); pdDoc = new PDDocument(parser.getDocument()); final TextHighlight pdfHighlight = new TextHighlight("UTF-8"); // depends on what you want to match, but this creates a long string // without newlines pdfHighlight.setSkipAllWhitespace(true); pdfHighlight.setNormalizeText(true); pdfHighlight.initialize(pdDoc); List<PDAnnotationTextMarkup> highlightDefault = pdfHighlight.highlightDefault(args[2]); pdDoc.save(args[1]); try { if (parser.getDocument() != null) { parser.getDocument().close(); } if (pdDoc != null) { pdDoc.close(); } } catch (final Exception e) { e.printStackTrace(); } }