Save PDF Files in C#

GdPicture.NET SDK offers different ways to save the PDF documents loaded to the GdPicturePDF class. Our saving methods allow you to save a PDF document to a file or a Stream object. The incremental saving feature allows you to save larger PDF documents more quickly. Our library also supports encrypting PDF documents with multiple advanced options.

To save a PDF document to a file on your local machine, use the SaveToFile method of the GdPicturePDF class. This method uses the following parameters:

  • FilePath — The file path where you want to save the PDF document. If the specified file already exists, the method overwrites it.

  • PackDocument — Optional: A Boolean value that specifies if the PDF is packed before saving it to a file. Packing reduces the file size but slows down the saving process.

  • Linearize — Optional: A Boolean value that specifies if the PDF has the Fast Web View mode enabled.

Packing a PDF document before saving it removes unused PDF objects and compresses used objects. This process creates a new PDF document by cloning all existing pages of the current document to a new document. You can also combine this mechanism with the RemoveUnusedResources method to delete all unused resources from your PDF document.

The Fast Web View mode makes it possible to download the content one page at a time from the web. This feature is useful for large documents.

To save a PDF document to a file with the packing and the Fast Web View mode enabled, use the following code:

using GdPicturePDF gdPicturePDF = new GdPicturePDF();
// Load a PDF document to GdPicture.
gdPicturePDF.LoadFromFile(@"C:\temp\source.pdf");
// Save the loaded PDF document to a file.
gdPicturePDF.SaveToFile(@"C:\temp\output.pdf", true, true);
Using gdPicturePDF As GdPicturePDF = New GdPicturePDF()
    ' Load a PDF document to GdPicture.
    gdPicturePDF.LoadFromFile("C:\temp\source.pdf")
    ' Save the loaded PDF document to a file.
    gdPicturePDF.SaveToFile("C:\temp\output.pdf", True, True)
End Using

Saving a Password-Protected PDF

Warning

Encrypting isn’t allowed for PDF/A documents.

You can set two password types for a PDF document:

  • User password (open password) — Allows opening and viewing a PDF document.

  • Owner password (master password or permissions password) — Allows a user to copy, edit, or print a PDF document.

Both password types can be set for the same PDF. This means that there are four possible scenarios:

  • No passwords are set — Anyone can open and edit a PDF document.

  • User password is set — The PDF document can be opened after providing the password. After opening the file, the user can then edit the PDF document.

  • Owner password is set — The PDF document can be opened without the password. The user may have limited access to printing, editing, and copying the content depending on the permissions settings, unless they provide the owner password to remove those restrictions.

  • Both the user password and owner password are set — The file can only be opened by providing either the user or the owner password. With the user password, access to printing, editing, and copying content might be restricted depending on the permission settings. With the owner password, the user has no restrictions and can print, edit, and copy the content.

To save a password-protected PDF document to a file on your local machine, use the SaveToFile method of the GdPicturePDF class with the following parameters:

  • FilePath — The file path where you want to save the PDF document. If the specified file already exists, the method will overwrite it.

  • EncryptionScheme — A member of the PdfEncryption enumeration that specifies which encryption algorithm to use.

  • UserPass — The user password (open password), which allows opening and viewing a PDF document. If you don’t want to set the user password, use an empty string.

  • OwnerPass — The owner password (master password or permissions password), which allows a user to copy, edit, or print a PDF document. If you don’t want to set the owner password, use an empty string.

  • CanPrint — A Boolean value that specifies if a user is able to print the PDF document.

  • CanCopy — A Boolean value that specifies if a user is able to copy or extract text and graphics from the PDF document.

  • CanModify — A Boolean value that specifies if a user is able to modify the PDF document.

  • CanAddNotes — A Boolean value that specifies if a user is able to add annotations to the PDF document.

  • CanFillFields — A Boolean value that specifies if a user is able to fill in form fields in the PDF document. This parameter only works with 128-bit encryption.

  • CanCopyAccess — A Boolean value that specifies if a user is able to copy or extract content from the PDF document using the accessibility features. This parameter only works with 128-bit encryption.

  • CanAssemble — A Boolean value that specifies if a user is able to assemble the PDF document. This parameter only works with 128-bit encryption.

  • CanPrintFull — A Boolean value that specifies if a user is able to print the PDF document in high resolution. This parameter only works with 128-bit encryption.

using GdPicturePDF gdPicturePDF = new GdPicturePDF();
// Load a PDF document to GdPicture.
gdPicturePDF.LoadFromFile(@"C:\temp\source.pdf");
// Save the loaded PDF document to a password-protected file.
gdPicturePDF.SaveToFile(@"C:\temp\output.pdf", PdfEncryption.PdfEncryption128BitAES, "", "owner",
    CanPrint: true,
    CanCopy: true,
    CanModify: false,
    CanAddNotes: false,
    CanFillFields: true,
    CanCopyAccess: false,
    CanAssemble: false,
    CanPrintFull: false);
