Build iOS PDF Reader with ComPDKit PDF SDK

Creating an iOS app for businesses and developers is essential in today's mobile-first world. As the demand for PDF document processing increases, using ComPDFKit, a powerful PDF software development kit (SDK), to build an iOS PDF reader and editor allows end users to easily view and edit PDF documents.

In this blog, we will first explore the steps required to integrate the ComPDFKit PDF SDK and use it to build an iOS PDF reader.

Start using ComPDFKit iOS PDF SDK

ComPDFKit is a powerful PDF software development kit (SDK). Embedding ComPDFKit PDF SDK into your iOS application is very simple with a few lines of Objective-C code. It only takes a few minutes to get started.

The following sections describe the required requirements, the structure of the installation package, and how to create an iOS PDF reader in Objective-C using the ComPDFKit PDF SDK

required environment

The following development environment is required to develop programs using ComPDFKit PDF SDK. If your development environment is low, you may not be able to use ComPDFKit PDF SDK normally.

The device system requires iOS10.0 and later systems

IDE version: Xcode12 and later. In this blog, we use Xcode14.0.1 to demonstrate the examples

1679552888371.jpg

Currently does not support running the simulator on the Apple Silicon series

For earlier versions of Xcode (such as Xcode 13), the Bitcode option may be turned on by default, and thus needs to be turned off for this to work. The specific operation steps are shown in the figure below:

1679991673904.jpg

iOS package structure

You can get our PDF SDK installation package by contacting us . Download and unzip the installation package of ComPDFKit PDF SDK for iOS, you will see all the following files in the SDK package.

ComPDKit.xcframework - includes ComPDKit dynamic library (arm64_armv7, x86_64-simulator) and related header files

PDFViewer - project containing iOS (Objective-C) samples

PDFViewer-Swift - project containing iOS(Swift) samples

api_reference_ios - API specification

developer_guide_ios.pdf- Developer guide

release_note - Release related information

legal.txt** - Legal related information

165217880019320.png

Create an iOS preview app using Objective-C

This part will help you quickly start using ComPDFKit PDF SDK to make iOS applications in Objective-C through step-by-step instructions. Through the following steps, you will have a simple application that can display the content of a specified PDF file. In this article, we use Xcode 14.0.1 as an example.

Create a brand new project

  1. Open Xcode, select File -> New -> Project , then select the iOS app, and then click the Next button

1679553012143.jpg

  1. New projects need to set the following options:
  • Enter the name of the product, for example: PDFViewer

  • Select and log in the Apple developer account you want to publish the application to.

  • Enter a name for your organization: (com.example.pdfviewer)

  • Select story board on panel

  • Programming language selection "Objective-C"

  • Click the "Next" button

  • Set the location of the project and click Create

1679554358758.jpg

Add the SDK package of ComPDFKit

  1. Find ComPDFKit.xcframework in the installation package of ComPDKit PDF SDK . It contains binaries for all supported architectures.

  2. Open the newly created iOS project and select General in the right panel . Then find the Frameworks, Libraries, and Embedded Content section, drag and drop ComPDFKit.xcframework directly to this section, and set the Embed option to Embed & Sign .

  3. Use the shortcut key "Command_B" to compile the project. If no error is reported, the configuration is correct, and you can proceed to the next step. If some errors are reported, you need to check the cause of the errors. If you are unsure of the error, you can contact our technical team to resolve the issue

1679556806689.jpg

Add License Key

  1. Import the header file ComPDFKit/ComPDFKit.h into AppDelegate.

  2. Follow the code below and didFinishLaunchingWithOptionscall CPDFKit setLicenseKey:@"LICENSE_KEY" secret:@"LICENSE_SECRET"the method in the method. You need to replace LICENSE_KEY and LICENSE_SECRET with the license you got .

#import <ComPDFKit/ComPDFKit.h>

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.

