PDF2HtmlTest

Sample Obj-C code for using Apryse SDK to programmatically convert generic PDF documents to HTML. Learn more about our Obj-C PDF to HTML

1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6#import <OBJC/PDFNetOBJC.h>
7#import <Foundation/Foundation.h>
8
9//---------------------------------------------------------------------------------------
10// The following sample illustrates how to use the PDF::Convert utility class to convert
11// documents and files to HTML.
12//
13// There are two HTML modules and one of them is an optional PDFNet Add-on.
14// 1. The built-in HTML module is used to convert PDF documents to fixed-position HTML
15// documents.
16// 2. The optional Structured Output add-on module is used to convert PDF documents to
17// HTML documents with text flowing across the browser window.
18//
19// The Apryse SDK Structured Output module can be downloaded from
20// https://docs.apryse.com/core/info/modules/
21//
22// Please contact us if you have any questions.
23//---------------------------------------------------------------------------------------
24
25int main (int argc, const char * argv[])
26{
27 @autoreleasepool {
28
29 NSString *inputPath = @"../../TestFiles/";
30 NSString *outputPath = @"../../TestFiles/Output/";
31
32 // The first step in every application using PDFNet is to initialize the
33 // library. The library is usually initialized only once, but calling
34 // Initialize() multiple times is also fine.
35 [PTPDFNet Initialize: 0];
36
37 int ret = 0;
38
39 //------------------------------------------------------------------------
40
41 @try {
42 // Convert PDF document to HTML with fixed positioning option turned on (default)
43 NSLog(@"Converting PDF to HTML with fixed positioning option turned on (default)");
44
45 NSString *inputFile = [inputPath stringByAppendingString:@"paragraphs_and_tables.pdf"];
46 NSString *outputFile = [outputPath stringByAppendingString:@"paragraphs_and_tables_fixed_positioning.html"];
47
48 // Convert PDF to HTML
49 [PTConvert ToHtml:inputFile out_path:outputFile];
50
51 NSLog(@"Result saved in %@", outputFile);
52 }
53 @catch (NSException *e) {
54 NSLog(@"Exception: %@ - %@\n", e.name, e.reason);
55 ret = 1;
56 }
57
58 //------------------------------------------------------------------------
59
60 [PTPDFNet AddResourceSearchPath:@"../../../Lib/"];
61
62 if (![PTStructuredOutputModule IsModuleAvailable]) {
63 NSLog(@"");
64 NSLog(@"Unable to run the sample: Apryse SDK Structured Output module not available.");
65 NSLog(@"---------------------------------------------------------------");
66 NSLog(@"The Structured Output module is an optional add-on, available for download");
67 NSLog(@"at https://docs.apryse.com/core/info/modules/. If you have already");
68 NSLog(@"downloaded this module, ensure that the SDK is able to find the required files");
69 NSLog(@"using the PDFNet::AddResourceSearchPath() function.");
70 NSLog(@"");
71
72 return 1;
73 }
74
75 //------------------------------------------------------------------------
76
77 @try {
78 // Convert PDF document to HTML with reflow full option turned on (1)
79 NSLog(@"Converting PDF to HTML with reflow full option turned on (1)");
80
81 NSString *inputFile = [inputPath stringByAppendingString:@"paragraphs_and_tables.pdf"];
82 NSString *outputFile = [outputPath stringByAppendingString:@"paragraphs_and_tables_reflow_full.html"];
83
84 PTHTMLOutputOptions *htmlOutputOptions = [[PTHTMLOutputOptions alloc] init];
85
86 // Set e_reflow_full content reflow setting
87 [htmlOutputOptions SetContentReflowSetting:e_pthtml_reflow_full];
88
89 // Convert PDF to HTML
90 [PTConvert ToHtmlWithFilename:inputFile out_path:outputFile options:htmlOutputOptions];
91
92 NSLog(@"Result saved in %@", outputFile);
93 }
94 @catch (NSException *e) {
95 NSLog(@"Exception: %@ - %@\n", e.name, e.reason);
96 ret = 1;
97 }
98
99 //------------------------------------------------------------------------
100
101 @try {
102 // Convert PDF document to HTML with reflow full option turned on (only converting the first page) (2)
103 NSLog(@"Converting PDF to HTML with reflow full option turned on (only converting the first page) (2)");
104
105 NSString *inputFile = [inputPath stringByAppendingString:@"paragraphs_and_tables.pdf"];
106 NSString *outputFile = [outputPath stringByAppendingString:@"paragraphs_and_tables_reflow_full_first_page.html"];
107
108 PTHTMLOutputOptions *htmlOutputOptions = [[PTHTMLOutputOptions alloc] init];
109
110 // Set e_reflow_full content reflow setting
111 [htmlOutputOptions SetContentReflowSetting:e_pthtml_reflow_full];
112
113 // Convert only the first page
114 [htmlOutputOptions SetPages:1 page_to:1];
115
116 // Convert PDF to HTML
117 [PTConvert ToHtmlWithFilename:inputFile out_path:outputFile options:htmlOutputOptions];
118
119 NSLog(@"Result saved in %@", outputFile);
120 }
121 @catch (NSException *e) {
122 NSLog(@"Exception: %@ - %@\n", e.name, e.reason);
123 ret = 1;
124 }
125
126 //------------------------------------------------------------------------
127
128 [PTPDFNet Terminate: 0];
129 return ret;
130 }
131}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales