.NET (1316)
Children categories

Creating PDFs in C# is a must-have skill for developers, especially when building apps that generate reports, invoices, or other dynamic documents. Libraries like Spire.PDF for .NET simplify the process by offering tools for adding text, tables, and even converting HTML to PDF—helping you create professional documents with minimal effort.
This guide walks you through C# PDF generation step by step, from basic text formatting to advanced features like HTML rendering and tenplate. By the end, you’ll be able to customize and creaete PDFs for any project.
- .NET Library for Creating PDF Documents
- Creating a PDF File from Scratch
- Creating PDF from HTML
- Creating PDF Based on Template
- Conclusion
- FAQs
.NET Library for Creating PDF Documents
Spire.PDF is a powerful and versatile .NET library designed for creating, editing, and manipulating PDF documents programmatically. It provides developers with a comprehensive set of features to generate high-quality PDFs from scratch, modify existing files, and convert various formats into PDF.
Key Features of Spire.PDF
- PDF Creation & Editing – Build PDFs with text, images, tables, lists, and more.
- Rich Formatting Options – Customize fonts, colors, alignment, and layouts.
- HTML to PDF Conversion – Render web pages or HTML content into PDFs with precise formatting.
- Template-Based PDF Generation – Use placeholders in existing PDFs to dynamically insert text and data.
- Interactive Elements – Support for forms, annotations, and bookmarks.
- Document Security – Apply passwords, permissions, and digital signatures.
Installing Spire.PDF
To begin using Spire.PDF to create PDF documents, install it directly through NuGet.
Install-Package Spire.PDF
Alternatively, you can download Spire.PDF from our official website and manually import the DLL in your project.
Creating a PDF File from Scratch in C#
Understanding the Coordinate System
Before delving into the code, it's important to understand the coordinate system used by Spire.PDF. It resembles those in many graphics libraries but has specific PDF considerations. The origin (0,0) is located at the top-left corner of the content area (excluding margins), with positive Y values extending downward.
Understanding this system is essential for accurate element placement when building document layouts.

Note: This coordinate system applies only to newly created pages. In existing PDF pages, the origin is at the top-left corner of the entire page.
Creating a Simple PDF File with Text
Now, let's create our first PDF document. Text content is fundamental to most PDFs, so mastering text handling is crucial.
The following code demonstrates how to create a simple PDF with text in C#:
using Spire.Pdf;
using Spire.Pdf.Graphics;
using System.Drawing;
namespace CreatePdfDocument
{
class Program
{
static void Main(string[] args)
{
// Create a PdfDocument object
PdfDocument doc = new PdfDocument();
// Add a page
PdfPageBase page = doc.Pages.Add(PdfPageSize.A4, new PdfMargins(50f));
// Create brush and font objects
PdfSolidBrush titleBrush = new PdfSolidBrush(new PdfRGBColor(Color.Blue));
PdfSolidBrush paraBrush = new PdfSolidBrush(new PdfRGBColor(Color.Black));
PdfTrueTypeFont titleFont = new PdfTrueTypeFont(new Font("Times New Roman", 18f, FontStyle.Bold), true);
PdfTrueTypeFont paraFont = new PdfTrueTypeFont(new Font("Times New Roman", 13f, FontStyle.Regular), true);
// Specify title text
String titleText = "What's Spire.PDF";
// Set the text alignment via PdfStringFormat class
PdfStringFormat format = new PdfStringFormat();
format.Alignment = PdfTextAlignment.Center;
// Draw title on the center of the page
page.Canvas.DrawString(titleText, titleFont, titleBrush, page.Canvas.ClientSize.Width / 2, 20, format);
// Get paragraph content from a .txt file
string paraText = "Spire.PDF is a .NET library designed for creating, reading, and manipulating PDF documents." +
" It offers a wide range of features, including the ability to generate PDF documents from scratch and " +
"convert various formats into PDF. Users can modify existing PDF files by adding text, images, or annotations, " +
"and it also supports filling and managing interactive PDF forms. Additionally, Spire.PDF allows for the " +
"extraction of text and images from PDF documents, as well as converting PDF files into other formats like " +
"Word, Excel, and images.";
// Create a PdfTextWidget object to hold the paragrah content
PdfTextWidget widget = new PdfTextWidget(paraText, paraFont, paraBrush);
// Create a rectangle where the paragraph content will be placed
RectangleF rect = new RectangleF(0, 50, page.Canvas.ClientSize.Width, page.Canvas.ClientSize.Height);
// Set the PdfLayoutType to Paginate to make the content paginated automatically
PdfTextLayout layout = new PdfTextLayout();
layout.Layout = PdfLayoutType.Paginate;
// Draw the widget on the page
widget.Draw(page, rect, layout);
// Save the document to file
doc.SaveToFile("SimpleDocument.pdf");
doc.Dispose();
}
}
}
In this code:
- PdfDocument: Serves as the foundation for creating a PDF, managing its structure and content.
- PdfPageBase: Adds a page to the document, specifying the A4 size and margins, setting up the drawing canvas.
- PdfSolidBrush: Defines colors for the title and paragraph text, filling shapes and text.
- PdfTrueTypeFont: Specifies font type, size, and style for text, creating distinct fonts for the title and paragraph.
- PdfStringFormat: Used to set text alignment (centered for the title), enhancing presentation.
- Canvas.DrawString(): Draws the title on the canvas at a specified location using the defined font, brush, and format.
- PdfTextWidget: Encapsulates the paragraph text, simplifying management and rendering of larger text blocks.
- PdfTextLayout: Configured for automatic pagination, ensuring text flows correctly to the next page if it exceeds the current one.
- PdfTextWidget.Draw(): Renders the paragraph within a specified rectangle on the page.
- SaveToFile(): Saves the document to the specified file path, completing the PDF creation process.
Output:

