dotnet add package \ RecursiveAI.OneDocument.Rendering
Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using RecursiveAI.OneDocument; Office.Convert("budget.xlsx", "budget.pdf");
Writes budget.pdf.
Download this example (.zip): unzip, then dotnet run.
The full program
The download creates budget.xlsx, calculates its SUM formula, saves it, and converts it to PDF. It reports the page count, the stored total and any font substitution.
using RecursiveAI.OneDocument; using RecursiveAI.OneDocument.Spreadsheets; using RecursiveAI.OneDocument.Rendering.Spreadsheets; using (Workbook made = Workbook.Create()) { Worksheet sheet = made.Sheets[0]; sheet.Rename("Budget"); sheet.Range("A1:B3").Values = new object?[,] { { "Item", "Cost" }, { "Ladder", 189.5 }, { "Bulbs", 42 } }; sheet["A4"].SetString("Total"); sheet["B4"].Formula = "SUM(B2:B3)"; sheet.Range("B2:B4").NumberFormat = "$#,##0.00"; made.Recalculate(); made.SaveAs("budget.xlsx"); } Office.Convert("budget.xlsx", "budget.pdf"); using Workbook book = Workbook.Open("budget.xlsx"); var layout = WorkbookRenderer.Layout(book); Console.WriteLine($"Wrote budget.pdf ({layout.Pages.Count} page)"); Console.WriteLine($"Printed total: {book.Sheets["Budget"]["B4"].FormattedText}"); foreach (var note in layout.FontNotes) Console.WriteLine($"Font: {note.Requested} -> {note.Actual} ({note.Kind})");
dotnet run prints:
Wrote budget.pdf (1 page) Printed total: $231.50 Font: Calibri -> Carlito (MetricCompatible)
The sample uses Carlito in place of Calibri. Rendering reads cached cell values and never recalculates; a missing input file raises FileNotFoundException.
Variations
Recalculate an edited workbook before exporting.
using RecursiveAI.OneDocument.Spreadsheets; using var book = Workbook.Open("budget.xlsx"); book.Sheets["Budget"]["B3"].Value = 58; book.Recalculate(); book.SaveAs("updated.pdf"); Console.WriteLine(book.Sheets["Budget"]["B4"].FormattedText);
$247.50Writes updated.pdf with the new total, without saving changes over budget.xlsx.
Related recipes
- Convert DOCX to PDF in C#
- Read an Excel file in C#
- Create an Excel XLSX file in C#
- Read a Word DOCX document in C#
The short answer
To convert Excel to PDF in C#, install OneDocument’s Rendering package and call Office.Convert("budget.xlsx", "budget.pdf"). It brings in the Spreadsheets dependency to read the workbook, then paginates it and writes PDF pages. You do not need an installed copy of Excel. The download creates a workbook with a calculated total and shows exactly which font was substituted during layout.
For a workbook you have edited in memory, call Recalculate and then SaveAs("updated.pdf"). Once the Rendering package is referenced, SaveAs picks PDF from the .pdf extension. The spreadsheet rendering documentation is explicit: “Rendering reads the cached cell values and never recalculates.” A formula expression and its saved result are different things. A successful PDF export does not mean a stale result was updated.
How do I convert Excel to PDF in C# for free?
OneDocument is free and open source under MIT or Apache-2.0. Use the published .NET 10 packages and the package reference shown above. No setup call is needed.
The full program saves budget.xlsx before converting it. That gives the path-based converter a file with current cached results. The variation instead edits an already-open workbook and renders that model directly. Its changed total is written to updated.pdf; budget.xlsx is not overwritten. Choose the route that matches where your application currently holds the data.
PDF output is a print view, not an interactive spreadsheet. The renderer lays out visible cell content and uses number formats to form the printed values. Keep the XLSX too if the recipient needs to edit cells or formulas. For formula text that you supply, the README’s scope is “en-US and invariant only; localized function names are not supported.” The sample uses SUM in that invariant syntax.
How do I convert Excel to PDF with only the selected area?
Set the workbook’s print area before rendering. _xlnm.Print_Area is the workbook’s built-in name for the range of cells to print on a particular sheet. The renderer honors that range; when several areas are defined, it paginates each separately and prints them one after another. Without a print area, it uses the used range starting at A1. That range can include cells with visible fills or borders, merged ranges and drawing anchors, not only cells containing values.
The print rendering rules also describe repeated title rows and columns, page margins, orientation, scale, fit-to-page and manual breaks. These are workbook print settings, not a crop applied after producing the PDF. If a wide report unexpectedly spreads across pages, check the print area and page setup before changing the source data.
By default every visible worksheet prints in tab order. Hidden sheets are excluded unless explicitly requested through rendering options. An empty workbook produces one blank Letter page. The sample’s one-page output follows from its small data range; it is not a promise that arbitrary workbooks fit on a single page.
With book opened as in the variation, print only the header and two item rows, leaving out the total:
book.Names.AddBuiltIn(BuiltInName.PrintArea, book.Sheets["Budget"], "Budget!$A$1:$B$3"); book.SaveAs("area.pdf"); Console.WriteLine("Wrote area.pdf");
Wrote area.pdfCan I put formulas in a PDF?
For this conversion path, the PDF shows formula results, not live spreadsheet formulas. The sample’s total is $231.50, calculated before rendering. After changing the second amount to 58 and recalculating, the variation prints $247.50. If you omit recalculation after an edit, the renderer continues to use the stored result.
Fonts affect the printed result too. This workbook names Calibri; the default font library supplies Carlito as its metric-compatible substitute. The font table defines that kind as “same advance widths by design,” not identical letter shapes. Other substitutions may be approximate and change widths. Review font and layout notes on your own workbooks, and supply appropriately licensed fonts when a specific face is required.
A value too wide for its cell can print as hash marks, while unwrapped text may be clipped or spill into adjacent empty cells according to the layout rules. Widening a column or adjusting the source print setup is a more useful fix than assuming the PDF writer lost the value.