Example usage for org.apache.pdfbox.pdmodel PDDocument save

List of usage examples for org.apache.pdfbox.pdmodel PDDocument save

Introduction

In this page you can find the example usage for org.apache.pdfbox.pdmodel PDDocument save.

Prototype

public void save(OutputStream output) throws IOException 

Source Link

Document

This will save the document to an output stream.

Usage

From source file:patient_records_mng_sys.send.java

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed

    String email1 = email.getText();

    String EMAIL_REGEX = "^[\\w-_\\.+]*[\\w-_\\.]\\@([\\w]+\\.)+[\\w]+[\\w]$";

    Boolean b = email1.matches(EMAIL_REGEX);

    if (b == true) {

        System.out.println("Correct Email");
        // String bloody=(String)search_key.getSelectedItem();
        //this.test();
        //Create the PDF Document//

        try {/* w  w  w  . j a  va  2 s  .c  om*/
            String key = (String) search_key.getSelectedItem();
            System.out.println("Creating PDF File");
            String fileName = "patientReport.pdf"; // name of our file

            PDDocument doc = new PDDocument();
            PDPage page = new PDPage();

            doc.addPage(page);

            PDPageContentStream content = new PDPageContentStream(doc, page);

            content.beginText();
            content.setFont(PDType1Font.HELVETICA, 20);
            content.moveTextPositionByAmount(220, 750);
            content.drawString("Patient Report Form");
            content.endText();

            int x = 80;
            int y = 700;
            String spac = "        ";
            String documen = "PatientID" + spac + "PatientName" + spac + "Clinic" + spac + "Diagnose";

            content.beginText();
            content.setFont(PDType1Font.HELVETICA, 16);
            content.moveTextPositionByAmount(x, 710);
            content.drawString(documen);
            content.endText();

            //To add from database//
            // Connection conn=null;
            try {
                Class.forName("com.mysql.jdbc.Driver");
                ///conn = DriverManager.getConnection("jdbc:mysql://localhost/itp?"+"user=root&password=sparksndl");
                //conn = DriverManager.getConnection("jdbc:mysql://localhost/itp?"+"user="+user+"&password="+pass+"");
                Statement sqlState = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
                        ResultSet.CONCUR_UPDATABLE);
                String selectStuff = "Select `ID`,`patient_id`,`clinicName`,`diagnosis`,`dateOfEntry`,`allergiesIdentified`,`Remarks`,`nextClinicDate`,`ward` FROM `medical_records` WHERE `patient_id` LIKE "
                        + key + " ";
                rows = sqlState.executeQuery(selectStuff);
                Object[] tempRow;
                String[] records;
                if (rows.next()) {
                    while (rows.next()) {
                        String id = rows.getString(1);
                        String name = "Classified";
                        String clinic = rows.getString(3);
                        String diagnose = rows.getString(4);
                        String aller = rows.getString(6);
                        String remark = rows.getString(7);
                        String space = "                 ";
                        String document = id + space + space + space + name + space + space + clinic + space
                                + diagnose;
                        content.beginText();
                        content.setFont(PDType1Font.HELVETICA, 8);
                        content.moveTextPositionByAmount(x, y);
                        content.drawString(document);

                        content.endText();
                        y = y - 12;
                    }
                } else {
                    JOptionPane.showMessageDialog(null,
                            "ID entry not available,Please register the patient or try again");
                }

            } catch (ClassNotFoundException | SQLException e) {
                System.out.println("Provide Correct Index");
                //e.printStackTrace();
            }

            //Document Creation Over//

            content.close();
            doc.save(fileName);
            doc.close();

            System.out.println("your report has been created in : " + System.getProperty("user.dir"));

        } catch (IOException | COSVisitorException e) {

            System.out.println(e.getMessage());

        }
        // String test="jsandunil@gmail.com";
        emailer mailme = new emailer();
        mailme.sendFromGMail(email1);

        //mailme.
        //Document Over//

    } else

        JOptionPane.showMessageDialog(null, "Incorrect Email Provided.", "Email Address Format",
                JOptionPane.ERROR_MESSAGE);

    // TODO add your handling code here:
}

From source file:pdf.DailyReportPDF.java