Enhancing Your PDF with Rich Elements
While text forms the foundation of most documents, professional PDFs often require richer content types to effectively communicate information. This section explores how to elevate your documents by incorporating images, tables, and lists.
1. Inserting an Image to PDF
Spire.PDF provides the PdfImage class to load and manage various image formats. You can position the image using the coordinate system discussed earlier with the DrawImage method.
// Load an image
PdfImage image = PdfImage.FromFile("C:\\Users\\Administrator\\Desktop\\ai.png");
// Specify the X and Y coordinates to start drawing the image
float x = 0f;
float y = 0f;
// Draw the image at a specified location on the page
page.Canvas.DrawImage(image, x, y);
2. Creating a Table in PDF
Spire.PDF provides the PdfTable class for creating and managing tables in a PDF document. You can populate the table with a DataTable and customize its appearance using various interfaces within PdfTable. Finally, the table is rendered on the page using the Draw method.
// Create a PdfTable
PdfTable table = new PdfTable();
// Create a DataTable
DataTable dataTable = new DataTable();
dataTable.Columns.Add("Name");
dataTable.Columns.Add("Age");
dataTable.Columns.Add("Deparment");
dataTable.Rows.Add(new object[] { "David", "35", "Development" });
dataTable.Rows.Add(new object[] { "Sophie", "32", "Support" });
dataTable.Rows.Add(new object[] { "Wayne", "28", "Marketing" });
// Show header (invisible by default)
table.Style.ShowHeader = true;
//Set font color and backgroud color of header row
table.Style.HeaderStyle.BackgroundBrush = PdfBrushes.Gray;
table.Style.HeaderStyle.TextBrush = PdfBrushes.White;
// Assign data source
table.DataSource = dataTable;
//Set text alignment in header row
table.Style.HeaderStyle.StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
//Set text alignment in other cells
for (int i = 0; i < table.Columns.Count; i++)
{
table.Columns[i].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
}
// Draw table on the page
table.Draw(page, new RectangleF(0, 150, 300, 150));
In addition to PdfTable, Spire.PDF offers the PdfGrid class, making it easier to create and manage complex tables in PDF documents. For detailed instruction, check this guide: Generate Tables in PDF Using C#.
3. Adding a List to PDF
The PdfSortedList class in Spire.PDF enables the creation of ordered lists with various numbering styles. You can specify the list content as a string and customize its appearance by adjusting properties such as font, indent, and text indent. The list is then rendered on the page using the Draw method at a specified location.
// Create a font
PdfFont listFont = new PdfFont(PdfFontFamily.TimesRoman, 12f, PdfFontStyle.Regular);
// Create a maker for ordered list
PdfOrderedMarker marker = new PdfOrderedMarker(PdfNumberStyle.Numeric, listFont);
//Create a numbered list
String listContent = "Data Structure\n"
+ "Algorithm\n"
+ "Computer Networks\n"
+ "Operating System\n"
+ "Theory of Computations";
PdfSortedList list = new PdfSortedList(listContent);
//Set the font, indent, text indent, brush of the list
list.Font = listFont;
list.Indent = 2;
list.TextIndent = 4;
list.Marker = marker;
//Draw list on the page at the specified location
list.Draw(page, 310, 125);
Output:
Below is a screenshot of the PDF file created by the code snippets in this section:

In addition to the mentioned elements, Spire.PDF supports the addition of nearly all common elements to PDFs. For more documentation, refer to the Spire.PDF Programming Guide.
Creating PDF from HTML in C#
In modern applications, it's increasingly common to need conversion of HTML content to PDF format—whether for web page archiving, report generation, or creating printable versions of web content.
Spire.PDF addresses this need through its HTML conversion capabilities, which leverage Chrome's rendering engine for exceptional fidelity. This approach ensures that your PDF output closely matches how the content appears in web browsers, including support for modern CSS features.
The conversion process is highly configurable, allowing you to control aspects like page size , margins , and timeout settings to accommodate various content types and network conditions.
Here's an exmaple demonstrating how to create PDF from HTML in C#:
using Spire.Additions.Chrome;
namespace ConvertHtmlToPdf
{
internal class Program
{
static void Main(string[] args)
{
// Specify the input URL and output PDF file path
string inputUrl = @"C:\Users\Administrator\Desktop\Html.html";
string outputFile = @"HtmlToPDF.pdf";
// Specify the path to the Chrome plugin
string chromeLocation = @"C:\Program Files\Google\Chrome\Application\chrome.exe";
// Create an instance of the ChromeHtmlConverter class
ChromeHtmlConverter converter = new ChromeHtmlConverter(chromeLocation);
// Create an instance of the ConvertOptions class
ConvertOptions options = new ConvertOptions();
options.Timeout = 10 * 3000;
// Set paper size and page margins of the converted PDF
options.PageSettings = new PageSettings()
{
PaperWidth = 8.27,
PaperHeight = 11.69,
MarginTop = 0,
MarginLeft = 0,
MarginRight = 0,
MarginBottom = 0
};
//Convert the URL to PDF
converter.ConvertToPdf(inputUrl, outputFile, options);
}
}
}
Output:

