Convert PDF to Html
This sample demonstrates how to convert a PDF to a HyperText Markup Language web page using SolidFramework.
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
54
using System;
using SolidFramework.Converters;
using SolidFramework.Converters.Plumbing;
namespace CSharp_Tutorials
{
public static partial class Tutorials
{
public static bool ConvertPdfToHtml(string pdfPath, string outputPath)
{
// Create a PdfToHtmlConverter
using (var converter = new PdfToHtmlConverter())
{
// Add the PDF file to convert
converter.AddSourceFile(pdfPath);
// Optional: Set PdfToHtmlConverter options to your liking
converter.Images = HtmlImages.Embed;
converter.ImageType = ImageDocumentType.Png;
converter.ExactMode = true;
// Optional: Add a progress/warning handler
double nextPercentToLog = 25;
converter.Progress += (sender, progress) =>
{
double percent = progress.Progress * 100.0 / progress.MaxProgress;
if (percent < nextPercentToLog || (percent > 70 && nextPercentToLog < 30)) { return; }
Console.WriteLine(progress.StatusDescription + " " + percent + "%");
nextPercentToLog = percent > 70 ? 25 : nextPercentToLog + 25;
};
Console.WriteLine("Converting " + pdfPath + " to " + outputPath);
// Convert the file
var result = converter.ConvertTo(outputPath, true);
// Check if it was successful
if (result != ConversionStatus.Success)
{
Console.WriteLine("Converting " + pdfPath + " to " + outputPath + " failed with status: " + result);
Console.WriteLine();
return false;
}
}
Console.WriteLine("Successfully converted " + pdfPath + " to " + outputPath);
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
51
52
53
54
55
56
Imports System
Imports SolidFramework.Converters
Imports SolidFramework.Converters.Plumbing
Namespace VBNet_Tutorials
Partial Module Tutorials
Function ConvertPdfToHtml(ByVal pdfPath As String, ByVal outputPath As String) As Boolean
' Create a PdfToHtmlConverter
Using converter As PdfToHtmlConverter = New PdfToHtmlConverter()
' Add the PDF file to convert
converter.AddSourceFile(pdfPath)
' Optional Set PdfToHtmlConverter options to your liking
converter.Images = HtmlImages.Embed
converter.ImageType = ImageDocumentType.Png
converter.ExactMode = True
' Optional Add a progress/warning handler
Dim nextPercentToLog As Double = 25
AddHandler converter.Progress,
Sub(sender, progress)
Dim percent As Double = progress.Progress * 100.0 / progress.MaxProgress
If percent < nextPercentToLog OrElse (percent > 70 AndAlso nextPercentToLog < 30) Then
Return
End If
Console.WriteLine(progress.StatusDescription & " " + percent.ToString() & "%")
nextPercentToLog = If(percent > 70, 25, nextPercentToLog + 25)
End Sub
Console.WriteLine("Converting " & pdfPath & " to " & outputPath)
' Convert the file
Dim result As ConversionStatus = converter.ConvertTo(outputPath, True)
' Check if it was successful
If result <> ConversionStatus.Success Then
Console.WriteLine("Converting " & pdfPath & " to " & outputPath & " failed with status: " & result)
Console.WriteLine()
Return False
End If
End Using
Console.WriteLine("Successfully converted " & pdfPath & " to " & outputPath)
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
46
#include "Tutorials.h"
using namespace SolidFramework::Converters::Plumbing;
using namespace SolidFramework::Converters;
bool ConvertPdfToHtml(const wstring & pdfPath, const wstring & outputPath)
{
// Create a PdfToHtmlConverter
auto converter = make_shared<PdfToHtmlConverter>();
// Add the PDF file to convert
converter->AddSourceFile(pdfPath);
// Optional: Set PdfToHtmlConverter options to your liking
converter->SetImages(HtmlImages::Embed);
converter->SetImageType(ImageDocumentType::Png);
converter->SetExactMode(true);
// Optional: Add a progress/warning handler
double nextPercentToLog = 25;
converter->OnProgress = [&nextPercentToLog] (SolidFramework::ProgressEventArgsPtr progress)
{
const double percent = progress->GetProgress() * 100.0 / progress->GetMaxProgress();
if (percent < nextPercentToLog || (percent > 70 && nextPercentToLog < 30)) { return; }
wcout << progress->GetStatusDescription() << L" " << percent << L"%" << endl;
nextPercentToLog = percent > 70 ? 25 : nextPercentToLog + 25;
};
wcout << L"Converting " << pdfPath << L" to " << outputPath << endl;
// Convert the file
auto result = converter->ConvertTo(outputPath, true);
// Check if it was successful
if (result != ConversionStatus::Success)
{
wcout << L"Converting " << pdfPath << L" to " << outputPath << L" failed with status: " << (int)result << endl << endl;
return false;
}
wcout << L"Successfully converted " << pdfPath << L" to " << outputPath << endl << endl;
return true;
}