合并PDF后如何解锁删除文件

时间:2016-03-10 12:39:27

标签: c# pdf itextsharp

更新

请参阅完整解决方案的答案。

我现在更新了代码,以便最后几行读取:

       reader.Close();
        f++;
        if (f >= orderedList.Count) continue;
        reader = new PdfReader(targetFolder + orderedList[f]);
        // we retrieve the total number of pages
        n = reader.NumberOfPages;
        pdfNewPageFlag = true;
       reader.Close();
    }
}

文档已成功完成,但最终的PDF已损坏(我假设因为它没有正确关闭)

作为Word-to-PDF转换和合并过程的一部分,我运行以下方法将所有PDF合并为1个超级PDF(带iTextSharp.text.pdf):

    public void Pdfs(string targetPdf, List<string> orderedList, string targetFolder, string spSite)
    {
        var f = 0;
        // we create a reader for a certain document
        var reader = new PdfReader(targetFolder + orderedList[f]);
        // we retrieve the total number of pages
        var n = reader.NumberOfPages;
        // step 1: creation of a document-object
        var document = new iTextSharp.text.Document(reader.GetPageSizeWithRotation(1));

        // step 2: we create a writer that listens to the document
        var writer = PdfWriter.GetInstance(document, new FileStream(targetPdf, FileMode.Create));
        // step 3: we open the document
        document.Open();
        var cb = writer.DirectContent;
        PdfImportedPage page;
        int rotation;
        // step 4: we add content
        var pdfPageName = 0;
        var pdfNewPageFlag = true;
        while (f < orderedList.Count)
        {
            var i = 0;
            while (i < n)
            {
                i++;
                document.SetPageSize(reader.GetPageSizeWithRotation(i));
                document.NewPage();
                if (pdfNewPageFlag)
                {
                    var chapter = new iTextSharp.text.Chapter(orderedList[pdfPageName], pdfPageName + 1);
                    document.Add(chapter);
                    pdfNewPageFlag = false;
                    pdfPageName++;
                }

                page = writer.GetImportedPage(reader, i);
                rotation = reader.GetPageRotation(i);

                if (rotation == 90 || rotation == 270)
                {
                    cb.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(i).Height);
                }
                else
                {
                    cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
                }
            }
            f++;
            if (f >= orderedList.Count) continue;
            reader = new PdfReader(targetFolder + orderedList[f]);
            // we retrieve the total number of pages
            n = reader.NumberOfPages;
            pdfNewPageFlag = true;
        }

        // step 5: we close the document
        document.Close();
    }

此方法还会在每个原始文档的开头创建一个书签。一旦文档全部合并,我调用此方法删除文件:

    private static void DeletePdfs()
    {
        // Delete the files
        foreach (var file in orderedListPdfFileNames)
        {
            var newFileName = tempfolder + file;
            File.Delete(newFileName);
        }
    }

但是在尝试实际删除文件时,我看到了这个异常:

  

mscorlib.dll中出现未处理的“System.IO.IOException”类型异常

     

其他信息:该进程无法访问文件'\ file \ IT \ SK \ test \ HrDocs \ Cover.pdf',因为它正由另一个进程使用。

PDFs方法中,我尝试在writer.Close()之前和之后调用reader.Close()document.Close(),但之后我收到了消息Cannot close a document which is already closed。< / p>

是否有人能够帮助解决这个问题或者指出我正确的方向?我似乎无法弄清楚Merge进程的哪个部分正在锁定文件!

1 个答案:

答案 0 :(得分:3)

这只是一个不在正确位置关闭读者的案例。

正如评论中提到的那样,如果document.Close()在关闭读者后失败,那么我需要在之后关闭读者。

在下面的修订中,我在创建后将每个读者添加到列表中,并且最后都将它们关闭:

    public void Pdfs(string targetPdf, List<string> orderedList, string targetFolder, string spSite)
    {
        var readerlist = new List<PdfReader>();

        var f = 0;
        // we create a reader for a certain document
        var reader = new PdfReader(targetFolder + orderedList[f]);
        readerlist.Add(reader);

        // we retrieve the total number of pages
        var n = reader.NumberOfPages;
        // step 1: creation of a document-object
        var document = new iTextSharp.text.Document(reader.GetPageSizeWithRotation(1));

        // step 2: we create a writer that listens to the document
        var writer = PdfWriter.GetInstance(document, new FileStream(targetPdf, FileMode.Create));
        // step 3: we open the document
        document.Open();
        var cb = writer.DirectContent;
        PdfImportedPage page;
        int rotation;
        // step 4: we add content
        var pdfPageName = 0;
        var pdfNewPageFlag = true;
        while (f < orderedList.Count)
        {
            var i = 0;
            while (i < n)
            {
                i++;
                document.SetPageSize(reader.GetPageSizeWithRotation(i));
                document.NewPage();
                if (pdfNewPageFlag)
                {
                    var chapter = new iTextSharp.text.Chapter(orderedList[pdfPageName], pdfPageName + 1);
                    document.Add(chapter);
                    pdfNewPageFlag = false;
                    pdfPageName++;
                }

                page = writer.GetImportedPage(reader, i);
                rotation = reader.GetPageRotation(i);

                if (rotation == 90 || rotation == 270)
                {
                    cb.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(i).Height);
                }
                else
                {
                    cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
                }
            }

            f++;
            if (f >= orderedList.Count) continue;
            reader = new PdfReader(targetFolder + orderedList[f]);
            readerlist.Add(reader);

            // we retrieve the total number of pages
            n = reader.NumberOfPages;
            pdfNewPageFlag = true;
        }

        // step 5: we close the document
        document.Close();

        foreach (var rdr in readerlist)
        {
            rdr.Close();
        }
    }