Example usage for com.itextpdf.text Document close

List of usage examples for com.itextpdf.text Document close

Introduction

In this page you can find the example usage for com.itextpdf.text Document close.

Prototype

boolean close

To view the source code for com.itextpdf.text Document close.

Click Source Link

Document

Has the document already been closed?

Usage

From source file:com.mycompany.mavenproject2.ItemHistoryController.java

@FXML
public void OnPrintButtonAction(ActionEvent a) {
    Document document = new Document();
    //JavaPdfHelloWorld jp=new JavaPdfHelloWorld();

    try {// w w  w.  ja  va 2 s. c  o  m
        PdfWriter.getInstance(document, new FileOutputStream("javaHello.pdf"));

        document.open();
        document.add(new Paragraph("A Hello World PDF document."));

        document.close();

        final File file = new File(
                "C:/Users/Third Ev/Documents/NetBeansProjects/FinalDemo/src/finaldemo/Controller/javaHello.pdf");

        f4.getHostServices().showDocument(file.toURI().toString());

        // no need to close PDFwriter?
    } catch (DocumentException e) {
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

}

From source file:com.mycompany.mavenproject2.JavaPdfHelloWorld.java

public static void main(String[] args) {
    Document document = new Document();
    try {//from  w ww. j a  va  2 s. c  o m
        PdfWriter.getInstance(document, new FileOutputStream("HelloWorld.pdf"));

        document.open();
        document.add(new Paragraph("A Hello World PDF document."));
        document.close(); // no need to close PDFwriter?

    } catch (DocumentException e) {
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}

From source file:com.mycompany.mavenproject2.VirtualkeyController.java

@FXML
public void handlePDFButtonAction(ActionEvent a) throws IOException, DocumentException {

    Document document = new Document();
    FileOutputStream f2 = new FileOutputStream(FILE1);
    PdfWriter.getInstance(document, f2);
    document.open();/*from  ww w. jav  a 2 s. c  o  m*/
    addMetaData(document);
    addTitlePage(document);
    //      addContent(document);
    // document.close();
    /* PdfReader reader = new PdfReader("C:/Users/Third Ev/Downloads/my.pdf");
    System.out.println("This PDF has "+reader.getNumberOfPages()+" pages.");
    String page = PdfTextExtractor.getTextFromPage(reader, 2);
    System.out.println("Page Content:\n\n"+page+"\n\n");
    System.out.println("Is this document tampered: "+reader.isTampered());
    System.out.println("Is this document encrypted: "+reader.isEncrypted());*/
    /* PdfDecoder pdf = new PdfDecoder();
     pdf.openPdfFile("C:/Users/Third Ev/Downloads/my.pdf");*/
    /*  hostServices = getHostServices();
        hostServices.showDocument("C:/Users/Third Ev/Documents/NetBeansProjects/FinalDemo/src/finaldemo/Controller/iText.pdf");*/
    document.close();
    f2.close();
    final File file = new File("/home/drashti/MyDemo.pdf");

    f4.getHostServices().showDocument(file.toURI().toString());

}

From source file:com.mycompany.peram_inclassexam.WritePDFFile.java

public void createPDF(AccountDetails account)
        throws DocumentException, FileNotFoundException, BadElementException, IOException {

    Document document = new Document(PageSize.A4, 50, 50, 50, 50);

    PdfWriter write = PdfWriter.getInstance(document,
            new FileOutputStream(account.getLastName() + "_output.pdf"));

    document.open();//w  w w . j  av a2 s  .  c o m

    Image image = Image.getInstance("image.png");

    image.scaleToFit(450f, 1800f);

    document.add(image);
    document.add(new Paragraph(
            "--------------------------------------------------------------------------------------------------------------------------"));

    Paragraph welcomeParagraph = new Paragraph(
            "Welcome! " + account.getFirstName() + " " + account.getLastName() + "!");
    welcomeParagraph.setSpacingBefore(50);

    document.add(welcomeParagraph);

    String firstName = "First Name: " + account.getFirstName();
    String lastName = "Last Name: " + account.getLastName();
    String acctNumber = "Account Number: " + account.getAccountNo();
    String acctBalance = String.format("Account Balance: $%.1f", account.getAccountBalance());

    Paragraph detailsPara = new Paragraph("Below are your Account Details:\n" + firstName + "\n" + lastName
            + "\n" + acctNumber + "\n" + acctBalance);
    detailsPara.setIndentationLeft(30);
    detailsPara.setSpacingBefore(20);
    document.add(detailsPara);

    document.close();
    write.close();
}

From source file:com.nerdcastle.nazmul.filetest.MainActivity.java

private void createPdf() {
    try {//  w w  w .  j av  a2 s .  c  o  m
        Document document = new Document();
        String root = Environment.getExternalStorageDirectory().toString();
        File myDir = new File(root + "/created_pdf");
        myDir.mkdirs();
        String fname = "FirstPdf.pdf";
        File file = new File(myDir, fname);
        //FileOutputStream out = new FileOutputStream(file);
        PdfWriter.getInstance(document, new FileOutputStream(file));
        document.open();
        addMetaData(document);
        addTitlePage(document);
        addContent(document);
        document.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.netsteadfast.greenstep.bsc.command.KpiReportPdfCommand.java

License:Apache License

private String createPdf(Context context) throws Exception {
    BscReportPropertyUtils.loadData();//w  w w  .j  a v a 2  s .com
    BscReportSupportUtils.loadExpression(); // 2015-04-18 add
    String visionOid = (String) context.get("visionOid");
    VisionVO vision = null;
    BscStructTreeObj treeObj = (BscStructTreeObj) this.getResult(context);
    for (VisionVO visionObj : treeObj.getVisions()) {
        if (visionObj.getOid().equals(visionOid)) {
            vision = visionObj;
        }
    }
    FontFactory.register(BscConstants.PDF_ITEXT_FONT);
    String fileName = UUID.randomUUID().toString() + ".pdf";
    String fileFullPath = Constants.getWorkTmpDir() + "/" + fileName;
    OutputStream os = new FileOutputStream(fileFullPath);
    //Document document = new Document(PageSize.A4.rotate(), 10, 10, 10, 10);
    Document document = new Document(PageSize.A4, 10, 10, 10, 10);
    document.left(100f);
    document.top(150f);
    PdfWriter writer = PdfWriter.getInstance(document, os);
    document.open();

    int dateRangeRows = 4 + vision.getPerspectives().get(0).getObjectives().get(0).getKpis().get(0)
            .getDateRangeScores().size();
    PdfPTable table = new PdfPTable(MAX_COLSPAN);
    PdfPTable dateRangeTable = new PdfPTable(dateRangeRows);
    PdfPTable chartsTable = new PdfPTable(2);
    PdfPTable signTable = new PdfPTable(1);
    table.setWidthPercentage(100f);
    dateRangeTable.setWidthPercentage(100f);
    chartsTable.setWidthPercentage(100f);
    signTable.setWidthPercentage(100f);

    this.createHead(table, vision);
    this.createBody(table, vision);
    this.createDateRange(dateRangeTable, vision, context, dateRangeRows);
    this.putCharts(chartsTable, context);
    this.putSignature(signTable, context);

    document.add(chartsTable);
    document.add(table);
    document.add(dateRangeTable);
    document.add(signTable);
    document.close();
    writer.close();

    os.flush();
    os.close();
    os = null;

    File file = new File(fileFullPath);
    String oid = UploadSupportUtils.create(Constants.getSystem(), UploadTypes.IS_TEMP, false, file,
            "kpi-report.pdf");
    file = null;
    return oid;
}

From source file:com.netsteadfast.greenstep.bsc.command.OrganizationReportPdfCommand.java

License:Apache License

private String createPdf(Context context) throws Exception {
    BscReportPropertyUtils.loadData();/*from   w  w w. j a v a 2 s . c o m*/
    String visionOid = (String) context.get("visionOid");
    VisionVO vision = null;
    BscStructTreeObj treeObj = (BscStructTreeObj) this.getResult(context);
    for (VisionVO visionObj : treeObj.getVisions()) {
        if (visionObj.getOid().equals(visionOid)) {
            vision = visionObj;
        }
    }
    FontFactory.register(BscConstants.PDF_ITEXT_FONT);
    String fileName = UUID.randomUUID().toString() + ".pdf";
    String fileFullPath = Constants.getWorkTmpDir() + "/" + fileName;
    OutputStream os = new FileOutputStream(fileFullPath);
    Document document = new Document(PageSize.A4.rotate(), 10, 10, 10, 10);
    document.left(100f);
    document.top(150f);
    PdfWriter writer = PdfWriter.getInstance(document, os);
    document.open();

    PdfPTable table = new PdfPTable(MAX_COLSPAN);
    table.setWidthPercentage(100f);
    PdfPTable signTable = new PdfPTable(1);
    signTable.setWidthPercentage(100f);

    this.createHead(table, vision, context);
    this.createBody(table, vision);

    this.putSignature(signTable, context);

    document.add(table);
    document.add(signTable);
    document.close();
    writer.close();

    os.flush();
    os.close();
    os = null;

    File file = new File(fileFullPath);
    String oid = UploadSupportUtils.create(Constants.getSystem(), UploadTypes.IS_TEMP, false, file,
            "department-report.pdf");
    file = null;
    return oid;
}

From source file:com.netsteadfast.greenstep.bsc.command.PersonalReportPdfCommand.java

License:Apache License

private String createPdf(Context context) throws Exception {
    BscReportPropertyUtils.loadData();//from www .ja  va2  s .  c o  m
    String visionOid = (String) context.get("visionOid");
    VisionVO vision = null;
    BscStructTreeObj treeObj = (BscStructTreeObj) this.getResult(context);
    for (VisionVO visionObj : treeObj.getVisions()) {
        if (visionObj.getOid().equals(visionOid)) {
            vision = visionObj;
        }
    }
    FontFactory.register(BscConstants.PDF_ITEXT_FONT);
    String fileName = UUID.randomUUID().toString() + ".pdf";
    String fileFullPath = Constants.getWorkTmpDir() + "/" + fileName;
    OutputStream os = new FileOutputStream(fileFullPath);
    Document document = new Document(PageSize.A4.rotate(), 10, 10, 10, 10);
    document.left(100f);
    document.top(150f);
    PdfWriter writer = PdfWriter.getInstance(document, os);
    document.open();

    PdfPTable table = new PdfPTable(MAX_COLSPAN);
    table.setWidthPercentage(100f);
    PdfPTable signTable = new PdfPTable(1);
    signTable.setWidthPercentage(100f);

    this.createHead(table, vision, context);
    this.createBody(table, vision);
    this.createFoot(table, context);
    this.putSignature(signTable, context);

    document.add(table);
    document.add(signTable);
    document.close();
    writer.close();

    os.flush();
    os.close();
    os = null;

    File file = new File(fileFullPath);
    String oid = UploadSupportUtils.create(Constants.getSystem(), UploadTypes.IS_TEMP, false, file,
            "personal-report.pdf");
    file = null;
    return oid;
}

From source file:com.norbsoft.pdfconverter.helpers.PDFHelper.java

License:Open Source License

public int generate(String saveUrl, SerializableBitmap sign, Form form, ArrayList<String> photos,
        ArrayList<String> workers) {
    try {/*from w  w w . j  a v  a 2  s  .c o m*/
        Document document = new Document();

        url = saveUrl;
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(url));
        document.setMargins(20, 20, 20, 20);
        document.open();

        Bitmap inputLogo = BitmapFactory.decodeStream(context.getAssets().open("logo.jpg"));
        ByteArrayOutputStream outstream = new ByteArrayOutputStream();
        inputLogo.compress(Bitmap.CompressFormat.JPEG, 100, outstream);
        Image logo = Image.getInstance(outstream.toByteArray());

        Bitmap inputQR = BitmapFactory.decodeStream(context.getAssets().open("qr.jpg"));
        outstream = new ByteArrayOutputStream();
        inputQR.compress(Bitmap.CompressFormat.JPEG, 100, outstream);
        Image qr = Image.getInstance(outstream.toByteArray());

        outstream = new ByteArrayOutputStream();
        sign.getImage().compress(Bitmap.CompressFormat.JPEG, 100, outstream);
        Image sgn = Image.getInstance(outstream.toByteArray());

        qr.scaleAbsolute(90f, 90f);
        qr.setAbsolutePosition(340f, 705f);

        logo.scaleAbsolute(150f, 50f);
        logo.setAbsolutePosition(50f, 750f);

        sgn.scaleAbsolute(130f, 30f);
        sgn.setAbsolutePosition(25f, 75f);

        Paragraph p = new Paragraph("\r\n\r\n\r\nMPWiK S.A.\r\n50-421 Wrocaw\r\nul.Na Grobli 14-16", normal);
        p.setIndentationLeft(420f);
        document.add(p);

        p = new Paragraph("RAPORT WYMIANY WODOMIERZA", bold);
        p.setAlignment(Element.ALIGN_CENTER);
        p.setSpacingBefore(60f);
        document.add(p);

        document.add(table(form));

        p = new Paragraph(form.getInformations(), normal);
        p.setAlignment(Element.ALIGN_LEFT);
        p.setIndentationLeft(50f);
        p.setSpacingBefore(20f);
        document.add(p);

        document.add(signTable(form, sgn, workers));

        document.add(logo);
        document.add(qr);

        if (photos != null && photos.size() > 0) {
            document.newPage();
            for (int i = 0; i < photos.size(); i++) {
                try {
                    if (photos.get(i) != null && !photos.get(i).equals("")) {
                        Log.d(TAG, photos.get(i));
                        Bitmap temp = PictureHelper.bitmapFromUrl(photos.get(i));
                        outstream = new ByteArrayOutputStream();
                        temp.compress(Bitmap.CompressFormat.JPEG, 100, outstream);
                        Image photo = Image.getInstance(outstream.toByteArray());
                        photo.scaleToFit(document.getPageSize().getWidth() - 50,
                                document.getPageSize().getHeight());
                        document.add(photo);
                    }
                } catch (Exception e) {

                }
            }
        }
        document.close();
        writer.close();
        return 1;
    } catch (FileNotFoundException e) {
        Log.e(TAG, e.getMessage());
        return 0;
    } catch (DocumentException e) {
        Log.e(TAG, e.getMessage());
        return 0;
    } catch (IOException e) {
        Log.e(TAG, e.getMessage());
        return 0;
    } catch (Exception e) {
        Log.e(TAG, e.getMessage());
        return 0;
    }
}

From source file:com.northcoders.controller.BookingReportsController.java

@FXML
private void generatePdf() throws Exception {
    BaseFont bf;/*from   w w w  . j  a v  a  2  s .c o m*/
    Font font;
    try {
        conditions = "Filtered by: Start Date:2017-03-15 End Date:2017-03-28";

        /* Step-2: Initialize PDF documents - logical objects */
        Document my_pdf_report = new Document(PageSize.LETTER.rotate());
        PdfWriter.getInstance(my_pdf_report, new FileOutputStream("pdf_booking_report.pdf"));
        my_pdf_report.open();
        //we have four columns in our table
        PdfPTable my_report_table = new PdfPTable(6);
        //create a cell object
        PdfPCell table_cell;

        bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.EMBEDDED);
        font = new Font(bf, 16);
        my_pdf_report.add(new Paragraph("Administration Report", font));
        font = new Font(bf, 12);
        my_pdf_report.add(new Paragraph("Filter applied:" + conditions, font));
        my_pdf_report.add(new Paragraph(" "));

        table_cell = new PdfPCell(new Phrase("Customer"));
        my_report_table.addCell(table_cell);
        table_cell = new PdfPCell(new Phrase("Start Date"));
        my_report_table.addCell(table_cell);
        table_cell = new PdfPCell(new Phrase("End Date"));
        my_report_table.addCell(table_cell);
        table_cell = new PdfPCell(new Phrase("Room"));
        my_report_table.addCell(table_cell);
        table_cell = new PdfPCell(new Phrase("Amount"));
        my_report_table.addCell(table_cell);
        table_cell = new PdfPCell(new Phrase("Transactions"));
        my_report_table.addCell(table_cell);

        for (Booking item : bookings) {
            String customer_b = item.getCustomerId().getId() + " " + item.getCustomerId().getFirstName() + " "
                    + item.getCustomerId().getLastName();
            table_cell = new PdfPCell(new Phrase(customer_b));
            my_report_table.addCell(table_cell);

            String date_start_b = dateToStr(item.getStartDate());
            table_cell = new PdfPCell(new Phrase(date_start_b));
            my_report_table.addCell(table_cell);

            String date_end_b = dateToStr(item.getEndDate());
            table_cell = new PdfPCell(new Phrase(date_end_b));
            my_report_table.addCell(table_cell);

            String room_b = String.valueOf(item.getRoomId().getRoomNumber());
            table_cell = new PdfPCell(new Phrase(room_b));
            my_report_table.addCell(table_cell);

            String price_b = String.format("%1$,.2f", item.getTotalPrice());
            table_cell = new PdfPCell(new Phrase(price_b));
            my_report_table.addCell(table_cell);

            String transaction_b = "";
            for (PaymentTransaction trans : item.getPaymentTransactionList()) {
                transaction_b += String.format("%1$,.2f", trans.getAmount()) + " "
                        + trans.getPaymentTypeId().getDescription() + "\n";
            }
            table_cell = new PdfPCell(new Phrase(transaction_b));
            my_report_table.addCell(table_cell);
        }

        /* Attach report table to PDF */
        my_pdf_report.add(my_report_table);
        my_pdf_report.add(new Paragraph(" "));
        font = new Font(bf, 6);
        my_pdf_report.add(new Paragraph("Report Time:" + dateTimeToStr(new Date())));
        my_pdf_report.close();

    } catch (DocumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}