0

我已将 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);
}
4

1 回答 1

0

According to spire's documentation, you have two ways to do that

Using SaveToHttpResponse() method

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);

Or, if the built-in method doesn't work, try to use a memory stream instead of a temporary file.

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);
}
于 2019-04-29T21:09:29.967 回答