PDF: Как разделить PDF документ?
В этом разделе
Вот C#/VB.NET код, который демонстрирует, как разделить PDF документ на несколько PDF документов:
public static void SplitPDFDocuments(string sourceDocumentFilename, params string[] resultDocumentsFilenames)
{
if (resultDocumentsFilenames.Length == 0)
return;
// open source document
Vintasoft.Imaging.Pdf.PdfDocument sourceDocument =
new Vintasoft.Imaging.Pdf.PdfDocument(sourceDocumentFilename);
// create result documents array
int resultDocumentsLength = System.Math.Min(resultDocumentsFilenames.Length, sourceDocument.Pages.Count);
Vintasoft.Imaging.Pdf.PdfDocument[] resultDocuments =
new Vintasoft.Imaging.Pdf.PdfDocument[resultDocumentsLength];
for (int i = 0; i < resultDocuments.Length; i++)
// create result document v1.4
resultDocuments[i] = new Vintasoft.Imaging.Pdf.PdfDocument(
resultDocumentsFilenames[i], System.IO.FileMode.Create, Vintasoft.Imaging.Pdf.PdfFormat.Pdf_14);
// split source document
int currenPageIndex = 0;
int totalPages = sourceDocument.Pages.Count;
while (currenPageIndex < totalPages)
{
for (int i = 0; i < resultDocuments.Length; i++)
{
resultDocuments[i].Pages.Add(sourceDocument.Pages[currenPageIndex]);
currenPageIndex++;
if (currenPageIndex >= totalPages)
break;
}
}
for (int i = 0; i < resultDocuments.Length; i++)
{
// save changes
resultDocuments[i].SaveChanges();
// close result document
resultDocuments[i].Dispose();
}
// close source document
sourceDocument.Dispose();
}
Public Shared Sub SplitPDFDocuments(sourceDocumentFilename As String, ParamArray resultDocumentsFilenames As String())
If resultDocumentsFilenames.Length = 0 Then
Return
End If
' open source document
Dim sourceDocument As New Vintasoft.Imaging.Pdf.PdfDocument(sourceDocumentFilename)
' create result documents array
Dim resultDocumentsLength As Integer = System.Math.Min(resultDocumentsFilenames.Length, sourceDocument.Pages.Count)
Dim resultDocuments As Vintasoft.Imaging.Pdf.PdfDocument() = New Vintasoft.Imaging.Pdf.PdfDocument(resultDocumentsLength - 1) {}
For i As Integer = 0 To resultDocuments.Length - 1
' create result document v1.4
resultDocuments(i) = New Vintasoft.Imaging.Pdf.PdfDocument(resultDocumentsFilenames(i), System.IO.FileMode.Create, Vintasoft.Imaging.Pdf.PdfFormat.Pdf_14)
Next
' split source document
Dim currenPageIndex As Integer = 0
Dim totalPages As Integer = sourceDocument.Pages.Count
While currenPageIndex < totalPages
For i As Integer = 0 To resultDocuments.Length - 1
resultDocuments(i).Pages.Add(sourceDocument.Pages(currenPageIndex))
currenPageIndex += 1
If currenPageIndex >= totalPages Then
Exit For
End If
Next
End While
For i As Integer = 0 To resultDocuments.Length - 1
' save changes
resultDocuments(i).SaveChanges()
' close result document
resultDocuments(i).Dispose()
Next
' close source document
sourceDocument.Dispose()
End Sub