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 PDF document 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 if PDF document must be changed and saved 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 PDF document 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 PDF document 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 changed PDF 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