|
C# PDF Generator Library
How to add a chapter and text section to PDF report file in C#
C# Demo Code to add a chapter and section to adobe pdf file
In this C# tutorial, you will learn how to add chapter text, section content to PDF report file using C# in ASP.NET MVC Web, Windows applications.
- Easy to add, insert chapter, section text content to PDF document
- Apply PDF bookmark entry for chapter and sections
- Adjust paragraph font style in the PDF section content
Create a document with three empty chapters in PDF using C#
The C# sample source code below will explain how to quickly add chapters to the PDF report document in C#.NET
- Utilize PDFBuildHandler.Create() to create a new PDF file
- Create several new Chapter objects with text content to the PDF document
- Apply the content changes to the PDF file
String outputFilePath = Program.RootPath + "\\" + "Sample61.pdf";
Document document = new Document();
PDFBuildHandler.Create(document, outputFilePath);
document.Open();
// add a chapter that title is "Chapter 1"
document.Add(new Chapter("Chapter 1", 1));
// add a chapter that title is "Chapter 2"
document.Add(new Chapter("Chapter 2", 2));
// add a chapter that title is "Chapter 3"
document.Add(new Chapter("Chapter 3", 3));
document.Close();
Add sections to a chapter
String outputFilePath = Program.RootPath + "\\" + "Sample62.pdf";
Document document = new Document();
PDFBuildHandler.Create(document, outputFilePath);
document.Open();
Chapter chapter = new Chapter("Chapter: Sample", 2);
// add section
chapter.AddSection("Section 1");
// add section
chapter.AddSection("Section 2");
// add section
chapter.AddSection("Section 3");
document.Add(chapter);
document.Close();
Change title string shown in the bookmark entry
String outputFilePath = Program.RootPath + "\\" + "Sample63.pdf";
Document document = new Document();
PDFBuildHandler.Create(document, outputFilePath);
document.Open();
Chapter chapter = new Chapter("This is a sample chapter", 2);
// change title string in bookmark entry
chapter.SetBookMarkTitle("Chapter");
// add section
Section section1 = chapter.AddSection("Introduction");
// change title string in bookmark entry
section1.SetBookMarkTitle("bookmark entry 1");
// change title string in bookmark entry
Section section2 = chapter.AddSection("Main");
section2.SetBookMarkTitle("bookmark entry 2");
// change title string in bookmark entry
Section section3 = chapter.AddSection("Discussion");
section3.SetBookMarkTitle("bookmark entry 3");
// change title string in bookmark entry
Section section4 = chapter.AddSection("Conclusion");
section4.SetBookMarkTitle("bookmark entry 4");
document.Add(chapter);
document.Close();
Add nested sections to chapter
String outputFilePath = Program.RootPath + "\\" + "Sample64.pdf";
Document document = new Document();
PDFBuildHandler.Create(document, outputFilePath);
document.Open();
Font chapter_font = new Font(Font.FontFamily.TimesRoman, 24F, Font.FontStyle.Bold, Color.Blue);
Font section_level_1_font = new Font(Font.FontFamily.TimesRoman, 16F, Font.FontStyle.Regular);
Font section_level_2_font = new Font(Font.FontFamily.TimesRoman, 14F, Font.FontStyle.Bold);
Font section_level_3_font = new Font(Font.FontFamily.TimesRoman, 14F, Font.FontStyle.Italic);
Font body_font = new Font(Font.FontFamily.TimesRoman, 12F, Font.FontStyle.Regular);
Chapter chapter = new Chapter(new Paragraph("This is a sample chapter", chapter_font), 2);
// Section 1
Section section1 = chapter.AddSection(new Paragraph("Introduction", section_level_1_font));
section1.Add(new Paragraph("Paragraph in section 1", body_font));
// Section 1.1
Section section1_1 = section1.AddSection(new Paragraph("Section A", section_level_2_font));
section1_1.Add(new Paragraph("Paragraph in section 1.1", body_font));
// Section 1.2
Section section1_2 = section1.AddSection(new Paragraph("Section B", section_level_2_font));
section1_2.Add(new Paragraph("Paragraph in section 1.2", body_font));
// Section 2
Section section2 = chapter.AddSection(new Paragraph("Main", section_level_1_font));
section2.Add(new Paragraph("Paragraph in section 2", body_font));
// Section 3
Section section3 = chapter.AddSection(new Paragraph("Discussion", section_level_1_font));
// Section 3.1
Section section3_1 = section3.AddSection(new Paragraph("Section A", section_level_2_font));
section3_1.Add(new Paragraph("Paragraph in section 3.1", body_font));
// Section 3.1.1
Section section3_1_1 = section3_1.AddSection(new Paragraph("Section i", section_level_3_font));
section3_1_1.Add(new Paragraph("Paragraph in section 3.1.1", body_font));
// Section 4
Section section4 = chapter.AddSection("Conclusion");
document.Add(chapter);
document.Close();
|