public String createDailyReport() throws IOException {
    PDDocument document = PDDocument.load(template);
    //prva strana
    PDPage page = document.getPage(0);//from  ww w  . j  a v a2  s  .c  o m
    contentStream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.PREPEND, false);

    this.insertProducts();
    this.dailyReportInfo();
    contentStream.close();
    SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy.");
    String path = "pdf_docs\\daily_reports\\" + "daily_report_" + dateOfDailyReport.getTime() + ".pdf";
    document.save(path);
    //createDailyReportPNG(path);
    document.close();

    return path;
}

From source file:pdf.NormativPDF.java

public String createNormativ() throws IOException {
    PDDocument document = PDDocument.load(template);
    //prva strana
    PDPage page = document.getPage(0);//from   w ww .j a v  a2  s .  c o m
    contentStream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.PREPEND, false);

    this.insertSupplies();
    this.insertProduct();
    this.normativInfo();
    contentStream.close();
    SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy.");
    String path = "pdf_docs\\norms\\" + "norm" + "_" + dateNormativ.getTime() + ".pdf";
    document.save(path);
    //createNormativPNG(path);
    document.close();
    return path;

}

From source file:PDF.PDFDateStamp.java

public void generatePDFFile(String pdfFileName) throws IOException, COSVisitorException {

    PDDocument pdf = PDDocument.load(pdfFileName);

    List pages = pdf.getDocumentCatalog().getAllPages();
    Iterator<PDPage> iter = pages.iterator();

    while (iter.hasNext()) {
        PDPage page = iter.next();//from   w ww  .j a v  a  2  s. c o  m
        PDPageContentStream stream = new PDPageContentStream(pdf, page, true, false);

        // == date stamp
        stream.beginText();
        stream.setFont(PDType1Font.HELVETICA, GlobalVar.DATE_STAMP_FONT_SIZE);
        stream.moveTextPositionByAmount(GlobalVar.DATE_STAMP_X_POSITION, GlobalVar.DATE_STAMP_Y_POSITION);
        stream.drawString(DATE); //date stamp 
        stream.endText();
        // end of date stamp

        stream.close();

    }
    // out put two pdf files: one for audit, the other for reject
    pdfFileName = pdfFileName.replace(".pdf", "_DateStamped.pdf");
    pdf.save(pdfFileName);

    pdf.close();
}

From source file:PDF.PDFDateStampPartial.java

private void generatePDFFile(String pdfFileName, Boolean[][] statusArray, String date)
        throws IOException, COSVisitorException {
    PDDocument pdf = PDDocument.load(pdfFileName);
    List pages = pdf.getDocumentCatalog().getAllPages();
    Iterator<PDPage> iter = pages.iterator();
    int pageNum = 0; // 0 based
    //int sequenceNum = 1; // start from 0001
    PDDocument pdfBlank = new PDDocument();

    while (iter.hasNext()) {
        PDPage page = iter.next();//  w  ww  .  ja v a  2s.c  om
        PDPage pageBlank = new PDPage();

        PDPageContentStream stream = new PDPageContentStream(pdf, page, true, false);
        PDPageContentStream streamBlank = new PDPageContentStream(pdfBlank, pageBlank, true, false);

        // == seq stamp
        if (statusArray[GlobalVar.SELECT_BUTTON_INDEX][pageNum]) {
            pageWrite(stream, date);
            pageWrite(streamBlank, date);
        }
        // == end of seq stamp
        pdfBlank.addPage(pageBlank);
        stream.close();
        streamBlank.close();

        pageNum++;
    }

    // out put two pdf files: one is template for printer print hardcopies, the other is digital copy
    String suffix = "_P dateStamped.pdf";
    pdfFileName = pdfFileName.replace(".pdf", suffix);
    String blankPdfFileName = pdfFileName.replace(".pdf", "BLANK.pdf");

    pdf.save(pdfFileName);
    pdfBlank.save(blankPdfFileName);

    pdf.close();
    pdfBlank.close();
}

From source file:PDF.PDFEditor.java

private void extractGoodBadPdf(PDDocument pdf, String auditPdfFileName, String rejectPdfFileName,
        Boolean[][] statusArray) throws COSVisitorException, IOException {
    PDDocument rejectPdf = new PDDocument();
    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]) {
            rejectPdf.addPage(page);//from w  w  w  .  j a v a  2  s  .c om
        } else {
            auditPdf.addPage(page);
        }
    }

    rejectPdf.save(rejectPdfFileName);
    rejectPdf.close();

    auditPdf.save(auditPdfFileName);
    auditPdf.close();
}

