Example usage for com.itextpdf.text Image scaleToFit

List of usage examples for com.itextpdf.text Image scaleToFit

Introduction

In this page you can find the example usage for com.itextpdf.text Image scaleToFit.

Prototype

public void scaleToFit(final float fitWidth, final float fitHeight) 

Source Link

Document

Scales the image so that it fits a certain width and height.

Usage

From source file:zzlorbittest3.PdfReporter.java

public void report(AccessInput ai, AccessOutput ao, String path, BufferedImage screenShot) {
    Document doc = new Document();
    try {//from w ww  .j  a v a 2  s.  c om
        PdfWriter.getInstance(doc, new FileOutputStream(path));
        doc.open();
        Image img;
        String str1, str2;
        addTitle(doc, "");

        /////////////////////////            1             ///////////////////////// 
        addSubTitle(doc, "");
        str1 = ":";
        for (SatelliteInput sli : ai.satelliteInput) {
            str1 += sli.satName + " ";
        }
        addContent(doc, str1);

        Time t1 = new Time(ai.startTime);
        Time t2 = new Time(ai.endTime);
        str1 = String.format("%d%d%d", t1.getBJ(Calendar.YEAR), t1.getBJ(Calendar.MONTH) + 1,
                t1.getBJ(Calendar.DAY_OF_MONTH));
        str2 = String.format("%d%d%d", t2.getBJ(Calendar.YEAR), t2.getBJ(Calendar.MONTH) + 1,
                t2.getBJ(Calendar.DAY_OF_MONTH));
        addContent(doc, ":" + str1 + "  :" + str2);

        /////////////////////////            2             ///////////////////////// 

        int len = ao.timeNodeArray.length;
        //            len=4;
        //            ao.progressArray[0]=0.6f;
        //            ao.progressArray[1]=0.3f;
        //            ao.progressArray[2]=0.9f;

        PdfPic pdfpic = new PdfPic();

        pdfpic.xArray = new float[len];
        pdfpic.xCoorStrArray = new String[len];

        Time t0 = new Time(ao.timeNodeArray[0]);

        //x 
        if (len >= 10) {
            for (int i = 0; i < len; i++) {
                Time t = new Time(ao.timeNodeArray[i]);
                pdfpic.xArray[i] = (t.getGregorianCalendar().getTime().getTime()
                        - t0.getGregorianCalendar().getTime().getTime()) / 1000.0f / 3600;
                if (t.getBJ(Calendar.DAY_OF_MONTH) == 1) {
                    pdfpic.xCoorStrArray[i] = Integer.toString(t.getBJ(Calendar.MONTH) + 1) + "";
                } else {
                    pdfpic.xCoorStrArray[i] = null;
                }
            }
        } else {
            for (int i = 0; i < len; i++) {
                Time t = new Time(ao.timeNodeArray[i]);
                pdfpic.xArray[i] = (t.getGregorianCalendar().getTime().getTime()
                        - t0.getGregorianCalendar().getTime().getTime()) / 1000.0f / 3600;
                if (t.getBJ(Calendar.DAY_OF_MONTH) == 1) {
                    pdfpic.xCoorStrArray[i] = Integer.toString(t.getBJ(Calendar.MONTH) + 1) + "";
                } else if (t.getBJ(Calendar.DAY_OF_MONTH) == 11) {
                    pdfpic.xCoorStrArray[i] = Integer.toString(t.getBJ(Calendar.MONTH) + 1) + "";
                } else if (t.getBJ(Calendar.DAY_OF_MONTH) == 21) {
                    pdfpic.xCoorStrArray[i] = Integer.toString(t.getBJ(Calendar.MONTH) + 1) + "";
                } else {
                    pdfpic.xCoorStrArray[i] = null;
                }
            }

        }

        addSubTitle(doc, "");

        pdfpic.yArray = new float[len];
        for (int i = 0; i < len - 1; i++) {
            for (int j = 0; j < 20; j++) {
                pdfpic.yArray[i] += ao.cloudArray[i][j] * ((1.0f / 20) * j + 0.025f);
            }
            pdfpic.yArray[i] /= ao.totalGrid;
        }
        img = pdfpic.getImage(PdfPic.PDFImageType.CLOUD);
        img.setAlignment(Element.ALIGN_CENTER);
        doc.add(img);

        /////////////////////////            3             ///////////////////////// 

        addSubTitle(doc, "");

        pdfpic.yArray = ao.progressArray;

        img = pdfpic.getImage(PdfPic.PDFImageType.PROGRESS);
        img.setAlignment(Element.ALIGN_CENTER);
        doc.add(img);

        str1 = String.format("%.1f", 100 * ao.progressArray[len - 2]);
        Time t = new Time(ao.timeNodeArray[len - 1]);
        str2 = String.format("%d%d%d", t.getBJ(Calendar.YEAR), 1 + t.getBJ(Calendar.MONTH),
                t.getBJ(Calendar.DAY_OF_MONTH));
        addContent(doc, "" + str1 + "%,:" + str2);

        /////////////////////////            5             ///////////////////////// 
        addSubTitle(doc, "");
        if (screenShot != null) {
            img = Image.getInstance(screenShot, Color.BLACK);
            img.scaleToFit(400, 500);
            img.setAlignment(1);
            doc.add(img);

        }
        str1 = "";
        addContent(doc, str1);

        /////////////////////////            6             ///////////////////////// 

        PdfPTable table;
        Paragraph para;
        PdfPCell cell;
        Font tableFont = new Font(songBaseFont, 8);

        addSubTitle(doc, "");

        table = new PdfPTable(3);
        table.setWidthPercentage(50);
        para = new Paragraph("", tableFont);
        para.setAlignment(Element.ALIGN_CENTER);
        cell = new PdfPCell(para);
        cell.setHorizontalAlignment(1);
        table.addCell(cell);

        para = new Paragraph("", tableFont);
        para.setAlignment(Element.ALIGN_CENTER);
        cell = new PdfPCell(para);
        cell.setHorizontalAlignment(1);
        table.addCell(cell);

        para = new Paragraph("", tableFont);
        para.setAlignment(Element.ALIGN_CENTER);
        cell = new PdfPCell(para);
        cell.setHorizontalAlignment(1);
        table.addCell(cell);

        for (int i = 0; i < ao.regionCoverageInfoArray.length; i++) {
            if (ao.regionCoverageInfoArray[i].regionCode == 0) {
                continue;
            }
            if (ao.regionCoverageInfoArray[i].groundTaskRegionTotalPoint <= 5) {
                continue;
            }
            str1 = ao.regionCoverageInfoArray[i].regionName;
            para = new Paragraph(str1, tableFont);
            para.setAlignment(Element.ALIGN_CENTER);
            cell = new PdfPCell(para);
            cell.setHorizontalAlignment(1);
            table.addCell(cell);

            str1 = String.format("%.1f%%", 100.0f * ao.regionCoverageInfoArray[i].groundTaskRegionTotalPoint
                    / ao.regionCoverageInfoArray[i].totalPoint);
            para = new Paragraph(str1, tableFont);
            para.setAlignment(Element.ALIGN_CENTER);
            cell = new PdfPCell(para);
            cell.setHorizontalAlignment(1);
            table.addCell(cell);

            str1 = String.format("%.1f%%", 100.0f * ao.regionCoverageInfoArray[i].coverageTimes
                    / ao.regionCoverageInfoArray[i].groundTaskRegionTotalPoint);
            para = new Paragraph(str1, tableFont);
            para.setAlignment(Element.ALIGN_CENTER);
            cell = new PdfPCell(para);
            cell.setHorizontalAlignment(1);
            table.addCell(cell);
        }
        doc.add(table);

        /////////////////////////            7             ///////////////////////// 
        addSubTitle(doc, "");
        String summaryString;
        str1 = String.format("%.1f", 100 * ao.progressArray[len - 2]);
        t = new Time(ao.timeNodeArray[len - 1]);
        str2 = String.format("%d%d%d", t.getBJ(Calendar.YEAR), 1 + t.getBJ(Calendar.MONTH),
                t.getBJ(Calendar.DAY_OF_MONTH));
        summaryString = "    "
                + (100.0f * ao.difficultyDegree[0] / ao.totalGrid
                        + 100.0f * ao.difficultyDegree[1] / ao.totalGrid >= 50f ? "" : "");
        addContent(doc, summaryString);
        summaryString = "    " + str1 + "  " + str2 + "";
        addContent(doc, summaryString);
        summaryString = String.format("    : %.1f%%,%.1f%%",
                100.0f * ao.difficultyDegree[0] / ao.totalGrid, 100.0f * ao.difficultyDegree[1] / ao.totalGrid);
        addContent(doc, summaryString);
        summaryString = String.format("    : %.1f%%,%.1f%%",
                100.0f * ao.difficultyDegree[2] / ao.totalGrid, 100.0f * ao.difficultyDegree[3] / ao.totalGrid);
        addContent(doc, summaryString);

        //            summaryString = "2";
        //            addContent(doc, summaryString);

        //////////////////////////////////////////////////////////////////////////////////////////////////////////
        /////////////////////////////////          1           ////////////////////////////////////////////
        //////////////////////////////////////////////////////////////////////////////////////////////////////////

        doc.newPage();
        this.addSubTitle(doc, "1 ");

        table = new PdfPTable(11);
        tableFont = new Font(songBaseFont, 8);
        table.addCell("");
        for (int i = 0; i < 100; i += 10) {
            String str = String.format("%02d%%-%02d%%", i, i + 10);
            para = new Paragraph(str, tableFont);
            para.setAlignment(Element.ALIGN_CENTER);
            cell = new PdfPCell(para);
            cell.setHorizontalAlignment(1);
            table.addCell(cell);
        }

        for (int i = 0; i < len - 1; i++) {

            t = new Time(ao.timeNodeArray[i]);
            int day = t.getBJ(Calendar.DAY_OF_MONTH);
            int mon = 1 + t.getBJ((Calendar.MONTH));
            String str = Integer.toString(mon) + "";
            if (i == 0) {
                if (day < 11) {
                    str += "";
                } else if (day < 21) {
                    str += "";
                } else {
                    str += "";
                }
            } else {
                if (day == 1) {
                    str += "";
                } else if (day == 11) {
                    str += "";
                } else if (day == 21) {
                    str += "";
                } else {
                    continue;
                }
            }

            para = new Paragraph(str, tableFont);
            para.setAlignment(Element.ALIGN_CENTER);
            cell = new PdfPCell(para);
            cell.setHorizontalAlignment(1);
            table.addCell(cell);
            for (int j = 0; j < 10; j++) {

                str = String.format("%.1f%%",
                        100.0f * (ao.cloudArray[i][2 * j] + ao.cloudArray[i][2 * j + 1]) / ao.totalGrid);
                para = new Paragraph(str, tableFont);
                para.setAlignment(Element.ALIGN_CENTER);
                cell = new PdfPCell(para);
                cell.setHorizontalAlignment(1);
                table.addCell(cell);
            }

        }

        doc.add(table);

        //////////////////////////////////////////////////////////////////////////////////////////////////////////
        /////////////////////////////////          2           ////////////////////////////////////////////
        //////////////////////////////////////////////////////////////////////////////////////////////////////////           

        doc.newPage();
        this.addSubTitle(doc, "2 ");

        float[] widthArray = new float[] { 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2 };
        table = new PdfPTable(widthArray);
        tableFont = new Font(songBaseFont, 8);
        table.addCell("");
        for (int i = 0; i < 13; i++) {
            String str = String.format("%02d", i);
            if (i == 12) {
                str = String.format("%02d", i);
            }
            para = new Paragraph(str, tableFont);
            para.setAlignment(Element.ALIGN_CENTER);
            cell = new PdfPCell(para);
            cell.setHorizontalAlignment(1);
            table.addCell(cell);
        }

        for (int i = 0; i < len - 1; i++) {

            t = new Time(ao.timeNodeArray[i]);
            int day = t.getBJ(Calendar.DAY_OF_MONTH);
            int mon = 1 + t.getBJ((Calendar.MONTH));
            String str = Integer.toString(mon) + "";
            if (i == 0) {
                if (day < 11) {
                    str += "";
                } else if (day < 21) {
                    str += "";
                } else {
                    str += "";
                }
            } else {
                if (day == 1) {
                    str += "";
                } else if (day == 11) {
                    str += "";
                } else if (day == 21) {
                    str += "";
                } else {
                    continue;
                }
            }

            para = new Paragraph(str, tableFont);
            para.setAlignment(Element.ALIGN_CENTER);
            cell = new PdfPCell(para);
            cell.setHorizontalAlignment(1);
            table.addCell(cell);
            for (int j = 0; j < 13; j++) {
                str = String.format("%.1f%%", 100.0f * ao.coverageTimesArray[i][j] / ao.totalGrid);
                if (j == 12) {
                    str = String.format("%.1f%%",
                            100.0f * (ao.coverageTimesArray[i][12] + ao.coverageTimesArray[i][13]
                                    + ao.coverageTimesArray[i][14] + ao.coverageTimesArray[i][15]
                                    + ao.coverageTimesArray[i][16] + ao.coverageTimesArray[i][17]
                                    + ao.coverageTimesArray[i][18] + ao.coverageTimesArray[i][19])
                                    / ao.totalGrid);
                }
                para = new Paragraph(str, tableFont);
                para.setAlignment(Element.ALIGN_CENTER);
                cell = new PdfPCell(para);
                cell.setHorizontalAlignment(1);
                table.addCell(cell);
            }

        }

        doc.add(table);
        doc.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}