Example usage for com.itextpdf.text.pdf PdfWriter close

List of usage examples for com.itextpdf.text.pdf PdfWriter close

Introduction

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

Prototype

@Override
public void close() 

Source Link

Document

Signals that the Document was closed and that no other Elements will be added.

Usage

From source file:ImagetoPDF.java

public void convertToPDF(String folderName, String fileName, float compressionFactor)
        throws DocumentException, FileNotFoundException, BadElementException, IOException {
    File folder = new File(folderName);
    File[] listOfFiles = folder.listFiles();
    Image img = Image.getInstance(listOfFiles[0].getAbsolutePath());
    float width, height, temp;
    width = img.getWidth();/* w  w  w  .  j  av a2  s.  co m*/
    height = img.getHeight();
    if (height < width) {
        temp = height;
        height = width;
        width = height;
    }
    Rectangle pageSize = new Rectangle(width, height);
    Document document = new Document(pageSize, 0, 0, 0, 0);

    for (int i = 0; i < listOfFiles.length; i++) {
        enhance(listOfFiles[i].getAbsolutePath());
    }
    float scalar;
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(fileName));
    writer.open();
    writer.setCompressionLevel(5);
    document.open();

    for (int i = 0; i < listOfFiles.length; i++) {
        img = Image.getInstance(listOfFiles[i].getAbsolutePath());
        if (img.getWidth() > img.getHeight()) {
            img.setRotationDegrees(270f);
        }
        scalar = ((document.getPageSize().getWidth() - document.leftMargin() - document.rightMargin() - 0)
                / img.getWidth()) * 100;
        img.scalePercent(scalar);

        document.add(img);
    }
    document.close();
    writer.close();

}

From source file:NPRImpl.java

