Example usage for com.lowagie.text.pdf PdfContentByte addImage

List of usage examples for com.lowagie.text.pdf PdfContentByte addImage

Introduction

In this page you can find the example usage for com.lowagie.text.pdf PdfContentByte addImage.

Prototype

public void addImage(Image image) throws DocumentException 

Source Link

Document

Adds an Image to the page.

Usage

From source file:org.openconcerto.erp.core.finance.accounting.report.PDFImageInsertor.java

License:Open Source License

public void insert(File pdf, Image img, int page, boolean under) throws Exception {

    PdfReader reader;//from  ww  w  .  j av  a 2 s.c o  m

    reader = new PdfReader(new FileInputStream(pdf));

    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(pdf));

    // img.setAbsolutePosition(20, 40);

    // int total = reader.getNumberOfPages() + 1;
    PdfContentByte content;
    if (under) {
        content = stamper.getUnderContent(page);
    } else {
        content = stamper.getOverContent(page);
    }
    content.addImage(img);
    stamper.close();
}

From source file:org.saiku.web.rest.resources.ExporterResource.java

License:Apache License

/**
 * Export chart to a file.//from  w  w  w .j  a  va 2  s.  c  o  m
 * @summary Export Chart.
 * @param type The export type (png, svg, jpeg)
 * @param svg The SVG
 * @param size The size
 * @param name The name
 * @return A reponse containing the chart export.
 */
@POST
@Produces({ "image/*" })
@Path("/saiku/chart")
public Response exportChart(@FormParam("type") @DefaultValue("png") String type, @FormParam("svg") String svg,
        @FormParam("size") Integer size, @FormParam("name") String name) {
    try {
        final String imageType = type.toUpperCase();
        Converter converter = Converter.byType("PDF");
        if (converter == null) {
            throw new Exception("Image convert is null");
        }

        //             resp.setContentType(converter.getContentType());
        //             resp.setHeader("Content-disposition", "attachment; filename=chart." + converter.getExtension());
        //             final Integer size = req.getParameter("size") != null? Integer.parseInt(req.getParameter("size")) : null;
        //             final String svgDocument = req.getParameter("svg");
        //             if (svgDocument == null)
        //             {
        //                 resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "Missing 'svg' parameter");
        //                 return;
        //             }
        if (StringUtils.isBlank(svg)) {
            throw new Exception("Missing 'svg' parameter");
        }
        final InputStream in = new ByteArrayInputStream(svg.getBytes("UTF-8"));
        final ByteArrayOutputStream out = new ByteArrayOutputStream();
        converter.convert(in, out, size);
        out.flush();
        byte[] doc = out.toByteArray();
        byte[] b = null;
        if (getVersion() != null && !getVersion().contains("EE")) {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();

            PdfReader reader = new PdfReader(doc);
            PdfStamper pdfStamper = new PdfStamper(reader, baos);

            URL dir_url = ExporterResource.class.getResource("/org/saiku/web/svg/watermark.png");
            Image image = Image.getInstance(dir_url);

            for (int i = 1; i <= reader.getNumberOfPages(); i++) {

                PdfContentByte content = pdfStamper.getOverContent(i);

                image.setAbsolutePosition(450f, 280f);
                /*image.setAbsolutePosition(reader.getPageSize(1).getWidth() - image.getScaledWidth(), reader.getPageSize
                   (1).getHeight() - image.getScaledHeight());*/
                //image.setAlignment(Image.MIDDLE);
                content.addImage(image);
            }
            pdfStamper.close();
            b = baos.toByteArray();
        } else {
            b = doc;
        }

        if (!type.equals("pdf")) {

            PDDocument document = PDDocument.load(new ByteArrayInputStream(b), null);

            PDPageTree pdPages = document.getDocumentCatalog().getPages();
            PDPage page = pdPages.get(0);
            BufferedImage o = new PDFRenderer(document).renderImage(0);
            ByteArrayOutputStream imgb = new ByteArrayOutputStream();
            String ct = "";
            String ext = "";
            if (type.equals("png")) {
                ct = "image/png";
                ext = "png";
            } else if (type.equals("jpg")) {
                ct = "image/jpg";
                ext = "jpg";
            }
            ImageIO.write(o, type, imgb);
            byte[] outfile = imgb.toByteArray();
            if (name == null || name.equals("")) {
                name = "chart";
            }
            return Response.ok(outfile).type(ct)
                    .header("content-disposition", "attachment; filename = " + name + "." + ext)
                    .header("content-length", outfile.length).build();
        } else {
            if (name == null || name.equals("")) {
                name = "chart";
            }
            return Response.ok(b).type(converter.getContentType())
                    .header("content-disposition",
                            "attachment; filename = " + name + "." + converter.getExtension())
                    .header("content-length", b.length).build();
        }
    } catch (Exception e) {
        log.error("Error exporting Chart to  " + type, e);
        return Response.serverError().entity(e.getMessage()).status(Status.INTERNAL_SERVER_ERROR).build();
    }
}

