Example usage for com.lowagie.text.pdf PdfPTable setLockedWidth

List of usage examples for com.lowagie.text.pdf PdfPTable setLockedWidth

Introduction

In this page you can find the example usage for com.lowagie.text.pdf PdfPTable setLockedWidth.

Prototype

public void setLockedWidth(boolean lockedWidth) 

Source Link

Document

Uses the value in setTotalWidth() in Document.add().

Usage

From source file:org.mapfish.print.config.layout.PivotTableBlock.java

License:Open Source License

public void render(final PJsonObject params, PdfElement target, final RenderingContext context)
        throws DocumentException {

    if (isAbsolute()) {
        context.getCustomBlocks().addAbsoluteDrawer(new PDFCustomBlocks.AbsoluteDrawer() {
            public void render(PdfContentByte dc) throws DocumentException {
                final PdfPTable table = buildPivotTable(params, context, tableConfig);
                if (table != null) {
                    table.setTotalWidth(width);
                    table.setLockedWidth(true);

                    if (widths != null) {
                        table.setWidths(widths);
                    }/*  w w  w  .j av a 2s.  c o  m*/

                    table.writeSelectedRows(0, -1, absoluteX, absoluteY, dc);
                }
            }
        });
    } else {
        final PdfPTable table = buildPivotTable(params, context, tableConfig);
        if (table != null) {
            if (widths != null) {
                table.setWidths(widths);
            }

            table.setSpacingAfter((float) spacingAfter);
            target.add(table);
        }
    }
}

From source file:org.mapfish.print.PDFUtils.java

License:Open Source License

/**
 * When we have to do some custom drawing in a block that is layed out by
 * iText, we first give an empty table with the good dimensions to iText,
 * then iText will call a callback with the actual position. When that
 * happens, we use the given drawer to do the actual drawing.
 *///from w w  w  .j  av  a2 s  .  co m
public static PdfPTable createPlaceholderTable(double width, double height, double spacingAfter,
        ChunkDrawer drawer, HorizontalAlign align, PDFCustomBlocks customBlocks) {
    PdfPTable placeHolderTable = new PdfPTable(1);
    placeHolderTable.setLockedWidth(true);
    placeHolderTable.setTotalWidth((float) width);
    final PdfPCell placeHolderCell = new PdfPCell();
    placeHolderCell.setMinimumHeight((float) height);
    placeHolderCell.setPadding(0f);
    placeHolderCell.setBorder(PdfPCell.NO_BORDER);
    placeHolderTable.addCell(placeHolderCell);
    customBlocks.addChunkDrawer(drawer);
    placeHolderTable.setTableEvent(drawer);
    placeHolderTable.setComplete(true);

    final PdfPCell surroundingCell = new PdfPCell(placeHolderTable);
    surroundingCell.setPadding(0f);
    surroundingCell.setBorder(PdfPCell.NO_BORDER);
    if (align != null) {
        placeHolderTable.setHorizontalAlignment(align.getCode());
        surroundingCell.setHorizontalAlignment(align.getCode());
    }

    PdfPTable surroundingTable = new PdfPTable(1);
    surroundingTable.setSpacingAfter((float) spacingAfter);
    surroundingTable.addCell(surroundingCell);
    surroundingTable.setComplete(true);

    return surroundingTable;
}

From source file:org.sonar.report.pdf.Style.java

License:Open Source License

public static PdfPTable createTwoColumnsTitledTable(List<String> titles, List<String> content) {
    PdfPTable table = new PdfPTable(10);
    Iterator<String> itLeft = titles.iterator();
    Iterator<String> itRight = content.iterator();
    while (itLeft.hasNext()) {
        String textLeft = itLeft.next();
        String textRight = itRight.next();
        table.getDefaultCell().setColspan(1);
        table.addCell(textLeft);/*  w  w  w .  j  ava  2s.c o  m*/
        table.getDefaultCell().setColspan(9);
        table.addCell(textRight);
    }
    table.setSpacingBefore(20);
    table.setSpacingAfter(20);
    table.setLockedWidth(false);
    table.setWidthPercentage(90);
    return table;
}

From source file:org.sonar.report.pdf.TeamWorkbookPDFReporter.java

