Conversion
Convert PDFs to Word, Excel, PowerPoint, HTML, JSON and Markdown, and create PDFs from Office files, HTML and images with NexPDF for Android.
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 com.nexdevit.nexpdf.NexPdfDocument
import com.nexdevit.nexpdf.NexPdfJni
NexPdfDocument.openBytes(bytes).use { doc ->
val docx = doc.toDocx()
val xlsx = NexPdfJni.toXlsx(doc.handle()) // one sheet per page, tables rebuilt
val pptx = NexPdfJni.toPptx(doc.handle()) // one slide per page
}PDF to text formats
| Call | Output |
|---|---|
doc.toJson() | The document content as JSON |
doc.toMarkdown() | Markdown (no module needed) |
NexPdfJni.reflowHtml(handle, optionsJson) | Responsive HTML for small screens |
NexPdfJni.toHtml(handle), toXml, toCsv, toRtf | HTML, XML, the tables as CSV, RTF |
val html = NexPdfJni.reflowHtml(doc.handle(), """{"images":true,"title":"Report"}""")
webView.loadDataWithBaseURL(null, html, "text/html", "UTF-8", null)For HTML, XML, CSV and RTF, call the NexPdfJni methods on the handle. An empty string means the
call failed; NexPdfJni.lastError() says why:
val csv = NexPdfJni.toCsv(doc.handle())
val xml = NexPdfJni.toXml(doc.handle())Office and HTML to PDF
NexPdfDocument.openOffice(docxBytes).use { pdf -> // Word, Excel or PowerPoint, format detected
val proposal = pdf.saveBytes()
}
NexPdfDocument.fromHtml("<h1>Invoice 1042</h1><p>Total: 120 EUR</p>").use { pdf ->
val invoice = pdf.saveBytes()
}NexPdfDocument.openBytes also converts e-books (EPUB, FictionBook, XPS/OpenXPS, CBZ). To skip the
format detection, call NexPdfDocument.openEbook directly:
NexPdfDocument.openEbook(epubBytes).use { book ->
val bookPdf = book.saveBytes()
}Word and Excel options
Word conversion lays out section columns (equal or unequal, separators, column and continuous breaks), conditional table styles with banding, tight and through wrapping along wrap polygons, floats in table cells and text boxes, and tables in text boxes. Comments are always exported as PDF text annotations anchored at the commented text, with replies and resolved state.
Excel formulas without a cached value are calculated; cached values still win. The calculation
covers references across sheets, defined names, shared and array formulas, dynamic arrays that
spill (#SPILL!), Excel error values, about 280 functions (including *IFS, XLOOKUP, INDEX,
OFFSET, INDIRECT, TEXT format codes, dates, finance and FILTER/SORT/UNIQUE/SEQUENCE),
LET and LAMBDA (named and recursive), 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.
Word, Excel, PowerPoint and HTML conversion shape text with OpenType (Arabic joining, Indic
reordering, marks, ligatures) and order it with the full Unicode bidi algorithm, following the
document's directions: w:bidi and w:rtl in Word, right-to-left sheets and readingOrder in
Excel, right-to-left paragraphs in PowerPoint, dir and direction in HTML.
NexPdfJni has no typed method for the two conversions with options. Reach them through
NexPdfJni.invoke, the whole C API as one JSON call. The documents it returns are its own
handles, not NexPdfDocument handles, so save them through invoke too:
| C function | Does |
|---|---|
open_docx_with_options_json(data, options_json) | Word with {"show_comments":false,"show_revisions":false}. show_comments draws comments in balloons in a markup area at the right of each page, the page content scaled down, as Word prints with markup. show_revisions shows tracked changes as markup (coloured underlined insertions, struck deletions, change bars) instead of the final document |
open_xlsx_with_clock(data, clock_unix_seconds) | Excel with NOW() and TODAY() fixed to that time (seconds since 1970-01-01 00:00, no time zone applied), for reproducible output |
import android.util.Base64
import org.json.JSONArray
import org.json.JSONObject
fun call(fn: String, vararg args: Any?): Any? {
val request = JSONObject().put("fn", fn).put("args", JSONArray(args.toList()))
val response = JSONObject(NexPdfJni.invoke(request.toString()))
if (!response.getBoolean("ok")) error(response.getString("error"))
return response.opt("result")
}
fun b64(bytes: ByteArray) = Base64.encodeToString(bytes, Base64.NO_WRAP)
val h = call("open_docx_with_options_json", b64(docxBytes), """{"show_comments":true,"show_revisions":true}""")
try {
val markupPdf = Base64.decode(call("save_bytes", h) as String, Base64.DEFAULT)
} finally {
call("close", h)
}
val sheet = call("open_xlsx_with_clock", b64(xlsxBytes), 1_798_761_600.0) // 2027-01-01 00:00; save_bytes, then closeTo fill Word, HTML and PDF templates with JSON data, see Templates.
Images to PDF
NexPdfDocument.openBytes opens JPEG, PNG and TIFF pictures as a PDF with one page per picture
(multi-page TIFF too):
NexPdfDocument.openBytes(tiffBytes).use { pdf ->
NexPdfJni.insertImagePages(pdf.handle(), pdf.pageCount(), jpegBytes) // append more pictures
val scans = pdf.saveBytes()
}Page images
To export pages as PNG, see Rendering.