From source file:PDF.PDFNumbering.java

public PDFNumbering(String pdfFileName) throws IOException, COSVisitorException {
    String cycle = JOptionPane.showInputDialog(null, "Please enter a cycle number");
    PDDocument pdf = PDDocument.load(pdfFileName);
    List pages = pdf.getDocumentCatalog().getAllPages();
    PDFTextStripper pdfStripper = new PDFTextStripper();
    String res = pdfStripper.getText(pdf);
    //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(pdf, cycle); // update the sequence number
    } else if (isPreProcessed) { // first time
        int pageNumber = 1;

        while (iter.hasNext()) {
            PDPage page = iter.next();/*from  ww w.j a v a  2  s. co  m*/

            pdfStripper.setStartPage(pageNumber);
            pdfStripper.setEndPage(pageNumber);
            res = pdfStripper.getText(pdf);
            // == numbering
            if (res.contains(GlobalVar.PRE_PROC_KEY_SYMBOL)) {
                PDPageContentStream stream = new PDPageContentStream(pdf, 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 + "/" + GlobalVar.globalCountGenerator5Digit(sequenceNum));
                sequenceNum++;
                stream.endText();
                stream.close();
            }
            pageNumber++;
            // end of numbering
        }

    } else { //not pre processed
        while (iter.hasNext()) {
            PDPage page = iter.next();
            PDPageContentStream stream = new PDPageContentStream(pdf, page, true, false);
            // == numbering
            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 + "/" + GlobalVar.globalCountGenerator5Digit(sequenceNum));
            sequenceNum++;
            stream.endText();
            stream.close();
        }
    }

    // out put two pdf files: one for audit, the other for reject
    String suffix = "_" + cycle + " Numbered.pdf";
    pdfFileName = pdfFileName.replace(".pdf", suffix);
    pdf.save(pdfFileName);
    pdf.close();
}

From source file:PDF.PDFNumberingPartial.java

private void generatePDFFile(String pdfFileName, Boolean[][] statusArray, String cycle)
        throws IOException, COSVisitorException {
    PDDocument pdf = PDDocument.load(pdfFileName);
    List pages = pdf.getDocumentCatalog().getAllPages();
    Iterator<PDPage> iter = pages.iterator();

    PDDocument pdfBlank = new PDDocument();

    int pageNum = 0; // 0 based
    int sequenceNum = 1; // start from 0001
    while (iter.hasNext()) {
        PDPage page = iter.next();/*from www. j a  v  a  2s.  co m*/
        PDPage pageBlank = new PDPage();

        PDPageContentStream stream = new PDPageContentStream(pdf, page, true, false);
        PDPageContentStream streamBlank = new PDPageContentStream(pdfBlank, pageBlank, true, false);

        if (statusArray[GlobalVar.SELECT_BUTTON_INDEX][pageNum]) {
            pageWrite(stream, sequenceNum, cycle);
            pageWrite(streamBlank, sequenceNum, cycle);
            sequenceNum++;
        }
        pdfBlank.addPage(pageBlank);

        stream.close();
        streamBlank.close();
        pageNum++;
    }

    // out put two pdf files: one is template for printer print hardcopies, the other is digital copy
    String suffix = "_" + cycle + "_P numbered.pdf";
    pdfFileName = pdfFileName.replace(".pdf", suffix);
    String blankPdfFileName = pdfFileName.replace(".pdf", "BLANK.pdf");

    pdf.save(pdfFileName);
    pdfBlank.save(blankPdfFileName);

    pdf.close();
    pdfBlank.close();
}

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);/* w w  w  .j av  a2  s  .  c  o  m*/
        }
    }

    auditPdf.save(auditPdfFileName);
    auditPdf.close();
}

From source file:pdf.PDFWindow.java

private void saveFile(PDDocument saving) throws IOException {
    String filename = "";
    String dir = "";
    JFileChooser c = new JFileChooser();
    FileNameExtensionFilter filter = new FileNameExtensionFilter("PDF Documents", "pdf");
    c.setFileFilter(filter);/*w w w .j ava 2s  . c o  m*/
    // Demonstrate "Save" dialog:
    int rVal = c.showSaveDialog(null);
    if (rVal == JFileChooser.APPROVE_OPTION) {
        filename = c.getSelectedFile().getName();
        dir = c.getCurrentDirectory().toString();
        saving.save(dir + "/" + filename + ".pdf");
    }
}