From source file:org.silverpeas.core.util.PdfUtil.java

License:Open Source License

/**
 * Add a image under or over content on each page of a PDF file.
 * @param pdfSource the source pdf file, this content is not modified by this method
 * @param imageToAdd the image file//from  w  w w .ja v a2 s  .  c o  m
 * @param pdfDestination the destination pdf file, with the image under or over content
 * @param isBackground indicates if image is addes under or over the content of the pdf source
 * file
 */
private static void addImageOnEachPage(InputStream pdfSource, File imageToAdd, OutputStream pdfDestination,
        final boolean isBackground) {

    // Verify given arguments
    if (imageToAdd == null || !imageToAdd.isFile()) {
        throw new SilverpeasRuntimeException("The image file doesn't exist");
    } else if (!FileUtil.isImage(imageToAdd.getPath())) {
        throw new SilverpeasRuntimeException("The picture to add is not an image file");
    }

    PdfReader reader = null;
    try {

        // Get a reader of PDF content
        reader = new PdfReader(pdfSource);

        // Obtain the total number of pages
        int pdfNbPages = reader.getNumberOfPages();
        PdfStamper stamper = new PdfStamper(reader, pdfDestination);

        // Load the image
        Image image = Image.getInstance(imageToAdd.getPath());
        float imageWidth = image.getWidth();
        float imageHeigth = image.getHeight();

        // Adding the image on each page of the PDF
        for (int i = 1; i <= pdfNbPages; i++) {

            // Page sizes
            Rectangle rectangle = reader.getPageSize(i);

            // Compute the scale of the image
            float scale = Math.min(100, (rectangle.getWidth() / imageWidth * 100));
            image.scalePercent(Math.min(scale, (rectangle.getHeight() / imageHeigth * 100)));

            // Setting the image position for the current page
            image.setAbsolutePosition(computeImageCenterPosition(rectangle.getWidth(), image.getScaledWidth()),
                    computeImageCenterPosition(rectangle.getHeight(), image.getScaledHeight()));

            // Adding image
            PdfContentByte imageContainer = isBackground ? stamper.getUnderContent(i)
                    : stamper.getOverContent(i);
            imageContainer.addImage(image);
        }

        // End of the treatment : closing the stamper
        stamper.close();

    } catch (Exception e) {
        SilverLogger.getLogger(PdfUtil.class).error(e);
        throw new SilverpeasRuntimeException(
                "A problem has occurred during the adding of an image into a pdf file", e);
    } finally {
        if (reader != null) {
            reader.close();
        }
    }
}

From source file:org.silverpeas.util.PdfUtil.java

License:Open Source License

/**
 * Add a image under or over content on each page of a PDF file.
 * @param pdfSource the source pdf file, this content is not modified by this method
 * @param imageToAdd the image file/*from  w w w . j  av  a 2  s  . c o  m*/
 * @param pdfDestination the destination pdf file, with the image under or over content
 * @param isBackground indicates if image is addes under or over the content of the pdf source
 * file
 */
