Aspose.Cells for.net (click to download) is an Excel spreadsheet programming API that speeds up spreadsheet management and processing tasks and enables building cross-platform applications with the ability to generate, modify, transform, render, and print spreadsheets.

Convert Excel workbook to PDF

PDF files are widely used to exchange documents between organizations, government departments, and individuals. It is a standard document format, and software developers are often asked to find a way to convert Microsoft Excel files into PDF documents. Aspose.Cells supports conversion of Excel files to PDF and maintains high visual fidelity during conversion.

Direct conversion

Aspose.Cells for.net supports conversion from spreadsheets to PDF independently of other software. Simply use the Workbook class ‘Save method to Save the Excel file as a PDF. The Save method provides the saveFormat.pdf enumerator to convert native Excel files to Pdf format.

  1. Workbook instantiates objects of that class by calling its empty constructor.
  2. To create a workbook from scratch, you can open/load an existing template file or skip this step.
  3. Use the Aspose.Cells API to do anything on a spreadsheet (enter data, apply formatting, set formulas, insert images or other drawing objects, etc.).
  4. When the spreadsheet code is complete, call the Save method of the Workbook class to Save the spreadsheet.

The file format should be PDF, so PDF is selected from the SaveFormat enumeration (predefined values) to produce the final PDF document.

/ / the document directory path string dataDir = RunExamples. GetDataDir (System. Reflection. MethodBase. GetCurrentMethod (). The DeclaringType); Workbook Workbook = new Workbook(dataDir +"Book1.xls"); // Save the document in PDF format.Save(dataDir +)"output.pdf", SaveFormat.Pdf);Copy the code

Advanced conversion

Use the PdfSaveOptions class to set different properties for the transformation. By setting different properties of the PdfSaveOptions class, you can control the printing, font, security, and compression Settings of the output PDF. The most important property is Compliance, which allows you to save Excel files as PDF/A-compatible PDF files.

Save the workbook as A PDF/A compilation file

/ / the document directory path string dataDir = RunExamples. GetDataDir (System. Reflection. MethodBase. GetCurrentMethod (). The DeclaringType); Workbook = new Workbook(); Cells[0, 0].putValue (workbook.worksheets [0].cells [0, 0].putValue ("Testing PDF/A"); // define PdfSaveOptions PdfSaveOptions PdfSaveOptions = new PdfSaveOptions(); / / set the compliance type pdfSaveOptions.Com pliance = PdfCompliance. PdfA1b; // Save the file workbook.save (dataDir +)"output.pdf", pdfSaveOptions);Copy the code

Set the creation time of the PDF

string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
string inputPath = dataDir + "Book1.xlsx"; Workbook Workbook = new Workbook(inputPath); // Create an instance of PdfSaveOptions and pass SaveFormat to the constructor PdfSaveOptions options = new PdfSaveOptions(saveformat.pdf); options.CreatedTime = DateTime.Now; // Save the workbook as a PDF workbook.save (dataDir +) when passing the PdfSaveOptions object"output.pdf", options);Copy the code

Export custom attributes as PDF

The PdfSaveOptions class allows you to export custom properties from a source workbook to a PDF. Derived PdfCustomPropertiesExport enumeration is used to specify properties. You can view these properties in Adobe Acrobat Reader by clicking File and then clicking the Properties option, as shown in the figure below. The template file “sourceWithCustProps. XLSX” can be downloaded here for testing and output. The Pdf file “outSourceWithCustProps” can be analyzed here.

Workbook Workbook = new Workbook("sourceWithCustProps.xlsx"); / / create PdfSaveOptions instance and pass SaveFormat constructor Aspose. Cells. PdfSaveOptions PdfSaveOptions = new Aspose.Cells.PdfSaveOptions(Aspose.Cells.SaveFormat.Pdf); / / will CustomPropertiesExport attribute is set to PdfCustomPropertiesExport. Standard pdfSaveOptions. CustomPropertiesExport = Aspose.Cells.Rendering.PdfCustomPropertiesExport.Standard; // Save the workbook as a PDF when passing the PdfSaveOptions object"outSourceWithCustProps.pdf", pdfSaveOptions);Copy the code