@Override
public boolean saveReservationToPdf(ReservationImpl reservationImpl) throws RemoteException {

    try {/*from   w ww. j a  v a 2 s .c o  m*/
        BufferedReader br = new BufferedReader(new FileReader("dir\\defaults.txt"));
        String path = "";
        try {
            StringBuilder sb = new StringBuilder();
            String line = br.readLine();

            while (line != null) {
                sb.append(line);
                sb.append(System.lineSeparator());
                line = br.readLine();
            }
            path = sb.toString().trim();
        } finally {
            br.close();
        }
        // TODO add your handling code here:
        Document doc = new Document();
        FileOutputStream fos = new FileOutputStream(path + "\\reservation\\" + reservationImpl.getlName().trim()
                + ", " + reservationImpl.getfName().trim() + "ReservationForm.pdf");
        PdfWriter pdfWriter = PdfWriter.getInstance(doc, fos);
        PdfReader pdfReader = new PdfReader("ReservationForm.pdf");

        PdfStamper pdfStamper = new PdfStamper(pdfReader, fos);
        for (int i = 1; i <= pdfReader.getNumberOfPages(); i++) {
            PdfContentByte content = pdfStamper.getOverContent(i);
            //Text over the existing page
            BaseFont bf = BaseFont.createFont(BaseFont.TIMES_ROMAN, BaseFont.WINANSI, BaseFont.EMBEDDED);
            content.beginText();
            content.setFontAndSize(bf, 10);
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, reservationImpl.getDatePaid().trim(), 435, 655,
                    0);
            if (reservationImpl.getmName().trim().isEmpty()) {
                content.showTextAligned(PdfContentByte.ALIGN_LEFT,
                        reservationImpl.getlName().trim() + ", " + reservationImpl.getfName(), 265, 625, 0);
            } else {
                content.showTextAligned(
                        PdfContentByte.ALIGN_LEFT, reservationImpl.getlName().trim() + ", "
                                + reservationImpl.getfName() + " " + reservationImpl.getmName().charAt(0) + ".",
                        265, 625, 0);
            }
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, reservationImpl.getAddress().trim(), 265, 600,
                    0);
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, reservationImpl.getZipCode().trim(), 265, 580,
                    0);
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, reservationImpl.getmNumber().trim(), 265, 565,
                    0);
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, reservationImpl.getEmail().trim(), 265, 550, 0);
            content.setFontAndSize(bf, 12);
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, "Room No.:" + reservationImpl.getRoom().trim(),
                    125, 370, 0);
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, reservationImpl.getRoomType().trim(), 325, 370,
                    0);
            //                content.showTextAligned(PdfContentByte.ALIGN_LEFT, reservationImpl.getsTerm().trim(), 125, 310, 0);
            if (reservationImpl.getsTerm() != null) {
                content.showTextAligned(PdfContentByte.ALIGN_LEFT, reservationImpl.getsTerm().trim(), 125, 310,
                        0);
                if (!reservationImpl.getAyTo().trim().isEmpty()
                        & !reservationImpl.getAyFrom().trim().isEmpty()) {
                    content.showTextAligned(PdfContentByte.ALIGN_LEFT,
                            reservationImpl.getAyFrom().trim() + " - " + reservationImpl.getAyTo().trim(), 325,
                            310, 0);
                    content.showTextAligned(PdfContentByte.ALIGN_LEFT, " - ", 435, 310, 0);
                } else {
                    content.showTextAligned(PdfContentByte.ALIGN_LEFT, " - ", 325, 310, 0);
                    content.showTextAligned(PdfContentByte.ALIGN_LEFT, " - ", 435, 310, 0);
                }
            } else {
                if (reservationImpl.getOthers().trim().isEmpty()) {
                    content.showTextAligned(PdfContentByte.ALIGN_LEFT, " - ", 325, 310, 0);
                    content.showTextAligned(PdfContentByte.ALIGN_LEFT, " - ", 435, 310, 0);
                } else {
                    content.showTextAligned(PdfContentByte.ALIGN_LEFT, " - ", 325, 310, 0);
                    content.showTextAligned(PdfContentByte.ALIGN_LEFT, reservationImpl.getOthers().trim(), 435,
                            310, 0);
                }
            }
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, reservationImpl.getDatePaid().trim(), 265, 210,
                    0);
            content.endText();
        }
        pdfStamper.close();
        pdfReader.close();
        fos.close();
        pdfWriter.close();
    } catch (DocumentException | FileNotFoundException ex) {
        //            Logger.getLogger(NPRImpl.class.getName()).log(Level.SEVERE, null, ex);
        label.setText(ex.getMessage());
        return false;
    } catch (IOException ex) {
        //            Logger.getLogger(NPRImpl.class.getName()).log(Level.SEVERE, null, ex);
        label.setText(ex.getMessage());
        return false;
    }
    return true;
}

From source file:NPRImpl.java