Creating PDF Based on Template in C#
Template-based PDF generation enhances maintainability and consistency in enterprise applications. Spire.PDF supports this with text replacement functionality, allowing the creation of master templates with placeholders filled at runtime. This separation enables non-technical users to update templates easily.
The PdfTextReplacer class facilitates replacements, including automatic text resizing to fit designated spaces, making it ideal for documents like contracts, certificates, or forms with a constant layout.
The following code demonstrates how to create a PDF file based on a predefined template in C#:
using Spire.Pdf;
using Spire.Pdf.Texts;
namespace GeneratePdfBasedOnTemplate
{
class Program
{
static void Main(string[] args)
{
// Create a PdfDocument object
PdfDocument doc = new PdfDocument();
// Load a PDF file
doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Template.pdf");
// Create a PdfTextReplaceOptions object and specify the options
PdfTextReplaceOptions textReplaceOptions = new PdfTextReplaceOptions
{
ReplaceType = PdfTextReplaceOptions.ReplaceActionType.AutofitWidth | PdfTextReplaceOptions.ReplaceActionType.WholeWord
};
// Get a specific page
PdfPageBase page = doc.Pages[0];
// Create a PdfTextReplacer object based on the page
PdfTextReplacer textReplacer = new PdfTextReplacer(page)
{
Options = textReplaceOptions
};
// Dictionary for old and new strings
Dictionary<string, string> replacements = new Dictionary<string, string>
{
{ "#name#", "John Doe" },
{ "#gender#", "Male" },
{ "#birthdate#", "January 15, 1990" },
{ "#address#", "123 Main St, Springfield, IL, 62701" }
};
// Loop through the dictionary to replace text
foreach (var pair in replacements)
{
textReplacer.ReplaceText(pair.Key, pair.Value);
}
// Save the document to a different PDF file
doc.SaveToFile("Output.pdf");
doc.Dispose();
}
}
}
Output:

Conclusion
The ability to create and manipulate PDF documents in C# is an invaluable skill for developers, especially in today's data-driven environment. This guide has covered various methods for generating PDFs, including creating documents from scratch, converting HTML to PDF, and utilizing templates for dynamic content generation.
With the robust features of Spire.PDF, developers can produce professional-quality PDFs that meet diverse requirements, from simple reports to complex forms. Dive into the world of PDF generation with Spire.PDF and unlock endless possibilities for your projects.
FAQs
Q1. What is the best library for creating PDFs in C#?
Spire.PDF is highly recommended due to its extensive feature set, user-friendly interface, and support for advanced operations like HTML-to-PDF conversion. It allows developers to easily create, manipulate, and customize PDF documents to meet a variety of needs.
Q2. Can I create PDF files in C# without external libraries?
While .NET has limited built-in capabilities, libraries like Spire.PDF are essential for complex tasks like adding tables or images.
Q3. How do I generate a PDF from HTML in C#?
Spire.PDF’s integration with Chrome allows for seamless conversion of HTML to PDF. You can customize page settings, such as margins and orientation, ensuring that the output PDF maintains the desired formatting and layout of the original HTML content.
Q4. How do I protect my PDF with passwords or permissions?
Spire.PDF offers robust encryption options through the PdfSecurityPolicy class. You can set owner and user passwords to restrict access, as well as define permissions for printing, copying, and editing the PDF. For more details, refer to: Encrypt or Decrypt PDF Files in C# .NET
Q5. Can I create PDF files from Word and Excel using Spire.PDF?
No, you cannot convert Word or Excel files directly with Spire.PDF. For Word to PDF conversion, use Spire.Doc, and for Excel to PDF, use Spire.XLS.
Get a Free License
To fully experience the capabilities of Spire.PDF for .NET without any evaluation limitations, you can request a free 30-day trial license.
Lists are used in Word documents to outline, arrange, and emphasize text, making it easy for users to scan and understand a series of items. There are three different types of lists in Word, namely numbered lists, bulleted lists and multi-level lists. Numbered lists are used for items that have a sequence or priority, such as a series of instructions. Bulleted lists are used for items that have no particular priority, such as a list of functions or facts. Multi-level lists are used where there is a sequence and you want each paragraph numbered separately.
In this article, you will learn how to insert these types of lists into a Word document in C# and VB.NET using Spire.Doc for .NET.
- Insert a Numbered List in Word
- Insert a Bulleted List in Word
- Insert a Multi-Level Numbered List in Word
- Insert a Multi-Level Mixed-Type List in Word
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
Insert a Numbered List in Word in C#, VB.NET
Spire.Doc for .NET offers the ListStyle class that you can use to create a numbered list style or a bulleted style. Then, the list style can be applied to a paragraph using Paragraph.ListFormat.ApplyStyle() method. The steps to create a numbered list are as follows.
- Create a Document object.
- Add a section using Document.AddSection() method.
- Create an instance of ListStyle class, specifying the list type to Numbered.
- Get a specific level of the list through ListStyle.Levels[index] property, and set the numbering type through ListLevel.PatternType property.
- Add the list style to the document using Document.ListStyles.Add() method.
- Add several paragraphs to the document using Section.AddParagraph() method.
- Apply the list style to a specific paragraph using Paragraph.ListFormat.ApplyStyle() method.
- Specify the list level through Paragraph.ListFormat.ListLevelNumber property.
- Save the document to a Word file using Document.SaveToFile() method.
- C#
- VB.NET
using Spire.Doc;
using Spire.Doc.Documents;
namespace CreateOrderedList
{
class Program
{
static void Main(string[] args)
{
//Create a Document object
Document document = new Document();
//Add a section
Section section = document.AddSection();
//Create a numbered list style
ListStyle listStyle = document.Styles.Add(ListType.Numbered, "numberedList");
listStyle.Name = "numberedList";
listStyle.ListRef.Levels[0].PatternType = ListPatternType.DecimalEnclosedParen;
listStyle.ListRef.Levels[0].TextPosition = 20;
//Add a paragraph
Paragraph paragraph = section.AddParagraph();
paragraph.AppendText("Required Web Development Skills:");
paragraph.Format.AfterSpacing = 5f;
//Add a paragraph and apply the numbered list style to it
paragraph = section.AddParagraph();
paragraph.AppendText("HTML");
paragraph.ListFormat.ApplyStyle("numberedList");
paragraph.ListFormat.ListLevelNumber = 0;
//Add another four paragraphs and apply the numbered list style to them
paragraph = section.AddParagraph();
paragraph.AppendText("CSS");
paragraph.ListFormat.ApplyStyle("numberedList");
paragraph.ListFormat.ListLevelNumber = 0;
paragraph = section.AddParagraph();
paragraph.AppendText("JavaScript");
paragraph.ListFormat.ApplyStyle("numberedList");
paragraph.ListFormat.ListLevelNumber = 0;
paragraph = section.AddParagraph();
paragraph.AppendText("Python");
paragraph.ListFormat.ApplyStyle("numberedList");
paragraph.ListFormat.ListLevelNumber = 0;
paragraph = section.AddParagraph();
paragraph.AppendText("MySQL");
paragraph.ListFormat.ApplyStyle("numberedList");
paragraph.ListFormat.ListLevelNumber = 0;
//Save the document to file
document.SaveToFile("NumberedList.docx", FileFormat.Docx);
}
}
}

Insert a Bulleted List in Word in C#, VB.NET
The process of creating a bulleted list is similar to that of creating a numbered list. The difference is that when creating a list style, you must specify the list type as Bulleted and set a bullet symbol for it. The following are the detailed steps.
- Create a Document object.
- Add a section using Document.AddSection() method.
- Create an instance of ListStyle class, specifying the list type to Bulleted.
- Get a specific level of the list through ListStyle.Levels[index] property, and set the bullet symbol through ListLevel.BulletCharacter property.
- Add the list style to the document using Document.ListStyles.Add() method.
- Add several paragraphs to the document using Section.AddParagraph() method.
- Apply the list style to a specific paragraph using Paragraph.ListFormat.ApplyStyle() method.
- Specify the list level through Paragraph.ListFormat.ListLevelNumber property.
- Save the document to a Word file using Document.SaveToFile() method.
- C#
- VB.NET
using Spire.Doc;
using Spire.Doc.Documents;
namespace CreateUnorderedList
{
class Program
{
static void Main(string[] args)
{
//Create a Document object
Document document = new Document();
//Add a section
Section section = document.AddSection();
//Create a bulleted list style
ListStyle listStyle = document.Styles.Add( ListType.Bulleted, "bulletedList");
listStyle.Name = "bulletedList";
listStyle.ListRef.Levels[0].BulletCharacter = "\x00B7";
listStyle.ListRef.Levels[0].CharacterFormat.FontName = "Symbol";
listStyle.ListRef.Levels[0].TextPosition = 20;
//Add a paragraph
Paragraph paragraph = section.AddParagraph();
paragraph.AppendText("Computer Science Subjects:");
paragraph.Format.AfterSpacing = 5f;
//Add a paragraph and apply the bulleted list style to it
paragraph = section.AddParagraph();
paragraph.AppendText("Data Structure");
paragraph.ListFormat.ApplyStyle("bulletedList");
paragraph.ListFormat.ListLevelNumber = 0;
//Add another five paragraphs and apply the bulleted list style to them
paragraph = section.AddParagraph();
paragraph.AppendText("Algorithm");
paragraph.ListFormat.ApplyStyle("bulletedList");
paragraph.ListFormat.ListLevelNumber = 0;
paragraph = section.AddParagraph();
paragraph.AppendText("Computer Networks");
paragraph.ListFormat.ApplyStyle("bulletedList");
paragraph.ListFormat.ListLevelNumber = 0;
paragraph = section.AddParagraph();
paragraph.AppendText("Operating System");
paragraph.ListFormat.ApplyStyle("bulletedList");
paragraph.ListFormat.ListLevelNumber = 0;
paragraph = section.AddParagraph();
paragraph.AppendText("C Programming");
paragraph.ListFormat.ApplyStyle("bulletedList");
paragraph.ListFormat.ListLevelNumber = 0;
paragraph = section.AddParagraph();
paragraph.AppendText("Theory of Computations");
paragraph.ListFormat.ApplyStyle("bulletedList");
paragraph.ListFormat.ListLevelNumber = 0;
//Save the document to file
document.SaveToFile("BulletedList.docx", FileFormat.Docx);
}
}
}

