0

私のJavaクラスには次のコードセクションがあります。

if(exportTo.equals("pdf"))
        {
        response.setHeader("Content-disposition", "attachment; filename=\"" + reportName + ".pdf\"");
        response.setContentType("application/pdf");
        PdfWriter.getInstance(document,response.getOutputStream());

        try {
            document.open();
            addTitlePage(document, reportName);

            Image image = Image.getInstance(path+"/"+"images/abi.png");
            image.setAbsolutePosition(40f, 770f);
            image.scaleAbsolute(70f, 50f);
            document.add(image);
            response.flushBuffer();

            //float[] colsWidth = {1.5f,3f,4f,4f,2f};

            List<Float> colsWidth = new ArrayList<Float>();
            int iterator = 1;
           while (iterator <= headerMap.size()) {
               if(iterator==1){
                   colsWidth.add(1.5f); 
               }else{
                colsWidth.add(3f); 
               }
                iterator++;
            }
           float[] floatArray = ArrayUtils.toPrimitive(colsWidth.toArray(new Float[0]), 0.0F);

           PdfPTable table = new PdfPTable(floatArray);
            table.setWidthPercentage(98);
            table.setHorizontalAlignment(Element.ALIGN_CENTER);

            PdfPCell c1 = new PdfPCell();
            for (Iterator it = headerMap.keySet().iterator(); it.hasNext();) {
                String headerName = (String) headerMap.get(it.next());
                c1 = new PdfPCell(new Phrase(headerName, headerFont));
                c1.setBackgroundColor(BaseColor.LIGHT_GRAY);
                table.addCell(c1);
            }
            table.setHeaderRows(1);
            table = custDAO.creadPDFTable(query, table);
            document.add(table);
            document.addAuthor(userViewModel.getUsername());
            document.addCreationDate();
            document.addCreator("POC");
            document.close();
            response.flushBuffer();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        }

コードから次の行を削除しても例外は発生しませんが、これらの行を含めると例外が発生しjava.lang.IllegalStateException: getOutputStream() has already been called for this responseます。

 Image image = Image.getInstance(path+"/"+"images/abi.png");
                image.setAbsolutePosition(40f, 770f);
                image.scaleAbsolute(70f, 50f);
                document.add(image);
                response.flushBuffer();

上記のコードを使用してPDFファイルに画像を追加します(itextpdflibrary

これに対する解決策は何でしょうか。私は長い間これに直面しているので、私にアドバイスしてください。

よろしく。

4

2 に答える 2

0

多分それは次のような解決策になるでしょう:

試行の最後にすべてのドキュメントを配置してみてください。

try{
// create all object here, but don't add it to document yet.

// add all to document:
PdfWriter.getInstance(document,response.getOutputStream()); 
document.open();
addTitlePage(document, reportName);
document.add(image);
document.add(table);
document.addAuthor(userViewModel.getUsername());
document.addCreationDate();
document.addCreator("POC");
document.close();
response.flushBuffer();

} catch (Exception ex) {
  ex.printStackTrace();
}

例外から逃れる方法は他にありません

于 2012-09-21T01:22:36.897 に答える
0

ドキュメントに画像を追加するための別の方法があり、それを呼び出して、問題を解決しようとしています。

ご協力ありがとうございます。

Image image = Image.getInstance(path+"/"+"images/abi.png");
                image.setAbsolutePosition(40f, 770f);
                image.scaleAbsolute(70f, 50f);
                document.add(image);
                response.flushBuffer();
于 2012-09-22T06:14:53.867 に答える