License:Open Source License

private PdfPTable createViolationsDetailedTable(String ruleName, List<String> files, List<String> lines) {

    // TODO: internationalize this

    PdfPTable table = new PdfPTable(10);
    Iterator<String> itLeft = files.iterator();
    Iterator<String> itRight = lines.iterator();
    table.getDefaultCell().setColspan(1);
    table.getDefaultCell().setBackgroundColor(new Color(255, 228, 181));
    table.addCell(new Phrase("Rule", Style.NORMAL_FONT));
    table.getDefaultCell().setColspan(9);
    table.getDefaultCell().setBackgroundColor(Color.WHITE);
    table.addCell(new Phrase(ruleName, Style.NORMAL_FONT));
    table.getDefaultCell().setColspan(10);
    table.getDefaultCell().setBackgroundColor(Color.GRAY);
    table.addCell("");
    table.getDefaultCell().setColspan(7);
    table.getDefaultCell().setBackgroundColor(new Color(255, 228, 181));
    table.addCell(new Phrase("File", Style.NORMAL_FONT));
    table.getDefaultCell().setColspan(3);
    table.addCell(new Phrase("Line", Style.NORMAL_FONT));
    table.getDefaultCell().setBackgroundColor(Color.WHITE);

    int i = 0;/*  w w w  .j  a  v a2  s. c  o m*/
    String lineNumbers = "";
    while (i < files.size() - 1) {
        if (lineNumbers.equals("")) {
            lineNumbers += lines.get(i);
        } else {
            lineNumbers += ", " + lines.get(i);
        }

        if (!files.get(i).equals(files.get(i + 1))) {
            table.getDefaultCell().setColspan(7);
            table.addCell(files.get(i));
            table.getDefaultCell().setColspan(3);
            table.addCell(lineNumbers);
            lineNumbers = "";
        }
        i++;
    }

    table.getDefaultCell().setColspan(7);
    table.addCell(files.get(files.size() - 1));
    table.getDefaultCell().setColspan(3);
    if (lineNumbers.equals("")) {
        lineNumbers += lines.get(i);
    } else {
        lineNumbers += ", " + lines.get(lines.size() - 1);
    }
    table.addCell(lineNumbers);

    table.setSpacingBefore(20);
    table.setSpacingAfter(20);
    table.setLockedWidth(false);
    table.setWidthPercentage(90);
    return table;
}

From source file:org.tellervo.desktop.print.BasicBoxLabel.java

License:Open Source License