@Override
public boolean saveRegistrationToPdf(RegistrationImpl registrationImpl, ResidentImpl residentImpl)
        throws RemoteException {
    try {/* w ww .j a  v  a  2  s.  c  om*/
        BufferedReader br = new BufferedReader(new FileReader("dir\\defaults.txt"));
        String path = "";
        try {
            StringBuilder sb = new StringBuilder();
            String line = br.readLine();

            while (line != null) {
                sb.append(line);
                sb.append(System.lineSeparator());
                line = br.readLine();
            }
            path = sb.toString().trim();
        } finally {
            br.close();
        }
        String gend = "";
        Document doc = new Document();
        FileOutputStream fos = new FileOutputStream(path + "\\registration\\" + residentImpl.getLName().trim()
                + ", " + residentImpl.getFName().trim() + "RegistrationResidentForm.pdf");
        PdfWriter pdfWriter = PdfWriter.getInstance(doc, fos);

        PdfReader pdfReader = new PdfReader("RegistrationResidentForm.pdf");

        PdfStamper pdfStamper = new PdfStamper(pdfReader, fos);

        for (int i = 1; i <= pdfReader.getNumberOfPages(); i++) {
            PdfContentByte content = pdfStamper.getOverContent(i);
            if (residentImpl.getPicture() != null) {
                Image image1 = Image.getInstance(residentImpl.getPicture());
                image1.scaleAbsolute(95, 105);
                image1.setAbsolutePosition(465f, 635f);
                content.addImage(image1);
            }

            //Text over the existing page
            BaseFont bf = BaseFont.createFont(BaseFont.TIMES_ROMAN, BaseFont.WINANSI, BaseFont.EMBEDDED);
            content.beginText();
            content.setFontAndSize(bf, 10);
            //last name
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, residentImpl.getLName().trim(), 175, 610, 0);
            //first name
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, residentImpl.getFName().trim(), 325, 610, 0);
            //middle name
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, residentImpl.getMName().trim(), 470, 610, 0);
            //college
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, registrationImpl.getCollege().trim(), 180, 582,
                    0);
            //Course and year
            content.showTextAligned(PdfContentByte.ALIGN_LEFT,
                    registrationImpl.getCourse().trim() + " - " + registrationImpl.getYear().trim(), 180, 568,
                    0);
            //department
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, registrationImpl.getDepartment().trim(), 400,
                    582, 0);
            //Sex
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, residentImpl.getGender().trim(), 400, 568, 0);
            //Home address
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, registrationImpl.getAddress().trim(), 110, 540,
                    0);
            //resident mobile no1
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, registrationImpl.getMobile_number().trim(), 187,
                    485, 0);
            //resident mobile no2
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, registrationImpl.getMobile_number2().trim(), 187,
                    470, 0);
            //student email
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, registrationImpl.getEmail().trim(), 397, 485, 0);
            //fathers name
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, registrationImpl.getFatherName().trim(), 110,
                    443, 0);
            //fathers area code
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, registrationImpl.getFatherAreaCode().trim(), 300,
                    443, 0);
            //father landline
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, registrationImpl.getFatherPhone().trim(), 355,
                    443, 0);
            //father mobile
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, registrationImpl.getFatherMobile().trim(), 490,
                    443, 0);
            //father email
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, registrationImpl.getFatherEmail().trim(), 110,
                    430, 0);
            //mother name
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, registrationImpl.getMotherName().trim(), 110,
                    402, 0);
            //mother area code
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, registrationImpl.getMotherAreaCode().trim(), 300,
                    402, 0);
            //mother landline
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, registrationImpl.getMotherPhone().trim(), 355,
                    402, 0);
            //mother mobile
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, registrationImpl.getMotherMobile().trim(), 490,
                    402, 0);
            //mother email
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, registrationImpl.getMotherEmail().trim(), 110,
                    387, 0);
            //guardian name
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, registrationImpl.getGuardianName().trim(), 170,
                    335, 0);
            //guardian contact number
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, registrationImpl.getGuardianMobile().trim(), 470,
                    335, 0);
            //guardian address
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, registrationImpl.getGuardianAddress().trim(),
                    125, 322, 0);
            //guardian relation
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, registrationImpl.getGuardianRelation().trim(),
                    170, 307, 0);
            content.setFontAndSize(bf, 12);
            //room details
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, "Room No.: "
                    + registrationImpl.getRoom_number().trim() + " - " + registrationImpl.getRoom_type().trim(),
                    355, 235, 0);
            content.endText();

        }
        pdfStamper.close();
        pdfReader.close();
        fos.close();
        pdfWriter.close();
    } catch (DocumentException | FileNotFoundException ex) {
        //            Logger.getLogger(NPRImpl.class.getName()).log(Level.SEVERE, null, ex);
        label.setText(ex.getMessage());
        return false;
    } catch (IOException ex) {
        //            Logger.getLogger(NPRImpl.class.getName()).log(Level.SEVERE, null, ex);
        label.setText(ex.getMessage());
        return false;
    }
    return true;
}

From source file:NPRImpl.java