private static void addImageOnEachPage(InputStream pdfSource, File imageToAdd, OutputStream pdfDestination,
        final boolean isBackground) {

    // Verify given arguments
    if (imageToAdd == null || !imageToAdd.isFile()) {
        throw new RuntimeException("The image file doesn't exist");
    } else if (!FileUtil.isImage(imageToAdd.getPath())) {
        throw new RuntimeException("The picture to add is not an image file");
    }

    PdfReader reader = null;
    try {

        // Get a reader of PDF content
        reader = new PdfReader(pdfSource);

        // Obtain the total number of pages
        int pdfNbPages = reader.getNumberOfPages();
        PdfStamper stamper = new PdfStamper(reader, pdfDestination);

        // Load the image
        Image image = Image.getInstance(imageToAdd.getPath());
        float imageWidth = image.getWidth();
        float imageHeigth = image.getHeight();

        // Adding the image on each page of the PDF
        for (int i = 1; i <= pdfNbPages; i++) {

            // Page sizes
            Rectangle rectangle = reader.getPageSize(i);

            // Compute the scale of the image
            float scale = Math.min(100, (rectangle.getWidth() / imageWidth * 100));
            image.scalePercent(Math.min(scale, (rectangle.getHeight() / imageHeigth * 100)));

            // Setting the image position for the current page
            image.setAbsolutePosition(computeImageCenterPosition(rectangle.getWidth(), image.getScaledWidth()),
                    computeImageCenterPosition(rectangle.getHeight(), image.getScaledHeight()));

            // Adding image
            PdfContentByte imageContainer = isBackground ? stamper.getUnderContent(i)
                    : stamper.getOverContent(i);
            imageContainer.addImage(image);
        }

        // End of the treatment : closing the stamper
        stamper.close();

    } catch (Exception e) {
        SilverTrace.error("util", "PdfUtil.stamp", "EX_ERROR_PDF_ADD_WATERWARK", e);
        throw new RuntimeException("A problem has occured during the adding of an image into a pdf file", e);
    } finally {
        if (reader != null) {
            reader.close();
        }
    }
}

From source file:org.sipfoundry.faxrx.FaxProcessor.java

License:Open Source License

private File tiff2Pdf(File tiffFile) {
    Pattern pattern = Pattern.compile("(.*).tiff");
    Matcher matcher = pattern.matcher(tiffFile.getName());
    boolean matchFound = matcher.find();

    // check if tiffFile is actually a TIFF file, just in case
    if (matchFound) {
        // located at default tmp-file directory
        File pdfFile = new File(System.getProperty("java.io.tmpdir"), matcher.group(1) + ".pdf");
        try {/*from ww  w  . ja  v  a  2 s.  co m*/
            // read TIFF file
            RandomAccessFileOrArray tiff = new RandomAccessFileOrArray(tiffFile.getAbsolutePath());

            // get number of pages of TIFF file
            int pages = TiffImage.getNumberOfPages(tiff);

            // create PDF file
            Document pdf = new Document(PageSize.LETTER, 0, 0, 0, 0);

            PdfWriter writer = PdfWriter.getInstance(pdf, new FileOutputStream(pdfFile));
            writer.setStrictImageSequence(true);

            // open PDF filex
            pdf.open();

            PdfContentByte contentByte = writer.getDirectContent();

            // write PDF file page by page
            for (int page = 1; page <= pages; page++) {
                Image temp = TiffImage.getTiffImage(tiff, page);
                temp.scalePercent(7200f / temp.getDpiX(), 7200f / temp.getDpiY());
                pdf.setPageSize(new Rectangle(temp.getScaledWidth(), temp.getScaledHeight()));
                temp.setAbsolutePosition(0, 0);
                contentByte.addImage(temp);
                pdf.newPage();
            }
            // close PDF file
            pdf.close();
        } catch (Exception e) {
            LOG.error("faxrx::tiff2Pdf error " + e.getMessage());
            e.printStackTrace();
            return null;
        }
        return pdfFile;
    }

    else {
        return null;
    }
}

From source file:oscar.oscarEncounter.oscarConsultationRequest.pageUtil.ImagePDFCreator.java

License:Open Source License

/**
 * Prints the consultation request./*from  w  w  w  .  j  av a 2  s. co m*/
 * @throws IOException when an error with the output stream occurs
 * @throws DocumentException when an error in document construction occurs
 */
