AVAILABLE NOW: Spring 2025 Release
By Shirley Gong | 2018 Oct 29
2 min
Tags
tutorial
iOS
swift
view
office
Adding a seamless Word, Excel, and PowerPoint viewer to iOS apps is made simple with the Apryse PDF SDK. Office files are converted directly to PDF in the client, and then displayed with a PDF viewer. No server, Microsoft Office license, or other third party software needed.
Although this blog will explain how to add an Office viewer to iOS apps (Swift and Objective-C), this is also possible on all major platforms through a single unified API, including Android and Xamarin.
If you are interested in trying this out for yourself you can download our free trial.
First we'll convert the .doc
, .docx
, .pptx
or .xlsx
file to PDF directly in the client:
//@data {"m":true}//
func ConvertOfficeFile(_ inPath: String, outPath: String, options: PTConversionOptions) {
let pdfDoc = PTPDFDoc()
PTConvert.office(toPDF: pdfDoc, in_filename: inPath, options: options)
pdfDoc?.save(toFile: outPath, flags: e_ptremove_unused.rawValue)
}
//@data {"m":true}//
- (void)convertOfficeFileWithPath:(NSString *)inPath outPath:(NSString *)outPath options:(PTConversionOptions *)options
{
PTPDFDoc *pdfDoc = [[PTPDFDoc alloc] init];
[PTConvert OfficeToPDF:pdfDoc in_filename:inPath options:options];
[pdfDoc SaveToFile:outPath flags:e_ptremove_unused];
}
You can now display the converted file saved in outPath
like this:
//@data {"m":true}//
// Create a PTDocumentViewController
let documentController = PTDocumentViewController()
// The PTDocumentViewController must be in a navigation controller before a document can be opened
let navigationController = UINavigationController(rootViewController: documentController)
// Open an existing local file URL.
let fileURL: URL! = Bundle.main.url(forResource: "sample", withExtension: "pdf")
documentController.openDocument(with: fileURL)
// Show navigation (and document) controller.
self.present(navigationController, animated: true, completion: nil)
//@data {"m":true}//
// Create a PTDocumentViewController
PTDocumentViewController *documentController = [[PTDocumentViewController alloc] init];
// The PTDocumentViewController must be in a navigation controller before a document can be opened
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:documentController];
// Open an existing local file URL.
NSURL *fileURL = [[NSBundle mainBundle] URLForResource:@"Sample" withExtension:@"pdf"];
[documentController openDocumentWithURL:fileURL];
// Show navigation (and document) controller.
[self presentViewController:navigationController animated:YES completion:nil];
Here's what it looks like:
You can read more about displaying a PDF in our documentation.
Apryse’s iOS PDF library can also convert a variety of other file formats to PDF for viewing, such as images (.jpeg, .jpg, .gif, .png, .bmp etc.), markdown (.md) or comic book archive (.cbz). Here's how:
//@data {"m":true}//
func ConvertNonPDFFile(_ inPath: String, outPath: String, options: PTConversionOptions) {
let documentConversion = PTConvert.streamingPDFConversion(inPath, options: options)
documentConversion?.convert()
let pdfDoc = documentConversion?.getDoc()
pdfDoc?.save(toFile: outPath, flags: e_ptremove_unused.rawValue)
}
//@data {"m":true}//
- (void)convertNonPDFFileWithPath:(NSString *)inPath outPath:(NSString *)outPath options:(PTConversionOptions *)options
{
PTDocumentConversion *documentConversion = [PTConvert StreamingPDFConversion:inPath options:options];
[documentConversion Convert];
PTPDFDoc *pdfDoc = [documentConversion GetDoc];
[pdfDoc SaveToFile:outPath flags:e_ptremove_unused];
}
These converted files can now be viewed in the same way as the PDF.
For a richer document experience, the Apryse SDK makes it easy to add more functionality to the viewer:
Adding an Office document viewer to your iOS app is simple with the Apryse SDK. To give it a try, download a free trial and check out our iOS Office viewer documentation.
For further guidance, please feel free to check out our comprehensive SDK Buying Guide, or get in touch. The developers who helped build our SDK from the ground up would be happy to walk you through your options..
Tags
tutorial
iOS
swift
view
office
Shirley Gong
Related Products
Share this post
PRODUCTS
Platform Integrations
End User Applications
Popular Content