@Override
public boolean saveFurnitureToPdf(ArrayList<String> info, ArrayList<FurnitureImpl> furnitureImpl)
        throws RemoteException {
    try {/*from w w w.  j a  v a2 s  .co m*/
        // TODO add your handling code here:
        Document doc = new Document();
        BufferedReader br = new BufferedReader(new FileReader("dir\\defaults.txt"));
        String path = "";
        try {
            StringBuilder sb = new StringBuilder();
            String line = br.readLine();

            while (line != null) {
                sb.append(line);
                sb.append(System.lineSeparator());
                line = br.readLine();
            }
            path = sb.toString().trim();
        } finally {
            br.close();
        }
        FileOutputStream fos = new FileOutputStream(path + "\\furniture\\" + info.get(0) + "InventoryForm.pdf");
        PdfWriter pdfWriter = PdfWriter.getInstance(doc, fos);

        PdfReader pdfReader = new PdfReader("InventoryForm.pdf");
        PdfStamper pdfStamper = new PdfStamper(pdfReader, fos);
        float added = 0;
        float rowSize = 175;
        float columnSize = 560;
        for (int i = 1; i <= pdfReader.getNumberOfPages(); i++) {
            PdfContentByte content = pdfStamper.getOverContent(i);
            //Text over the existing page
            BaseFont bf = BaseFont.createFont(BaseFont.TIMES_ROMAN, BaseFont.WINANSI, BaseFont.EMBEDDED);
            content.beginText();
            content.setFontAndSize(bf, 10);
            //room number
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, info.get(0).trim(), 185, 650, 0);
            //room type
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, info.get(2).trim(), 185, 635, 0);
            //resident
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, info.get(1).trim(), 185, 620, 0);
            for (FurnitureImpl f : furnitureImpl) {
                content.showTextAligned(PdfContentByte.ALIGN_LEFT, f.getItem_name().trim(), rowSize - 60,
                        columnSize + added, 0);
                content.showTextAligned(PdfContentByte.ALIGN_LEFT, f.getControl_number().trim(), rowSize + 30,
                        columnSize + added, 0);
                added -= 15;
            }

            content.endText();
        }
        pdfStamper.close();
        pdfReader.close();
        fos.close();
        pdfWriter.close();
    } catch (FileNotFoundException ex) {
        //            Logger.getLogger(NPRImpl.class.getName()).log(Level.SEVERE, null, ex);
        label.setText(ex.getMessage());
        return false;
    } catch (DocumentException | IOException ex) {
        //            Logger.getLogger(NPRImpl.class.getName()).log(Level.SEVERE, null, ex);
        label.setText(ex.getMessage());
        return false;
    }
    return true;
}

From source file:NPRImpl.java

