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 Java.
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
try (NexPdfDocument doc = NexPdfDocument.open(Path.of("report.pdf"))) {
Files.write(Path.of("report.docx"), doc.toDocx());
Files.write(Path.of("report.xlsx"), doc.toXlsx()); // one sheet per page, tables rebuilt
Files.write(Path.of("report.pptx"), doc.toPptx()); // one slide per page
}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) |
String html = doc.toReflowHtml("{\"images\":true,\"title\":\"Report\"}");Office, HTML and e-books to PDF
try (NexPdfDocument pdf = NexPdfDocument.openOffice(Files.readAllBytes(Path.of("proposal.docx")))) {
pdf.save(Path.of("proposal.pdf")); // Word, Excel or PowerPoint, format detected
}
try (NexPdfDocument invoice = NexPdfDocument.fromHtml("<h1>Invoice 1042</h1><p>Total: 120 EUR</p>")) {
invoice.save(Path.of("invoice.pdf"));
}
try (NexPdfDocument book = NexPdfDocument.openEbook(Files.readAllBytes(Path.of("book.epub")))) {
book.save(Path.of("book.pdf")); // EPUB, FictionBook, XPS/OpenXPS, CBZ
}NexPdfDocument.open and openBytes also convert Word, Excel and PowerPoint files directly. The
same converters exist as static methods of NexPdf.
Word options
NexPdf.openDocxWithOptionsJson(data, optionsJson) converts a Word file with options
(null = defaults):
try (NexPdfDocument pdf = NexPdf.openDocxWithOptionsJson(Files.readAllBytes(Path.of("draft.docx")),
"{\"show_comments\":true,\"show_revisions\":true}")) {
pdf.save(Path.of("draft-markup.pdf"));
}| Option | Default | Meaning |
|---|---|---|
show_comments | false | Draws the 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 | false | Shows 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 resolved state. Word conversion also 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.
Excel formulas
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.
NOW() and TODAY() use the current time. For reproducible output, fix the clock (seconds since
1970-01-01 00:00, no time zone applied):
try (NexPdfDocument pdf = NexPdf.openXlsxWithClock(Files.readAllBytes(Path.of("budget.xlsx")),
1_798_761_600.0)) { // 2027-01-01 00:00
pdf.save(Path.of("budget.pdf"));
}Right-to-left and complex scripts
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.
Images to PDF
try (NexPdfDocument pdf = NexPdfDocument.openImage(Files.readAllBytes(Path.of("scan.tiff")))) {
// one page per picture (multi-page TIFF too)
pdf.insertImagePages(pdf.pageCount(), Files.readAllBytes(Path.of("photo.jpg"))); // append more pictures
pdf.save(Path.of("scans.pdf"));
}Page images
To export pages as PNG, see Rendering.