/*
 // Set your license key here. ComPDFKit is commercial software.
 // Each ComPDFKit license is bound to a specific app bundle id.
 
 // Notice: This is a demo project, presenting completed ComPDFKit functions.
 // The functions might be different based on the license you have purchased.
 // Please check the functions you chose work fine in this demo project.
 
 // BOOL tIsFeatureLocked = ![[CPDFKit sharedInstance] allowsFeature:CPDFKitFeatureSecurityWatermark];
 */

 // [CPDFKit setLicenseKey:@"YOUR_LICENSE_KEY_GOES_HERE" secret:@"YOUR_LICENSE_SECRET_GOES_HERE"];
  
    [CPDFKit setLicenseKey:@"" secret:@""];
    
    NSString *tAnnotateAuther = CPDFKitShareConfig.annotationAuthor;
    NSLog(@"CPDFKit Annotation Author: \t %@", tAnnotateAuther);
    
    CPDFKitShareConfig.enableAnnotationNoRotate = YES;
    
    return YES;
}


Compile and run the project. If "version information" is output from the console, the license has been successfully set up. Otherwise, see the "Troubleshooting" section at the end of this blog or check the error log in the console to quickly identify and fix the problem.

display pdf document

Prepare a test PDF file, drag and drop it into the newly created pdfView project. In this way, you can use NSBundleto load and preview local PDF documents. The image below shows an example of importing a PDF document named "Online5" into a project.

1680714636385.jpg

  1. Prepare a test PDF file, drag and drop it into the newly created pdfView project. In this way, you can use NSBundleto load and preview local PDF documents. The image below shows an example of importing a PDF document named "Online5" into a project.
    NSString *pdfPath= [bunle pathForResource:@"Online5" ofType:@"pdf"];
    NSURL *url = [NSURL fileURLWithPath:pdfPath];
    CPDFDocument *document = [[[CPDFDocument alloc] initWithURL:url] autorelease];

    CGRect rect = self.view.bounds;
    CPDFView *pdfView = [[[CPDFView alloc] initWithFrame:rect] autorelease];
    pdfView.document = document;
  1. Add the created CPDFView to the current controller's view. The sample code is as follows:
 [self.view addSubview:pdfView];
  1. Connect your device or emulator and use the shortcut key "Command_R" to run the application. The PDF file will be opened and displayed on the screen.

1680715626618.jpg

  1. If the PDF content cannot be displayed, check if the created NSURL and "CPDFDocument" objects are empty, or if the created "CPDFView" has zero size. They should not be empty.

If the file path contains special characters, your NSURL will be nil. Please use the following code for processing.