@Override
public boolean saveGadgetToPdf(ArrayList<String> info, ArrayList<GadgetImpl> gadgetImpl)
        throws RemoteException {
    try {//from  w ww  . java2s .  co  m
        // TODO add your handling code here:
        BufferedReader br = new BufferedReader(new FileReader("dir\\defaults.txt"));
        String path = "";
        try {
            StringBuilder sb = new StringBuilder();
            String line = br.readLine();

            while (line != null) {
                sb.append(line);
                sb.append(System.lineSeparator());
                line = br.readLine();
            }
            path = sb.toString().trim();
        } finally {
            br.close();
        }
        Document doc = new Document();
        FileOutputStream fos = new FileOutputStream(
                path + "\\gadget\\" + info.get(0).trim() + "GadgetForm.pdf");
        PdfWriter pdfWriter = PdfWriter.getInstance(doc, fos);

        PdfReader pdfReader = new PdfReader("GadgetForm.pdf");

        PdfStamper pdfStamper = new PdfStamper(pdfReader, fos);
        float rowSize = 175;
        float columnSize = 495;
        float added = 0;
        for (int i = 1; i <= pdfReader.getNumberOfPages(); i++) {
            PdfContentByte content = pdfStamper.getOverContent(i);
            //Text over the existing page
            BaseFont bf = BaseFont.createFont(BaseFont.TIMES_ROMAN, BaseFont.WINANSI, BaseFont.EMBEDDED);
            content.beginText();
            content.setFontAndSize(bf, 10);
            //room number
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, info.get(1).trim(), 240, 630, 0);
            //room type
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, info.get(2).trim(), 240, 615, 0);
            //resident
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, info.get(0).trim(), 240, 600, 0);
            for (GadgetImpl g : gadgetImpl) {
                content.showTextAligned(PdfContentByte.ALIGN_LEFT, g.getItem_name().trim(), rowSize - 60,
                        columnSize + added, 0);
                content.showTextAligned(PdfContentByte.ALIGN_LEFT, g.getDescription().trim(), rowSize + 65,
                        columnSize + added, 0);
                added -= 15;
            }

            content.endText();
        }
        pdfStamper.close();
        pdfReader.close();
        fos.close();
        pdfWriter.close();
    } catch (DocumentException | FileNotFoundException | IllegalPdfSyntaxException ex) {
        //            Logger.getLogger(NPRImpl.class.getName()).log(Level.SEVERE, null, ex);
        label.setText(ex.getMessage());
        return false;
    } catch (IOException ex) {
        //            Logger.getLogger(NPRImpl.class.getName()).log(Level.SEVERE, null, ex);
        label.setText(ex.getMessage());
        return false;
    }
    return true;
}

From source file:NPRImpl.java

@Override
public boolean saveStatementOfAccountToPDF(ArrayList<String> info) throws RemoteException {
    try {/*from w  ww .j  av  a  2 s  .c  om*/
        BufferedReader br = new BufferedReader(new FileReader("dir\\defaults.txt"));
        String path = "";
        try {
            StringBuilder sb = new StringBuilder();
            String line = br.readLine();

            while (line != null) {
                sb.append(line);
                sb.append(System.lineSeparator());
                line = br.readLine();
            }
            path = sb.toString().trim();
        } finally {
            br.close();
        }

        com.itextpdf.text.Document doc = new com.itextpdf.text.Document();
        FileOutputStream fos = new FileOutputStream(
                path + "\\residentStatementOfAccount\\" + info.get(0) + "statementOfAccounts.pdf");
        PdfWriter pdfWriter = PdfWriter.getInstance(doc, fos);

        PdfReader pdfReader = new PdfReader("statementOfAccounts.pdf");

        PdfStamper pdfStamper = new PdfStamper(pdfReader, fos);

        java.util.Date date = new SimpleDateFormat("yyyy-MM-dd").parse(info.get(6));

        for (int i = 1; i <= pdfReader.getNumberOfPages(); i++) {
            PdfContentByte content = pdfStamper.getOverContent(i);
            BaseFont bf = BaseFont.createFont(BaseFont.TIMES_ROMAN, BaseFont.WINANSI, BaseFont.EMBEDDED);
            content.beginText();
            content.setFontAndSize(bf, 10);
            //month
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, new SimpleDateFormat("MMMM").format(date), 205,
                    625, 0);
            //name
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, info.get(0), 125, 610, 0);
            //roomNumber
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, info.get(1), 130, 598, 0);
            //monthly rate
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, info.get(2), 205, 560, 0);
            //Shuttle rate
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, info.get(3), 205, 545, 0);
            //gadget rate
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, info.get(4), 205, 530, 0);
            //additional fee
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, info.get(5), 205, 518, 0);
            //total amount
            Double totalAmount = Double.parseDouble(info.get(2)) + Double.parseDouble(info.get(3))
                    + Double.parseDouble(info.get(4));
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, totalAmount + "", 205, 490, 0);
            //due date

            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            cal.add(Calendar.MONTH, Calendar.MONTH - 1);
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, info.get(7), 130, 463, 0);

            content.endText();

        }
        pdfStamper.close();
        pdfReader.close();
        fos.close();
        pdfWriter.close();

        return true;
    } catch (DocumentException | ParseException ex) {
        //            Logger.getLogger(NPRImpl.class.getName()).log(Level.SEVERE, null, ex);
        label.setText(ex.getMessage());
        return false;
    } catch (FileNotFoundException ex) {
        //            Logger.getLogger(NPRImpl.class.getName()).log(Level.SEVERE, null, ex);
        label.setText(ex.getMessage());
        return false;
    } catch (IOException ex) {
        //            Logger.getLogger(NPRImpl.class.getName()).log(Level.SEVERE, null, ex);
        label.setText(ex.getMessage());
        return false;
    }
}