public void generateBoxLabel(OutputStream output) {

    try {//from w  w w . j  av  a  2s.co  m

        PdfWriter writer = PdfWriter.getInstance(document, output);

        document.setPageSize(PageSize.LETTER);

        document.open();

        cb = writer.getDirectContent();

        // Set basic metadata
        document.addAuthor("Tellervo");
        document.addSubject("Tellervo Box Labels");

        PdfPTable table = new PdfPTable(2);
        table.setTotalWidth(495f);
        table.setLockedWidth(true);

        for (WSIBox b : boxlist) {
            Paragraph p = new Paragraph();

            p.add(new Chunk(b.getTitle() + Chunk.NEWLINE, labelTitleFont));
            p.add(new Chunk(Chunk.NEWLINE + b.getComments() + Chunk.NEWLINE, bodyFont));
            p.add(new Chunk(App.getLabName() + Chunk.NEWLINE + Chunk.NEWLINE, bodyFont));
            p.add(new Chunk(this.getBarCode(b), 0, 0, true));

            PdfPCell cell = new PdfPCell(p);
            cell.setPaddingLeft(15f);
            cell.setPaddingRight(15f);
            cell.setBorderColor(Color.LIGHT_GRAY);

            table.addCell(cell);

        }

        PdfPCell cell = new PdfPCell(new Paragraph());
        cell.setBorderColor(Color.LIGHT_GRAY);

        table.addCell(cell);
        document.add(table);
        document.close();

        /*float top = document.top(15);
        int row = 1;
                
        for(int i = 0; i< boxlist.size(); i = i+2)
        {
                   
           log.debug("Document left : "+document.left());
           log.debug("Document right: "+document.right());
           log.debug("Top           : "+top);
                   
                   
                   
                   
                   
                   
          // Column 1      
          ColumnText ct1a = new ColumnText(cb);
          ct1a.setSimpleColumn(document.left(), 
                   top-210, 
                   368, 
                   top, 
                   20, 
                   Element.ALIGN_LEFT);
                  
          ColumnText ct1b = new ColumnText(cb);
          ct1b.setSimpleColumn(document.left(), 
             top-70, 
             document.left()+206, 
             top-150, 
             20, 
             Element.ALIGN_LEFT);
                  
          try{
        WSIBox b1 = boxlist.get(i);
        ct1a.addText(getTitlePDF(b1));
        ct1a.go();
                     
                
        ct1b.addElement(getBarCode(b1));
        ct1b.go();
                
          } catch (Exception e)
          {
             log.debug("Failed writing box label in left column where i="+i);
          }
                  
                  
          // Column 2      
          ColumnText ct2a = new ColumnText(cb);
          ct2a.setSimpleColumn(306, 
                   top-210, 
                   document.right(), 
                   top, 
                   20, 
                   Element.ALIGN_LEFT);
                  
          ColumnText ct2b = new ColumnText(cb);
          ct2b.setSimpleColumn(306, 
             top-70, 
             512,  
             top-80, 
             20, 
             Element.ALIGN_LEFT);
                  
          try{
        WSIBox b2 = boxlist.get(i+1);
        ct2a.addText(getTitlePDF(b2));
        ct2a.go();
                     
                
        ct2b.addElement(getBarCode(b2));
        ct2b.go();
                
          } catch (Exception e)
          {
             log.debug("Failed writing box label in right column where i="+i);
             //e.printStackTrace();
          }
                  
                  
          // Column 2
        /*   ColumnText ct2 = new ColumnText(cb);
          ct2.setSimpleColumn(370,     //llx 
          top-100,            //lly   
          document.right(0),   //urx
          top+15,            //ury
          20,               //leading
          Element.ALIGN_RIGHT  //alignment
          );
                  
          try{
          WSIBox b2 = boxlist.get(i+1);
          ct2.addText(getTitlePDF(b2));
          ct2.addElement(getBarCode(b2));
          ct2.go();
          } catch (Exception e)
          {
             log.debug("Failed writing box label where i="+i+1);
          }
          */
        /*
                
        top = top-160;
                
        if(row==5)
        {
           top = document.top(15);
        document.newPage();
        row=1;
        }
        else
        {
           row++;
        }
                
                
                
                
                
        }*/

    } catch (DocumentException de) {
        System.err.println(de.getMessage());
    }

    // Close the document
    document.close();
}

From source file:sg.edu.nus.util.ReportWriter.java

