我有以下代码打开PDF并添加一些文本和图像。有一次,我打开第二个PDF,将其包装在图像中并将其添加到PDF中。操作成功完成后,我想删除第二个PDF(我在图像中包含的那个)。问题是该文件现在已锁定,直到我重置我的ASP.NET应用程序。
在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);
}
答案 0 :(得分:1)
试试这个
using (PdfReader reader = new PdfReader(FilePath),PdfReader reader1 = new PdfReader(MapFilePath))
{
pass that reader1 object to AddMap method.
}