我想使用iTextSharp创建PDF文档,并直接在应用程序中预览。这不仅是一次,而且在用户运行期间,当用户对文本输入进行更改时,它的频率是我喜欢的。
到目前为止,它是可行的,但是正如我之前只说过一次,当程序启动时。当我尝试再次生成PDF文件时,出现错误消息,该进程无法访问已保存的PDF文档,因为该文件当前正在被另一个进程使用。
我已经尝试阻止访问,但是到目前为止没有成功。
private void CreateDocument()
{
//my attempt to stop the browser from blocking the file acces
if (browser.IsBusy())
{
browser.Stop();
}
doc = new Document(PageSize.A4);
writer = PdfWriter.GetInstance(doc, new FileStream("document.pdf", FileMode.Create));
doc.Open();
cb = writer.DirectContent;
//here is the actual pdf generation
doc.Close();
//this is the part where I set the pdf document reference from the web browser
browser.Navigate(@"path\document.pdf");
}
实际错误发生在我设置PDFwriter实例的位置。
我已经从iTextSharp的工具箱中找到了页面预览组件,但遗憾的是没有关于如何使用它的参考。使用该功能可能比使用网络浏览器更容易。
如果您不介意闪烁,则在尝试保存之前,先导航到“ about:blank”。
[如果您对此有疑问,请制作该文件的临时副本,然后使用浏览器打开该副本。可能不是最好的解决方案,但应该可以工作