PDF processing control Aspose.PDF function demonstration: converting MemoryStream and PDF into each other in C#

MemoryStream is often used due to its efficiency and ease of use. We can convert MemoryStream to PDF programmatically using C# or VB.NET to send files as well as PDF files to MemoryStream.

One of the many advantages of using a MemoryStream is that the system avoids the delays common when reading and writing files on disk or a FileStream. Additionally, because the content remains in memory, physical space on disk is not utilized. For example, if a file is loaded in a MemoryStream and further processing is required, there is no need to write the file to disk and then read it from there. Instead, file contents in a MemoryStream can be leveraged easily and quickly. Let us explore MemoryStream to File and File to MemoryStream conversion using C# under the following headings:

  • Convert MemoryStream to PDF file using C# or VB.NET
  • Convert PDF file to MemoryStream using C# or VB.NET

Click to download the latest version of Aspose.PDF icon-default.png?t=N7T8https://www.evget.com/product/565/download


Convert MemoryStream to PDF file using C# or VB.NET

MemoryStream can be converted to PDF files as needed. Let us consider an example where a source HTML file is loaded into a MemoryStream and then converted to a PDF file. The following steps are required:

  • Load input file in MemoryStream
  • Initialize the object of Document class
  • Save the output PDF file

The following code shows how to convert a MemoryStream to a PDF file in C# or VB.NET:

// Specify HtmlLoadOptions to load input file
HtmlLoadOptions options = new HtmlLoadOptions();
var fileName = dataDir + "Test.html";
string pdfFileName = dataDir + "Test.pdf";
Document document = null;

// Load input HTML file in MemoryStream
using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(File.ReadAllText(fileName))))
{
    // Initialize a doument from input MemoryStream
    document = new Document(ms, options);
    // Save the MemoryStream to PDF file
    document.Save(pdfFileName);
}

Convert PDF file to MemoryStream using C# or VB.NET

It may be necessary to save the file to a MemoryStream to avoid disk space usage and access delays. Here we convert PDF file to MemoryStream where the output format is presentation format file. Let us demonstrate this feature by following the steps below:

  • Load input file
  • Initialize the MemoryStream object
  • Write output to MemorySteam

The code below illustrates how to convert a PDF file to a MemoryStream in C# or VB.NET:

var inputFile = dataDir + @"Test.pdf";
string fnameppt = dataDir + "Test.pptx";
{
    Aspose.Pdf.Document pdfDoc = new Aspose.Pdf.Document(inputFile);
    using (MemoryStream pptStream = new MemoryStream())
    {
        pdfDoc.Save(pptStream, Aspose.Pdf.SaveFormat.Pptx);
        //File.WriteAllBytes(fnameppt, pptStream.ToArray());
    }
}

Guess you like

Origin blog.csdn.net/m0_67129275/article/details/133301530