From source file:FirstStatMain.java

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
    Document document = new Document();
    try {/*from  ww  w  . ja v a 2  s . c  o m*/
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("HelloWorld.pdf"));
        document.open();
        document.add(new Paragraph("A Hello World PDF document."));
        document.add(new Paragraph(tacomment.getText()));
        if (jRadioButton1.isSelected())
            document.add(new Paragraph("Performance Status:Good"));
        else
            document.add(new Paragraph("Performance Status:Bad"));
        document.close();
        writer.close();
    } catch (DocumentException e) {
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}

From source file:Registration_Tenant.java

private void saveToPdf() {
    try {/*from w w  w.ja  v a2  s.  c  o m*/
        String gend = "";
        Document doc = new Document();
        BufferedReader br = new BufferedReader(new FileReader("dir\\defaults.txt"));
        String path = "";
        try {
            StringBuilder sb = new StringBuilder();
            String line = br.readLine();

            while (line != null) {
                sb.append(line);
                sb.append(System.lineSeparator());
                line = br.readLine();
            }
            path = sb.toString().trim();
        } finally {
            br.close();
        }
        FileOutputStream fos = new FileOutputStream(
                path + "\\registration\\" + lastNameTextField.getText().trim() + ", "
                        + firstNameTextField.getText().trim() + "RegistrationResidentForm.pdf");
        PdfWriter pdfWriter = PdfWriter.getInstance(doc, fos);

        PdfReader pdfReader = new PdfReader("RegistrationResidentForm.pdf");

        PdfStamper pdfStamper = new PdfStamper(pdfReader, fos);

        for (int i = 1; i <= pdfReader.getNumberOfPages(); i++) {
            PdfContentByte content = pdfStamper.getOverContent(i);

            byte[] cc = null;
            if (file != null) {
                File thisFile = new File(file);
                cc = Files.readAllBytes(thisFile.toPath());
            }
            if (cc != null) {
                Image image1 = Image.getInstance(cc);
                image1.scaleAbsolute(95, 105);
                image1.setAbsolutePosition(465f, 635f);
                content.addImage(image1);
            }

            //Text over the existing page
            BaseFont bf = BaseFont.createFont(BaseFont.TIMES_ROMAN, BaseFont.WINANSI, BaseFont.EMBEDDED);
            content.beginText();
            content.setFontAndSize(bf, 10);
            //last name
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, lastNameTextField.getText().trim(), 175, 610, 0);
            //first name
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, firstNameTextField.getText().trim(), 325, 610,
                    0);
            //middle name
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, middleNameTextField.getText().trim(), 470, 610,
                    0);
            //college
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, collegeTextField.getText().trim(), 180, 582, 0);
            //Course and year
            content.showTextAligned(PdfContentByte.ALIGN_LEFT,
                    courseTextField.getText().trim() + " - " + yearTextField.getText().trim(), 180, 568, 0);
            //department
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, departmentTextField.getText().trim(), 400, 582,
                    0);
            String g;
            if (male.isSelected()) {
                g = "Male";//6
            } else {
                g = "Female";//6
            }
            //Sex
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, g, 400, 568, 0);
            //Home address
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, homeAddressTextArea.getText(), 110, 540, 0);
            //resident mobile no1
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, MobileNumber1.getText().trim(), 187, 485, 0);
            //resident mobile no2
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, MobileNumber2.getText().trim(), 187, 470, 0);
            //student email
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, email.getText().trim(), 397, 485, 0);
            //fathers name
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, fatherNameTextField.getText().trim(), 110, 443,
                    0);
            //fathers area code
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, fACodeTextField.getText().trim(), 300, 443, 0);
            //father landline
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, fphoneNumberTextField.getText().trim(), 355, 443,
                    0);
            //father mobile
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, fMobileNumberTextField.getText().trim(), 490,
                    443, 0);
            //father email
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, fEmailTextField.getText().trim(), 110, 430, 0);
            //mother name
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, motherNameTextField.getText().trim(), 110, 402,
                    0);
            //mother area code
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, mACodeTextField.getText().trim(), 300, 402, 0);
            //mother landline
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, mphoneNumberTextField.getText().trim(), 355, 402,
                    0);
            //mother mobile
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, mMobileNumberTextField.getText().trim(), 490,
                    402, 0);
            //mother email
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, mEmailTextField.getText().trim(), 110, 387, 0);
            //guardian name
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, guardianNameTextField.getText().trim(), 170, 335,
                    0);
            //guardian contact number
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, gContactNumber1.getText().trim(), 470, 335, 0);
            //guardian address
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, guardianAddress.getText().trim(), 125, 322, 0);
            //guardian relation
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, relationToGuardian.getText().trim(), 170, 307,
                    0);
            content.setFontAndSize(bf, 12);
            //room details
            content.showTextAligned(PdfContentByte.ALIGN_LEFT,
                    "Room No.: " + roomNumberTextField.getSelectedItem().toString().trim() + " - "
                            + roomTypeTextField.getText().trim(),
                    355, 235, 0);
            content.endText();

        }
        pdfStamper.close();
        pdfReader.close();
        fos.close();
        pdfWriter.close();
    } catch (DocumentException ex) {
        //            Logger.getLogger(Registration_Tenant.class.getName()).log(Level.SEVERE, null, ex);
        new MessageDialog().error(this, ex.getMessage());
    } catch (FileNotFoundException ex) {
        //            Logger.getLogger(Registration_Tenant.class.getName()).log(Level.SEVERE, null, ex);
        new MessageDialog().error(this, ex.getMessage());
    } catch (IOException ex) {
        //            Logger.getLogger(Registration_Tenant.class.getName()).log(Level.SEVERE, null, ex);
        new MessageDialog().error(this, ex.getMessage());
    }
}