Using gdPicturePDF As GdPicturePDF = New GdPicturePDF()
    ' Load a PDF document to GdPicture.
    gdPicturePDF.LoadFromFile("C:\temp\source.pdf")
    ' Save the loaded PDF document to a password-protected file.
    gdPicturePDF.SaveToFile("C:\temp\output.pdf", PdfEncryption.PdfEncryption128BitAES, "", "owner",
        CanPrint:=True,
        CanCopy:=True,
        CanModify:=False,
        CanAddNotes:=False,
        CanFillFields:=True,
        CanCopyAccess:=False,
        CanAssemble:=False,
        CanPrintFull:=False)
End Using

If you have a large PDF file and you want to modify only a small part of the PDF, use the SaveToFileInc method of the GdPicturePDF class. This method allows the content of the PDF document to be updated incrementally without rewriting it entirely. As a result, small changes to a larger PDF document are saved more quickly.

The following code draws a black rectangle on the first page of a PDF document and saves the PDF document incrementally to a file:

using GdPicturePDF gdPicturePDF = new GdPicturePDF();
// Load a PDF document to GdPicture.
gdPicturePDF.LoadFromFile(@"C:\temp\source.pdf");
// Select the first page.
gdPicturePDF.SelectPage(1);
// Set the fill color to black.
gdPicturePDF.SetFillColor(0, 0, 0);
// Draw a filled rectangle on the entire page.
gdPicturePDF.DrawRectangle(
    Left: 0, Top: 0,
    Width: gdPicturePDF.GetPageWidth(),
    Height: gdPicturePDF.GetPageHeight(),
    Fill: true,
    Stroke: false);
// Save the PDF document incrementally.
gdPicturePDF.SaveToFileInc(@"C:\temp\source.pdf");
Using gdPicturePDF As GdPicturePDF = New GdPicturePDF()
    ' Load a PDF document to GdPicture.
    gdPicturePDF.LoadFromFile("C:\temp\source.pdf")
    ' Select the first page.
    gdPicturePDF.SelectPage(1)
    ' Set the fill color to black.
    gdPicturePDF.SetFillColor(0, 0, 0)
    ' Draw a filled rectangle on the entire page.
    gdPicturePDF.DrawRectangle(
        Left:=0, Top:=0,
        Width:=gdPicturePDF.GetPageWidth(),
        Height:=gdPicturePDF.GetPageHeight(),
        Fill:=True,
        Stroke:=False)
    ' Save the PDF document incrementally.
    gdPicturePDF.SaveToFileInc("C:\temp\source.pdf")
End Using

To save a PDF document to a Stream object, use the SaveToStream method of the GdPicturePDF class. This method uses the following parameters:

  • Stream — The Stream object where you want to save the PDF document.

  • PackDocument — Optional: A Boolean value that specifies if the PDF is packed before saving it to a Stream object. Packing reduces the file size but slows down the saving process.

  • Linearize — Optional: A Boolean value that specifies if the PDF has the Fast Web View mode enabled. For more information, refer to the linearize PDF guide.

Packing a PDF document before saving it removes unused PDF objects and compresses used objects. This process creates a new PDF document by cloning all existing pages of the current document to a new document. You can also combine this mechanism with the RemoveUnusedResources method to delete all unused resources from your PDF document.

The Fast Web View mode makes it possible to download the content one page at a time from the web. This feature is useful for large documents.

To save a PDF document to a Stream object with the packing and the Fast Web View mode enabled, use the following code:

using GdPicturePDF gdPicturePDF = new GdPicturePDF();
// Create an empty `Stream` object.
MemoryStream streamPDF = new MemoryStream();
// Load a PDF document to GdPicture.
gdPicturePDF.LoadFromFile(@"C:\temp\source.pdf");
// Save the loaded PDF document to the stream.
gdPicturePDF.SaveToStream(streamPDF, true, false);
Using gdPicturePDF As GdPicturePDF = New GdPicturePDF()
    ' Create an empty `Stream` object.
    Dim streamPDF As MemoryStream = New MemoryStream()
    ' Load a PDF document to GdPicture.
    gdPicturePDF.LoadFromFile("C:\temp\source.pdf")
    ' Save the loaded PDF document to the stream.
    gdPicturePDF.SaveToStream(streamPDF, True, False)
End Using

Saving a Password-Protected PDF to a Stream

Warning

Encrypting isn’t allowed for PDF/A documents.

You can set two password types for a PDF document:

  • User password (open password) — Allows opening and viewing a PDF document.

  • Owner password (master password or permissions password) — Allows a user to copy, edit, or print a PDF document.

Both password types can be set for the same PDF. This means that there are four possible scenarios:

  • No passwords are set — Anyone can open and edit a PDF document.

  • User password is set — The PDF document can be opened after providing the password. After opening the file, the user can then edit the PDF document.

  • Owner password is set — The PDF document can be opened without the password. The user may have limited access to printing, editing, and copying the content depending on the permissions settings, unless they provide the owner password to remove those restrictions.

  • Both the user password and owner password are set — The file can only be opened by providing either the user or the owner password. With the user password, access to printing, editing, and copying content might be restricted depending on the permission settings. With the owner password, the user has no restrictions and can print, edit, and copy the content.