[pdfPath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

If the created CPDFViewsize is zero, follow the code below to adjust pdfviewthe size.

pdfview.frame = self.view.frame.bounds

All the code looks like this:

    NSString *pdfPath= [bunle pathForResource:@"Online5" ofType:@"pdf"];

    NSURL *url = [NSURL fileURLWithPath:pdfPath];
    CPDFDocument *document = [[[CPDFDocument alloc] initWithURL:url] autorelease];
    if (document.error && document.error.code != CPDFDocumentPasswordError) {
        return;
    }
    
    // Initialize a CPDFView object with the size of the entire screen
    CPDFView *pdfView = [[[CPDFView alloc] initWithFrame:self.view.bounds] autorelease];
    
    // Set the document to display
    pdfView.document = document;
    
    // Add the pdfView to the root view
    [self.view addSubview:pdfView];

Troubleshooting

  1. bit code

    Even if everything is configured correctly, compilation errors may still occur. First, check that bitcode is disabled. In older Xcode versions (like Xcode 13), the bitcode option might be enabled by default. It needs to be set to "No" in order to run the application.

  2. License

    If you get a license setup error, make sure the Identity (Bundle ID) setting in General Settings matches the Bundle ID you provided when you contacted us to get a license. If an expired license message appears, please contact the ComPDFKit team to obtain the latest license and key.

  3. PDF cannot be displayed

    Please check whether the path we pass in requires special encoding, or whether the local path we pass in exists

  4. other problems

    If you encounter other problems while integrating our ComPDFKit PDF SDK for iOS, please feel free to contact the ComPDFKit team
    In today's mobile-first world, creating an iOS application for businesses and developers is essential. As the demand for PDF document processing increases, using ComPDFKit, a powerful PDF software development kit (SDK), to build an iOS PDF reader and editor allows end users to easily view and edit PDF documents.

In this blog, we will first explore the steps required to integrate the ComPDFKit PDF SDK and use it to build an iOS PDF reader.

Start using ComPDFKit iOS PDF SDK

ComPDFKit is a powerful PDF software development kit (SDK). Embedding ComPDFKit PDF SDK into your iOS application is very simple with a few lines of Objective-C code. It only takes a few minutes to get started.

The following sections describe the required requirements, the structure of the installation package, and how to create an iOS PDF reader in Objective-C using the ComPDFKit PDF SDK

required environment

The following development environment is required to develop programs using ComPDFKit PDF SDK. If your development environment is low, you may not be able to use ComPDFKit PDF SDK normally.

The device system requires iOS10.0 and later systems

IDE version: Xcode12 and later. In this blog, we use Xcode14.0.1 to demonstrate the examples

[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-vJRtAPYZ-1690448461859)(https://developer-private-1258344699.cos.ap-guangzhou.myqcloud.com /column/article/10679850/20230727-f12f15ef.jpg?x-cos-security-token=BJdJsHEW9tRdV4U1c2UC8j4hz1AQDOiad9eabc3fe036a764ae609698273ee434ELpcYO4-9LL7ugDY7fTqEzBaRJKrOsDTkDtXM7zGLaKwHKL2sAHtn3_6b6bBiHmb8QXsDi2ZSk4Mfij6vE6wrklmlNqWFKH6KdMOHGq0KHPxiL5SFiK0pzMJTc8Nl9m0YENqdo-fm7Hr_Rn9xhNTN5yvFRQgPJqSVSM72uOFS37MXhTfHBVUL3nORH5Vg_Til5TF8XdIoOYTVx3Ks1P5nPe_6aG3UASiZU_1E-9pjeDKpCweKAvgji3ETHiFjEjuKjOz2H0-4eWcFDDYYY3q9vbyD83V3BxwgoXRoFwAVmIJXWnJG2d1RBRO3sj-40Skgrs81_udF8paUXEPlEkrsoY5-7GeTE4JY2ROVS3RaHjyAm6I0szTNCikbbU3LO2PGVMH46Hw6PcqvNc92dyplg&q-sign-algorithm=sha1&q-ak=AKIDANg5dNM5f9D8uxxwgM3dVxpc5tfCPYqPpHqqYMKsHsXGLcIfh7r_y3gFkNRi1S9q&q-sign-time=1690448075%3B1690455270&q-key-time=1690448075%3B1690455270&q-header-list=host&q-url-param-list=x-cos-security-token&q-signature=c24998cfc572398f422139639a7ffb755bafb6d6)]p;q-signature=c24998cfc572398f422139639a7ffb755bafb6d6)]p;q-signature=c24998cfc572398f422139639a7ffb755bafb6d6)]

Currently does not support running the simulator on the Apple Silicon series

For earlier versions of Xcode (such as Xcode 13), the Bitcode option may be turned on by default, and thus needs to be turned off for this to work. The specific operation steps are shown in the figure below:

[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-7Ez2lkrs-1690448461860)(https://developer-private-1258344699.cos.ap-guangzhou.myqcloud.com /column/article/10679850/20230727-24aefc53.jpg?x-cos-security-token=2DvTlaTJkqLbUbKZEwEcLA7IMp51aNyaaf7adb55df5d8a1f6c416b6163835df8zpMHRHzjIezKVlcXhhCVpkroW1dNVtfYRN64iO0cGWgQPHbi0tnfSw76g15p5LXDQugdqEk1f8Zuzwka9VdrrLd3h2I0ev_eiZW-YiqR-Mwp0IO_vL6CkCoUOj5uREp6LzqBRVtrwuwf4n2-6PkYycz9Sl_wpw7Hww-t4J9sbwaz2hFF2gMxi577Tv2MOc8Odg6SUzW5ktFwCO5PsPKXEMtEkW7T2AEiyfBnSFqczblBpec7lKaOJvzRItVPBMb8JJaGybvhwgTg3oSwdcLMSFPYDM-Tpe9vz61GR52MIHLUf1cFGSsJfQfpv_XUVm6livJgKT93kQ_qJL4fLwTmjNysRYh36UWQNglUH8FegUM9cQz6QNOVDvGdTG9UupgqCuXOazgj_RxsfI47JEmizw&q-sign-algorithm=sha1&q-ak=AKIDVKhQ4uBgkTq_t-2pdw1vZhsOn1wlTJFx3kTUGaufm5AW6X8r635Sk3aIVZBoNpAg&q-sign-time=1690448083%3B1690455278&q-key-time=1690448083%3B1690455278&q-header-list=host&q-url-param-list=x-cos-security-token&q-signature=5ee26126e198659ceb1f68f6ceaeee27ccc75f35)]p;q-signature=5ee26126e198659ceb1f68f6ceaeee27ccc75f35)]p;q-signature=5ee26126e198659ceb1f68f6ceaeee27ccc75f35)]

iOS package structure

You can get our PDF SDK installation package by contacting us . Download and unzip the installation package of ComPDFKit PDF SDK for iOS, you will see all the following files in the SDK package.

ComPDKit.xcframework - includes ComPDKit dynamic library (arm64_armv7, x86_64-simulator) and related header files

PDFViewer - project containing iOS (Objective-C) samples

PDFViewer-Swift - project containing iOS(Swift) samples

api_reference_ios - API specification

developer_guide_ios.pdf- Developer guide

release_note - Release related information

legal.txt** - Legal related information

[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-A7PbvvGE-1690448461860)(https://developer-private-1258344699.cos.ap-guangzhou.myqcloud.com /column/article/10679850/20230727-688f1f3f.png?x-cos-security-token=2DvTlaTJkqLbUbKZEwEcLA7IMp51aNyacfd929f7e17b74f145b4f4dde7f09d42zpMHRHzjIezKVlcXhhCVplbHw0ZQcgA_4HcvHxN8DRIAqFbV0f5Txnup13H3GeRVFwmdWWTET8fQtCtNmCR-n70DqR_9PImNJ3K7loOjAa2fmhrn5_l0hZBFD2CjKoJHyz8A0HYmQpYJ-JWmBpjxZcEDJtCTkyAyW_EXW62AVvJSKgLmxiqUF-FRuohbDuqf52xHAG8c6k_-E0IYomA-LxCZvIF-ptmG-nrv6q27xjY_5RVmlHqae1fPicYoGnSeHBp4j2Gf-JFxDy0LcAJ1p18noTdtasSt3uZo9DJh–ThmZSa-3E1p6WiKxwRN33d3S10KzGsKsY6yMVXgBHUVT-ZiYaylN7f-KUxUkpoO8lCCq2UL_xisHcvPzTlNXeTU8OJkpoeWGptLZ605dSV9w&q-sign-algorithm=sha1&q-ak=AKIDocT-i4cCmMYCeVFuuP6orCVqxeplyXV8zkKP8tpCINHKTtbpqXg35hVYy9H0g59a&q-sign-time=1690448162%3B1690455357&q-key-time=1690448162%3B1690455357&q-header-list=host&q-url-param-list=x-cos-security-token&q-signature=f5e247a6504c506f70966825c97998e032d285db)]q-signature=f5e247a6504c506f70966825c97998e032d285db)]q-signature=f5e247a6504c506f70966825c97998e032d285db)]

Create an iOS preview app using Objective-C

This part will help you quickly start using ComPDFKit PDF SDK to make iOS applications in Objective-C through step-by-step instructions. Through the following steps, you will have a simple application that can display the content of a specified PDF file. In this article, we use Xcode 14.0.1 as an example.

Create a brand new project

  1. Open Xcode, select File -> New -> Project , then select the iOS app, and then click the Next button

[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-uNts5Iiz-1690448461860)(https://developer-private-1258344699.cos.ap-guangzhou.myqcloud.com /column/article/10679850/20230727-cf9b59e6.jpg?x-cos-security-token=2DvTlaTJkqLbUbKZEwEcLA7IMp51aNya146e45b438f870507952cf1569723a15zpMHRHzjIezKVlcXhhCVpo30aiDycHEY02_aUaUvPMfQEg-fu7kZAlZ1LesPkfSi8skn2c7mbrLHrDspb87loy4gbJtsJnckY1qepYw8PISzAi8gr95KeR_khbV2mwxofAThAsRccASCfWFtC3iRtvHjzEz39zwN5KPjv_6kjluBoYS-IjMvLjFEHt3BZ02I-A6t3YQ1t6VbuL95FcyFl_Jm6iKOQNNrlGUFwoR3d0g_XFeflmKouIrWHSSBrWZNsE1SHeepCWDxdH1PucjGlyTzm_U-DtSynqxaI780X72xTUF0kX4ND9IZZL9zEzCRRieoTYu93zuVYBbxZbm84aSEn4jTC_iiZzNlYO21utIongDUuP3MiJB1x4g2BjAi-E9Cvec7hdEqQe0B6ipTUg&q-sign-algorithm=sha1&q-ak=AKIDx0q6-e8XcN1zd5GPHfiLg9HS50q-mojLOv0XMcShRWclbfgwCe893KheHsQkvMI2&q-sign-time=1690448187%3B1690455382&q-key-time=1690448187%3B1690455382&q-header-list=host&q-url-param-list=x-cos-security-token&q-signature=6ae5611027a8965f209577e560e0d90a417dc2ef)]p;q-signature=6ae5611027a8965f209577e560e0d90a417dc2ef)]p;q-signature=6ae5611027a8965f209577e560e0d90a417dc2ef)]

  1. New projects need to set the following options:
  • Enter the name of the product, for example: PDFViewer

  • Select and log in the Apple developer account you want to publish the application to.

  • Enter a name for your organization: (com.example.pdfviewer)

  • Select story board on panel

  • Programming language selection "Objective-C"

  • Click the "Next" button

  • Set the location of the project and click Create

[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-OEUihjG6-1690448461861)(https://developer-private-1258344699.cos.ap-guangzhou.myqcloud.com /column/article/10679850/20230727-59e4b3f0.jpg?x-cos-security-token=BJdJsHEW9tRdV4U1c2UC8j4hz1AQDOiaf4da32f8919a94517467ce57d6293e84ELpcYO4-9LL7ugDY7fTqEz6HgKIR9E1tig4O2Hpa8ErIROWyBrsRqzGudlEyg002RH7qvCHahg5tnJEUbyE6tsLB7tThV4Yvjh7cHGsjzgBExgIXW_G2peaMnEm3QJkHrMcu40a7S46coZk-BSj7AGRuAYTRZM9fkYR8NpJ56g-m8z5XsAN6ixX5TW5v6Hi9KL9SdTCgnn00LbJstCtTtYHrPf-Hj8jZN_CNc-JBvcvfTeViJ5qI4Bovp6QLYQuTU7F0mv8veXIUzHcvicDF-jxLSMDb9PQ5QxOTFjM-t-NBTgfFZtjeQsYTVQEQwfE3WtN4VBpd34t3-7MzFTtKEKwVsNBf7_uNaBvLNdIR_3KKCBfy2aBRyEFjdwgfREd8m9UOc_56WUwD05yDC014wA&q-sign-algorithm=sha1&q-ak=AKIDZx89zje9_yf8xTTwuOooH-ipJK7t0694fEoNqh0LmIXoEeX3RUSdwoBYcKW9jya6&q-sign-time=1690448310%3B1690455505&q-key-time=1690448310%3B1690455505&q-header-list=host&q-url-param-list=x-cos-security-token&q-signature=4fc1f7bea6722715fa978559a2f96cf3c8bf33b6)]p;q-signature=4fc1f7bea6722715fa978559a2f96cf3c8bf33b6)]p;q-signature=4fc1f7bea6722715fa978559a2f96cf3c8bf33b6)]

Add the SDK package of ComPDFKit

  1. Find ComPDFKit.xcframework in the installation package of ComPDKit PDF SDK . It contains binaries for all supported architectures.

  2. Open the newly created iOS project and select General in the right panel . Then find the Frameworks, Libraries, and Embedded Content section, drag and drop ComPDFKit.xcframework directly to this section, and set the Embed option to Embed & Sign .

  3. Use the shortcut key "Command_B" to compile the project. If no error is reported, the configuration is correct, and you can proceed to the next step. If some errors are reported, you need to check the cause of the errors. If you are unsure of the error, you can contact our technical team to resolve the issue

[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-RoEW9p2b-1690448461861)(https://developer-private-1258344699.cos.ap-guangzhou.myqcloud.com /column/article/10679850/20230727-13b47eaa.jpg?x-cos-security-token=BJdJsHEW9tRdV4U1c2UC8j4hz1AQDOia4510ee2f0b75adf0638ee2ff43d58be2ELpcYO4-9LL7ugDY7fTqE1DAu7so_hZ0d-KTMmMc8lrqP391U9YzIcudjry39U1uWLThzIfs8tSw2i0Nm9Vh5A6fJp3pkQWB8F6XOYhaKcPAxdWzcrkqeaoPY1ujghnhWLnXv4FkmKN7Hoe6qvIw-POOvu5o3x9WqYNSoLv4X-vlDowv623Y5N4kyjvXt23meXofzR4QU7V8ZqAPzjTLZh9HaWkHbe9WnhrU9yq2pkNtqbic6h0kjQlYXRZzUjptO5-G5i3M3PXIFS36di7g9LbMYl4evcRSJeYzBwKsKIgylmf_Vom_eoFdKhTYdJ3emUqUfE1v6yQhkv7BlnFJEAMGg2inzAGiZ0UdLeZPhPJQg8lW006myeSby4SBhQ-LbvNyPmo-SiuHcy6y7udiNA&q-sign-algorithm=sha1&q-ak=AKIDVUWFagSBfmdFsbkPHxU2L5M4LgEks9yAJ3GqWiifcRnTKEaO3lgwmHwtCh0vjKwz&q-sign-time=1690448341%3B1690455536&q-key-time=1690448341%3B1690455536&q-header-list=host&q-url-param-list=x-cos-security-token&q-signature=384f6e4c7385b8732a6471578cb3fdc5ba383eba)]p;q-signature=384f6e4c7385b8732a6471578cb3fdc5ba383eba)]p;q-signature=384f6e4c7385b8732a6471578cb3fdc5ba383eba)]

Add License Key

  1. Import the header file ComPDFKit/ComPDFKit.h into AppDelegate.

  2. Follow the code below and didFinishLaunchingWithOptionscall CPDFKit setLicenseKey:@"LICENSE_KEY" secret:@"LICENSE_SECRET"the method in the method. You need to replace LICENSE_KEY and LICENSE_SECRET with the license you got .

#import <ComPDFKit/ComPDFKit.h>

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.

/*
 // Set your license key here. ComPDFKit is commercial software.
 // Each ComPDFKit license is bound to a specific app bundle id.
 
 // Notice: This is a demo project, presenting completed ComPDFKit functions.
 // The functions might be different based on the license you have purchased.
 // Please check the functions you chose work fine in this demo project.
 
 // BOOL tIsFeatureLocked = ![[CPDFKit sharedInstance] allowsFeature:CPDFKitFeatureSecurityWatermark];
 */

 // [CPDFKit setLicenseKey:@"YOUR_LICENSE_KEY_GOES_HERE" secret:@"YOUR_LICENSE_SECRET_GOES_HERE"];
  
    [CPDFKit setLicenseKey:@"" secret:@""];
    
    NSString *tAnnotateAuther = CPDFKitShareConfig.annotationAuthor;
    NSLog(@"CPDFKit Annotation Author: \t %@", tAnnotateAuther);
    
    CPDFKitShareConfig.enableAnnotationNoRotate = YES;
    
    return YES;
}


Compile and run the project. If "version information" is output from the console, the license has been successfully set up. Otherwise, see the "Troubleshooting" section at the end of this blog or check the error log in the console to quickly identify and fix the problem.

display pdf document

Prepare a test PDF file, drag and drop it into the newly created pdfView project. In this way, you can use NSBundleto load and preview local PDF documents. The image below shows an example of importing a PDF document named "Online5" into a project.

[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-ErttdG0d-1690448461861)(https://developer-private-1258344699.cos.ap-guangzhou.myqcloud.com /column/article/10679850/20230727-76d5ff81.jpg?x-cos-security-token=BJdJsHEW9tRdV4U1c2UC8j4hz1AQDOia3815c88c47721e6bb71051e0f7f0424cELpcYO4-9LL7ugDY7fTqE0S-7mQ31RaKi320guAM036P5TiVFICTdzr7OAtAtPw6pRBAjRt_g7m5VgzngXz-oaB98vh5cRIvwLenm-_p-vDNR9IiOKw5d7h5wj1zzORwopu1nLxnnkjcodCu28Qo701G44Drr34uu9WrVPrToEnybcgX5o97x17L3QAWO32n7ETsg_Qaa2YfwftpDJheb4eM6tZTXWy6RjpOlVvcGi1CTjfXJ9EXwJvZxPSJ0na0UltepeN91dqAqMwNSUqQk-NtYM55Q-YQrHD0uAT101z9rogTjxftuIcmqKs-NQuvQTCZtQX4w3-rVaaAZPE-a6a7wh32gM9jiSq42NWSK-MS8w9ujs5z5n8tmi9WmDHyDFgI6kJablZ5zYb5rvagow&q-sign-algorithm=sha1&q-ak=AKID4FR2rFJa4bCi1CHaZ3tS-XWqFCDxhlPXTnJvR5koLimvxwQCDSJtMiYiOWbWXiYw&q-sign-time=1690448414%3B1690455609&q-key-time=1690448414%3B1690455609&q-header-list=host&q-url-param-list=x-cos-security-token&q-signature=8a71af0815150afd9bb0e7af3daf927196f430e5)]p;q-signature=8a71af0815150afd9bb0e7af3daf927196f430e5)]p;q-signature=8a71af0815150afd9bb0e7af3daf927196f430e5)]

  1. Prepare a test PDF file, drag and drop it into the newly created pdfView project. In this way, you can use NSBundleto load and preview local PDF documents. The image below shows an example of importing a PDF document named "Online5" into a project.
    NSString *pdfPath= [bunle pathForResource:@"Online5" ofType:@"pdf"];
    NSURL *url = [NSURL fileURLWithPath:pdfPath];
    CPDFDocument *document = [[[CPDFDocument alloc] initWithURL:url] autorelease];

    CGRect rect = self.view.bounds;
    CPDFView *pdfView = [[[CPDFView alloc] initWithFrame:rect] autorelease];
    pdfView.document = document;
  1. Add the created CPDFView to the current controller's view. The sample code is as follows:
 [self.view addSubview:pdfView];
  1. Connect your device or emulator and use the shortcut key "Command_R" to run the application. The PDF file will be opened and displayed on the screen.

[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-hmnkTaQL-1690448461862)(https://developer-private-1258344699.cos.ap-guangzhou.myqcloud.com /column/article/10679850/20230727-e8c1f4d9.jpg?x-cos-security-token=5k37kcaEnQva7z6hqkSrfzpVKfj5daDaf611dd67a1df9daa077c5526e075f61c0NEaMxG3eQ9Su4KDqw4KiZJKiT5Hs-LhIdOHlhJLjL8XYGJcZPRH4l_GZ2d-TU28-fbWJO5meIoHCA_FYbOqD_-oh7o7lXSl4om2WGabqJo3F79rumC4R3aLLvO0XfgcyV7kFvWA8SRhyBPAJiyeJ3NGgmw7FcbuTgjr_Vb0PjMiWZiCjZIyJcVs4zu_c20PST-fI5iugwITLnhxpUew8C7oRp02LX9K_PcLz7yFcHe_fckD23sIFicJsmxnalSkMYUdFzn5v6s-W39tT5IumTmlI3gHwrqDrELZD-mkc3ZmKvxjSq8-RHHjr9cWsDuR9hVfsTlMBgQ7vV3y99d3olRyMKvFT1RRorXE766jFwMmI6dE-E2V-LPtJWufdG-Q9Xhh0OxszfoYetvHBvPu5w&q-sign-algorithm=sha1&q-ak=AKIDUP36ZIVKSCohmvCglJKaujA6Bbfm1iRfBuCMedA23wPRaLiCbxAAtws09Yg6ubqB&q-sign-time=1690448430%3B1690455625&q-key-time=1690448430%3B1690455625&q-header-list=host&q-url-param-list=x-cos-security-token&q-signature=fa6938fb2ff06e60331f0ef583db1d319920397c)]p;q-signature=fa6938fb2ff06e60331f0ef583db1d319920397c)]p;q-signature=fa6938fb2ff06e60331f0ef583db1d319920397c)]

  1. If the PDF content cannot be displayed, check if the created NSURL and "CPDFDocument" objects are empty, or if the created "CPDFView" has zero size. They should not be empty.

If the file path contains special characters, your NSURL will be nil. Please use the following code for processing.

[pdfPath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

If the created CPDFViewsize is zero, follow the code below to adjust pdfviewthe size.

pdfview.frame = self.view.frame.bounds

All the code looks like this:

    NSString *pdfPath= [bunle pathForResource:@"Online5" ofType:@"pdf"];

    NSURL *url = [NSURL fileURLWithPath:pdfPath];
    CPDFDocument *document = [[[CPDFDocument alloc] initWithURL:url] autorelease];
    if (document.error && document.error.code != CPDFDocumentPasswordError) {
        return;
    }
    
    // Initialize a CPDFView object with the size of the entire screen
    CPDFView *pdfView = [[[CPDFView alloc] initWithFrame:self.view.bounds] autorelease];
    
    // Set the document to display
    pdfView.document = document;
    
    // Add the pdfView to the root view
    [self.view addSubview:pdfView];

Troubleshooting

  1. bit code

    Even if everything is configured correctly, compilation errors may still occur. First, check that bitcode is disabled. In older Xcode versions (like Xcode 13), the bitcode option might be enabled by default. It needs to be set to "No" in order to run the application.

  2. License

    If you get a license setup error, make sure the Identity (Bundle ID) setting in General Settings matches the Bundle ID you provided when you contacted us to get a license. If an expired license message appears, please contact the ComPDFKit team to obtain the latest license and key.

  3. PDF cannot be displayed

    Please check whether the path we pass in requires special encoding, or whether the local path we pass in exists

  4. other problems

    If you encounter other problems when integrating our ComPDFKit PDF SDK for iOS, please feel free to contact the ComPDFKit team

Guess you like

Origin blog.csdn.net/PDFReaderPro/article/details/131964033
pdf