如果PDF文件已打开,如何关闭PDF文件?

时间:2015-11-26 11:26:53

标签: java pdf itext

我可以使用Java中的iText库创建PDF文件。这工作,但现在我有这个问题,我执行方法,然后我有一个PDF文件,它的名称是file.pdf,它没关系。现在我重新调用函数来创建PDF文件,它的名字是file.pdf但是现在,如果文件刚刚打开,我就有错误。

FileNotFoundException:Impossible to access at file

这是好的,所以我希望如果文件是打开的,从代码中关闭文件然后重新创建文件并打开它。

这是我创建PDF文件的代码:

public static void printFile(String nomeFolder, String nomeFile,List<Articoli> listaArticoli, boolean aprire)throws DocumentException{
    String folderName = DateUtil.getDataGiornaliera();
    nomeFolder = (new StringBuilder()).append(nomeFolder).append(nomeFile+"_"+folderName).append(".pdf").toString();
    File f = new File(nomeFolder);
    try {
        OutputStream os = new FileOutputStream(f);
        Document doc = new Document(PageSize.A4.rotate(), -65F, -65F, 85F, 40F);

        PdfWriter docWriter = PdfWriter.getInstance(doc, os);

        EndPageFoglioFatturaFatt hp2 = new EndPageFoglioFatturaFatt(nomeFolder, "Img/ineco1.jpg",folderName);
        docWriter.setPageEvent(hp2);

        FooterDocumenti hp = new FooterDocumenti(nomeFolder, "Img/ineco1.jpg",folderName);
        docWriter.setPageEvent(hp);

        doc.open();

        float[] colonne = {0.7f,1.5f,4.5f,0.5f,0.5f,1.5f,1.5f,1.5f};

        PdfPTable table = new PdfPTable(colonne);
        table.setHeaderRows(1);

        String[] intestazioni = {"C.ART","C.BARRE", "NOME ARTICOLO","IVA(%)", "Q.TA",
                "PR.VENDITA", "ULT.PR.VENDITA", "ULT.DATA ACQUISTO"};
        PdfPCell  cell = new PdfPCell();
        for(int i = 0; i< intestazioni.length; i++){
            cell = new PdfPCell(new Paragraph(intestazioni[i], FontFactory.getFont("Century Gothic", 8F)));
            cell.setGrayFill(0.9F);
            cell.setUseAscender(true);
            cell.setHorizontalAlignment(1);
            cell.setVerticalAlignment(5);
            cell.setBorderWidth(0.5F);
            cell.setFixedHeight(15F);
            table.addCell(cell);
        }

        //Vector v = db.eseguiQueryTuttiArticoli("SELECT ARTICOLIDETT.CodArticolo,NomeArticolo,Iva, Quantita,PrezzoAttuale, PrezzoRivenditore, PrezzoIngrosso,  Soglia FROM Articolidett,Articoliquantita WHERE ARTICOLIDETT.CODARTICOLO = ARTICOLIQUANTITA.CODARTICOLO ORDER BY NOMEARTICOLO");
        for (Articoli articoli : listaArticoli) {
            cell = new PdfPCell(new Paragraph(articoli.getCodArticoloString(), FontFactory.getFont("Century Gothic", 10F)));
            cell.setVerticalAlignment(5);
            cell.setHorizontalAlignment(0);
            cell.setColspan(0);
            cell.setBorderWidth(0.5F);
            table.addCell(cell);

            ....
            ....
            CODE TO BUILD A FILE
            .....
            .....
        }
        doc.add(table);
        doc.close();
        os.close();

    } catch (FileNotFoundException e) {
        log.logStackTrace(e);
        VisualMessageStampe.getErroreFileAperto();
    }
    catch(IOException exp)
    {
        log.logStackTrace(exp);
    }
    catch(DocumentException exp2)
    {
        log.logStackTrace(exp2);
    }

    if(aprire)
    {
        if(Desktop.isDesktopSupported())
        {
            try
            {
                Desktop.getDesktop().open(f.getCanonicalFile());
            }
            catch(IOException ex)
            {
                log.logStackTrace(ex);
            }
        } else
        {
            VisualMessageStampe.getErroreAcrobatInesistente();
        }
    }

}

我如何解决问题?

1 个答案:

答案 0 :(得分:2)

您无法在外部应用程序(Adobe Reader或其他内容)中打开的文件中打开OutputStream。你可以做的一些事情:

  • 为每次迭代创建新文件名(创建临时文件很便宜)
  • 在进行覆盖之前检查文件是否存在。如果存在则创建后缀(_1,_2,...)并检查它是否不存在。
  • 在您看到该消息后提醒用户&#34;请在创建新&#34;
  • 之前关闭PDF文件

这样的事情可能有所帮助:

protected File getFile(String nomeFile, String nomeFolder) {
      String folderName = DateUtil.getDataGiornaliera();
      nomeFolder = (new StringBuilder()).append(nomeFolder).append(nomeFile+"_"+folderName).append(".pdf").toString();
      File f = new File(nomeFolder);
      int suffix = 1;
      while(f.exists()) {
          nomeFolder = (new StringBuilder()).append(nomeFolder).append(nomeFile+"_"+folderName+"_"+(suffix++)).append(".pdf").toString();
          f = new File(nomeFolder);
      }
      return f;
    }