Convert PDF to Images
The code below shows you how to convert a PDF file to images.
It is recommended that you have already reviewed the Getting Started sample, since that includes Licensing and Framework initialization code required to make this sample run.
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
using System;
using SolidFramework.Converters;
using SolidFramework.Converters.Plumbing;
namespace CSharp_Tutorials
{
public static partial class Tutorials
{
public static bool ConvertPdfToImages(string pdfPath, string outputFolder)
{
// Create a PdfToImageConverter
using (var converter = new PdfToImageConverter())
{
// Add the PDF file to convert
converter.AddSourceFile(pdfPath);
// Set the output folder and overwrite mode
converter.OutputDirectory = outputFolder;
converter.OverwriteMode = SolidFramework.Plumbing.OverwriteMode.ForceOverwrite;
// Set the conversion type to ExtractPages
converter.ConversionType = ImageConversionType.ExtractPages;
// Optional: Set PdfToImageConverter options to your liking
converter.PageDPI = 300;
converter.OutputType = ImageDocumentType.Png;
Console.WriteLine("Converting " + pdfPath + " to images in folder " + outputFolder);
// Convert the file
converter.Convert();
// Check the results were successful
foreach (var result in converter.Results)
{
if (result.Status != ConversionStatus.Success)
{
Console.WriteLine("Converting " + pdfPath + " to images failed with status: " + result);
Console.WriteLine();
return false;
}
}
}
Console.WriteLine("Successfully converted " + pdfPath + " to images");
Console.WriteLine();
return true;
}
}
}
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
Imports System
Imports SolidFramework.Converters
Imports SolidFramework.Converters.Plumbing
Namespace VBNet_Tutorials
Partial Module Tutorials
Function ConvertPdfToImages(ByVal pdfPath As String, ByVal outputFolder As String) As Boolean
' Create a PdfToImageConverter
Using converter As PdfToImageConverter = New PdfToImageConverter()
' Add the PDF file to convert
converter.AddSourceFile(pdfPath)
' Set the output folder And overwrite mode
converter.OutputDirectory = outputFolder
converter.OverwriteMode = SolidFramework.Plumbing.OverwriteMode.ForceOverwrite
' Set the conversion type to ExtractPages
converter.ConversionType = ImageConversionType.ExtractPages
' Optional Set PdfToImageConverter options to your liking
converter.PageDPI = 300
converter.OutputType = ImageDocumentType.Png
Console.WriteLine("Converting " & pdfPath & " to images in folder " & outputFolder)
' Convert the file
converter.Convert()
' Check the results were successful
For Each result As ConversionResult In converter.Results
If result.Status <> ConversionStatus.Success Then
Console.WriteLine("Converting " & pdfPath & " to images failed with status: " & result.ToString())
Console.WriteLine()
Return False
End If
Next
End Using
Console.WriteLine("Successfully converted " & pdfPath & " to images")
Console.WriteLine()
Return True
End Function
End Module
End Namespace
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
#include "Tutorials.h"
using namespace SolidFramework::Converters::Plumbing;
using namespace SolidFramework::Converters;
bool ConvertPdfToImages(const wstring & pdfPath, const wstring & outputFolder)
{
// Create a PdfToImageConverter
auto converter = make_shared<PdfToImageConverter>();
// Add the PDF file to convert
converter->AddSourceFile(pdfPath);
// Set the output folder and overwrite mode
converter->SetOutputDirectory(outputFolder);
converter->SetOverwriteMode(SolidFramework::Plumbing::OverwriteMode::ForceOverwrite);
// Set the conversion type to ExtractPages
converter->SetConversionType(ImageConversionType::ExtractPages);
// Optional: Set PdfToImageConverter options to your liking
converter->SetPageDPI(300);
converter->SetOutputType(ImageDocumentType::Png);
wcout << L"Converting " << pdfPath << L" to images in folder " << outputFolder << endl;
// Convert the file
converter->Convert();
// Check the results were successful
for (const auto & result : converter->GetResults())
{
if (result->GetStatus() != ConversionStatus::Success)
{
wcout << L"Converting " << pdfPath << L" to images failed with status: " << (int)result->GetStatus() << endl << endl;
return false;
}
}
wcout << L"Successfully converted " << pdfPath << L" to images" << endl << endl;
return true;
}