VintaSoft Imaging .NET SDK 12.4: Documentation for .NET developer
In This Topic
    PDF: Save PDF document
    In This Topic
    New PDF document can be saved to a file or stream.

    Here is C#/VB.NET code that demonstrates how to create a new PDF/A document, add a page into the document and save it to a file:
    public static void CreatePdfADocumentInFile(string pdfFilename, string pageImage)
    {
        // create new PDF document version 1.4
        using (Vintasoft.Imaging.Pdf.PdfDocument document = new Vintasoft.Imaging.Pdf.PdfDocument(
            pdfFilename, Vintasoft.Imaging.Pdf.PdfFormat.Pdf_14))
        {
            // create an image
            using (Vintasoft.Imaging.VintasoftImage image = new Vintasoft.Imaging.VintasoftImage(pageImage))
            {
                // add image to PDF document
                document.Pages.Add(image);
            }
    
            // convert documen to PDF/A-1b
            document.ConvertDocument(Vintasoft.Imaging.Pdf.PdfDocumentConformance.PdfA_1b);
        }
    }
    
    Public Shared Sub CreatePdfADocumentInFile(pdfFilename As String, pageImage As String)
        ' create new PDF document version 1.4
        Using document As New Vintasoft.Imaging.Pdf.PdfDocument(pdfFilename, Vintasoft.Imaging.Pdf.PdfFormat.Pdf_14)
            ' create an image
            Using image As New Vintasoft.Imaging.VintasoftImage(pageImage)
                ' add image to PDF document
                document.Pages.Add(image)
            End Using
    
            ' convert documen to PDF/A-1b
            document.ConvertDocument(Vintasoft.Imaging.Pdf.PdfDocumentConformance.PdfA_1b)
        End Using
    End Sub
    



    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 C#/VB.NET code that demonstrates how to load an existing PDF document from file, add a page to the document and save it to a new file:
    /// <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);
        }
    }
    
    ''' <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
    



    Here is C#/VB.NET code that demonstrates how to load an existing PDF document from stream, add a page to the document and save it to source stream:
    /// <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();
        }
    }
    
    ''' <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
    



    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 C#/VB.NET code 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:
    /// <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);
        }
    }
    
    ''' <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
    



    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 C#/VB.NET code that demonstrates how to review the warnings, which appeared while saving a PDF/A document:
    /// <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++;
            }
        }
    }
    
    ''' <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