gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 04:09:22 24 4
gpt4 key购买 nike

我可以使用 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();
}
}

}

如何解决我的问题?

最佳答案

您无法将 OutputStream 打开到在外部应用程序(Adobe Reader 等)中打开的文件中。您可以做一些事情:

  • 为每次迭代创建新文件名(创建临时文件很便宜)
  • 在覆盖之前检查文件是否存在。如果存在,则创建后缀(_1、_2、...)并检查其不存在。
  • 在看到“请在创建新文件之前关闭 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;
}

关于java - 如果PDF文件已打开,如何关闭它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33937458/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com