Conversion
Convert PDFs to Word, Excel, PowerPoint, HTML, JSON, XML, CSV and RTF, and create PDFs from Office files, HTML, e-books and images with NexPDF for iOS (Swift).
License module
Conversion to and from Office, HTML, e-book and data formats needs the conversion module (Startup
tier and above). Images to PDF and Markdown export need no module.
PDF to Office
import NexPdf
let doc = try NexPdfDocument.open(url: reportURL)
try doc.toDocx().write(to: docxURL)
try doc.toXlsx().write(to: xlsxURL) // one sheet per page, tables rebuilt
try doc.toPptx().write(to: pptxURL) // one slide per pageShare the result with a UIActivityViewController or ShareLink.
PDF to text formats
| Method | Output |
|---|---|
toHtml() | HTML |
toReflowHtml(optionsJson:) | Responsive HTML for small screens |
toJson() | The document content as JSON |
toXml() | The document content as XML |
toCsv() | The tables as CSV |
toRtf() | RTF |
extractMarkdown() | Markdown (no module needed) |
let html = try doc.toReflowHtml(optionsJson: #"{"images":true,"title":"Report"}"#)
webView.loadHTMLString(html, baseURL: nil)Office, HTML and e-books to PDF
let proposal = try NexPdfDocument.openOffice(data: try Data(contentsOf: docxURL)) // Word, Excel or PowerPoint
try proposal.write(to: proposalURL)
let invoice = try NexPdfDocument.fromHtml(html: "<h1>Invoice 1042</h1><p>Total: 120 EUR</p>")
try invoice.write(to: invoiceURL)
let book = try NexPdfDocument.openEbook(data: try Data(contentsOf: epubURL)) // EPUB, FictionBook, XPS/OpenXPS, CBZNexPdfDocument.open(data:) and open(url:) also convert Word, Excel and PowerPoint files, and
(since 1.0.1) EPUB, FictionBook, XPS and CBZ files, directly.
Word options
NexPdfDocument.openDocxWithOptionsJson(data:optionsJson:) converts a Word file with options:
let reviewed = try NexPdfDocument.openDocxWithOptionsJson(data: try Data(contentsOf: docxURL),
optionsJson: #"{"show_comments":true,"show_revisions":true}"#)
try reviewed.write(to: reviewURL)| Option | Default | Meaning |
|---|---|---|
show_comments | false | Draw the comments in balloons in a markup area at the right of each page (the page content is scaled down, as Word prints with markup) |
show_revisions | false | Show tracked changes as markup (coloured underlined insertions, struck deletions, change bars) instead of the final document |
Comments are always exported as PDF text annotations anchored at the commented text, with replies
and the resolved state kept. Without show_revisions the document is converted as it reads after
accepting every change.
Word conversion also lays out section columns (equal or unequal, with separators, column breaks and continuous section breaks), conditional table styles (first and last rows and columns, banding), tight and through wrapping along the wrap polygon, floating objects in table cells and text boxes, and tables in text boxes.
Excel formulas and a fixed clock
Formulas without a cached value are calculated; cached values still win. The calculation follows
references across sheets, defined names, shared and array formulas and dynamic arrays that spill
(#SPILL! when blocked), with Excel's error values and about 280 functions: maths, statistics
(including the *IFS family), lookup (XLOOKUP, INDEX, OFFSET, INDIRECT), text (including
TEXT format codes), dates and times, logic, finance, information, FILTER, SORT, UNIQUE,
SEQUENCE, and LET and LAMBDA (named and recursive) with MAP, REDUCE, SCAN, MAKEARRAY,
BYROW, BYCOL and ISOMITTED. Circular references show 0 as Excel does, or iterate when the
workbook turns on iterative calculation. Conditional formatting draws every built-in and x14 icon
set and x14 data bars (negative colours, axis, direction, gradient, border).
NOW() and TODAY() use the current time. For output that does not change from run to run, fix
the clock in Unix seconds (no time zone is applied):
let sheet = try NexPdfDocument.openXlsxWithClock(data: try Data(contentsOf: xlsxURL), clockUnixSeconds: 1_796_000_000)Right-to-left and complex scripts
Word, Excel, PowerPoint and HTML conversion shapes text with OpenType (Arabic joining, Indic
reordering, marks, ligatures) and orders it with the Unicode bidi algorithm: Word w:bidi and
w:rtl, Excel right-to-left sheets and readingOrder, PowerPoint right-to-left paragraphs and HTML
dir and direction.
Images to PDF
let pdf = try NexPdfDocument.openImage(data: try Data(contentsOf: tiffURL)) // one page per picture (multi-page TIFF too)
try pdf.insertImagePages(index: pdf.pages, data: jpegData) // append more pictures
try pdf.write(to: scansURL)A camera photo from UIImage can be passed as image.jpegData(compressionQuality: 0.9).
Page images
To export pages as PNG, see Rendering.