public void printPdf() throws IOException, DocumentException {

    Image image;
    try {
        image = Image.getInstance((String) request.getAttribute("imagePath"));
    } catch (Exception e) {
        logger.error("Unexpected error:", e);
        throw new DocumentException(e);
    }

    // Create the document we are going to write to
    document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, os);

    document.setPageSize(PageSize.LETTER);
    ResourceBundle.getBundle("oscarResources", request.getLocale())
            .getString("oscarEncounter.oscarConsultationRequest.consultationFormPrint.msgImage");
    document.addCreator("OSCAR");
    document.open();

    int type = image.getOriginalType();
    if (type == Image.ORIGINAL_TIFF) {
        // The following is composed of code from com.lowagie.tools.plugins.Tiff2Pdf modified to create the 
        // PDF in memory instead of on disk
        RandomAccessFileOrArray ra = new RandomAccessFileOrArray((String) request.getAttribute("imagePath"));
        int comps = TiffImage.getNumberOfPages(ra);
        boolean adjustSize = false;
        PdfContentByte cb = writer.getDirectContent();
        for (int c = 0; c < comps; ++c) {
            Image img = TiffImage.getTiffImage(ra, c + 1);
            if (img != null) {
                if (adjustSize) {
                    document.setPageSize(new Rectangle(img.getScaledWidth(), img.getScaledHeight()));
                    document.newPage();
                    img.setAbsolutePosition(0, 0);
                } else {
                    if (img.getScaledWidth() > 500 || img.getScaledHeight() > 700) {
                        img.scaleToFit(500, 700);
                    }
                    img.setAbsolutePosition(20, 20);
                    document.newPage();
                    document.add(
                            new Paragraph((String) request.getAttribute("imageTitle") + " - page " + (c + 1)));
                }
                cb.addImage(img);

            }
        }
        ra.close();
    } else {
        PdfContentByte cb = writer.getDirectContent();
        if (image.getScaledWidth() > 500 || image.getScaledHeight() > 700) {
            image.scaleToFit(500, 700);
        }
        image.setAbsolutePosition(20, 20);
        cb.addImage(image);
    }
    document.close();
}

From source file:questions.ocg.AddOptionalWatermark.java

public static void main(String[] args) throws DocumentException, IOException {
    PdfReader reader = new PdfReader(RESOURCE);
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT));
    Image image1 = Image.getInstance(IMAGE_PRINTED);
    Image image2 = Image.getInstance(IMAGE_NOT_PRINTED);
    PdfLayer watermark_printed = new PdfLayer("printed", stamper.getWriter());
    watermark_printed.setOn(false);//from w w  w  . java2 s. c o m
    watermark_printed.setOnPanel(false);
    watermark_printed.setPrint("print", true);
    PdfLayer watermark_not_printed = new PdfLayer("not_printed", stamper.getWriter());
    watermark_not_printed.setOn(true);
    watermark_not_printed.setOnPanel(false);
    watermark_not_printed.setPrint("print", false);
    for (int i = 0; i < stamper.getReader().getNumberOfPages();) {
        PdfContentByte cb = stamper.getUnderContent(++i);
        Rectangle rectangle = stamper.getReader().getPageSizeWithRotation(i);
        cb.beginLayer(watermark_printed);
        float AbsoluteX = rectangle.getLeft() + (rectangle.getWidth() - image1.getPlainWidth()) / 2;
        float AbsoluteY = rectangle.getBottom() + (rectangle.getHeight() - image1.getPlainHeight()) / 2;
        image1.setAbsolutePosition(AbsoluteX, AbsoluteY);
        cb.addImage(image1);
        cb.endLayer();
        cb.beginLayer(watermark_not_printed);
        AbsoluteX = rectangle.getLeft() + (rectangle.getWidth() - image2.getPlainWidth()) / 2;
        AbsoluteY = rectangle.getBottom() + (rectangle.getHeight() - image2.getPlainHeight()) / 2;
        image2.setAbsolutePosition(AbsoluteX, AbsoluteY);
        cb.addImage(image2);
        cb.endLayer();
    }
    stamper.close();
}

From source file:sms.ReportForms.java