Insert a Multi-Level Numbered List in Word in C#, VB.NET
A multi-level list consists of at least two different levels. Each level of a nested list is represented by the ListStyle.Levels[index] property, through which you can set the numbering type and prefix for a certain level. The following are the steps to create a multi-level numbered list in Word.
- Create a Document object.
- Add a section using Document.AddSection() method.
- Create an instance of ListStyle class, specifying the list type to Numbered.
- Get a specific level of the list through ListStyle.Levels[index] property, and set the numbering type and prefix.
- Add the list style to the document using Document.ListStyles.Add() method.
- Add several paragraphs to the document using Section.AddParagraph() method.
- Apply the list style to a specific paragraph using Paragraph.ListFormat.ApplyStyle() method.
- Specify the list level through Paragraph.ListFormat.ListLevelNumber property.
- Save the document to a Word file using Document.SaveToFile() method.
- C#
- VB.NET
using Spire.Doc;
using Spire.Doc.Documents;
namespace CreateMultiLevelList
{
class Program
{
static void Main(string[] args)
{
//Create a Document object
Document document = new Document();
//Add a section
Section section = document.AddSection();
//Create a numbered list style, specifying number prefix and pattern type of each level
ListStyle listStyle = document.Styles.Add(ListType.Numbered, "levelstyle");
listStyle.Name = "levelstyle";
listStyle.ListRef.Levels[0].PatternType = ListPatternType.Arabic;
listStyle.ListRef.Levels[0].TextPosition = 20;
listStyle.ListRef.Levels[1].NumberPrefix = "\x0000.";
listStyle.ListRef.Levels[1].PatternType = ListPatternType.Arabic;
listStyle.ListRef.Levels[2].NumberPrefix = "\x0000.\x0001.";
listStyle.ListRef.Levels[2].PatternType = ListPatternType.Arabic;
//Add a paragraph
Paragraph paragraph = section.AddParagraph();
paragraph.AppendText("Here's a Multi-Level Numbered List:");
paragraph.Format.AfterSpacing = 5f;
//Add a paragraph and apply the numbered list style to it
paragraph = section.AddParagraph();
paragraph.AppendText("The first item");
paragraph.ListFormat.ApplyStyle("levelstyle");
paragraph.ListFormat.ListLevelNumber = 0;
//Add another five paragraphs and apply the numbered list stype to them
paragraph = section.AddParagraph();
paragraph.AppendText("The second item");
paragraph.ListFormat.ApplyStyle("levelstyle");
paragraph.ListFormat.ListLevelNumber = 0;
paragraph = section.AddParagraph();
paragraph.AppendText("The first sub-item");
paragraph.ListFormat.ApplyStyle("levelstyle");
paragraph.ListFormat.ListLevelNumber = 1;
paragraph = section.AddParagraph();
paragraph.AppendText("The second sub-item");
paragraph.ListFormat.ContinueListNumbering();
paragraph.ListFormat.ApplyStyle("levelstyle");
paragraph = section.AddParagraph();
paragraph.AppendText("A sub-sub-item");
paragraph.ListFormat.ApplyStyle("levelstyle");
paragraph.ListFormat.ListLevelNumber = 2;
paragraph = section.AddParagraph();
paragraph.AppendText("The third item");
paragraph.ListFormat.ApplyStyle("levelstyle");
paragraph.ListFormat.ListLevelNumber = 0;
//Save the document to file
document.SaveToFile("MultilevelNumberedList.docx", FileFormat.Docx);
}
}
}

