VintaSoft Imaging .NET SDK 15.2: Документация для .NET разработчика
В этом разделе
PDF: Сохранение PDF документа
В этом разделе
Новый PDF документ можно сохранить в файл или поток.

Вот C#/VB.NET код, который демонстрирует, как создать новый PDF/A документ, добавить в него страницу и сохранить PDF документ в файл:
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



Сохранение измененного PDF документа

Измененный PDF документ может быть сохранен в новый файл или поток. SDK выполняет инкрементное сохранение изменений в существующих PDF документах, т.е. сохраняет историю изменений.

ВАЖНО! PDF документ должен быть загружен из потока, если необходимости изменить документ и сохранить его обратно в источник.

Вот C#/VB.NET код, который демонстрирует, как загрузить существующий PDF документ из файла, добавить страницу в документ и сохранить 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);
    }
}
''' <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



Вот C#/VB.NET код, который демонстрирует, как загрузить существующий PDF документ из потока, добавить страницу в документ и сохранить 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();
    }
}
''' <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



Упаковка и сохранение PDF документа

Измененный PDF документ может быть упакован перед сохранением в файл или поток. При упаковке удаляются ненужные объекты и уменьшается размер документа, но после упаковки история изменений в PDF документе будет потеряна.

Вот C#/VB.NET код, который демонстрирует, как загрузить существующий 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);
    }
}
''' <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