0

I have PDF document saved into my PDFs folder. I have created one function whose duty is to load the PDF into PdfDocument class, add some styles on runtime, save it back as temporary file and preview it in WebClient. My logic is working absolutely fine. I want to eliminate saving it back as temporary file. I want to directly preview it without saving, is it possible? I searched online but didn't get any good source. Following is my code:

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);
}
Armaan Labib
  • 125
  • 1
  • 2
  • 16

1 Answers1

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);
}
William
  • 249
  • 3
  • 10