public void utext(String query, String Name, String dob, String house, String formclass, String kcpe,
        String imgurl, String kcpegrade, String id) {
    methods nn = new methods();
    String u = unig();// w  w w  .  j a  va  2s  .  co m
    checkPreviousResults(u, kcpe, kcpegrade, id);
    ArrayList<ExamDbDataHolder> users = selectExamResults(u, query);
    if (users.size() < 2) {
        Form1Exams n = new Form1Exams();
        String[] Subjects = n.findSubjectid();
        String[] Subjectsnames = n.findSubjectname();

        JFileChooser chooser = new JFileChooser();
        chooser.setCurrentDirectory(new java.io.File(","));
        chooser.setDialogTitle("Save at");
        chooser.setApproveButtonText("save");
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
            try {

                Document pdfp = new Document();
                PdfWriter w = PdfWriter.getInstance(pdfp,
                        new FileOutputStream(new File(chooser.getSelectedFile(), "" + sid.getText() + ".pdf")));
                pdfp.open();
                PdfContentByte canvas = w.getDirectContentUnder();
                Image imgb = Image.getInstance("C:\\Users\\kimani kogi\\Pictures\\icons\\logo.png");
                imgb.setAbsolutePosition(2, 2);
                imgb.scaleAbsoluteHeight(PageSize.A4.getHeight() / 4);
                imgb.scaleAbsoluteWidth(PageSize.A4.getWidth() / 4);
                canvas.saveState();
                PdfGState state = new PdfGState();
                state.setFillOpacity(0.6f);
                canvas.setGState(state);
                canvas.addImage(imgb);
                canvas.restoreState();

                PdfPTable tbl1 = new PdfPTable(2);
                //  tbl.setWidthPercentage(100);

                tbl1.setTotalWidth(575);
                tbl1.setLockedWidth(true);

                tbl1.setWidths(new int[] { 1, 4 });
                if (imgurl.equals("image")) {
                    tbl1.addCell("no image ");

                    String img = imgurl;
                } else {
                    tbl1.addCell(createImageCell(imgurl));

                }

                tbl1.addCell(createTextCell(schooldetails));

                PdfPTable tbl = new PdfPTable(8);
                //  tbl.setWidthPercentage(100);
                tbl.setTotalWidth(575);
                tbl.setLockedWidth(true);
                tbl.setSpacingBefore(8);
                tbl.setSpacingAfter(6);
                tbl.getDefaultCell().setBorderWidthTop(2);
                tbl.getDefaultCell().setBorderWidthLeft(0);
                tbl.getDefaultCell().setBorderWidthRight(0);
                tbl.setWidths(new int[] { 1, 1, 1, 2, 1, 1, 1, 1 });
                tbl.addCell("Adm No:");
                tbl.addCell(sid.getText());
                tbl.addCell("Name:");
                tbl.addCell(Name);
                tbl.addCell("Form:");
                tbl.addCell(formclass);
                tbl.addCell("Kcpe:");
                tbl.addCell(kcpe);

                tbl.addCell("House:");
                tbl.addCell(house);
                tbl.addCell("Term:");
                tbl.addCell("Second term");
                tbl.addCell("Year:");
                tbl.addCell("2017");
                tbl.addCell("DOB:");
                tbl.addCell(dob);

                PdfPTable tbl2 = new PdfPTable(5);
                //  tbl.setWidthPercentage(100);
                tbl2.setTotalWidth(575);
                tbl2.setLockedWidth(true);
                //  tbl2.getDefaultCell().setFixedHeight(35f);
                tbl2.setWidths(new int[] { 2, 1, 1, 2, 2 });

                tbl2.addCell(creatTextCellHeader("Subjects"));
                tbl2.addCell(creatTextCellHeader("Exams"));
                tbl2.addCell(creatTextCellHeader("Grade"));
                tbl2.addCell(creatTextCellHeader("Ratings"));
                tbl2.addCell(creatTextCellHeader("Remarks"));
                // String []  Subjectsnames=n.findSubjectname();
                for (int i = 0; i < users.size(); i++) {
                    for (int a = 0; a < Subjectsnames.length; a++) {
                        tbl2.addCell(Subjectsnames[a]);
                        String re = null;
                        if (Subjects[a].equals("s1")) {
                            tbl2.addCell(((ExamDbDataHolder) users.get(i)).getMathematics());
                            re = ((ExamDbDataHolder) users.get(i)).getMathematics();
                            //  JOptionPane.showMessageDialog(null,((ExamDbDataHolder)users.get(i)).getMathematics());
                            // String maths = ((ExamDbDataHolder)users.get(i)).getMathematics();

                        } else if (Subjects[a].equals("s2")) {
                            tbl2.addCell(((ExamDbDataHolder) users.get(i)).getEnglish());
                            re = ((ExamDbDataHolder) users.get(i)).getEnglish();
                            //row[c] = ((ExamDbDataHolder)users.get(i)).getEnglish();
                            // c++;
                        } else if (Subjects[a].equals("s3")) {
                            tbl2.addCell(((ExamDbDataHolder) users.get(i)).getKiswahili());
                            re = ((ExamDbDataHolder) users.get(i)).getKiswahili();
                            // row[c] = ((ExamDbDataHolder)users.get(i)).getKiswahili();
                            // c++;
                        }

                        else if (Subjects[a].equals("s4")) {
                            tbl2.addCell(((ExamDbDataHolder) users.get(i)).getPhysics());
                            re = ((ExamDbDataHolder) users.get(i)).getPhysics();
                            //   row[c] = ((ExamDbDataHolder)users.get(i)).getPhysics();
                            //  c++;
                        } else if (Subjects[a].equals("s5")) {
                            tbl2.addCell(((ExamDbDataHolder) users.get(i)).getChemistry());
                            re = ((ExamDbDataHolder) users.get(i)).getChemistry();
                            // row[c] = ((ExamDbDataHolder)users.get(i)).getChemistry();
                            // c++;
                        } else if (Subjects[a].equals("s6")) {
                            tbl2.addCell(((ExamDbDataHolder) users.get(i)).getBiology());
                            re = ((ExamDbDataHolder) users.get(i)).getBiology();
                            //   row[c] = ((ExamDbDataHolder)users.get(i)).getBiology();
                            //  c++;
                        } else if (Subjects[a].equals("s7")) {
                            tbl2.addCell(((ExamDbDataHolder) users.get(i)).getHistory());
                            re = ((ExamDbDataHolder) users.get(i)).getHistory();
                            //  row[c] = ((ExamDbDataHolder)users.get(i)).getHistory();
                            //  c++;
                        } else if (Subjects[a].equals("s8")) {
                            tbl2.addCell(((ExamDbDataHolder) users.get(i)).getGeography());
                            re = ((ExamDbDataHolder) users.get(i)).getGeography();
                            // row[c] = ((ExamDbDataHolder)users.get(i)).getGeography();
                            // c++;
                        } else if (Subjects[a].equals("s9")) {
                            tbl2.addCell(((ExamDbDataHolder) users.get(i)).getCre());
                            re = ((ExamDbDataHolder) users.get(i)).getCre();
                            //  row[c] = ((ExamDbDataHolder)users.get(i)).getCre();
                            //  c++;
                        } else if (Subjects[a].equals("s10")) {
                            tbl2.addCell(((ExamDbDataHolder) users.get(i)).getIre());
                            re = ((ExamDbDataHolder) users.get(i)).getIre();
                            //   row[c] = ((ExamDbDataHolder)users.get(i)).getIre();
                            //  c++;
                        } else if (Subjects[a].equals("s11")) {
                            tbl2.addCell(((ExamDbDataHolder) users.get(i)).getHre());
                            re = ((ExamDbDataHolder) users.get(i)).getHre();
                            //   row[c] = ((ExamDbDataHolder)users.get(i)).getHre();
                            //  c++;
                        } else if (Subjects[a].equals("s12")) {
                            tbl2.addCell(((ExamDbDataHolder) users.get(i)).getAgriculture());
                            re = ((ExamDbDataHolder) users.get(i)).getAgriculture();
                            //   row[c] = ((ExamDbDataHolder)users.get(i)).getAgriculture();
                            //  c++;
                        } else if (Subjects[a].equals("s13")) {
                            tbl2.addCell(((ExamDbDataHolder) users.get(i)).getHomescience());
                            re = ((ExamDbDataHolder) users.get(i)).getHomescience();
                            //  row[c] = ((ExamDbDataHolder)users.get(i)).getHomescience();
                            //  c++;
                        } else if (Subjects[a].equals("s14")) {
                            tbl2.addCell(((ExamDbDataHolder) users.get(i)).getArtdesign());
                            re = ((ExamDbDataHolder) users.get(i)).getArtdesign();
                            //   row[c] = ((ExamDbDataHolder)users.get(i)).getArtdesign();
                            //  c++;
                        } else if (Subjects[a].equals("s15")) {
                            tbl2.addCell(((ExamDbDataHolder) users.get(i)).getComputer());
                            re = ((ExamDbDataHolder) users.get(i)).getComputer();
                            // row[c] = ((ExamDbDataHolder)users.get(i)).getComputer();
                            // c++;

                        } else if (Subjects[a].equals("s16")) {
                            tbl2.addCell(((ExamDbDataHolder) users.get(i)).getBuilding());
                            re = ((ExamDbDataHolder) users.get(i)).getBuilding();
                            // row[c] = ((ExamDbDataHolder)users.get(i)).getBuilding();
                            //  c++;
                        } else if (Subjects[a].equals("s17")) {
                            tbl2.addCell(((ExamDbDataHolder) users.get(i)).getWoodwork());
                            re = ((ExamDbDataHolder) users.get(i)).getWoodwork();
                            //  row[c] = ((ExamDbDataHolder)users.get(i)).getWoodwork();
                            // c++;
                        } else if (Subjects[a].equals("s18")) {
                            tbl2.addCell(((ExamDbDataHolder) users.get(i)).getMetalwork());
                            re = ((ExamDbDataHolder) users.get(i)).getMetalwork();
                            //  row[c] = ((ExamDbDataHolder)users.get(i)).getMetalwork();
                            // c++;
                        } else if (Subjects[a].equals("s19")) {
                            tbl2.addCell(((ExamDbDataHolder) users.get(i)).getMusic());
                            re = ((ExamDbDataHolder) users.get(i)).getMusic();
                            //   row[c] = ((ExamDbDataHolder)users.get(i)).getMusic();
                            //  c++;
                        } else if (Subjects[a].equals("s20")) {
                            tbl2.addCell(((ExamDbDataHolder) users.get(i)).getFrench());
                            re = ((ExamDbDataHolder) users.get(i)).getFrench();
                            // row[c] = ((ExamDbDataHolder)users.get(i)).getFrench();
                            // c++;
                        } else if (Subjects[a].equals("s21")) {
                            tbl2.addCell(((ExamDbDataHolder) users.get(i)).getGerman());
                            re = ((ExamDbDataHolder) users.get(i)).getGerman();
                            //  row[c] = ((ExamDbDataHolder)users.get(i)).getGerman();
                            // c++;
                        } else if (Subjects[a].equals("s22")) {
                            tbl2.addCell(((ExamDbDataHolder) users.get(i)).getArabic());
                            re = ((ExamDbDataHolder) users.get(i)).getArabic();
                            // row[c] = ((ExamDbDataHolder)users.get(i)).getArabic();
                            // c++;
                        } else if (Subjects[a].equals("s23")) {
                            tbl2.addCell(((ExamDbDataHolder) users.get(i)).getBusiness());
                            re = ((ExamDbDataHolder) users.get(i)).getBusiness();
                            //  row[c] = ((ExamDbDataHolder)users.get(i)).getBusiness();
                            // c++;
                        }
                        String[] gr = nn.checkGrade(re, Subjects[a]);
                        tbl2.addCell(gr[0]);
                        tbl2.addCell(gr[1]);
                        tbl2.addCell("");
                    }
                }

                //add for all other subjects

                PdfPTable tbl3 = new PdfPTable(4);
                //  tbl.setWidthPercentage(100);
                tbl3.setTotalWidth(575);
                tbl3.setLockedWidth(true);
                tbl3.setSpacingBefore(8);
                tbl3.setSpacingAfter(6);
                //  tbl3.getDefaultCell().setBorderWidthTop(0);
                tbl3.getDefaultCell().setBorder(0);
                //  tbl3.getDefaultCell().setBorderWidthLeft(0);
                //  tbl3.getDefaultCell().setBorderWidthRight(0);
                tbl3.setWidths(new int[] { 1, 1, 1, 1 });
                tbl3.addCell(creatTextCellHeader("CURRENT MARKS:"));
                tbl3.addCell(creatTextCellHeader(String.valueOf(((ExamDbDataHolder) users.get(0)).getTotal())));
                float g = Float.valueOf(((ExamDbDataHolder) users.get(0)).getTotal());
                //  tbl.addCell(String.format("%.1f", g));

                String gr = nn.checkGrade(yearid, String.format("%.1f", g / getYear()));

                tbl3.addCell(creatTextCellHeader("PREVIOUS MARKS:"));
                tbl3.addCell(creatTextCellHeader("459"));
                tbl3.addCell(creatTextCellHeader("AVERAGE MARKS:"));
                tbl3.addCell(creatTextCellHeader(String.format("%.1f", g / getYear())));
                tbl3.addCell(creatTextCellHeader("PREVIOUS AVERAGE:"));
                tbl3.addCell(creatTextCellHeader("50"));
                tbl3.addCell(creatTextCellHeader("MEAN GRADE:"));
                tbl3.addCell(creatTextCellHeader(gr));
                tbl3.addCell(creatTextCellHeader("PREVIOUS MEAN GRADE:"));
                tbl3.addCell(creatTextCellHeader("B"));
                tbl3.addCell(creatTextCellHeader("POS"));
                tbl3.addCell(creatTextCellHeader("3 "));
                tbl3.addCell(creatTextCellHeader("OUT OF"));
                tbl3.addCell(creatTextCellHeader("79"));

                PdfPTable tbl4 = new PdfPTable(2);
                //  tbl.setWidthPercentage(100);
                tbl4.setHorizontalAlignment(0);
                //tbl4.HorizontalAlignment=Element.ALIGN_LEFT;
                tbl4.setTotalWidth(575 / 2);
                tbl4.setLockedWidth(true);
                tbl4.setSpacingBefore(8);
                tbl4.setSpacingAfter(6);
                //  tbl3.getDefaultCell().setBorderWidthTop(0);
                tbl4.getDefaultCell().setBorder(0);
                //  tbl3.getDefaultCell().setBorderWidthLeft(0);
                //  tbl3.getDefaultCell().setBorderWidthRight(0);
                tbl4.setWidths(new int[] { 2, 1 });
                PdfPCell cell = new PdfPCell(new Paragraph("REMARKS"));
                cell.setColspan(2);

                tbl4.addCell(cell);
                tbl4.addCell(creatTextCellHeader(
                        "CLASS TEACHERS........................................................."
                                + ":............................\n\n\n"));
                tbl4.addCell(creatTextCellChart("chart"));
                tbl4.addCell(creatTextCellHeader(
                        "SIGNATURE-------------------------------------------------------------"
                                + "-------------------------------\n\n\n"));
                tbl4.addCell(creatTextCellHeader(
                        "-------------\n\n\n-----------------------------------------------------------"
                                + "-----------------------------"));

                pdfp.add(tbl1);

                pdfp.add(tbl);

                pdfp.add(tbl2);
                pdfp.add(tbl3);
                pdfp.add(tbl4);

                //        Paragraph p=new Paragraph();
                //        p.setAlignment(Element.ALIGN_CENTER);
                //        p.setFont(FontFactory.getFont(FontFactory.TIMES_BOLD,18,Font.BOLD));
                //         Paragraph po=new Paragraph();
                //        po.setAlignment(Element.ALIGN_CENTER);
                //        po.setFont(FontFactory.getFont(FontFactory.TIMES_BOLD,16,Font.BOLD));
                //        
                //        Paragraph pd=new Paragraph();
                //          pd.setAlignment(Element.ALIGN_CENTER);
                //         pd.setFont(FontFactory.getFont(FontFactory.TIMES_BOLD,14,Font.BOLD));
                //        p.add("ITHANGA SECONDARY SCHOOL");
                //        po.add("PO.BOX 238  ITHANGA THIKA");
                //         pd.add(new Date().toString());
                //        pdfp.add(p);
                //        pdfp.add(po);
                //        pdfp.add(pd);
                //         
                //        
                //      
                //        pdfp.add(new Paragraph("\n.................................................................."
                //                + ".................................................................................\n"));
                //         String []names=  n.findSubjectname();
                //        PdfPTable tbl=new PdfPTable(names.length+6);
                //        tbl.setWidths(new float[]{1,1,4,1,1,1,1,1,1,1,1,1,1,1,1,1,1});
                //       // tbl.setWidthPercentage(100);
                //        tbl.setTotalWidth(575);
                //        tbl.setLockedWidth(true);
                //         PdfPTable tbl1=new PdfPTable(names.length+6);
                //        tbl1.setWidths(new float[]{1,1,4,1,1,1,1,1,1,1,1,1,1,1,1,1,1});
                //       // tbl.setWidthPercentage(100);
                //        tbl1.setTotalWidth(575);
                //        tbl1.setLockedWidth(true);
                //     PdfPCell cell=new PdfPCell (new Paragraph("RESULTS"));
                //     cell.setColspan((names.length+4)*2);
                //     cell.setBackgroundColor(Color.CYAN);
                //        tbl1.addCell(cell);
                //        tbl1.addCell("Pos");
                //          tbl1.addCell("id");
                //          tbl1.addCell("Name");
                //          int a;

                pdfp.close();
            } catch (Exception j) {
                j.printStackTrace();
            }

            // Image img=new Image.getInstance("j.png");

        }
    }
}