System.ObjectDisposedException:'无法访问已关闭的文件。'

时间:2017-07-15 14:53:20

标签: c# wpf itext

过去一周左右,这个例外让我头疼,我无法解决它的生活。我使用iTextSharp合并PDF文件,并在用户选择时添加水印。

以下是合并代码:

private void CreateMergedPdf(object sender, DoWorkEventArgs e)
{
        using (FileStream stream = new FileStream(pdfname, FileMode.Create)) {

            Document pdfDoc = new Document(PageSize.A4);
            PdfCopy pdf = new PdfCopy(pdfDoc, stream);

            pdfDoc.Open();
            int i = 0;


            foreach (File_class newpdf in AddedPDFs)
            {
                (sender as BackgroundWorker).ReportProgress(i++);

                if (newpdf.toMerge)
                {
                    PdfReader reader = new PdfReader(newpdf.file_path);
                    pdf.AddDocument(reader); //<!> Exception here

                    this.Dispatcher.Invoke(() => progBtxt.Text = "Merging file #" + newpdf.file_id + "..."); //Dispatcher.Invoke since UI is on seperate thread

                    if (add_wtrmk)//This is called for every FILE
                    {
                        AddWatermark(reader, stream);
                    }
                }


            }
     }
 }

以下是水印的代码:

 private void AddWatermark(PdfReader reader, FileStream stream)
    {
        using (PdfStamper pdfStamper = new PdfStamper(reader, stream))//This is called for every PAGE of the file
        {

            for (int pgIndex = 1; pgIndex <= reader.NumberOfPages; pgIndex++)
            {
                Rectangle pageRectangle = reader.GetPageSizeWithRotation(pgIndex);
                PdfContentByte pdfData; //Contains graphics and text content of page returned by pdfstamper


                if (this.Dispatcher.Invoke(() => dropdown.Text == "Under Content"))
                {
                    pdfData = pdfStamper.GetUnderContent(pgIndex);
                }
                else if (this.Dispatcher.Invoke(() => dropdown.Text == "Over Content"))
                {
                    pdfData = pdfStamper.GetOverContent(pgIndex);
                }
                else//Just in case

                {
                    MessageBox.Show("Something went wrong when adding the watermark");
                    return;
                }

                //Set font
                pdfData.SetFontAndSize(BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, BaseFont.NOT_EMBEDDED), 40);

                //Create new graphics state and assign opacity
                PdfGState graphicsState = new PdfGState();
                graphicsState.FillOpacity = 0.25F;

                //Set graphics state to pdfcontentbyte
                pdfData.SetGState(graphicsState);

                //Color of watermark
                pdfData.SetColorFill(BaseColor.GRAY);

                pdfData.BeginText();

                //Show text as per position and rotation
                this.Dispatcher.Invoke(() => pdfData.ShowTextAligned(Element.ALIGN_CENTER, WtrmkTextbox.Text, pageRectangle.Width / 2, pageRectangle.Height / 2, 45));

                pdfData.EndText();

            }
        }
    }

错误出现在合并代码上,特别是行&#34; pdf.AddDocument(读取器);&#34; 但是 只有当我尝试在多个文件上添加水印时才会出现此错误(只有一个文件可以完美运行)。

我想过早关闭某些内容,或者添加了标记()确实 - 我已尝试将使用状态更改为否无济于事。我一定错过了什么

1 个答案:

答案 0 :(得分:1)

好吧,似乎PdfStamper是罪魁祸首,我将必要的争论传递给AddWatermark()并添加了一个简单的if语句。现在一切都很完美。

非常感谢Mark Rucker