public static String writeDataArrayToPdf(String userName, ArrayList<String[]> data, String title) {

    String rawFileName = userName + "_report.pdf";

    String fileName = ServerPeer.getReportFolderPath() + "/" + rawFileName;

    int ncol = data.get(0).length;

    Document document = new Document();
    try {//from  w w w .  j  ava 2  s.  c o m

        PdfWriter.getInstance(document, new FileOutputStream(fileName));
        document.open();

        /* Create the font. The font name must exactly match the font name on the client system. */
        Paragraph para = null;

        //         RtfFont embossedFont = new RtfFont("Times New Roman", 12,
        //               RtfFont.STYLE_EMBOSSED);

        // We add one empty line
        para = new Paragraph();
        addEmptyLine(para, 1);
        // Lets write a big header
        para.add(new Paragraph(title, catFont));

        addEmptyLine(para, 1);
        // Will create: Report generated by: _name, _date
        para.add(new Paragraph("Report generated by: " + userName + ", " + new Date(), //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                smallBold));
        addEmptyLine(para, 3);

        document.add(para);

        //add header and table content

        Font font8 = FontFactory.getFont(FontFactory.HELVETICA, 12);
        PdfPTable table = null;

        float columnPercentage = (float) (100.0 / ncol);
        float[] columnDefinitionSize = new float[ncol];
        for (int c = 0; c < ncol; c++) {
            columnDefinitionSize[c] = columnPercentage;
        }

        table = new PdfPTable(columnDefinitionSize);
        // table.getDefaultCell().setBorder(0);
        table.setHorizontalAlignment(0);
        float width = document.getPageSize().getWidth();
        table.setTotalWidth(width - 72);
        table.setLockedWidth(true);

        //write header
        String[] arr = data.get(0);
        for (int j = 0; j < ncol; j++) {
            table.addCell(new Phrase(arr[j], subFont));
        }

        //write data
        for (int i = 1; i < data.size(); i++) {
            arr = data.get(i);
            for (int j = 0; j < ncol; j++) {
                table.addCell(new Phrase(arr[j], font8));
            }
        }

        document.add(table);

    } catch (DocumentException de) {
        System.err.println(de.getMessage());
    } catch (IOException ioe) {
        System.err.println(ioe.getMessage());
    }
    document.close();

    return ServerPeer.getWebDownloadAddress() + "/" + rawFileName;
}

From source file:sg.edu.nus.util.ReportWriter.java

public static String writeDataArrayToRtf(String userName, ArrayList<String[]> data, String title) {

    String rawFileName = userName + "_report.rtf";

    String fileName = ServerPeer.getReportFolderPath() + "/" + rawFileName;

    int ncol = data.get(0).length;

    Document document = new Document();
    try {/*w ww.  j a va 2  s  .  c o  m*/

        RtfWriter2.getInstance(document, new FileOutputStream(fileName));
        document.open();

        Paragraph para = null;

        // We add one empty line
        para = new Paragraph();
        addEmptyLine(para, 1);
        // Lets write a big header
        para.add(new Paragraph(title, catFont));

        addEmptyLine(para, 1);
        // Will create: Report generated by: _name, _date
        para.add(new Paragraph("Report generated by: " + userName + ", " + new Date(), //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                smallBold));
        addEmptyLine(para, 3);

        document.add(para);
        //add header and table content

        Font font8 = FontFactory.getFont(FontFactory.HELVETICA, 12);
        PdfPTable table = null;

        float columnPercentage = (float) (100.0 / ncol);
        float[] columnDefinitionSize = new float[ncol];
        for (int c = 0; c < ncol; c++) {
            columnDefinitionSize[c] = columnPercentage;
        }

        table = new PdfPTable(columnDefinitionSize);
        // table.getDefaultCell().setBorder(0);
        table.setHorizontalAlignment(0);
        float width = document.getPageSize().getWidth();
        table.setTotalWidth(width - 72);
        table.setLockedWidth(true);

        //write header
        String[] arr = data.get(0);
        for (int j = 0; j < ncol; j++) {
            table.addCell(new Phrase(arr[j], subFont));
        }

        //write data
        for (int i = 1; i < data.size(); i++) {
            arr = data.get(i);
            for (int j = 0; j < ncol; j++) {
                table.addCell(new Phrase(arr[j], font8));
            }
        }

        document.add(table);

    } catch (DocumentException de) {
        System.err.println(de.getMessage());
    } catch (IOException ioe) {
        System.err.println(ioe.getMessage());
    }
    document.close();

    return ServerPeer.getWebDownloadAddress() + "/" + rawFileName;
}

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();/*from   w  w  w . j  a va  2s.  c  om*/
    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");

        }
    }
}

From source file:sms.ViewResults.java

private void itextPrint() {
    //  String searchQuery = "SELECT * FROM `exam` WHERE `yearid` ='" + yearid + "'AND `termid`='"+termid+"'AND `examid`='"+yearid+"'"
    //          + "AND CONCAT(`class`) LIKE '%" + stream + "%'AND YEAR(updated_at)='"+yearchooser.getYear()+"'";

    methods nn = new methods();
    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 {/*from  w  w w .j a  v  a  2s .  c o m*/
            Form1Exams n = new Form1Exams();
            Document pdfp = new Document();
            PdfWriter.getInstance(pdfp,
                    new FileOutputStream(new File(chooser.getSelectedFile(), "report.pdf")));
            pdfp.open();
            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;

            for (a = 0; a < names.length; a++) {
                String s = names[a];
                tbl1.addCell(s.substring(0, Math.min(s.length(), 3)));
            }

            tbl1.addCell("Tot");
            tbl1.addCell("Ave");
            tbl1.addCell("Agg");
            pdfp.add(tbl1);

            try {

                String[] Subjects = n.findSubjectid();
                String[] Subjectsnames = n.findSubjectname();
                int subjectCount = Subjects.length;
                ArrayList<ExamDbDataHolder> users = ListUsers(this.sid.getText());

                for (int i = 0; i < users.size(); i++) {
                    tbl.addCell(String.valueOf(i + 1));
                    String id = ((ExamDbDataHolder) users.get(i)).getSid();
                    String nam = nn.getStudentName(id);
                    String name = "Eric";
                    tbl.addCell(id);
                    tbl.addCell(nam);
                    int c = 2;
                    for (int s = 0; s < Subjects.length; s++) {

                        if (Subjects[s].equals("s1")) {
                            tbl.addCell(((ExamDbDataHolder) users.get(i)).getMathematics());
                            //  JOptionPane.showMessageDialog(null,((ExamDbDataHolder)users.get(i)).getMathematics());
                            // String maths = ((ExamDbDataHolder)users.get(i)).getMathematics();
                            c++;
                        } else if (Subjects[s].equals("s2")) {
                            tbl.addCell(((ExamDbDataHolder) users.get(i)).getEnglish());
                            //row[c] = ((ExamDbDataHolder)users.get(i)).getEnglish();
                            c++;
                        } else if (Subjects[s].equals("s3")) {
                            tbl.addCell(((ExamDbDataHolder) users.get(i)).getKiswahili());
                            // row[c] = ((ExamDbDataHolder)users.get(i)).getKiswahili();
                            c++;
                        } else if (Subjects[s].equals("s4")) {
                            tbl.addCell(((ExamDbDataHolder) users.get(i)).getPhysics());
                            //   row[c] = ((ExamDbDataHolder)users.get(i)).getPhysics();
                            c++;
                        } else if (Subjects[s].equals("s5")) {
                            tbl.addCell(((ExamDbDataHolder) users.get(i)).getChemistry());
                            // row[c] = ((ExamDbDataHolder)users.get(i)).getChemistry();
                            c++;
                        } else if (Subjects[s].equals("s6")) {
                            tbl.addCell(((ExamDbDataHolder) users.get(i)).getBiology());
                            //   row[c] = ((ExamDbDataHolder)users.get(i)).getBiology();
                            c++;
                        } else if (Subjects[s].equals("s7")) {
                            tbl.addCell(((ExamDbDataHolder) users.get(i)).getHistory());
                            //  row[c] = ((ExamDbDataHolder)users.get(i)).getHistory();
                            c++;
                        } else if (Subjects[s].equals("s8")) {
                            tbl.addCell(((ExamDbDataHolder) users.get(i)).getGeography());
                            // row[c] = ((ExamDbDataHolder)users.get(i)).getGeography();
                            c++;
                        } else if (Subjects[s].equals("s9")) {
                            tbl.addCell(((ExamDbDataHolder) users.get(i)).getCre());
                            //  row[c] = ((ExamDbDataHolder)users.get(i)).getCre();
                            c++;
                        } else if (Subjects[s].equals("s10")) {
                            tbl.addCell(((ExamDbDataHolder) users.get(i)).getIre());
                            //   row[c] = ((ExamDbDataHolder)users.get(i)).getIre();
                            c++;
                        } else if (Subjects[s].equals("s11")) {
                            tbl.addCell(((ExamDbDataHolder) users.get(i)).getHre());
                            //   row[c] = ((ExamDbDataHolder)users.get(i)).getHre();
                            c++;
                        } else if (Subjects[s].equals("s12")) {
                            tbl.addCell(((ExamDbDataHolder) users.get(i)).getAgriculture());
                            //   row[c] = ((ExamDbDataHolder)users.get(i)).getAgriculture();
                            c++;
                        } else if (Subjects[s].equals("s13")) {
                            tbl.addCell(((ExamDbDataHolder) users.get(i)).getHomescience());
                            //  row[c] = ((ExamDbDataHolder)users.get(i)).getHomescience();
                            c++;
                        } else if (Subjects[s].equals("s14")) {
                            tbl.addCell(((ExamDbDataHolder) users.get(i)).getArtdesign());
                            //   row[c] = ((ExamDbDataHolder)users.get(i)).getArtdesign();
                            c++;
                        } else if (Subjects[s].equals("s15")) {
                            tbl.addCell(((ExamDbDataHolder) users.get(i)).getComputer());
                            // row[c] = ((ExamDbDataHolder)users.get(i)).getComputer();
                            c++;

                        } else if (Subjects[s].equals("s16")) {
                            tbl.addCell(((ExamDbDataHolder) users.get(i)).getBuilding());
                            // row[c] = ((ExamDbDataHolder)users.get(i)).getBuilding();
                            c++;
                        } else if (Subjects[s].equals("s17")) {
                            tbl.addCell(((ExamDbDataHolder) users.get(i)).getWoodwork());
                            //  row[c] = ((ExamDbDataHolder)users.get(i)).getWoodwork();
                            c++;
                        } else if (Subjects[s].equals("s18")) {
                            tbl.addCell(((ExamDbDataHolder) users.get(i)).getMetalwork());
                            //  row[c] = ((ExamDbDataHolder)users.get(i)).getMetalwork();
                            c++;
                        } else if (Subjects[s].equals("s19")) {
                            tbl.addCell(((ExamDbDataHolder) users.get(i)).getMusic());
                            //   row[c] = ((ExamDbDataHolder)users.get(i)).getMusic();
                            c++;
                        } else if (Subjects[s].equals("s20")) {
                            tbl.addCell(((ExamDbDataHolder) users.get(i)).getFrench());
                            // row[c] = ((ExamDbDataHolder)users.get(i)).getFrench();
                            c++;
                        } else if (Subjects[s].equals("s21")) {
                            tbl.addCell(((ExamDbDataHolder) users.get(i)).getGerman());
                            //  row[c] = ((ExamDbDataHolder)users.get(i)).getGerman();
                            c++;
                        } else if (Subjects[s].equals("s22")) {
                            tbl.addCell(((ExamDbDataHolder) users.get(i)).getArabic());
                            // row[c] = ((ExamDbDataHolder)users.get(i)).getArabic();
                            c++;
                        } else if (Subjects[s].equals("s23")) {
                            tbl.addCell(((ExamDbDataHolder) users.get(i)).getBusiness());
                            //  row[c] = ((ExamDbDataHolder)users.get(i)).getBusiness();
                            c++;
                        }

                    }

                    int tt = ((ExamDbDataHolder) users.get(i)).getTotal();
                    tbl.addCell(String.valueOf(tt));
                    float g = Float.valueOf(tt) / 11;
                    tbl.addCell(String.format("%.1f", g));
                    String gr = nn.checkGrade(yearid, String.format("%.1f", g));
                    tbl.addCell(gr);

                }
                pdfp.add(tbl);
            } catch (Exception j) {
                j.printStackTrace();
            }

            pdfp.close();

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

        } catch (DocumentException ex) {
            Logger.getLogger(ViewResults.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(ViewResults.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}

From source file:storemanagment.Printing.java

public final void TransactionGiven(String transactionReceiptNo, String officer, ArrayList<CartPojo> items) {
    Calendar c = Calendar.getInstance();
    Date today = c.getTime();/*from  w ww . j  a  v  a2 s  .c o m*/

    java.util.Date d = (today);

    java.sql.Date DATE = new java.sql.Date(d.getTime());

    // String OrgDetails=res[1]+"\n"+DATE.toString();
    String orgDetails[] = getRes();
    String orgImg = orgDetails[0];
    String orgAbout = orgDetails[1];

    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(null) == JFileChooser.APPROVE_OPTION) {
        try {

            Document pdfp = new Document();
            PdfWriter writer = PdfWriter.getInstance(pdfp, new FileOutputStream(
                    new File(chooser.getSelectedFile(), transactionReceiptNo + "" + DATE + ".pdf")));

            // PdfWriter writer = PdfWriter.getInstance(pdfp, new FileOutputStream(new File(chooser.getSelectedFile(),"Group "+jComboBoxGroup.getSelectedItem().toString()+".pdf")));
            HeaderFooterPageEvent event = new HeaderFooterPageEvent(officer, transactionReceiptNo, DATE);
            writer.setPageEvent(event);
            pdfp.open();

            //        Document pdfp=new Document();
            //            PdfWriter.getInstance(pdfp, new FileOutputStream(new File(chooser.getSelectedFile(),transactionReceiptNo+""+DATE+".pdf")));
            //                pdfp.open();
            PdfPTable header1 = new PdfPTable(1);

            //                //  tbl.setWidthPercentage(100);
            //                
            header1.setTotalWidth(575);
            header1.setLockedWidth(true);
            header1.addCell(createTextCell(""));
            header1.addCell(createTextCell(""));
            header1.addCell(createTextCell(""));
            header1.addCell(createTextCell(""));
            PdfPTable header = new PdfPTable(3);

            //                //  tbl.setWidthPercentage(100);
            //                
            header.setTotalWidth(575);
            header.setLockedWidth(true);

            header.setWidths(new int[] { 1, 4, 1 });

            //THE FIRST ROW
            //first column ///Logo
            if (orgImg.equals("image")) {
                header.addCell("no image ");

            } else {
                header.addCell(createImageCell(orgImg));

            }
            //second column ///description
            header.addCell(createTextCell(orgAbout));

            header.addCell(createTextCell(""));

            PdfPTable RecieptTilte = new PdfPTable(3);

            //                //  tbl.setWidthPercentage(100);
            //                
            RecieptTilte.setTotalWidth(575);
            RecieptTilte.setLockedWidth(true);
            // RecieptTilte.setHorizontalAlignment(Align.CENTER);
            RecieptTilte.setWidths(new int[] { 1, 1, 1 });
            RecieptTilte.setSpacingBefore(8);
            RecieptTilte.setSpacingAfter(6);
            //       RecieptTilte.getDefaultCell().setBorderWidthBottom(2);
            RecieptTilte.getDefaultCell().setBorderWidthLeft(0);
            RecieptTilte.getDefaultCell().setBorderWidthRight(0);
            RecieptTilte.addCell("");

            RecieptTilte.addCell("Transaction Receipt");

            RecieptTilte.setSpacingAfter(8);
            RecieptTilte.addCell("");

            PdfPTable RecieptitemsTitles = new PdfPTable(3);

            RecieptitemsTitles.setTotalWidth(575);
            RecieptitemsTitles.setWidths(new int[] { 2, 1, 1 });

            RecieptitemsTitles.setLockedWidth(true);

            RecieptitemsTitles.addCell(creatTextCellTitles("Item-Name"));
            RecieptitemsTitles.addCell(creatTextCellTitles("Quantiy"));
            RecieptitemsTitles.addCell(creatTextCellTitles("Unit"));
            //    RecieptitemsTitles.addCell(creatTextCellTitles("Id"));

            //  PdfPTable Recieptitems=new PdfPTable(4);
            for (int a = 0; a < items.size(); a++) {
                RecieptitemsTitles.addCell(createTextCellcolor(((CartPojo) items.get(a)).getItem_name(), a));
                RecieptitemsTitles
                        .addCell(createTextCellcolor(((CartPojo) items.get(a)).getTransaction_quantity(), a));
                RecieptitemsTitles.addCell(
                        createTextCellcolor(((CartPojo) items.get(a)).getTransaction_quantity_in(), a));
                //  RecieptitemsTitles.addCell(createTextCellcolor(String.valueOf(((CartPojo)items.get(a)).getItem_id()),a));

            }

            pdfp.add(header1);
            pdfp.add(header);
            pdfp.add(RecieptTilte);
            pdfp.add(RecieptitemsTitles);

            pdfp.close();

        } catch (DocumentException ex) {
            Logger.getLogger(Printing.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Printing.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Printing.class.getName()).log(Level.SEVERE, null, ex);
        }
        // jComboBoxGroup
        if (Desktop.isDesktopSupported()) {
            try {
                File file = new File(chooser.getSelectedFile(), transactionReceiptNo + "" + DATE + ".pdf");
                Desktop.getDesktop().open(file);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

}