Insert a Multi-Level Mixed-Type List in Word in C#, VB.NET
In some cases, you may want to mix number and symbol bullet points in a multi-level list. To create a mixed-type list, you just need to create a numbered list style and a bulleted list style and apply them to different paragraphs. The detailed steps are as follows.
- Create a Document object.
- Add a section using Document.AddSection() method.
- Create a numbered list style and a bulleted list style.
- Add several paragraphs to the document using Section.AddParagraph() method.
- Apply different list style to different paragraphs using Paragraph.ListFormat.ApplyStyle() method.
- Save the document to a Word file using Document.SaveToFile() method.
- C#
- VB.NET
using Spire.Doc;
using Spire.Doc.Documents;
namespace CreateMultilevelMixedList
{
class Program
{
static void Main(string[] args)
{
//Create a Document object
Document document = new Document();
//Add a section
Section section = document.AddSection();
//Create a numbered list style
ListStyle numberedListStyle = document.Styles.Add(ListType.Numbered, "numberedStyle");
numberedListStyle.ListRef.Levels[0].PatternType = ListPatternType.Arabic;
numberedListStyle.ListRef.Levels[0].TextPosition = 20;
numberedListStyle.ListRef.Levels[1].PatternType = ListPatternType.LowLetter;
//Create a bulleted list style
ListStyle bulletedListStyle = document.Styles.Add(ListType.Bulleted, "bulltedStyle");
bulletedListStyle.Name = "bulltedStyle";
bulletedListStyle.ListRef.Levels[2].BulletCharacter = "\x002A";
bulletedListStyle.ListRef.Levels[2].CharacterFormat.FontName = "Symbol";
//Add a paragraph
Paragraph paragraph = section.AddParagraph();
paragraph.AppendText("Here's a Multi-Level Mixed List:");
paragraph.Format.AfterSpacing = 5f;
//Add a paragraph and apply the numbered list style to it
paragraph = section.AddParagraph();
paragraph.AppendText("The first item");
paragraph.ListFormat.ApplyStyle("numberedStyle");
paragraph.ListFormat.ListLevelNumber = 0;
//Add another five paragraphs and apply different list stype to them
paragraph = section.AddParagraph();
paragraph.AppendText("The first sub-item");
paragraph.ListFormat.ApplyStyle("numberedStyle");
paragraph.ListFormat.ListLevelNumber = 1;
paragraph = section.AddParagraph();
paragraph.AppendText("The second sub-item");
paragraph.ListFormat.ListLevelNumber = 1;
paragraph.ListFormat.ApplyStyle("numberedStyle");
paragraph = section.AddParagraph();
paragraph.AppendText("The first sub-sub-item");
paragraph.ListFormat.ApplyStyle("bulltedStyle");
paragraph.ListFormat.ListLevelNumber = 2;
paragraph = section.AddParagraph();
paragraph.AppendText("The second sub-sub-item");
paragraph.ListFormat.ApplyStyle("bulltedStyle");
paragraph.ListFormat.ListLevelNumber = 2;
paragraph = section.AddParagraph();
paragraph.AppendText("The second item");
paragraph.ListFormat.ApplyStyle("numberedStyle");
paragraph.ListFormat.ListLevelNumber = 0;
//Save the document to file
document.SaveToFile("MultilevelMixedList.docx", FileFormat.Docx);
}
}
}

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.
PDF margins are blank areas between content and page edges. In most cases, we set moderate or narrow margins in order to create a compact appearance. However, if we wish to place a company logo or other relevant information in the margins, we need to make the margins a bit wider. In this article, you will learn how to increase or decrease the margins of an existing PDF document in C# and VB.NET using Spire.PDF for .NET.
- Increase the Margins of a PDF Document in C#, VB.NET
- Decrease the Margins of a PDF Document in C#, VB.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
Increase the Margins of a PDF Document in C#, VB.NET
The way to enlarge the margins of a PDF document is to create a new PDF that has a larger page size, and then draw the source page on the large page at the appropriate location. The following are the steps to increase the margins of a PDF document using Spire.PDF for .NET.
- Load the original PDF document while initialing the PdfDocument object.
- Create another PdfDocument object, which is used to create a new PDF document that has a larger page size.
- Set the increasing values of the margins.
- Calculate the page size of the new PDF document.
- Loop through the pages in the original document, and create a template based on a specific page using PdfPageBase.CreateTemplate() method.
- Add a page to the new PDF document using PdfDocument.Pages.Add() method.
- Draw the template on the page at the coordinate (0, 0) using PdfTemplate.Draw() method.
- Save the new PDF document to file using PdfDocument.SaveToFile() method.
- C#
- VB.NET
using Spire.Pdf;
using Spire.Pdf.Graphics;
using System.Drawing;
namespace IncreaseMargins
{
class Program
{
static void Main(string[] args)
{
//Load the original PDF document
PdfDocument originalPdf = new PdfDocument("C:\\Users\\Administrator\\Desktop\\sample.pdf");
//Get the first page
PdfPageBase firstPage = originalPdf.Pages[0];
//Create a new PdfDocument object
PdfDocument newPdf = new PdfDocument();
//Set increasing value of the margins
PdfMargins margins = newPdf.PageSettings.Margins;
margins.Top = 40;
margins.Bottom=40;
margins.Left=40;
margins.Right= 40;
//Calculate the new page size
SizeF sizeF = new SizeF(firstPage.Size.Width + margins.Left + margins.Right, firstPage.Size.Height + margins.Top + margins.Bottom);
//Loop through the pages in the original document
for (int i = 0; i < originalPdf.Pages.Count; i++)
{
//Create a template based on a spcific page
PdfTemplate pdfTemplate = originalPdf.Pages[i].CreateTemplate();
//Add a page to the new PDF
PdfPageBase page = newPdf.Pages.Add(sizeF);
//Draw template on the page
pdfTemplate.Draw(page, 0, 0);
}
//Save the new document
newPdf.SaveToFile("IncreaseMargins.pdf", FileFormat.PDF);
}
}
}

