0
我有以下代碼可以打開PDF並添加一些文本和圖像。有一次,我打開第二個PDF文件,將它封裝在一個Image中並將其添加到PDF中。操作成功完成後,我想刪除第二個PDF(我包裝在圖像中的一個)。問題是該文件現在被鎖定,直到我重置我的ASP.NET應用程序。在iTextSharp的使用區塊內打開第二個PdfReader
在GetImageFromPdf方法中,您可以看到我正在創建第二個PdfReader實例。問題是,如果我關閉這個閱讀器,我的Create方法中的閱讀器也會關閉!難道我做錯了什麼?有另一種方法來實現我的目標嗎?
下面是相關代碼:
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);
}
這做到了。謝謝您的幫助! – 2013-03-23 14:13:50