.NET (1317)
Children categories
This article shows how to place a footnote in a paragraph using Spire.Doc for .NET, for example, after the word "Spire.Doc" in the following example:


Step 1: Load a word document, Sample.docx.
Document document1 = new Document();
document1.LoadFromFile("D:\\Sample.docx",FileFormat.Docx2010);
Step 2: Get the first paragraph of the first section of Sample.docx.
Paragraph paragraph1 = document1.Sections[0].Paragraphs[0];
Step 3: Add a footnote for paragraph1.
Footnote footnote1 = paragraph1.AppendFootnote(FootnoteType.Footnote);
Step 4: Find the word "Spire.Doc" and insert footnote1 after it.
DocumentObject obj=null;
for (int i = 0; i < paragraph1.ChildObjects.Count; i++)
{
obj=paragraph1.ChildObjects[i];
if (obj.DocumentObjectType == DocumentObjectType.TextRange)
{
TextRange textRange = obj as TextRange;
// Find the word "Spire.Doc" in paragraph1
if (textRange.Text == "Spire.Doc")
{
//Set bold format for the word "Spire.Doc"
textRange.CharacterFormat.Bold = true;
//Insert footnote1 after the word "Spire.Doc"
paragraph1.ChildObjects.Insert(i + 1, footnote1);
break;
}
}
}
Step 5: Type the footnote1 text and set the text's FontName, FontSize and Color.
TextRange text = footnote1.TextBody.AddParagraph().AppendText("Welcome to evaluate Spire.Doc");
text.CharacterFormat.FontName = "Arial Black";
text.CharacterFormat.FontSize = 10;
text.CharacterFormat.TextColor = Color.DarkGray;
Step 6: Set FontName, FontSize, Bold and Color for the footnote1 number.
footnote1.MarkerCharacterFormat.FontName = "Calibri"; footnote1.MarkerCharacterFormat.FontSize = 12; footnote1.MarkerCharacterFormat.Bold = true; footnote1.MarkerCharacterFormat.TextColor = Color.DarkGreen;
Step 7: Save Sample.docx to a new document, Footnote.docx.
document1.SaveToFile("D:\\ Footnote.docx"", FileFormat.Docx2010);
Full code:
Document document1 = new Document();
document1.LoadFromFile("D:\\Sample.docx" ,FileFormat.Docx2010);
Paragraph paragraph1= document1.Sections[0].Paragraphs[0];
Footnote footnote1 = paragraph1.AppendFootnote(FootnoteType.Footnote);
DocumentObject obj=null;
for (int i = 0; i < paragraph1.ChildObjects.Count; i++)
{
obj=paragraph1.ChildObjects[i];
if (obj.DocumentObjectType == DocumentObjectType.TextRange)
{
TextRange textRange = obj as TextRange;
// Find the word "Spire.Doc" in paragraph1
if (textRange.Text == "Spire.Doc")
{
//Set bold format for the word "Spire.Doc"
textRange.CharacterFormat.Bold = true;
//Insert footnote1 after the word "Spire.Doc"
paragraph1.ChildObjects.Insert(i + 1, footnote1);
break;
}
}
}
TextRange text = footnote1.TextBody.AddParagraph().AppendText("Welcome to evaluate Spire.Doc");
text.CharacterFormat.FontName = "Arial Black";
text.CharacterFormat.FontSize = 10;
text.CharacterFormat.TextColor = Color.DarkGray;
footnote1.MarkerCharacterFormat.FontName = "Calibri";
footnote1.MarkerCharacterFormat.FontSize = 12;
footnote1.MarkerCharacterFormat.Bold = true;
footnote1.MarkerCharacterFormat.TextColor = Color.DarkGreen;
document1.SaveToFile("Footnote.docx",FileFormat.Docx2010);
PDF/A is an ISO-standardized version of the Portable Document Format (PDF) specialized for the digital preservation of electronic documents. It is widely used for long term archiving for PDF format. This article mainly shows how to convert word document (doc and docx) to PDF/A in C# by using Spire.Doc.
Make sure Spire.Doc for .NET Version 5.0.26 (or above) has been installed correctly and then add Spire.Doc.dll as reference in the downloaded Bin folder though the below path: "..\Spire.Doc\Bin\NET4.0\ Spire.Doc.dll".
First, check the original word document that will be converted to PDF/A.
Here comes to the details of how developers convert word document to PDF/A directly by using Spire.Doc:
Step 1: Load a word document from the file.
Document document = new Document(); document.LoadFromFile(@"D:\test.docx",FileFormat.Docx);
Step 2: Sets the Pdf document's Conformance-level to PDF_A1B.
ToPdfParameterList toPdf = new ToPdfParameterList(); toPdf.PdfConformanceLevel = Spire.Pdf.PdfConformanceLevel.Pdf_A1B;
Step 3: Save word document to PDF
document.SaveToFile("result.Pdf",toPdf);
Please check the effective screenshot of the result PDF in PDF/A format.
When dealing with Word documents, sometimes developers need to merge multiple files into a single file. Spire.Doc, especially designed for developers enables you to manipulate doc files easily and flexibly.
There is already a document introducing how to merge doc files. Check it here:
.NET Merge Word - Merge Multiple Word Documents into One in C# and VB.NET
Using the method above, you have to copy sections one by one. But the new method just concatenates them. It has improved and is very easy to use
Step 1: Load the original word file "A Good Man.docx".
document.LoadFromFile("A Good Man.docx", FileFormat.Docx);
Step 2: Merge another word file "Original Word.docx" to the original one.
document.InsertTextFromFile("Original Word.docx", FileFormat.Docx);
Step 3: Save the file.
document.SaveToFile("MergedFile.docx", FileFormat.Docx);
Full code and screenshot:
static void Main(string[] args)
{
Document document = new Document();
document.LoadFromFile("A Good Man.docx", FileFormat.Docx);
document.InsertTextFromFile("Original Word.docx", FileFormat.Docx);
document.SaveToFile("MergedFile.docx", FileFormat.Docx);
System.Diagnostics.Process.Start("MergedFile.docx");
}
Full code and screenshot:
using Spire.Doc;
namespace MergeWord
{
class Program
{
static void Main(string[] args)
{
Document document = new Document();
document.LoadFromFile("A Good Man.docx", FileFormat.Docx);
document.InsertTextFromFile("Original Word.docx", FileFormat.Docx);
document.SaveToFile("MergedFile.docx", FileFormat.Docx);
System.Diagnostics.Process.Start("MergedFile.docx");
}
}
}

Word Marco is widely used to record the operation that needs to be done repeatedly and you can apply it with only a single click. It can save lots of time for you. It is not an easy work to load a word document with Macro in C# and sometimes, you need to remove the Marco in the word documents in C#. This article will focus on demonstrate how to load and save word document with Marco, and clear the Marco by using Spire.Doc in C# in simple lines of codes.
First, download Spire.Doc and install on your system. The Spire.Doc installation is clean, professional and wrapped up in a MSI installer.
Then adds Spire.Doc.dll as reference in the downloaded Bin folder though the below path: "..\Spire.Doc\Bin\NET4.0\ Spire.Doc.dll".
Here comes to the steps.
Step 1: Load and save the document with Marco. Spire.Doc for .NET supports .doc, .docx(Word 97-2003) document with macros and .docm(Word 2007 and Word 2010) document.
//Loading document with macros.
document.LoadFromFile(@"D:\Macros.docm", FileFormat.Docm);
//Save docm file.
document.SaveToFile("Sample.docm", FileFormat.Docm);

Step 2: Clear the Marco in word document. With Spire.Doc, you only need one line of code to remove all the Marcos at one time.
//Removes the macros from the document
document.ClearMacros();
//Save docm file.
document.SaveToFile("Sample.docm", FileFormat.Docm);
Here comes to the screenshot which has removed the Marco in word document.

Insert Image at Specified Location in C#, VB.NET
2014-03-06 02:26:37Image can add interest and take effect to your Word documents. Suppose you've written an article introducing a city about its beautiful scenery. Just using words describe, you cannot present perfect scenery to your readers, because that page of text looks indistinct and dull. You need image to embellish your scenery.
Spire.Doc for .NET, a professional .NET word component to fast generate, open, modify and save Word documents without using MS Office Automation, enables users to insert image into Word document and set its size according to page by using C#.NET. This guide introduces an easy method how to insert image via Spire.Doc for .NET.
At first, create new Word document and add section, paragraph for this document. Then, insert image in the new created paragraph. You can set this image's size, position, rotate it and choice the wrapping-style about the text. Download and Install Spire.Doc for .NET. Use the following code to insert image in Word by using C#.
The sample demo shows how to insert an image into a document.

using System;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;
namespace Insert_image_to_Word_Document
{
class Program
{
static void Main(string[] args)
{
//Open a blank word document as template
Document document = new Document(@"Blank.doc");
Section section = document.Sections[0];
Paragraph paragraph
= section.Paragraphs.Count > 0 ? section.Paragraphs[0] : section.AddParagraph();
paragraph.AppendText("The sample demonstrates how to insert an image into a document.");
paragraph.ApplyStyle(BuiltinStyle.Heading2);
paragraph = section.AddParagraph();
//get original image
Bitmap p = new Bitmap(Image.FromFile(@"Word.jpg"));
//rotate image and insert image to word document
p.RotateFlip(RotateFlipType.Rotate90FlipX);
DocPicture picture = document.Sections[0].Paragraphs[0].AppendPicture(p);
//set image's position
picture.HorizontalPosition = 50.0F;
picture.VerticalPosition = 60.0F;
//set image's size
picture.Width = 200;
picture.Height = 200;
//set textWrappingStyle with image;
picture.TextWrappingStyle = TextWrappingStyle.Through;
//Save doc file.
document.SaveToFile("Sample.doc", FileFormat.Doc);
//Launching the MS Word file. System.Diagnostics.Process.Start("Sample.doc");
}
}
}
If you have finished the tutorial Spire.Doc Quick Start, the steps above will be simpler.
With Spire.Doc, you can insert an image into your Word documents and do more same thing in your ASP.NET, WPF and Silverlight applications without Word automation and any other third party add-ins.
When uploading or submitting PDF files on certain platforms, you are sometimes faced with the dilemma that the platforms require a specific version of PDF file. If your PDF files fail to meet the requirements, it is necessary to convert them to a different version for compatibility purposes. This article will demonstrate how to programmatically convert PDF between different versions using Spire.PDF for .NET.
Install Spire.PDF for .NET
To begin with, you need to add the DLL files included in the Spire.PDF for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.PDF
Change PDF Version in C# and VB.NET
Spire.PDF for .NET supports PDF versions from 1.0 to 1.7. To convert a PDF file to a newer or older version, you can use the PdfDocument.FileInfo.Version property. The following are the detailed steps.
- Create a PdfDocument object.
- Load a sample PDF file using PdfDocument.LoadFromFile() method.
- Change the PDF version to a newer or older version using PdfDocument.FileInfo.Version property.
- Save the result document using PdfDocument.SaveToFile() method.
- C#
- VB.NET
using Spire.Pdf;
namespace ConvertPDFVersion
{
class Program
{
static void Main(string[] args)
{
//Create a PdfDocument object
PdfDocument pdf = new PdfDocument();
//Load a sample PDF file
pdf.LoadFromFile("sample.pdf");
//Change the PDF to version 1.7
pdf.FileInfo.Version = PdfVersion.Version1_7;
//Save the result file
pdf.SaveToFile("PDFVersion.pdf");
}
}
}

Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
Document properties (also known as metadata) are a set of information about a document. All Word documents come with a set of built-in document properties, including title, author name, subject, keywords, etc. In addition to the built-in document properties, Microsoft Word also allows users to add custom document properties to Word documents. In this article, we will explain how to add these document properties to Word documents in C# and VB.NET using Spire.Doc for .NET.
- Add Built-in Document Properties to a Word Document
- Add Custom Document Properties to a Word Document
Install Spire.Doc for .NET
To begin with, you need to add the DLL files included in the Spire.Doc for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.Doc
Add Built-in Document Properties to a Word Document in C# and VB.NET
A built-in document property consists of a name and a value. You cannot set or change the name of a built-in document property as it's predefined by Microsoft Word, but you can set or change its value. The following steps demonstrate how to set values for built-in document properties in a Word document:
- Initialize an instance of Document class.
- Load a Word document using Document.LoadFromFile() method.
- Get the built-in document properties of the document through Document.BuiltinDocumentProperties property.
- Set values for specific document properties such as title, subject and author through Title, Subject and Author properties of BuiltinDocumentProperties class.
- Save the result document using Document.SaveToFile() method.
- C#
- VB.NET
using Spire.Doc;
namespace BuiltinDocumentProperties
{
class Program
{
static void Main(string[] args)
{
//Create a Document instance
Document document = new Document();
//Load a Word document
document.LoadFromFile("Sample.docx");
//Add built-in document properties to the document
BuiltinDocumentProperties standardProperties = document.BuiltinDocumentProperties;
standardProperties.Title = "Add Document Properties";
standardProperties.Subject = "C# Example";
standardProperties.Author = "James";
standardProperties.Company = "Eiceblue";
standardProperties.Manager = "Michael";
standardProperties.Category = "Document Manipulation";
standardProperties.Keywords = "C#, Word, Document Properties";
standardProperties.Comments = "This article shows how to add document properties";
//Save the result document
document.SaveToFile("StandardDocumentProperties.docx", FileFormat.Docx2013);
}
}
}

Add Custom Document Properties to a Word Document in C# and VB.NET
A custom document property can be defined by a document author or user. Each custom document property should contain a name, a value and a data type. The data type can be one of these four types: Text, Date, Number and Yes or No. The following steps demonstrate how to add custom document properties with different data types to a Word document:
- Initialize an instance of Document class.
- Load a Word document using Document.LoadFromFile() method.
- Get the custom document properties of the document through Document.CustomDocumentProperties property.
- Add custom document properties with different data types to the document using CustomDocumentProperties.Add(string, object) method.
- Save the result document using Document.SaveToFile() method.
- C#
- VB.NET
using Spire.Doc;
using System;
namespace CustomDocumentProperties
{
class Program
{
static void Main(string[] args)
{
//Create a Document instance
Document document = new Document();
//Load a Word document
document.LoadFromFile("Sample.docx");
//Add custom document properties to the document
CustomDocumentProperties customProperties = document.CustomDocumentProperties;
customProperties.Add("Document ID", 1);
customProperties.Add("Authorized", true);
customProperties.Add("Authorized By", "John Smith");
customProperties.Add("Authorized Date", DateTime.Today);
//Save the result document
document.SaveToFile("CustomDocumentProperties.docx", FileFormat.Docx2013);
}
}
}

Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
A document can have one or more pages. It is probably easy to add a header for all pages of the document. If you want to add the header only for the first page of the document, Spire.Doc for .NET component can provide you an easy and flexible solution to handle it. The following steps will guide how to add a header into the first page of a document using Spire.Doc for .NET component in C#. In the example, the header is got from an existing document.
Step 1: Load a word document, documen1.docx.
Document document1 = new Document();
document1.LoadFromFile("D:\\document1.docx");
Step 2: Get the header of document1.docx.
HeaderFooter header = document1.Sections[0].HeadersFooters.Header;
Step 3: Load another word document which will be added the header, document2.docx.
Document document2 = new Document();
document2.LoadFromFile("D:\\document2.docx");
Step 4: Get the first page header of document2.docx.
HeaderFooter firstPageHeader = document2.Sections[0].HeadersFooters.FirstPageHeader;
Step 5: Specify that the current section has a different header/footer for the first page.
foreach (Section section in document2.Sections)
{
section.PageSetup.DifferentFirstPageHeaderFooter = true;
}
Step 6: Removes all child objects in firstPageHeader.
firstPageHeader.Paragraphs.Clear();
Step 7: Add all child objects of the header to firstPageHeader.
foreach (DocumentObject obj in header.ChildObjects)
{
firstPageHeader.ChildObjects.Add(obj.Clone());
}
Step 8: Save document2.docx to a new document, header.docx.
document2.SaveToFile("D:\\Header.docx"", FileFormat.Docx);
Full code:
Document document1 = new Document();
document1.LoadFromFile(@"..\..\document1.docx");
Document document2 = new Document();
document2.LoadFromFile(@"..\..\document2.docx");
HeaderFooter header = document1.Sections[0].HeadersFooters.Header;
HeaderFooter firstPageHeader = document2.Sections[0].HeadersFooters.FirstPageHeader;
foreach (Section section in document2.Sections)
{
section.PageSetup.DifferentFirstPageHeaderFooter = true;
}
firstPageHeader.Paragraphs.Clear();
foreach (DocumentObject obj in header.ChildObjects)
{
firstPageHeader.ChildObjects.Add(obj.Clone());
}
document2.SaveToFile("Header.docx", FileFormat.Docx);
Screenshots:
document1.docx:

document2.docx:

Header.docx:

How to Convert Excel (XLS or XLSX) to PDF in C# .NET: Complete Guide
2025-05-08 08:50:00 Written by Koohji
Converting Excel files to PDF is a crucial task for anyone looking to share spreadsheet data in a secure, consistent, and universally accessible format. Whether you are generating financial reports, creating invoices, or sharing analytical data, PDFs ensure that your document's layout and formatting remain intact across all devices and platforms. Unlike Excel files, which require compatible software to open, PDFs are universally viewable without any dependency.
This guide provides a comprehensive overview of how to efficiently convert Excel files to PDF in C# using a .NET Excel library – Spire.XLS for .NET. You will learn both basic and advanced conversion techniques, including how to export specific sheets or cell ranges, customize page setup, secure converted PDFs with passwords, generate PDF/A-compliant files, and more.
Table of Contents
- Why Convert Excel to PDF
- C# .NET Excel to PDF Conversion Library
- Basic Excel to PDF Conversion
- Advanced Excel to PDF Conversion Features
- FAQs
- Conclusion
Why Convert Excel to PDF
Converting Excel files to PDF offers several key advantages:
- Preserved Layout and Formatting: PDF maintains the original structure and formatting of your Excel file, ensuring consistent appearance across devices.
- Cross-Platform Accessibility: PDF is universally compatible, viewable on any device or operating system without requiring Excel or other spreadsheet software.
- Enhanced Security: PDF files can be encrypted, digitally signed, and restricted to prevent unauthorized access, copying, or editing.
C# .NET Excel to PDF Conversion Library
Spire.XLS for .NET is a comprehensive Excel library that enables seamless conversion of Excel files to PDF within .NET applications without the need for Microsoft Office. It provides developers with full control over how the content is rendered and ensures that the layout and formatting are preserved during the conversion process.
Install Spire.XLS for .NET
Before starting the conversion process, install Spire.XLS for .NET using one of the following methods:
- Option 1: Install via NuGet (Recommended)
Install-Package Spire.XLS
- Option 2: Manually Add DLLs to Your Project
- Download the Spire.XLS package and extract the files.
- In Visual Studio, right-click References > Add Reference > Browse, then select the appropriate Spire.Xls.dll based on your target framework.
Basic Excel to PDF Conversion
Converting an Excel file to PDF with Spire.XLS is simple and requires only a few lines of code. The following example demonstrates how to load an Excel file and save it as a PDF:
- C#
using Spire.Xls;
namespace ExcelToPdf
{
internal class Program
{
static void Main(string[] args)
{
// Create a Workbook object
Workbook workbook = new Workbook();
// Load an Excel file
workbook.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Example.xlsx");
// Save the Excel file to PDF
workbook.SaveToFile("Output.pdf", FileFormat.PDF);
// Dispose resources
workbook.Dispose();
}
}
}

Advanced Excel to PDF Conversion Features
In addition to basic conversion, Spire.XLS for .NET provides advanced options for customized Excel-to-PDF conversions, including:
- Export specific sheet or cell range as PDF.
- Fit sheet to one page.
- Adjust page setup options (e.g., margins, orientation, paper size) for customized PDF output.
- Secure the converted PDF with password.
- Generate PDF/A-compliant files for long-term preservation.
Export Specific Sheet or Cell Range as PDF
Sometimes, you may want to export only a specific sheet or range of cells from an Excel file to PDF. Here's how to do that:
- C#
using Spire.Xls;
namespace WorksheetOrCellRangeToPdf
{
internal class Program
{
static void Main(string[] args)
{
// Create a Workbook object
Workbook workbook = new Workbook();
// Load the Excel file
workbook.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Example.xlsx");
// Get the first worksheet
Worksheet sheet = workbook.Worksheets[0];
// Set the print area to a specific cell range
// Comment this line out if you need to export the entire worksheet as a PDF
sheet.PageSetup.PrintArea = "B1:E6";
// Save the cell range as a PDF
sheet.SaveToPdf("CellRange.pdf");
// Dispose resources
workbook.Dispose();
}
}
}
Fit Sheet to One Page
Spire.XLS allows you to fit the content of a sheet to one page, which is particularly useful for printing or distributing concise reports.
- C#
using Spire.Xls;
namespace FitWorksheetToOnePage
{
internal class Program
{
static void Main(string[] args)
{
// Create a Workbook object
Workbook workbook = new Workbook();
// Load the Excel file
workbook.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Example.xlsx");
// Fit every worksheet in the workbook to one page
workbook.ConverterSetting.SheetFitToPage = true;
// Save the Excel file to PDF
workbook.SaveToFile("FitToOnePage.pdf", FileFormat.PDF);
// Dispose resources
workbook.Dispose();
}
}
}
Adjust Page Setup for Customized PDF Output
Before converting an Excel worksheet to PDF, you can adjust page setup options such as margins, paper size, orientation, and gridline visibility. This ensures the final PDF is accurately formatted to meet your presentation requirements.
- C#
using Spire.Xls;
namespace AdjustPageSetup
{
internal class Program
{
static void Main(string[] args)
{
// Create a Workbook object
Workbook workbook = new Workbook();
// Load the Excel file
workbook.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Example.xlsx");
// Get the first worksheet
Worksheet sheet = workbook.Worksheets[0];
// Adjust page setup settings
// Set page orientation
sheet.PageSetup.Orientation = PageOrientationType.Landscape;
// Set paper size
sheet.PageSetup.PaperSize = PaperSizeType.PaperA4;
// Set margins
sheet.PageSetup.LeftMargin = 0.5;
sheet.PageSetup.RightMargin = 0.5;
sheet.PageSetup.TopMargin = 0.5;
sheet.PageSetup.BottomMargin = 0.5;
// Display Gridlines
sheet.PageSetup.IsPrintGridlines = true;
// Save the worksheet as a PDF
sheet.SaveToPdf("CustomPageSetup.pdf");
// Dispose resources
workbook.Dispose();
}
}
}
Secure the Converted PDF with Password
You can secure the converted PDF by applying password protection. This ensures that unauthorized users cannot access or modify the document.
- C#
using Spire.Xls;
using Spire.Xls.Pdf.Security;
namespace SecurePdfWithPassword
{
internal class Program
{
static void Main(string[] args)
{
// Create a Workbook object
Workbook workbook = new Workbook();
// Load the Excel file
workbook.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Example.xlsx");
// Set the open and permission passwords for the converted PDF
workbook.ConverterSetting.PdfSecurity.Encrypt("openPassword", "persmissionPassword", PdfPermissionsFlags.Print, PdfEncryptionKeySize.Key128Bit);
// Save the Excel file to PDF
workbook.SaveToFile("SecurePdf.pdf", FileFormat.PDF);
// Dispose resources
workbook.Dispose();
}
}
}
Generate PDF/A-compliant Files
If you need to archive your documents for long-term storage or ensure they meet certain accessibility standards, you can generate PDF/A-compliant files from Excel.
- C#
using Spire.Xls;
using Spire.Xls.Pdf;
namespace ExcelToPdfA
{
internal class Program
{
static void Main(string[] args)
{
// Create a Workbook object
Workbook workbook = new Workbook();
// Load the Excel file
workbook.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Example.xlsx");
// Set the compliance for the converted PDF
workbook.ConverterSetting.PdfConformanceLevel = PdfConformanceLevel.Pdf_A1A;
// Save the Excel file to PDF
workbook.SaveToFile("PdfA_Compliant.pdf", FileFormat.PDF);
// Dispose resources
workbook.Dispose();
}
}
}
FAQs
Q1: Can I convert a password-protected Excel file to PDF?
Yes, you can load password-protected Excel files by providing the password before loading:
- C#
Workbook workbook = new Workbook();
workbook.OpenPassword = "ExcelPassword";
workbook.LoadFromFile("ProtectedExcel.xlsx");
workbook.SaveToFile("Output.pdf", FileFormat.PDF);
Q2: Is Spire.XLS compatible with .NET Core?
Yes, Spire.XLS supports .NET Framework, .NET Core, and .NET Standard.
Q3: Can I batch convert multiple Excel files to PDF?
Yes, you can batch convert multiple Excel files to PDF using a loop:
- C#
string[] files = Directory.GetFiles("ExcelFolder", "*.xlsx");
foreach (string file in files)
{
Workbook workbook = new Workbook();
workbook.LoadFromFile(file);
string outputPath = Path.ChangeExtension(file, ".pdf");
workbook.SaveToFile(outputPath, FileFormat.PDF);
}
Q4: Can I convert multiple Excel files to a single PDF?
Yes, you can combine multiple Excel files into a single workbook and then save them as a single PDF:
- C#
Workbook combinedWorkbook = new Workbook();
combinedWorkbook.Worksheets.Clear();
foreach (string file in Directory.GetFiles("ExcelFolder", "*.xlsx"))
{
Workbook tempWorkbook = new Workbook();
tempWorkbook.LoadFromFile(file);
foreach (Worksheet sheet in tempWorkbook.Worksheets)
{
combinedWorkbook.Worksheets.AddCopy(sheet);
}
}
combinedWorkbook.SaveToFile("Combined.pdf", FileFormat.PDF);
Q5: Why does my converted PDF look different from the Excel file?
This issue could be due to missing fonts on the system. Make sure that all fonts used in the Excel file are installed on the machine performing the conversion.
Conclusion
Spire.XLS for .NET provides a powerful and flexible solution for converting Excel files to PDF in C#. Whether you need simple conversions or advanced features—such as exporting specific sheets or cell ranges, customizing page setup, applying password protection to the converted PDF, or generating PDF/A-compliant files—Spire.XLS offers a comprehensive set of tools to meet all your requirements. By following the steps outlined in this guide, you can easily integrate Excel-to-PDF conversion capabilities into your .NET applications.
Get a Free License
To fully experience the capabilities of Spire.XLS for .NET without any evaluation limitations, you can request a free 30-day trial license.
Superscript and subscript are formatting styles that allow you to display characters or numerals above or below the regular text baseline respectively. By utilizing these formatting styles, you can emphasize certain elements, denote exponents, powers, chemical formulas, or mathematical equations, and present data in a more visually appealing and informative manner. In this article, we will demonstrate how to apply superscript and subscript styles in Excel in C# and VB.NET using Spire.XLS for .NET.
Install Spire.XLS for .NET
To begin with, you need to add the DLL files included in the Spire.XLS for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.XLS
Apply Superscript and Subscript in Excel in C# and VB.NET
To apply the superscript or subscript style to specific characters in a cell, you need to create a custom font, enable the superscript or subscript property of the font, and then assign the custom font to the specific characters within the cell. The detailed steps are as follows:
- Create a Workbook object.
- Get a specific worksheet using Workbook.Worksheets[int index] property.
- Get a specific cell using Worksheet.Range[string name] property and add rich text to the cell using CellRange.RichText.Text property.
- Create a custom font using Workbook.CreateFont() method.
- Enable the subscript property of the font by setting ExcelFont.IsSubscript property to true.
- Assign the custom font to specific characters of the rich text in the cell using CellRange.RichText.SetFont() method.
- Get a specific cell using Worksheet.Range[string name] property and add rich text to the cell using CellRange.RichText.Text property.
- Create a custom font using Workbook.CreateFont() method.
- Enable the superscript property of the font by setting ExcelFont.IsSuperscript property to true.
- Assign the custom font to specific characters of the rich text in the cell using CellRange.RichText.SetFont() method.
- Save the result file using Workbook.SaveToFile() method.
- C#
- VB.NET
using Spire.Xls;
using System.Drawing;
namespace ApplySuperscriptAndSubscript
{
internal class Program
{
static void Main(string[] args)
{
//Create a Workbook object
Workbook workbook = new Workbook();
//Get the first worksheet
Worksheet sheet = workbook.Worksheets[0];
//Add text to specific cells
sheet.Range["B2"].Text = "This is an example of subscript:";
sheet.Range["D2"].Text = "This is an example of superscript:";
//Add rich text to a specific cell
CellRange range = sheet.Range["B3"];
range.RichText.Text = "an = Sn - Sn-1";
//Create a custom font
ExcelFont font = workbook.CreateFont();
//Enable the subscript property of the font
font.IsSubscript = true;
//Set font color
font.Color = Color.Green;
//Assign the font to specific characters of the rich text in the cell
range.RichText.SetFont(6, 6, font);
range.RichText.SetFont(11, 13, font);
//Add rich text to a specific cell
range = sheet.Range["D3"];
range.RichText.Text = "a2 + b2 = c2";
//Create a custom font
font = workbook.CreateFont();
//Enable the superscript property of the font
font.IsSuperscript = true;
//Assign the font to specific characters of the rich text in the cell
range.RichText.SetFont(1, 1, font);
range.RichText.SetFont(6, 6, font);
range.RichText.SetFont(11, 11, font);
//Auto-fit column widths
sheet.AllocatedRange.AutoFitColumns();
//Save the result file
workbook.SaveToFile("ApplySubscriptAndSuperscript.xlsx", ExcelVersion.Version2013);
workbook.Dispose();
}
}
}

Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.