title: Opening a second PdfReader within a using block in iTextSharp
body: <p>I have the following code which opens a PDF and adds some text and images. At one point, I am opening a second PDF, wrapping it in an Image and adding it to the PDF. After the operation completes successfully, I want to delete the second PDF (the one I wrapped in an Image). The problem is that the file is now locked until I reset my ASP.NET application. </p>
  
  <p>In the GetImageFromPdf method, you can see that I am creating a second PdfReader instance. The problem is, if I close this reader, the reader in my Create method is also closed! Am I doing something wrong? Is there another way to achieve my goal?</p>
  
  <p>Here is the relevant code:</p>
  
  <pre><code>public void Create(string outputFilePath, bool preview = false)
  {
      using (PdfReader reader = new PdfReader(FilePath))
      {
          using (PdfStamper stamper = new PdfStamper(reader, new FileStream(outputFilePath, FileMode.Create)))
          {
              PdfContentByte canvas = stamper.GetOverContent(1);
  
              ...
  
              AddMap(canvas, stamper);
          }
      }
  }
  
  ...
  
  private void AddMap(PdfContentByte Canvas, PdfStamper stamper)
  {
      Image ImageWrapper = GetImageFromPdf(stamper.Writer, _MapFilePath);
  
      //set the position and scale the image
      ImageWrapper.ScaleAbsolute(152.5f, 152.5f);
      ImageWrapper.SetAbsolutePosition(58.5f, 197.5f);
  
      Canvas.AddImage(ImageWrapper);
  }
  
  ...
  
  public Image GetImageFromPdf(PdfWriter Writer, string MapFilePath)
  {
      //since vector images are not supported natively by iTextSharp
      //we have saved the location maps as PDF (originally in eps format) 
      //we use GetImportedPage to import the file, and wrap it in an Image object so we can scale it
  
      //If I close this reader, the reader in the Create method is closed as well
      PdfReader reader = new PdfReader(MapFilePath);
      PdfImportedPage ImportedPage = Writer.GetImportedPage(reader, 1);
  
      return Image.GetInstance(ImportedPage);
  }
  </code></pre>