SelectPdf 26.6: Excel and PowerPoint to PDF, PDF to Word, validation and XFA forms

SelectPdf 26.6: Excel and PowerPoint to PDF, PDF to Word, validation and XFA forms

selectpdf HTML to PDF Converter

SelectPdf 26.6 is out. The cross-platform SelectPdf.Universal library now converts Excel workbooks and PowerPoint presentations to PDF, and PDF documents back to Word, Excel and PowerPoint. It checks a document against PDF/A, PDF/UA and PDF/X and fixes what fails, fills XFA forms, and cleans PDF files that come from an untrusted source. Word to PDF gets much closer to Word’s own output, and damaged PDF files that other readers open now open in SelectPdf too.

The short version

  • Excel and PowerPoint to PDF. xlsx, xls, csv and more, and pptx slides, notes pages or handouts, on Windows, Linux and macOS, with no Office to install and no browser to start.
  • PDF to Word, Excel and PowerPoint. Editable documents, with tables as cells and a slide per page.
  • Validation and preflight for PDF/A, PDF/UA and PDF/X: every rule a document fails, and a fix for what can be fixed.
  • XFA forms filled through the same field types as AcroForms, with the form’s own scripts running.
  • Sanitize untrusted PDFs: scan for JavaScript, embedded files and actions, then remove them or rasterize every page.
  • More for existing PDFs: split, extract images, read every word with its position, edit page content and layers, Fast Web View, certification signatures.
  • Word to PDF draws SVG pictures as vectors and sets lines and fields the way Word does.
  • .NET 11 is supported.

Excel and PowerPoint to PDF

ExcelToPdf converts xlsx, xlsm, xls, csv, tsv and other workbooks. Every visible sheet is printed as Excel prints it, with its print area, page breaks and own headers and footers, or laid out on the converter’s page, and each sheet can get its own entry in the document outline:

using SelectPdf.Universal;

ExcelToPdf converter = new ExcelToPdf();

// every sheet keeps its own paper and margins; all its columns fit the width of a page
converter.Options.Layout = ExcelLayout.FitAllColumnsOnOnePage;
converter.Options.PdfDocumentInformation.Title = "Quarterly sales by region";

PdfDocument doc = converter.ConvertFile("sales.xlsx");
doc.Save("sales.pdf");
doc.Close();

PowerPointToPdf does the same for pptx, pptm, ppsx and the other presentation formats: one page per slide, the notes pages, or handouts with 1 to 9 slides on a page. Both converters share the HTML converter’s document setup — margins, headers and footers, PDF/A, PDF/X, security — and both produce tagged, accessible documents, declared as PDF/UA-1 or PDF/UA-2. A workbook or a presentation can also be drawn into a region of a page, or into the header or footer of any conversion, with PdfExcelElement and PdfPowerPointElement.

Step by step: Excel to PDF in C# and PowerPoint to PDF in C#. Overview: Excel to PDF for .NET and PowerPoint to PDF for .NET.

PDF to Word, Excel and PowerPoint

The other direction is new too. PdfToWord writes Word (.docx), RTF, OpenDocument, HTML or Markdown, either as flowing text to edit or with every page reproduced exactly. PdfToExcel writes the document’s tables as cells with typed values, and PdfToPowerPoint makes a slide of every page:

PdfToWord converter = new PdfToWord();

// Flowing (the default): paragraphs that reflow as you edit, as in any Word document
converter.Options.Layout = PdfToWordLayout.Flowing;
converter.Options.Format = PdfToWordFormat.Docx;

byte[] docx = converter.ConvertFile("report.pdf");
File.WriteAllBytes("report.docx", docx);

Step by step: PDF to Word in C#. Overview: PDF to Word, PDF to Excel and PDF to PowerPoint for .NET.

Check a PDF against PDF/A, PDF/UA and PDF/X, and fix it

PdfValidator checks an existing document against PDF/A (parts 1 to 4, every conformance level), PDF/UA-1 and PDF/UA-2, and PDF/X-1a, PDF/X-3 and PDF/X-4. It reports every rule the document fails: what the rule requires, why the object fails it, and where the object is. PdfPreflight.Fix then fixes the document for the standard you need and validates the result again, listing what still needs a person, such as a picture’s alternative text. PDF/A and PDF/UA results match veraPDF’s.

byte[] pdf = File.ReadAllBytes("annual-report.pdf");

// validate against PDF/A-2b; the file is only read, never changed
ValidationReport report = PdfValidator.Validate(pdf, null, PdfConformanceStandard.PdfA2B);
Console.WriteLine($"{report.ChecksFailed} checks failed");

// fix it for PDF/A-2b; the fixed document is validated again
PreflightResult result = PdfPreflight.Fix(pdf, null, PdfConformanceStandard.PdfA2B);
if (result.After.IsCompliant)
    File.WriteAllBytes("annual-report-pdfa2b.pdf", result.Pdf);

Step by step: Validate PDF/A compliance in C# and fix what fails.

XFA forms

Dynamic XFA forms, the forms that lay themselves out as Adobe LiveCycle Designer makes them, used to show no fields at all. PdfFormManager now fills them through the same field types as an AcroForm, named by their path in the form. Setting a value runs the form’s own scripts, its calculations and its validation, and the form is saved as the same dynamic form or flattened into the pages it lays out:

PdfFormManager form = new PdfFormManager();
form.Load("xfa-form.pdf");

