VintaSoft PDF .NET Plug-in Discussions
Questions, comments and suggestions concerning VintaSoft PDF .NET Plug-in.
Board index < VintaSoft Imaging < VintaSoft PDF .NET Plug-in Discussions
using (PdfDocument pageDocument = new PdfDocument())
{
// create interactive form in PDF document
pageDocument.InteractiveForm = new PdfDocumentInteractiveForm(pageDocument);
// Get the first page from the source document.
PdfPage sourcePage = sourceDocument.Pages[0];
// Get the list of interactive form fields.
PdfInteractiveFormField[] fieldList = sourceDocument.InteractiveForm.GetFieldsLocatedOnPage(sourcePage);
// Add the first page of the newly created document. I would expect that the form fields would be added.
pageDocument.Pages.Add(sourcePage);
// Try to add the form fields to the Interactive form
foreach(PdfInteractiveFormField formField in fieldList)
{
// Remove field from the source document.
sourceDocument.InteractiveForm.RemoveField(formField);
// Error when adding the field.
pageDocument.InteractiveForm.AddField(formField, pageDocument.Pages.Last());
}
// Remove the first page from the source document.
sourceDocument.Pages.Remove(sourcePage);
...
...
}
private static FileStream CopyPageToDocument(PdfDocument sourceDocument, List<int> extractedPages)
{
FileStream temporaryParentStream = null;
using (new StopwatchLog(string.Format($"Copying {extractedPages.Count} page(s).")))
{
// create document and copy from source.
using (PdfDocument pageDocument = new PdfDocument())
{
// Create copy command from the newly created page document.
PdfDocumentCopyCommand documentCopyCommand = new PdfDocumentCopyCommand(pageDocument)
{
CopyBookmarks = true,
CopyDocumentLevelJavaScripts = true,
CopyInteractiveForm = true
};
// Execute copy from the source document to the target document.
documentCopyCommand.Execute(sourceDocument);
// Create a list of pages that will be kept in the source document.
// The extractedPages list is 1 based. Keep both lists 1 based.
List<int> pagesToKeep = new List<int>();
for (int pageIndex = 1; pageIndex <= sourceDocument.Pages.Count(); pageIndex++)
{
if (extractedPages.Exists(item => item == pageIndex) == false)
{
pagesToKeep.Add(pageIndex);
}
}
// Remove the extracted pages from the source document.
extractedPages.Reverse();
foreach (int pageIndex in extractedPages)
{
// Check to make sure the InteractiveForm exist. There are scenarios where it is null
if (sourceDocument.InteractiveForm != null)
{
// Get a list of fields that belong to the pages that are not part of the new document and remove them.
PdfInteractiveFormField[] pageFields = sourceDocument.InteractiveForm.GetFieldsLocatedOnPage(sourceDocument.Pages[pageIndex - 1]);
foreach (PdfInteractiveFormField field in pageFields)
{
field.Remove();
}
}
// Remove unwanted pages.
sourceDocument.Pages.RemoveAt(pageIndex - 1);
}
// Remove the pages that are kept in the source document from the copied document.
pagesToKeep.Reverse();
foreach (int pageIndex in pagesToKeep)
{
// Check to make sure the InteractiveForm exist. There are scenarios where it is null
if (pageDocument.InteractiveForm != null)
{
// Get a list of fields that belong to the pages that are kept with the source document and remove them from the copied document.
PdfInteractiveFormField[] pageFields = pageDocument.InteractiveForm.GetFieldsLocatedOnPage(pageDocument.Pages[pageIndex - 1]);
foreach (PdfInteractiveFormField field in pageFields)
{
field.Remove();
}
}
// Remove unwanted pages.
pageDocument.Pages.RemoveAt(pageIndex - 1);
}
// Return the copied document as a file stream.
temporaryParentStream = new FileStream(FileUtilities.GetTemporaryFile(FileUtilities.TemporarySubFolders.FileProcessing), FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite, 4096, FileOptions.DeleteOnClose);
pageDocument.SaveChanges(temporaryParentStream);
}
}
return temporaryParentStream;
}
using System;
using System.Collections.Generic;
using System.IO;
using Vintasoft.Imaging.Pdf;
using Vintasoft.Imaging.Pdf.Processing;
using Vintasoft.Imaging.Pdf.Tree;
using Vintasoft.Imaging.Pdf.Tree.Annotations;
using Vintasoft.Imaging.Pdf.Tree.InteractiveForms;
namespace SplitPagesConsole
{
class Program
{
static void Main(string[] args)
{
List<string> splitPageDocuments = SplitAllPages("test.pdf");
}
/// <summary>
/// Splits all pages of the pdf file into separate documents.
/// </summary>
/// <param name="pdfFile">The source PDF file.</param>
/// <returns>An array of file names for the newly created files.</returns>
private static List<string> SplitAllPages(string pdfFile)
{
List<string> pages = new List<string>();
using (PdfDocument document = new PdfDocument(pdfFile))
{
for (int i = 0; i < document.Pages.Count; i++)
{
Console.Write("Processing page {0}...", i + 1);
string outputFile = string.Format("{0}_p{1}.pdf", Path.GetFileNameWithoutExtension(pdfFile), i + 1);
CopyPageToDocument(document, i, outputFile);
pages.Add(outputFile);
Console.WriteLine("done.");
}
}
return pages;
}
/// <summary>
/// Create a document from a page from the source document.
/// </summary>
/// <param name="sourceDocumentFilename">The source document that the copy is made from.</param>
/// <param name="extractingPageIndex">The page that to be separated from the source document.</param>
/// <param name="outputfile">The filename to save the extracted page to.</param>
private static void CopyPageToDocument(PdfDocument sourceDocument, int extractingPageIndex, string outputfile)
{
using (PdfDocument outputDocument = new PdfDocument())
{
PdfDocumentCopyCommand copyCommand = new PdfDocumentCopyCommand(outputDocument);
copyCommand.CopyInteractiveForm = true;
copyCommand.CopyDocumentLevelJavaScripts = true;
copyCommand.CopyBookmarks = true;
copyCommand.PageIndexes = new int[] { extractingPageIndex };
copyCommand.Execute(sourceDocument);
outputDocument.Save(outputfile);
}
}
}
}
Best regards, Alexander
using System;
using System.Collections.Generic;
using System.IO;
using Vintasoft.Imaging.Pdf;
using Vintasoft.Imaging.Pdf.Processing;
using Vintasoft.Imaging.Pdf.Tree;
using Vintasoft.Imaging.Pdf.Tree.Annotations;
using Vintasoft.Imaging.Pdf.Tree.InteractiveForms;
namespace SplitPagesConsole
{
class Program
{
static void Main(string[] args)
{
string documentFile = "test.pdf";
// Get the filepath of the document to be split.
documentFile = AppDomain.CurrentDomain.BaseDirectory + documentFile;
Console.WriteLine();
// Set Timer.
System.Diagnostics.Stopwatch timer = new System.Diagnostics.Stopwatch();
timer.Start();
List<string> splitPageDocuments = SplitAllPages(documentFile);
timer.Stop();
Console.WriteLine();
Console.WriteLine($"Execution time to split multi page pdf document to {splitPageDocuments.Count} seperate pdf documents: {timer.Elapsed.TotalSeconds} seconds.");
Console.ReadKey();
}
/// <summary>
/// Splits all pages of the pdf file into separate documents.
/// </summary>
/// <param name="pdfFile">The source PDF file.</param>
/// <returns>An array of file names for the newly created files.</returns>
private static List<string> SplitAllPages(string pdfFile)
{
List<string> pages = new List<string>();
using (PdfDocument document = new PdfDocument(pdfFile))
{
for (int i = 0; i < document.Pages.Count; i++)
{
Console.Write("Processing page {0}...", i + 1);
string outputFile = string.Format("{0}_p{1}.pdf", Path.GetFileNameWithoutExtension(pdfFile), i + 1);
CopyPageToDocument(document, i, outputFile);
pages.Add(outputFile);
Console.WriteLine("done.");
}
}
return pages;
}
/// <summary>
/// Create a document from a page from the source document.
/// </summary>
/// <param name="sourceDocumentFilename">The source document that the copy is made from.</param>
/// <param name="extractingPageIndex">The page that to be separated from the source document.</param>
/// <param name="outputfile">The filename to save the extracted page to.</param>
private static void CopyPageToDocument(PdfDocument sourceDocument, int extractingPageIndex, string outputfile)
{
using (PdfDocument outputDocument = new PdfDocument())
{
PdfDocumentCopyCommand copyCommand = new PdfDocumentCopyCommand(outputDocument);
copyCommand.CopyInteractiveForm = true;
copyCommand.CopyDocumentLevelJavaScripts = true;
copyCommand.CopyBookmarks = true;
copyCommand.PageIndexes = new int[] { extractingPageIndex };
copyCommand.Execute(sourceDocument);
outputDocument.Pack(outputfile, PdfFormat.Pdf_17);
}
}
}
}
New code creates PDF document with size 135Kb. Previous code created PDF document with size 5.200Kb.