Save PDF file to Stream and Load PDF file from Stream in C#

2014-06-20 03:13:32 Written by  support iceblue
Rate this item
(0 votes)

As a standalone component, compatible with all .NET developing platforms, Spire.PDF for .NET enables developers to create, read, write, edit and handle PDF files without any external PDF reader or software its alike. In this section, I’ll introduce you how to create a PDF file and save it to stream, and how to load PDF file from stream on the contrary.

Part I. Create PDF file and save it to stream

Step 1: New a PDF instance.

PdfDocument doc = new PdfDocument();

Step 2: Create one page.

PdfPageBase page = doc.Pages.Add();

Step 3: Add text to that page.

page.Canvas.DrawString("Hello, World!",
                        new PdfFont(PdfFontFamily.Helvetica, 30f),
                        new PdfSolidBrush(Color.Black),
                        10, 10);

Step 4: Save PDF file to Stream.

FileStream to_strem = new FileStream("To_stream.pdf", FileMode.Open);
doc.SaveToStream(to_stream);
to_stream.Close();
doc.Close();

Part II. Load PDF file from stream

Step 1: New a PDF instance.

PdfDocument doc = new PdfDocument();

Step 2: Load PDF file from stream.

FileStream from_stream = File.OpenRead("sample.pdf");
doc.LoadFromStream(from_stream);

Step 3: Save the PDF document.

doc.SaveToFile("From_stream.pdf",FileFormat.PDF);
System.Diagnostics.Process.Start("From_stream.pdf");

Full Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spire.Pdf;
using System.IO;
using Spire.Pdf.Graphics;
using System.Drawing;

namespace PdfAndStream
{
    class Program
    {
        static void Main(string[] args)
        {
            //A: create PDF file and save it to stream

            //create a pdf document.
            PdfDocument doc = new PdfDocument();

            // create one page
            PdfPageBase page = doc.Pages.Add();

            //draw the text
            page.Canvas.DrawString("Hello, World!",
                                   new PdfFont(PdfFontFamily.Helvetica, 30f),
                                   new PdfSolidBrush(Color.Black),
                                   10, 10);   
    
            //save pdf file to Stream
            FileStream to_stream = new FileStream("To_stream.pdf", FileMode.Open);
            doc.SaveToStream(to_stream);
            to_stream.Close();
            doc.Close();

            System.Diagnostics.Process.Start("To_stream.pdf");

            
            //B: Load PDF file from Stream

            //create a pdf document.
            PdfDocument docFrom = new PdfDocument();

            //load PDF file from stream
            FileStream from_stream = File.OpenRead("sample.pdf");
            docFrom.LoadFromStream(from_stream);
            
            //save the pdf document
            docFrom.SaveToFile("From_stream.pdf",FileFormat.PDF);

            System.Diagnostics.Process.Start("From_stream.pdf");


        }
    }
}

Besides loading PDF document from stream, Spire.PDF also provide easy access to load PDF document from file and byte array. See Spire.PDF Program Guide, to get more info regarding processing PDF in C#, VB.NET.

Additional Info

  • tutorial_title: Save PDF file to Stream and Load PDF file from Stream
Last modified on Sunday, 26 September 2021 01:44