((PdfFormFieldTextBox)form.Fields["form1.Page1.Name"]).Text = "Jane Doe";
// setting the quantity runs the form's own calculation of the total
((PdfFormFieldTextBox)form.Fields["Quantity"]).Text = "4";
Console.WriteLine($"Total: {form.Fields["Total"].FormattedValue}");

form.Save("xfa-form-filled.pdf");
form.Close();

A form set in Myriad Pro, LiveCycle Designer’s default font, keeps its layout on a server without Acrobat, and an XFA form can be signed. Step by step: Fill a dynamic XFA form in C#.

Sanitize PDFs from an untrusted source

For uploads and e-mail attachments, PdfSanitizeManager scans a document and reports what it holds, so a server can refuse a file instead of cleaning it. Or it removes JavaScript, actions that leave the document, embedded files, multimedia, XFA, hidden layers and private data, and writes the file whole. For the strictest case, it replaces every page with an image of itself.

PdfSanitizeManager sanitizer = new PdfSanitizeManager();
sanitizer.Load("untrusted.pdf");

PdfSanitizeReport found = sanitizer.Scan();
Console.WriteLine($"Active content: {found.HasActiveContent}");

sanitizer.Options = PdfSanitizeOptions.Default;
sanitizer.Sanitize();
sanitizer.Save("sanitized.pdf");
sanitizer.Close();

Step by step: Sanitize a PDF from an untrusted source in C#.

More for existing PDFs

  • Split a document by page ranges, odd or even pages, a fixed page count, its top level bookmarks or a file size, with PdfSplitManager.
  • Extract the images of a document with PdfImageExtractor.
  • Every word with its position and font, in reading order, with PdfToText.GetWords.
  • Edit what a page draws: PdfPage.ObjectModel moves, resizes, recolors, replaces or removes its text, images and paths, and PdfDocument.Layers adds, shows, hides and flattens layers.
  • Fix the alternative text of the images in a tagged document with PdfAccessibilityManager.
  • Fast Web View: with Linearize, Chrome, Edge and Acrobat show the first page of a large document before the rest has arrived.
  • Security and signatures: read a document’s security with GetSecurityInfo, remove it with RemoveSecurity, add empty signature fields to sign later, and certify a document with a certification signature.
  • Damaged files: a damaged PDF file that other readers open is opened too, its structure rebuilt from the objects it contains.

Word to PDF, closer to Word

SVG pictures, shapes and SmartArt are drawn as vectors instead of from the PNG copy Word stores beside them. Lines are spaced as Word sets them for fonts such as Aptos and Montserrat, charts take their own theme colors, EMF+ pictures are drawn, Indic scripts are shaped more accurately, and Chinese, Japanese and Korean fonts named in their own language are found. Fields are now updated as Word does when it saves a PDF (see below).

Packages and .NET

  • .NET 11 is supported: applications targeting net11.0 use the library unchanged. SelectPdf.Universal targets .NET 8, .NET 10 and .NET Standard 2.0, so it runs from .NET Framework 4.6.1 to the current .NET releases.
  • A Tools package for every platform, SelectPdf.Universal.Tools.<rid>: the PDF tool for PDF to text, PDF to image and image extraction, without the Chromium engine, for applications that only work with existing documents. The engine and platform packages already include it, so nothing changes for them.
  • packages.config projects (ASP.NET Web Forms, MVC 5, desktop applications) now get everything in their bin folder.

The Community Edition

The free SelectPdf.HtmlToPdf.Universal gets Fast Web View, waits for web fonts that start loading after the page has loaded (WaitForWebFonts, on by default), prints right-to-left pages in place, converts pages that call window.print() while they load, and supports .NET 11. Excel and PowerPoint to PDF, PDF to Office, validation, XFA forms and sanitizing are in the full SelectPdf.Universal library.

The classic library

The classic Windows library is 26.6 too. Its Chromium and Blink engine packages now deploy the engine in packages.config projects (Web Forms, MVC 5 and older desktop applications) on build and publish, and it supports .NET 11. The Chromium engine waits for web fonts still loading, and no longer draws a sticky header over the content, shifts right-to-left pages or hangs on window.print(). The commercial library opens encrypted PDFs in every viewer, accepts AES-256 passwords with non-ASCII characters, keeps a protected document’s security on save and can remove it, produces signatures validators accept, and keeps file attachments in PDF/A-3.

Worth knowing before you upgrade

Code written for 26.5 compiles unchanged. A few things behave differently:

  • Word to PDF updates a document’s fields as Word does when it saves a PDF: dates, page numbers, cross-references and IF fields are updated, and formulas, document properties and merge fields keep the result saved in the document. Set UpdateFields to true to recalculate every field as before.
  • ConvertToPdfA refuses a document that arrived signed, since the conversion would invalidate the signature: convert first, then sign.
  • Pages appended from another document no longer get the document’s header and footer drawn over their own content.
  • With the remote engine, the library connects to the engine directly instead of through the system’s HTTP proxy.

The full list is in the release notes: SelectPdf.Universal, Community Edition and the classic library.

Get 26.6

SelectPdf 26.6 is on NuGet now: SelectPdf.Universal (or one platform package, such as SelectPdf.Universal.Windows or SelectPdf.Universal.Linux) and the free SelectPdf.HtmlToPdf.Universal. One perpetual license, from $499 for a single developer, covers both SelectPdf.Universal and the classic Windows library. A free trial is available, and the how-to guides have the complete code for each new feature, tested on Windows, Linux and macOS.

Questions about upgrading, or about a setup not covered here? Get in touch: support@selectpdf.com.