Create a New PDF for Each Page in a PDF

The code below shows you how to create a new PDF file for each page in an existing file.

This sample requires a Tools license.

C#
C++
VB.Net
C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using System;
using System.IO;
using SolidFramework.Model.Pdf.Pages;
 
namespace PagestoPDFs
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            // Call your Solid Documents License
            SolidFramework.License.Import(@"C:\MyFolder\license.xml");
 
            // Get multiple files
            string pdfPath = @"C:\YourFolder\yourpdf.pdf";
 
            //*Extract Single PDF Pages from a Multiple Page PDF File*//
 
            PagesModel model = new PagesModel();
            model.OpenPDF(pdfPath);
 
            //Define a string called partsFolder to get the directory information for the selected file
            String partsFolder = Path.GetDirectoryName(pdfPath);
            partsFolder = Path.Combine(partsFolder, Path.GetFileNameWithoutExtension(pdfPath));
            Directory.CreateDirectory(partsFolder);
 
            // Find the number of pages in your pdf file
            if (model.PagesCount > 1)
            {
                for (int pageIndex = 0;
                    pageIndex < model.PagesCount;
                    pageIndex++
                    )
                {
                    model.Selection.Clear();
                    model.Selection.Add(pageIndex);
 
                    String path = Path.Combine(partsFolder, Path.GetFileName(pdfPath));
                    path = Path.ChangeExtension(path, "." + (pageIndex + 1).ToString() + ".pdf");
                    model.Save(path, true);
                }
            }
            else // single page case workaround
            {
                String path = Path.Combine(partsFolder, Path.GetFileName(pdfPath));
                path = Path.ChangeExtension(path, ".1.pdf");
                File.Copy(pdfPath, path, true);
                File.SetAttributes(path, FileAttributes.Normal); // make sure the copy is not readonly
            }
            model.Close();
 
        }
    }
}
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include "stdafx.h"
#include "SolidFramework.h"
 
using namespace std;
 
int main()
{
    // Call your Solid Documents License
    SolidFramework::License::Import(L"c:\\MyFolder\\license.xml");
 
    // Add the PDF file to convert
    wstring pdfPath(L"c:\\YourFolder\\yourpdf.pdf");
 
    /*Extract Single PDF Pages from a Multiple Page PDF File*/
    SolidFramework::Model::Pdf::Pages::PagesModelBasePtr model(new SolidFramework::Model::Pdf::Pages::PagesModelBase());
    model->OpenPDF(pdfPath);
 
    //Define a string called partsFolder to get the directory information for the selected file
    wstring partsFolder = SolidFramework::Platform::Path::GetDirectoryName(pdfPath);
    partsFolder = SolidFramework::Platform::Path::Combine(partsFolder, SolidFramework::Platform::Path::GetFileName(pdfPath));
    if (!SolidFramework::Platform::Directory::Exists(partsFolder))
    {
        SolidFramework::Platform::Directory::CreateDirectoryW(partsFolder);
    }
 
    // Find the number of pages in your pdf file
    int pagesCount = model->getPagesCount();
    if (pagesCount > 1)
    {
        for (int pageIndex = 0;
            pageIndex < pagesCount; pageIndex++ ) { SolidFramework::Plumbing::PagesArrayPtr pageArray(new SolidFramework::Plumbing::PagesArray); pageArray->Add(pageIndex);
            model->setSelection(pageArray);
            wstring path = SolidFramework::Platform::Path::Combine(partsFolder, SolidFramework::Platform::Path::GetFileName(pdfPath));
            wstring changeExtension(L".");
            changeExtension.append(to_wstring(pageIndex + 1));
            changeExtension.append(L".pdf");
            path = SolidFramework::Platform::Path::ChangeExtension(path, changeExtension);
            model->Save(path, true);
            pageArray->Dispose();
        }
    }
    else // single page case workaround
    {
        wstring path = SolidFramework::Platform::Path::Combine(partsFolder, SolidFramework::Platform::Path::GetFileName(pdfPath));
        path = SolidFramework::Platform::Path::ChangeExtension(path, L".1.pdf");
        SolidFramework::Platform::File::Copy(pdfPath, path, true);
        SetFileAttributes(path.c_str(), FILE_ATTRIBUTE_NORMAL); // make sure the copy is not readonly
    }
 
    model->Close();
    model->Dispose();
 
    return 0;
}
VB.Net
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
Imports System.IO
Imports SolidFramework.Converters.Plumbing
 
Module PDFtoWord
 
Sub Main()
 
' Call your Solid Documents License
SolidFramework.License.Import("C:MyFolderlicense.xml")
 
'Define a variable for your source file
Dim sPdfPath As String
 
' Define a String for the output file
Dim sDocxPath As String
 
' Define your Solid Framework Converter
Dim myConverter As SolidFramework.Converters.PdfToWordConverter
 
' Set your file path
sPdfPath = ("C:MyfolderMyFile.pdf")
 
' Set your output file and location
sDocxPath = Path.ChangeExtension(sPdfPath, ".docx")
 
' Set the converter
myConverter = New SolidFramework.Converters.PdfToWordConverter
 
' Set the preferred conversion properties
 
' Add files to convert.
myConverter.AddSourceFile(sPdfPath)
 
' Detect Headers and Footers
myConverter.HeaderAndFooterMode = HeaderAndFooterMode.Detect
 
' Turn on Solid Documents Optical Character Recognition (OCR) for Scanned Files
myConverter.TextRecoveryEngine = TextRecoveryEngine.Automatic
 
' Set the layout of the reconstruction (Exact for Forms)
myConverter.ReconstructionMode = ReconstructionMode.Flowing
 
' Convert the File.
myConverter.ConvertTo(sDocxPath, True)
 
' Clean up
myConverter.Dispose()
 
End Sub
 
End Module