From source file:ImprimirTransServlet.java

/**
 * Handles the HTTP <code>POST</code> method.
 *
 * @param request servlet request//w w w  .  j  a va2 s. com
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String valorDDLCuenta = request.getParameter("btnImprimir");

    System.out.println(valorDDLCuenta);

    int pos = valorDDLCuenta.indexOf(" ");
    String idTrans = valorDDLCuenta.substring(pos + 1);

    System.out.println(idTrans);
    String infoTrans = getInfoTransaccion(Integer.parseInt(idTrans));
    String monto = admon.getCadenaEtiquetas(infoTrans, "<monto>");
    String idSalida = admon.getCadenaEtiquetas(infoTrans, "<idSalida>");
    String idDestino = admon.getCadenaEtiquetas(infoTrans, "<idDestino>");
    String seguro = admon.getCadenaEtiquetas(infoTrans, "<idSeguro>");
    String prestamo = admon.getCadenaEtiquetas(infoTrans, "<idPrestamo>");
    String tipo = admon.getCadenaEtiquetas(infoTrans, "<tipo>");
    String fecha = admon.getCadenaEtiquetas(infoTrans, "<fecha>");

    Document document = new Document();
    try {
        PdfWriter writer = PdfWriter.getInstance(document,
                new FileOutputStream("C://pdf//ReciboTransaccion" + idTrans + ".pdf"));
        document.open();
        document.add(new Paragraph("Transaccion: " + idTrans));
        document.add(new Paragraph("Cuenta Origen: " + idSalida));
        if (!idDestino.equals("-1")) {
            document.add(new Paragraph("Cuenta Destino: " + idDestino));
        }
        document.add(new Paragraph("Monto: " + monto));
        if (!seguro.equals("-1")) {
            document.add(new Paragraph("Seguro: " + seguro));
        }
        if (!prestamo.equals("-1")) {
            document.add(new Paragraph("Prestamo: " + prestamo));
        }

        document.add(new Paragraph("Tipo: " + tipo));
        document.add(new Paragraph("Fecha: " + fecha));

        document.close();
        writer.close();
    } catch (DocumentException e) {
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    request.getRequestDispatcher("/menuTransaccion.jsp").forward(request, response);
}

From source file:Billing_v2.java

public boolean saveStatementOfAccountToPDF(ArrayList<String> info) {
    try {//from ww w  .  ja  v a2  s.c o  m
        BufferedReader br = new BufferedReader(new FileReader("dir\\defaults.txt"));
        String path = "";
        try {
            StringBuilder sb = new StringBuilder();
            String line = br.readLine();

            while (line != null) {
                sb.append(line);
                sb.append(System.lineSeparator());
                line = br.readLine();
            }
            path = sb.toString().trim();
        } finally {
            br.close();
        }

        com.itextpdf.text.Document doc = new com.itextpdf.text.Document();
        FileOutputStream fos = new FileOutputStream(
                path + "\\residentStatementOfAccount\\" + info.get(0) + "statementOfAccounts.pdf");
        PdfWriter pdfWriter = PdfWriter.getInstance(doc, fos);

        PdfReader pdfReader = new PdfReader("statementOfAccounts.pdf");

        PdfStamper pdfStamper = new PdfStamper(pdfReader, fos);

        java.util.Date date = new SimpleDateFormat("yyyy-MM-dd").parse(info.get(6));

        for (int i = 1; i <= pdfReader.getNumberOfPages(); i++) {
            PdfContentByte content = pdfStamper.getOverContent(i);
            BaseFont bf = BaseFont.createFont(BaseFont.TIMES_ROMAN, BaseFont.WINANSI, BaseFont.EMBEDDED);
            content.beginText();
            content.setFontAndSize(bf, 10);
            //month
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, new SimpleDateFormat("MMMM").format(date), 205,
                    625, 0);
            //name
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, info.get(0), 125, 610, 0);
            //roomNumber
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, info.get(1), 130, 598, 0);
            //monthly rate
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, info.get(2), 205, 560, 0);
            //Shuttle rate
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, info.get(3), 205, 545, 0);
            //gadget rate
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, info.get(4), 205, 530, 0);
            //additional fee
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, info.get(5), 205, 518, 0);
            //total amount
            Double totalAmount = Double.parseDouble(info.get(2)) + Double.parseDouble(info.get(3))
                    + Double.parseDouble(info.get(4));
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, totalAmount + "", 205, 490, 0);
            //due date

            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            cal.add(Calendar.MONTH, Calendar.MONTH - 1);
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, info.get(7), 130, 463, 0);

            content.endText();

        }
        pdfStamper.close();
        pdfReader.close();
        fos.close();
        pdfWriter.close();

        return true;
    } catch (DocumentException | ParseException ex) {
        //            Logger.getLogger(Billing_v2.class.getName()).log(Level.SEVERE, null, ex);
        new MessageDialog().error(this, ex.getMessage());
        return false;
    } catch (FileNotFoundException ex) {
        //            Logger.getLogger(Billing_v2.class.getName()).log(Level.SEVERE, null, ex);
        new MessageDialog().error(this, ex.getMessage());
        return false;
    } catch (IOException ex) {
        //            Logger.getLogger(Billing_v2.class.getName()).log(Level.SEVERE, null, ex);
        new MessageDialog().error(this, ex.getMessage());
        return false;
    }
}