如何在不保存的情况下以编程方式打开PDF文档?

问题描述 投票:0回答:1

我将PDF文档保存到我的PDF文件夹中。我创建了一个函数,其职责是将PDF加载到PdfDocument类,在runtime上添加一些样式,将其保存为临时文件并在WebClient中预览。我的逻辑非常好。我想消除它作为临时文件保存。我想直接预览它而不保存,是否可能?我在网上搜索但没有得到任何好的消息来源。以下是我的代码:

PdfDocument pdf = new PdfDocument();
pdf.LoadFromFile("MyFile.pdf");
pdf.SaveToFile("ModifiedMyFile.pdf"); // Eliminate this part
WebClient User = new WebClient();
Byte[] FileBuffer = User.DownloadData("ModifiedMyFile.pdf");
if (FileBuffer != null)
{
  Response.ContentType = "application/pdf";
  Response.AddHeader("content-length", FileBuffer.Length.ToString());
  Response.BinaryWrite(FileBuffer);
}
c# pdf web spire
1个回答
0
投票

根据spire的文档,您有两种方法可以做到这一点

使用SaveToHttpResponse()方法

https://www.e-iceblue.com/Tutorials/Spire.PDF/Spire.PDF-Program-Guide/How-to-Create-PDF-Dynamically-and-Send-it-to-Client-Browser-Using-ASP.NET.html

PdfDocument pdf = new PdfDocument();
pdf.LoadFromFile("MyFile.pdf");

.... edit the document

pdf.SaveToHttpResponse("sample.pdf",this.Response, HttpReadType.Save);

或者,如果内置方法不起作用,请尝试使用内存流而不是临时文件。

https://www.e-iceblue.com/Tutorials/Spire.PDF/Spire.PDF-Program-Guide/Document-Operation/Save-PDF-file-to-Stream-and-Load-PDF-file-from-Stream-in-C-.NET.html

PdfDocument pdf = new PdfDocument();

.... edit the document

using (MemoryStream ms = new MemoryStream())
{
  pdfDocument.SaveToStream(ms);

  Byte[] bytes = ms.ToArray();

  Response.ContentType = "application/pdf";
  Response.AddHeader("content-length", bytes.Length.ToString());
  Response.BinaryWrite(bytes);
}
© www.soinside.com 2019 - 2024. All rights reserved.