To save a password-protected PDF document to a Stream object, use the SaveToStream method of the GdPicturePDF class with the following parameters:

  • Stream — The Stream object you want to save the PDF document to.

  • EncryptionScheme — A member of the PdfEncryption enumeration that specifies which encryption algorithm to use.

  • UserPass — The user password (open password), which allows opening and viewing a PDF document. If you don’t want to set the user password, use an empty string.

  • OwnerPass — The owner password (master password or permissions password), which allows a user to copy, edit, or print a PDF document. If you don’t want to set the owner password, use an empty string.

  • CanPrint — A Boolean value that specifies if a user is able to print the PDF document.

  • CanCopy — A Boolean value that specifies if a user is able to copy or extract text and graphics from the PDF document.

  • CanModify — A Boolean value that specifies if a user is able to modify the PDF document.

  • CanAddNotes — A Boolean value that specifies if a user is able to add annotations to the PDF document.

  • CanFillFields — A Boolean value that specifies if a user is able to fill in form fields in the PDF document. This parameter only works with 128-bit encryption.

  • CanCopyAccess — A Boolean value that specifies if a user is able to copy or extract content from the PDF document using the accessibility features. This parameter only works with 128-bit encryption.

  • CanAssemble — A Boolean value that specifies if a user is able to assemble the PDF document. This parameter only works with 128-bit encryption.

  • CanPrintFull — A Boolean value that specifies if a user is able to print the PDF document in high resolution. This parameter only works with 128-bit encryption.

using GdPicturePDF gdPicturePDF = new GdPicturePDF();
// Create an empty `Stream` object.
MemoryStream streamPDF = new MemoryStream();
// Load a PDF document to GdPicture.
gdPicturePDF.LoadFromFile(@"C:\temp\source.pdf");
// Save the loaded PDF document to the stream with password-protection enabled.
gdPicturePDF.SaveToStream(streamPDF, PdfEncryption.PdfEncryption128BitAES, "", "owner",
    CanPrint: true,
    CanCopy: true,
    CanModify: false,
    CanAddNotes: false,
    CanFillFields: true,
    CanCopyAccess: false,
    CanAssemble: false,
    CanPrintFull: false);
Using gdPicturePDF As GdPicturePDF = New GdPicturePDF()
    ' Create an empty `Stream` object.
    Dim streamPDF As MemoryStream = New MemoryStream()
    ' Load a PDF document to GdPicture.
    gdPicturePDF.LoadFromFile("C:\temp\source.pdf")
    ' Save the loaded PDF document to the stream with password-protection enabled.
    gdPicturePDF.SaveToStream(streamPDF, PdfEncryption.PdfEncryption128BitAES, "", "owner",
        CanPrint:=True,
        CanCopy:=True,
        CanModify:=False,
        CanAddNotes:=False,
        CanFillFields:=True,
        CanCopyAccess:=False,
        CanAssemble:=False,
        CanPrintFull:=False)
End Using

If you have a large PDF document in a Stream object and you want to modify only a small part of that PDF, use the SaveToFileInc method of the GdPicturePDF class. This method allows the content of the PDF document to be updated incrementally without rewriting it entirely. As a result, small changes to a larger PDF document are saved more quickly.

The following code draws a black rectangle on the first page of a PDF document and saves it incrementally to a stream:

using GdPicturePDF gdPicturePDF = new GdPicturePDF();
// Create a `Stream` object from a PDF document.
Stream streamPDF = new FileStream(@"C:\temp\source.pdf", FileMode.Open);
// Load a PDF document from the stream.
gdPicturePDF.LoadFromStream(streamPDF);
// Select the first page.
gdPicturePDF.SelectPage(1);
// Set the fill color to black.
gdPicturePDF.SetFillColor(0, 0, 0);
// Draw a filled rectangle on the entire page.
gdPicturePDF.DrawRectangle(
    Left: 0, Top: 0,
    Width: gdPicturePDF.GetPageWidth(),
    Height: gdPicturePDF.GetPageHeight(),
    Fill: true,
    Stroke: false);
// Save the PDF document incrementally.
gdPicturePDF.SaveToStreamInc(streamPDF);
Using gdPicturePDF As GdPicturePDF = New GdPicturePDF()
    ' Create a `Stream` object from a PDF document.
    Dim streamPDF As Stream = New FileStream("C:\temp\source.pdf", FileMode.Open)
    ' Load a PDF document from the stream.
    gdPicturePDF.LoadFromStream(streamPDF)
    ' Select the first page.
    gdPicturePDF.SelectPage(1)
    ' Set the fill color to black.
    gdPicturePDF.SetFillColor(0, 0, 0)
    ' Draw a filled rectangle on the entire page.
    gdPicturePDF.DrawRectangle(
        Left:=0, Top:=0,
        Width:=gdPicturePDF.GetPageWidth(),
        Height:=gdPicturePDF.GetPageHeight(),
        Fill:=True,
        Stroke:=False)
    ' Save the PDF document incrementally.
    gdPicturePDF.SaveToStreamInc(streamPDF)
End Using