Decrease the Margins of a PDF Document in C#, VB.NET
The way to decrease the margins of a PDF is to create a new PDF that has a smaller page size, and then draw the source page on the small page at a specified coordinate. The following are the steps to decrease the margins of a PDF document using Spire.PDF for .NET.
- Load the original PDF document while initialing the PdfDocument object.
- Create another PdfDocument object, which is used to create a new PDF document that has a smaller page size.
- Set the decreasing values of the margins.
- Calculate the page size of the new PDF document.
- Loop through the pages in the original document, and create a template based on a specific page using PdfPageBase.CreateTemplate() method.
- Add a page to the new PDF document using PdfDocument.Pages.Add() method.
- Draw the template on the page at a specified coordinate using PdfTemplate.Draw() method.
- Save the new PDF document to file using PdfDocument.SaveToFile() method.
- C#
- VB.NET
using Spire.Pdf;
using Spire.Pdf.Graphics;
using System.Drawing;
namespace DecreaseMargins
{
class Program
{
static void Main(string[] args)
{
//Load the original PDF document
PdfDocument originalPdf = new PdfDocument("C:\\Users\\Administrator\\Desktop\\sample.pdf");
//Get the first page
PdfPageBase firstPage = originalPdf.Pages[0];
//Create a new PdfDocument object
PdfDocument newPdf = new PdfDocument();
//Set decreasing value
float left = -20;
float right = -20;
float top = -20;
float bottom = -20;
//Calculate the new page size
SizeF sizeF = new SizeF(firstPage.Size.Width + left + right, firstPage.Size.Height + top + bottom);
//Loop through the pages in the original document
for (int i = 0; i < originalPdf.Pages.Count; i++)
{
//Create a template based on a specific page
PdfTemplate pdfTemplate = originalPdf.Pages[i].CreateTemplate();
//Add a page to the new PDF
PdfPageBase page = newPdf.Pages.Add(sizeF, new PdfMargins(0));
//Draw template on the page
pdfTemplate.Draw(page, left, top);
}
//Save the new document
newPdf.SaveToFile("DecreaseMargins.pdf", FileFormat.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.
Spire.OCR for .NET is a professional OCR library that supports recognizing text from Images (such as JPG, PNG, GIF, BMP, and TIFF) in both .NET Framework and .NET Core applications. In this article, we will explain how to use Spire.OCR for .NET to read text from images in .NET Framework applications.
Step 1: Create a console application (.NET Framework) in Visual Studio.


Step 2: Change the platform target of the application to X64.
In the application's solution explorer, right-click on the solution name and then click "Properties".

Change the platform target of the application to X64. This step must be performed since Spire.OCR only supports 64-bit platforms.

Step 3: Add a reference to Spire.OCR for .NET DLL in the application.
We recommend installing Spire.OCR for .NET through NuGet (Note: only Spire.OCR for .NET Version 1.8.0 or above supports working with .NET Framework). The detailed steps are as follows:
- In the application's solution explorer, right-click on the solution name or "References" and select "Manage NuGet Packages".
- Click the "Browse" tab and search for Spire.OCR.
- Click "Install" to install Spire.OCR.

Step 4: Copy DLLs from the "packages" directory to the "Debug" directory in the application.
When you install Spire.OCR through NuGet, NuGet downloads the packages and puts them in your application under a directory called "packages". You need to find the "Spire.OCR" directory under the "packages" directory, then copy the DLLs under the "Spire.OCR" directory (packages\Spire.OCR.1.8.0\runtimes\win-x64\native) to the "Debug" directory of your application.

Now you have successfully included Spire.OCR in your .NET Framework application. You can refer to the following code example to read text from images using Spire.OCR.
- C#
using Spire.OCR;
using System.IO;
namespace OcrImage
{
internal class Program
{
static void Main(string[] args)
{
//Create an instance of the OcrScanner class
OcrScanner scanner = new OcrScanner();
//Call the OcrScanner.Scan() method to scan text from an image
scanner.Scan("image.png");
//Save the scanned text to a .txt file
string text = scanner.Text.ToString();
File.WriteAllText("output.txt", text);
}
}
}
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.
Data bars in Excel are a built-in type of conditional formatting that inserts colored bars in cells to compare the values within them. The length of a bar depends on the value of a cell and the longest bar corresponds to the largest value in a selected data range, which allows you to spot it at a glance. In this article, you will learn how to add data bars in a cell range 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
Add Data Bars in Excel in C# and VB.NET
Data bars are a great tool for visually comparing data within a selected range of cells. With Spire.XLS for .NET, you are allowed to add a data bar to a specified data range and also set its format. The following are the detailed steps.
- Create a Workbook instance.
- Load a sample Excel document using Workbook.LoadFromFile() method.
- Get a specified worksheet using Workbook.Worsheets[index] property.
- Add a conditional formatting to the worksheet using Worksheet.ConditionalFormats.Add() method and return an object of XlsConditionalFormats class.
- Set the cell range where the conditional formatting will be applied using XlsConditionalFormats.AddRange() method.
- Add a condition using XlsConditionalFormats.AddCondition() method, and then set its format type to DataBar using IConditionalFormat.FormatType property.
- Set the fill effect and color of the data bars using IConditionalFormat.DataBar.BarFillType and IConditionalFormat.DataBar.BarColor properties.
- Save the result document using Workbook.SaveToFile() method.
- C#
- VB.NET
using Spire.Xls;
using Spire.Xls.Core;
using Spire.Xls.Core.Spreadsheet.Collections;
using Spire.Xls.Core.Spreadsheet.ConditionalFormatting;
using System.Drawing;
namespace ApplyDataBar
{
class Program
{
static void Main(string[] args)
{
//Create a Workbook instance
Workbook workbook = new Workbook();
//Load a sample Excel docuemnt
workbook.LoadFromFile("sample.xlsx");
//Get the first worksheet
Worksheet sheet = workbook.Worksheets[0];
//Add a conditional format to the worksheet
XlsConditionalFormats xcfs = sheet.ConditionalFormats.Add();
//Set the range where the conditional format will be applied
xcfs.AddRange(sheet.Range["C2:C13"]);
//Add a condition and set its format type to DataBar
IConditionalFormat format = xcfs.AddCondition();
format.FormatType = ConditionalFormatType.DataBar;
//Set the fill effect and color of the data bars
format.DataBar.BarFillType = DataBarFillType.DataBarFillGradient;
format.DataBar.BarColor = Color.Red;
//Save the result document
workbook.SaveToFile("ApplyDataBarsToDataRange.xlsx", ExcelVersion.Version2013);
}
}
}

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.
Spire.Email for .NET is a professional .NET Email library specially designed for developers to create, read and manipulate emails from any .NET (C#, VB.NET, ASP.NET, .Net Core, .Net Standard, .NET 5.0, MonoAndroid, Xamarin iOS) platform with fast and high quality performance.
As an independent .NET Email API, Spire.Email for .NET doesn't need Microsoft outlook to be installed on the machine. However, it can incorporate Microsoft Outlook document creation capabilities into any developers' .NET applications.
Spire.PDFViewer for .NET is a powerful PDF Viewer library for .NET. It allows developers to load PDF document from stream, file and byte array. Spire.PDFViewer is available on viewing PDF/A-1B, PDF/X1A and enables to open and read encrypted PDF files. This PDF Viewer control supports multiple printing orientation including landscape, portrait and automatic.
Furthermore, it can export PDFs to popular image formats like .bmp, .png and .jpeg. When viewing PDF document through Spire.PDFViewer, users can set display as fit page, page down/up, zoom in/out, etc. Spire.PDFViewer is a totally independent .NET library which designed for viewing PDF files from .NET application. It does NOT require Adobe Reader or any other 3rd party software/library installed on system.
Spire.DocViewer for .NET is a powerful Word Viewer library for developers to display, convert and interact with Word Documents easily. When developers use Spire.DocViewer for .NET within their own .NET application, they do not require any additional installation to manipulate Word Documents with high performance and strong stability.
Spire.DocViewer for .NET expresses scalability, time-saving and cost-effective to view and print Word Documents. Developers can render Word Documents by using Word elements including text, paragraph, image, list, table, bookmark etc. Furthermore, Spire.DocViewer for .NET allows developers to load all versions of Word Documents and convert Word Documents to PDF, HTML and RTF.
Spire.OCR for .NET is a professional OCR library to read text from Images in JPG, PNG, GIF, BMP and TIFF formats. Developers can easily add OCR functionalities within .NET applications in C# and VB.NET. It supports commonly used image formats and provides functionalities like reading multiple characters and fonts from images, bold and italic styles, scanning of the whole image and much more.
Spire.OCR for .NET provides a very easy way to read text from images. With just one line of code in C# and VB.NET, Spire.OCR supports variable common image formats, such as Bitmap, JPG, PNG, TIFF and GIF.
Spire.Barcode for .NET is a professional barcode library specially designed for .NET developers ( C#, VB.NET, ASP.NET, .NET Core, .Net Standard, .NET 5.0, MonoAndroid, Xamarin.iOS ) to generate, read and scan 1D & 2D barcodes. Developers and programmers can use Spire.Barcode to add Enterprise-Level barcode formats to their .net applications quickly and easily.
Spire.Barcode for .NET provides a very easy way to integrate barcode processing. With just one line of code to create, read 1D & 2D barcode, Spire.Barcode supports variable common image formats, such as Bitmap, JPG, PNG, EMF, TIFF, GIF and WMF.






