VintaSoft Imaging .NET SDK v8.6
In This Topic
    PDF: Save PDF document
    In This Topic

    New PDF document can be saved to a file or stream.

    Here is an example that demonstrates how to create a new PDF/A document, add a page into the document and save it to a file:

    ' The project, which uses this code, must have references to the following assemblies:
    ' - Vintasoft.Imaging
    ' - Vintasoft.Imaging.Pdf
    
    Public Sub CreatePdfADocumentInFile(filename As String)
        ' create new PDF document version 1.4 in the memory
        Using document As New Vintasoft.Imaging.Pdf.PdfDocument(filename, Vintasoft.Imaging.Pdf.PdfFormat.Pdf_A)
            ' create an empty image
            Using image As New Vintasoft.Imaging.VintasoftImage(600, 800)
                ' add image to PDF document
                document.Pages.Add(image)
            End Using
    
            ' save document to a file
            document.SaveChanges()
        End Using
    End Sub
                  
    
    // The project, which uses this code, must have references to the following assemblies:
    // - Vintasoft.Imaging
    // - Vintasoft.Imaging.Pdf
    
    public void CreatePdfADocumentInFile(string filename)
    {
        // create new PDF document version 1.4 in the memory
        using (Vintasoft.Imaging.Pdf.PdfDocument document = new Vintasoft.Imaging.Pdf.PdfDocument(
            filename, Vintasoft.Imaging.Pdf.PdfFormat.Pdf_A))
        {
            // create an empty image
            using (Vintasoft.Imaging.VintasoftImage image = new Vintasoft.Imaging.VintasoftImage(600, 800))
            {
                // add image to PDF document
                document.Pages.Add(image);
            }
    
            // save document to a file
            document.SaveChanges();
        }
    }
                    
    



    Save changed PDF document

    Changed PDF document can be saved to a new file or stream. The SDK performs an incremental saving of changes in existing PDF documents, i.e. it is also saves the changes history.

    IMPORTANT! PDF document must be loaded from stream, in case it is necessary to change the document and save it back to the source.

    Here is an example that demonstrates how to load an existing PDF document from file, add a page to the document and save it to a new file:
    ' The project, which uses this code, must have references to the following assemblies:
    ' - Vintasoft.Imaging
    ' - Vintasoft.Imaging.Pdf
    
    ''' <summary>
    ''' Loads PDF document from a file, adds page to PDF document and
    ''' saves changes to the new file.
    ''' </summary>
    ''' <param name="sourcePdfFilename">The filename of source PDF document.</param>
    ''' <param name="destPdfFilename">The filename of destination PDF document.</param>
    Public Shared Sub OpenPdfDocumentAndSaveToNewFile(sourcePdfFilename As String, destPdfFilename As String)
        ' open PDF document
        Using document As New Vintasoft.Imaging.Pdf.PdfDocument(sourcePdfFilename)
            ' add new page to PDF document
            document.Pages.Add(Vintasoft.Imaging.PaperSizeKind.A4)
    
            ' save PDF document to the new file
            document.Save(destPdfFilename)
        End Using
    End Sub
                  
    
    // The project, which uses this code, must have references to the following assemblies:
    // - Vintasoft.Imaging
    // - Vintasoft.Imaging.Pdf
    
    /// <summary>
    /// Loads PDF document from a file, adds page to PDF document and
    /// saves changes to the new file.
    /// </summary>
    /// <param name="sourcePdfFilename">The filename of source PDF document.</param>
    /// <param name="destPdfFilename">The filename of destination PDF document.</param>
    public static void OpenPdfDocumentAndSaveToNewFile(
        string sourcePdfFilename,
        string destPdfFilename)
    {
        // open PDF document
        using (Vintasoft.Imaging.Pdf.PdfDocument document = 
            new Vintasoft.Imaging.Pdf.PdfDocument(sourcePdfFilename))
        {
            // add new page to PDF document
            document.Pages.Add(Vintasoft.Imaging.PaperSizeKind.A4);
    
            // save PDF document to the new file
            document.Save(destPdfFilename);
        }
    }
                    
    

    Here is an example that demonstrates how to load an existing PDF document from stream, add a page to the document and save it to source stream:
    ' The project, which uses this code, must have references to the following assemblies:
    ' - Vintasoft.Imaging
    ' - Vintasoft.Imaging.Pdf
    
    ''' <summary>
    ''' Loads PDF document from a stream, adds page to PDF document and
    ''' saves changes back to the source stream.
    ''' </summary>
    ''' <param name="pdfFilename">The filename of PDF document.</param>
    Public Shared Sub OpenChangeAndSavePdfDocumentFromStream(pdfFilename As String)
        ' open stream
        Using stream As System.IO.Stream = System.IO.File.Open(pdfFilename, System.IO.FileMode.Open, System.IO.FileAccess.ReadWrite)
            ' open PDF document
            Using document As New Vintasoft.Imaging.Pdf.PdfDocument(stream)
                ' add new page to PDF document
                document.Pages.Add(Vintasoft.Imaging.PaperSizeKind.A4)
    
                ' save changes to the source stream
                document.SaveChanges()
            End Using
    
            ' close the stream
            stream.Close()
        End Using
    End Sub
                  
    
    // The project, which uses this code, must have references to the following assemblies:
    // - Vintasoft.Imaging
    // - Vintasoft.Imaging.Pdf
    
    /// <summary>
    /// Loads PDF document from a stream, adds page to PDF document and
    /// saves changes back to the source stream.
    /// </summary>
    /// <param name="pdfFilename">The filename of PDF document.</param>
    public static void OpenChangeAndSavePdfDocumentFromStream(string pdfFilename)
    {
        // open stream
        using (System.IO.Stream stream = System.IO.File.Open(
            pdfFilename, System.IO.FileMode.Open, System.IO.FileAccess.ReadWrite))
        {
            // open PDF document
            using (Vintasoft.Imaging.Pdf.PdfDocument document = 
                new Vintasoft.Imaging.Pdf.PdfDocument(stream))
            {
                // add new page to PDF document
                document.Pages.Add(Vintasoft.Imaging.PaperSizeKind.A4);
    
                // save changes to the source stream
                document.SaveChanges();
            }
    
            // close the stream
            stream.Close();
        }
    }
                    
    



    Pack and save PDF document

    Changed PDF document can be packed before saving to a file or stream. The packing removes unnecessary objects and reduces the size of document, but after packing the changes history of PDF document will be lost.

    Here is an example that demonstrates how to load an existing PDF document from file, delete a page from document, pack and save the changed document to a new file:
    ' The project, which uses this code, must have references to the following assemblies:
    ' - Vintasoft.Imaging.Pdf
    
    ''' <summary>
    ''' Loads PDF document from a file,
    ''' removes the first page of PDF docuemnt and
    ''' packs PDF document to the new file.
    ''' </summary>
    ''' <param name="sourcePdfFilename">The filename of source PDF document.</param>
    ''' <param name="destPdfFilename">The filename of destination PDF document.</param>
    Public Shared Sub RemovePageFromPdfDocumentAndPackPdfDocument(sourcePdfFilename As String, destPdfFilename As String)
        ' open PDF document
        Using document As New Vintasoft.Imaging.Pdf.PdfDocument(sourcePdfFilename)
            ' remove the first page from PDF document
            document.Pages.RemoveAt(0)
    
            ' pack PDF document to the new file
            document.Pack(destPdfFilename)
        End Using
    End Sub
                  
    
    // The project, which uses this code, must have references to the following assemblies:
    // - Vintasoft.Imaging.Pdf
    
    /// <summary>
    /// Loads PDF document from a file,
    /// removes the first page of PDF docuemnt and
    /// packs PDF document to the new file.
    /// </summary>
    /// <param name="sourcePdfFilename">The filename of source PDF document.</param>
    /// <param name="destPdfFilename">The filename of destination PDF document.</param>
    public static void RemovePageFromPdfDocumentAndPackPdfDocument(
        string sourcePdfFilename,
        string destPdfFilename)
    {
        // open PDF document
        using (Vintasoft.Imaging.Pdf.PdfDocument document = 
            new Vintasoft.Imaging.Pdf.PdfDocument(sourcePdfFilename))
        {
            // remove the first page from PDF document
            document.Pages.RemoveAt(0);
    
            // pack PDF document to the new file
            document.Pack(destPdfFilename);
        }
    }
                    
    



    Errors and warnings which might appear while saving a PDF/A document

    The SDK would generate an error and wouldn't save a PDF/A document if the document includes nonstandard external fonts. The algorithm for embedding of external fonts into a PDF document will be implemented in further versions of the SDK.

    The SDK would generate a warning, if while saving of PDF/A document some its objects were changed to make them compatible with PDF/A standard.

    Here is an example that demonstrates how to review the warnings, which appeared while saving a PDF/A document:
    ' The project, which uses this code, must have references to the following assemblies:
    ' - Vintasoft.Imaging.Pdf
    
    ''' <summary>
    ''' Saves PDF document as a PDF/A document.
    ''' </summary>
    Public Shared Sub SaveAsPdfADocument(sourceFilename As String, destFilename As String, ByRef errors As Integer, ByRef warnings As Integer)
        ' load PDF document
        Using document As New Vintasoft.Imaging.Pdf.PdfDocument(sourceFilename)
            errors = 0
            warnings = 0
            ' clear runtime errors and warnings
            document.ClearRuntimeMessages()
    
            ' save PDF document as PDF/A document
            document.Pack(destFilename, Vintasoft.Imaging.Pdf.PdfFormat.Pdf_A)
    
            ' check runtime errors and warnings
            For Each message As Vintasoft.Imaging.Pdf.PdfRuntimeMessage In document.RuntimeMessages
                If TypeOf message Is Vintasoft.Imaging.Pdf.PdfRuntimeWarning Then
                    warnings += 1
                ElseIf TypeOf message Is Vintasoft.Imaging.Pdf.PdfRuntimeError Then
                    errors += 1
                End If
            Next
        End Using
    End Sub
                  
    
    // The project, which uses this code, must have references to the following assemblies:
    // - Vintasoft.Imaging.Pdf
    
    /// <summary>
    /// Saves PDF document as a PDF/A document.
    /// </summary>
    public static void SaveAsPdfADocument(
        string sourceFilename, string destFilename, out int errors, out int warnings)
    {
        // load PDF document
        using (Vintasoft.Imaging.Pdf.PdfDocument document = 
            new Vintasoft.Imaging.Pdf.PdfDocument(sourceFilename))
        {
            errors = 0;
            warnings = 0;
            // clear runtime errors and warnings
            document.ClearRuntimeMessages();
    
            // save PDF document as PDF/A document
            document.Pack(destFilename, Vintasoft.Imaging.Pdf.PdfFormat.Pdf_A);
    
            // check runtime errors and warnings
            foreach (Vintasoft.Imaging.Pdf.PdfRuntimeMessage message in document.RuntimeMessages)
            {
                if (message is Vintasoft.Imaging.Pdf.PdfRuntimeWarning)
                    warnings++;
                else if (message is Vintasoft.Imaging.Pdf.PdfRuntimeError)
                    errors++;
            }
        }
    }