收到此错误:升级到itext 5.5.13 jar时不平衡的保存/还原状态运算符

时间:2018-06-27 11:26:19

标签: itext

此代码用于在我的应用程序中生成pdf,并在升级到itexpdf 5.5.13后引发错误

boolean generatePDF(String title, List<String[]> tableVals, String fileName) {
    try {
        if(dir == null || dir.length() == 0){
            outFile = File.createTempFile("report", ".pdf");
        }else{
            outFile = new File(dir,fileName+".pdf");
        }
    } catch (IOException e1) {
        log.warn("Could not create a temporary output file");
    }
    setHeaders();
    int numRows = tableVals.size();
    Rectangle paperTypeRectangle = pageTypeMap.get(pdfSettings.getPaperType());
    if(pdfSettings.getOrientation() == ReportPDFSettings.LANDSCAPE) {
        paperTypeRectangle = paperTypeRectangle.rotate();
    }
    Document doc = new Document(paperTypeRectangle,50,50,50,50);
    FileOutputStream itOS = null;
    try {
        itOS = new FileOutputStream(outFile);
    } catch (FileNotFoundException e) {
        log.warn("unable to open output file:"+ outFile);
        return false;
    }
    PdfWriter writer;
    try {
        writer = PdfWriter.getInstance(doc, itOS);
        if(addPagination){
            writer.setPageEvent(new OurItextPdfPageEventHelper()); // adds page footer
        }
        doc.open();
        Image logo = null;
        boolean foundLogo = true;
        try {
            logo = Image.getInstance(pdfSettings.getLogoFile());
        } catch (IOException e) {
            foundLogo = false;
            log.info("unable to find/read logo file:" + pdfSettings.getLogoFile());
        }
        if(foundLogo) {
            logo.setAlignment(Image.LEFT);
            logo.scalePercent(pdfSettings.getLogoSizing());
            doc.add(logo);
        }
        Font headingFont = FontFactory.getFont(FontFactory.HELVETICA_BOLD,18,Font.BOLD);
        String heading = pdfSettings.getHeading();
        heading = heading.replace(enteredNewLine,newLine);
        Paragraph rptHeading = new Paragraph(heading,headingFont);
        rptHeading.setAlignment(Element.ALIGN_CENTER);
        doc.add(rptHeading);

        Font titleFont = FontFactory.getFont(FontFactory.HELVETICA,14);
        Paragraph rptTitle = new Paragraph(title,titleFont);
        rptTitle.setAlignment(Element.ALIGN_CENTER);
        doc.add(rptTitle);

        // add the table
        Font tableFont = FontFactory.getFont(FontFactory.HELVETICA,8);
        Paragraph tableP = new Paragraph("",tableFont);
        doc.add(tableP);
        PdfPTable table = new PdfPTable(numCols);
        if(colSizePcts != null) {
            if( colSizePcts.length == 6){
                //Fixed issue FM-781
                int totalWidth = 33;
                colSizePcts = new int[6];
                colSizePcts[0] = totalWidth + 3;
                colSizePcts[1] = totalWidth;
                colSizePcts[2] = totalWidth;
                colSizePcts[3] = totalWidth - 2;
                colSizePcts[4] = totalWidth - 1;
                colSizePcts[5] = totalWidth;
            }
            table.setWidths(colSizePcts);
        }
        table.getDefaultCell().setPadding(3);
        table.getDefaultCell().setBorderWidth(2);
        table.getDefaultCell().setHorizontalAlignment(
                Element.ALIGN_CENTER);
        table.setWidthPercentage(100);
        table.getDefaultCell().setGrayFill(0.9f);
        for (int col=0; col<numCols; col++) {
            table.addCell(headers[col]);
        }
        table.setHeaderRows(1); // this is the end of the table header
        table.getDefaultCell().setGrayFill(1);
        Phrase value;
        table.getDefaultCell().setBorderWidth(1);
        String[] row;
        for(int rowNum = 0; rowNum<numRows; rowNum++){
            row = tableVals.get(rowNum);
            for(int colNum = 0; colNum<numCols; colNum++) {
                value = new Phrase(row[colNum],tableFont);
                table.addCell(value);
            }
        }
        table.setSpacingBefore(25f);
        doc.add(table);
        table.setSpacingBefore(10f);

        // add the count
        String count = pdfSettings.getCountText() + " " + numRows;
        count = count.replace(enteredNewLine,newLine);
        Font countFont = FontFactory.getFont(FontFactory.HELVETICA_BOLD,8,Font.BOLD);
        Paragraph countP = new Paragraph(count,countFont);
        doc.add(countP);
        doc.close();
    } catch (DocumentException e) {
        log.warn("Unable to generate PDF file:" + e.getMessage());
        return false;
    } finally{
        try {
            itOS.close();
        } catch (IOException e) {
            log.error("Error: ", e);
        }
    }
    return true;
}

0 个答案:

没有答案