<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Sharnouby55</title>
    <description>The latest articles on DEV Community by Sharnouby55 (@mo_elsharnouby).</description>
    <link>https://dev.to/mo_elsharnouby</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F693857%2F2221556a-fc48-4bc0-9623-15961c533fe7.jpg</url>
      <title>DEV Community: Sharnouby55</title>
      <link>https://dev.to/mo_elsharnouby</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mo_elsharnouby"/>
    <language>en</language>
    <item>
      <title>The easiest way to create PDF documents in C#</title>
      <dc:creator>Sharnouby55</dc:creator>
      <pubDate>Mon, 13 Sep 2021 10:51:22 +0000</pubDate>
      <link>https://dev.to/mo_elsharnouby/the-easiest-way-to-create-pdf-documents-in-c-256h</link>
      <guid>https://dev.to/mo_elsharnouby/the-easiest-way-to-create-pdf-documents-in-c-256h</guid>
      <description>&lt;p&gt;In this tutorial, we would like to share some of our experience in generating PDF documents in C# using the PDFFlow library.&lt;/p&gt;

&lt;p&gt;Getting started&lt;br&gt;
First, we need to create a new console application project “MyPDFDoc” in Visual Studio and choose Language “C#”.&lt;br&gt;
Then we need to add the Gehtsoft.PDFFlowlib library to our project. You can download it as a NuGet package from NuGet Gallery | Gehtsoft.PDFFlowLib.&lt;/p&gt;

&lt;p&gt;You can also install it from the Visual Studio NuGet Package Manager:&lt;br&gt;
In Solution Explorer, right-click either References or a project and select Manage NuGet Packages.&lt;br&gt;
Go to the Browse tab and search for the Gehtsoft.PDFFlowLib package using the search box on the upper left.&lt;br&gt;
Select the package from the list and click Install.&lt;/p&gt;

&lt;p&gt;Hello World!&lt;br&gt;
Now the PDFFlow library is installed, we are ready to create our first “Hello world” PDF document.&lt;/p&gt;

&lt;p&gt;Add sections to the document.&lt;br&gt;
A section is a part of a document holding content and layout. A document may contain one or more sections.&lt;br&gt;
You can add various content to a section, including paragraphs, images, tables, lines, and repeating areas.&lt;/p&gt;

&lt;p&gt;In your Main class start typing this code to create a simple paragraph:&lt;br&gt;
static void Main(string[] args)&lt;br&gt;
{&lt;br&gt;
DocumentBuilder.New().AddSection()&lt;br&gt;
.AddParagraph(“Hello World!”).ToDocument()&lt;br&gt;
.Build(“Result.pdf”);&lt;br&gt;
}&lt;br&gt;
The result will be like this:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--BE0vaji4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j3vyw1rtz8tois4sbwmj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--BE0vaji4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j3vyw1rtz8tois4sbwmj.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Set font color and size&lt;br&gt;
use the method SetFontColor() to set text color and the method SetFontSize() to set the text size.&lt;/p&gt;

&lt;p&gt;DocumentBuilder builder = DocumentBuilder.New();&lt;br&gt;
var section = builder.AddSection();&lt;br&gt;
section.AddParagraph(“Hello World!”).ToDocument();&lt;br&gt;
section.AddParagraph(“Bigger text goes here!”).SetFontColor(Color.FromHtml(“#FC6A03”))&lt;br&gt;
.SetFontSize(20).ToDocument();&lt;br&gt;
builder.Build(“Result.pdf”);&lt;br&gt;
The result will be like this:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--R9ynnf2Z--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a8h7t3ot1tsjnhmck0bn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--R9ynnf2Z--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a8h7t3ot1tsjnhmck0bn.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Adding image to document&lt;br&gt;
Suppose we need to add a Logo to our PDF document, all we need to do is use the method AddImage() and pass the image path to it, set its size and alignment.&lt;/p&gt;

&lt;p&gt;DocumentBuilder builder = DocumentBuilder.New();&lt;br&gt;
var section = builder.AddSection();&lt;br&gt;
section.AddParagraph(“Hello World!”).ToDocument();&lt;br&gt;
//Add an image and set its size, alignment and margins&lt;br&gt;
section.AddImage(“MyImage.png”).SetWidth(150).SetAlignment(HorizontalAlignment.Right).SetMargins(10, 10, 10, 10);&lt;br&gt;
section.AddParagraph(“Bigger text goes here!”).SetFontColor(Color.FromHtml(“#FC6A03”))&lt;br&gt;
.SetFontSize(20).ToDocument();&lt;br&gt;
builder.Build(“Result.pdf”);&lt;br&gt;
The result will be like this:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--t9q6KrOw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/525vnvol6x3ea8u51b1u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--t9q6KrOw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/525vnvol6x3ea8u51b1u.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Layout using tables&lt;br&gt;
Now let’s add a table and use it to layout our document.&lt;/p&gt;

&lt;p&gt;We will create a table with two columns, then insert the text in the first column and the image (logo) in the second column, then set the table borders to zero to make it invisible.&lt;/p&gt;

&lt;p&gt;First to create a table with 2 columns and one row use this code:&lt;/p&gt;

&lt;p&gt;section.AddTable()&lt;br&gt;
// add 2 columns to the table&lt;br&gt;
.AddColumnToTable().AddColumnToTable()&lt;br&gt;
// add the first row&lt;br&gt;
.AddRow()&lt;br&gt;
// add two cells to the row&lt;br&gt;
.AddCellToRow().AddCellToRow()&lt;br&gt;
.ToDocument();&lt;br&gt;
the result will be like this:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Uph4FUIV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4fpbykro1tbv0127vm2h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Uph4FUIV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4fpbykro1tbv0127vm2h.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then to add the text and the image to the cells use the methods AddParagraph() and AddImage()&lt;/p&gt;

&lt;p&gt;DocumentBuilder builder = DocumentBuilder.New();&lt;br&gt;
var section = builder.AddSection();&lt;br&gt;
section.AddParagraph(“Hello World!”).ToDocument();&lt;br&gt;
section.AddTable()&lt;br&gt;
// add 2 columns to the table&lt;br&gt;
.AddColumnToTable().AddColumnToTable()&lt;br&gt;
// add the first row&lt;br&gt;
.AddRow()&lt;br&gt;
.AddCell().AddParagraph(“Bigger text goes here!”).SetFontColor(Color.FromHtml(“#FC6A03”)).SetFontSize(20).ToRow()&lt;br&gt;
.AddCell().AddImage(“MyImage.png”).SetWidth(150).SetAlignment(HorizontalAlignment.Right).SetMargins(10, 10, 10, 10).ToRow()&lt;br&gt;
.ToDocument();&lt;br&gt;
builder.Build(“Result.pdf”);&lt;br&gt;
The result will be like this:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--001HpWNw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kg86rh3b5chvxv6wk74q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--001HpWNw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kg86rh3b5chvxv6wk74q.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To remove the table borders set the border width to 0&lt;br&gt;
section.AddTable().SetBorderWidth(0)&lt;/p&gt;

&lt;p&gt;Add text after the paragraph&lt;br&gt;
If you need to add more than one block of text in the same cell, just use the method AddText() and set its font properties.&lt;br&gt;
Also, add \n at the beginning of the paragraph to have a new line.&lt;br&gt;
The following code will add some dummy text to the main text in orange:&lt;br&gt;
DocumentBuilder builder = DocumentBuilder.New();&lt;br&gt;
var section = builder.AddSection();&lt;br&gt;
section.AddParagraph(“Hello World!”).ToDocument();&lt;br&gt;
section.AddTable().SetBorderWidth(0)&lt;br&gt;
// add 2 columns to the table&lt;br&gt;
.AddColumnToTable().AddColumnToTable()&lt;br&gt;
// add the first row&lt;br&gt;
.AddRow()&lt;br&gt;
.AddCell().AddParagraph(“Lorem ipsum dolor sit amet”).SetFontColor(Color.FromHtml(“#FC6A03”)).SetFontSize(20)&lt;br&gt;
.AddText(“\nVestibulum eget lorem ultricies, convallis sapien eu, tristique dui. Donec tincidunt efficitur interdum. Interdum et malesuada fames ac ante ipsum primis in faucibus. Praesent lacus ex, iaculis at aliquam vel, mattis eget lacus. Aenean eget velit purus. Integer euismod pretium est, a efficitur nunc dapibus at. Nam malesuada at nisl a tempor.“).SetFontSize(12).SetFontColor(Color.FromHtml(“#000000”)).ToRow()&lt;br&gt;
.AddCell().AddImage(“MyImage.png”).SetWidth(150).SetAlignment(HorizontalAlignment.Right).SetMargins(10, 10, 10, 10).ToRow().ToDocument();&lt;br&gt;
builder.Build(“Result.pdf”);&lt;br&gt;
The result will be like this:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--F7t-kTVZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/me5klclq01hp3fbgots1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--F7t-kTVZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/me5klclq01hp3fbgots1.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Add a horizontal line&lt;br&gt;
Now let’s separate the page into two sections using a horizontal line.&lt;br&gt;
Use this code to add a horizontal line and set its length, color, and alignment to center:&lt;br&gt;
section.AddLine().SetLength(500).SetColor(Color.Gray).SetAlignment(HorizontalAlignment.Center).ToDocument();&lt;br&gt;
The result will be like this:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--OlOQEiWL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/16en68vgbmxk14dmy5se.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--OlOQEiWL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/16en68vgbmxk14dmy5se.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Drawing a table with colspan and rowspan&lt;br&gt;
Now we will draw a table with 4 columns and 6 rows, change the text style, change the back color of some cells, and span some rows and columns.&lt;/p&gt;

&lt;p&gt;DocumentBuilder builder = DocumentBuilder.New();&lt;br&gt;
var section = builder.AddSection();&lt;br&gt;
section.AddParagraph(“Hello World!”).ToDocument();&lt;br&gt;
section.AddTable().SetBorderWidth(0)&lt;br&gt;
// add 2 columns to the table&lt;br&gt;
.AddColumnToTable()&lt;br&gt;
.AddColumnToTable()&lt;br&gt;
// add the first row&lt;br&gt;
.AddRow()&lt;br&gt;
.AddCell().AddParagraph(“Lorem ipsum dolor sit amet”).SetFontColor(Color.FromHtml(“#FC6A03”)).SetFontSize(20)&lt;br&gt;
.AddText(“\nVestibulum eget lorem ultricies, convallis sapien eu, tristique dui. Donec tincidunt efficitur interdum. Interdum et malesuada fames ac ante ipsum primis in faucibus. Praesent lacus ex, iaculis at aliquam vel, mattis eget lacus. Aenean eget velit purus. Integer euismod pretium est, a efficitur nunc dapibus at. Nam malesuada at nisl a tempor. “).SetFontSize(12).SetFontColor(Color.FromHtml(“#000000”)).ToRow()&lt;br&gt;
.AddCell().AddImage(“MyImage.png”).SetWidth(150).SetAlignment(HorizontalAlignment.Right).SetMargins(10, 10, 10, 10).ToRow().ToDocument();&lt;br&gt;
//// horizontal line&lt;br&gt;
section.AddLine().SetLength(500).SetColor(Color.Gray).SetAlignment(HorizontalAlignment.Center).ToDocument();&lt;br&gt;
//// table with colspan and rowspan&lt;br&gt;
section.AddParagraph(“Table with colspan and rowspan:”)&lt;br&gt;
.SetBold().SetMarginBottom(5).SetMarginTop(20).ToSection()&lt;br&gt;
// create table&lt;br&gt;
.AddTable().SetWidth(300)&lt;br&gt;
// create 4 coumns&lt;br&gt;
.AddColumnToTable().AddColumnToTable()&lt;br&gt;
.AddColumnToTable().AddColumnToTable()&lt;br&gt;
// add the first row&lt;br&gt;
.AddRow()&lt;br&gt;
// change the back color of the row&lt;br&gt;
.SetBackColor(Color.FromRgba(0, 0.69, 0.94, 1)).SetBold()&lt;br&gt;
.AddCell(“Product”)&lt;br&gt;
// span this cell in 2 rows&lt;br&gt;
.SetRowSpan(2)&lt;br&gt;
.SetVerticalAlignment(VerticalAlignment.Center)&lt;br&gt;
.ToRow()&lt;br&gt;
.AddCell(“Month”)&lt;br&gt;
// span this cell in 3 columns&lt;br&gt;
.SetColSpan(3)&lt;br&gt;
.SetHorizontalAlignment(HorizontalAlignment.Center).ToTable()&lt;br&gt;
// add the second row&lt;br&gt;
.AddRow().SetHorizontalAlignment(HorizontalAlignment.Center)&lt;br&gt;
.AddCellToRow().AddCellToRow(“January”).AddCellToRow(“February”).AddCellToRow(“March”)&lt;br&gt;
.SetBackColor(Color.FromRgba(0, 0.69, 0.94, 1)).SetBold()&lt;br&gt;
.SetHorizontalAlignment(HorizontalAlignment.Center).ToTable()&lt;br&gt;
// add the third row&lt;br&gt;
.AddRow()&lt;br&gt;
.SetHorizontalAlignment(HorizontalAlignment.Center)&lt;br&gt;
.AddCell().AddParagraphToCell(“Product 1”)&lt;br&gt;
.SetHorizontalAlignment(HorizontalAlignment.Left).ToRow()&lt;br&gt;
.AddCellToRow(“1,000”)&lt;br&gt;
.AddCellToRow(“1,015”)&lt;br&gt;
.AddCellToRow(“1,030”).ToTable()&lt;br&gt;
// add the fourth row&lt;br&gt;
.AddRow()&lt;br&gt;
.SetHorizontalAlignment(HorizontalAlignment.Center).AddCell()&lt;br&gt;
.AddParagraphToCell(“Product 2”)&lt;br&gt;
.SetHorizontalAlignment(HorizontalAlignment.Left).ToRow()&lt;br&gt;
.AddCellToRow(“2,000”)&lt;br&gt;
.AddCellToRow(“2,040”)&lt;br&gt;
.AddCellToRow(“2,075”).ToTable()&lt;br&gt;
// add the fifth row&lt;br&gt;
.AddRow().SetHorizontalAlignment(HorizontalAlignment.Center).AddCell().AddParagraphToCell(“Product 3”)&lt;br&gt;
.SetHorizontalAlignment(HorizontalAlignment.Left)&lt;br&gt;
.ToRow()&lt;br&gt;
.AddCellToRow(“3,000”)&lt;br&gt;
.AddCellToRow(“3,060”)&lt;br&gt;
.AddCellToRow(“3,100”).ToTable()&lt;br&gt;
// add the sixth row&lt;br&gt;
.AddRow()&lt;br&gt;
.SetBackColor(Color.FromRgba(0, 0.69, 0.94, 1)).SetBold()&lt;br&gt;
.AddCellToRow(“Total for Q1”)&lt;br&gt;
.AddCell(“18,320”)&lt;br&gt;
.SetHorizontalAlignment(HorizontalAlignment.Left)&lt;br&gt;
.SetColSpan(3).ToDocument();&lt;br&gt;
builder.Build(“Result.pdf”);&lt;br&gt;
The result will be like this:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--e1s2D39E--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oiirsdqvy6cp8i4ndbwr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--e1s2D39E--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oiirsdqvy6cp8i4ndbwr.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Create Repeating Areas (headers and footers)&lt;br&gt;
Almost in all business documents, it is necessary to set dynamic page counters, headers, and footers. Let’s see how to do this.&lt;/p&gt;

&lt;p&gt;To create a header you the method AddHeaderToBothPages(height) and set its height, and for creating a footer use the method AddFooterToBothPages(height) and set its height too.&lt;/p&gt;

&lt;p&gt;To create an automatic page number(page counter) in the footer use the method AddPageNumberToParagraph(“page #”).&lt;/p&gt;

&lt;p&gt;The following code will generate a header with image and text, a footer with text, and an automatic page counter.&lt;/p&gt;

&lt;p&gt;// repeating areas&lt;br&gt;
// set text style for repeating area&lt;br&gt;
var styleMain = StyleBuilder.New().SetFontSize(11);&lt;br&gt;
section.ApplyStyle(styleMain)&lt;br&gt;
// create header and se its height&lt;br&gt;
.AddHeaderToBothPages(30)&lt;br&gt;
.AddParagraph().SetFont(Fonts.Courier(11))&lt;br&gt;
.SetAlignment(HorizontalAlignment.Center)&lt;br&gt;
// create header image&lt;br&gt;
.AddInlineImage(“MyImage.png”, 24, 24)&lt;br&gt;
.SetMarginRight(5).SetMarginTop(5).ToParagraph()&lt;br&gt;
// create header text&lt;br&gt;
.AddText(“PDFFlow Library”).ToSection()&lt;br&gt;
// this code will create a new PDF page&lt;br&gt;
.InsertPageBreak()&lt;br&gt;
.AddParagraphToSection(“This document is an example of a PDF document:” +“ a header with an image, “ +“a footer with a line and a page number, an empty page at the end.”)&lt;br&gt;
// create footer and set its height&lt;br&gt;
.AddFooterToBothPages(20).AddLineToRepeatingArea()&lt;br&gt;
// add text to footer&lt;br&gt;
.AddParagraph(“A footer with custom text, “)&lt;br&gt;
// generate an outomatic page number&lt;br&gt;
.AddPageNumberToParagraph(“page #”)&lt;br&gt;
.SetAlignment(HorizontalAlignment.Center).ToDocument();&lt;br&gt;
The result will be like this:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--kcAD596w--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7t9vvnrgh3kzmg0w5gv5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--kcAD596w--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7t9vvnrgh3kzmg0w5gv5.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Creating Multilevel List&lt;br&gt;
To create a Multilevel list you can use the method SetListNumbered() for numerical/Latin/Roman listed items, and the method SetListBulleted() for bulleted/dashed listed items.&lt;br&gt;
For example, if you need to create a bulleted first-level list item add the following code to the&lt;br&gt;
.AddParagraph(“Europe”).SetListBulleted(ListBullet.Bullet, 0)&lt;br&gt;
For creating a numbered second-level list item add the following code&lt;br&gt;
.AddParagraph(“Australia”).SetListNumbered(NumerationStyle.Arabic, 1)&lt;br&gt;
Use the following code to generate a three-leveled list with bulleted, dashed, and numbered items:&lt;br&gt;
////Multilevel List&lt;br&gt;
section.AddParagraph(“Asia”).SetMarginTop(50).SetFontColor(Color.Red)&lt;br&gt;
.SetListBulleted().ToSection()&lt;br&gt;
.AddParagraph(“China”)&lt;br&gt;
.SetListNumbered(NumerationStyle.Arabic,1).SetFontColor(Color.Red).ToSection()&lt;br&gt;
.AddParagraph(“Russia”)&lt;br&gt;
.SetListNumbered(NumerationStyle.Arabic,1).SetFontColor(Color.Red).ToSection()&lt;br&gt;
.AddParagraph(“Moscow”).SetListBulleted(ListBullet.Dash,2).SetFontColor(Color.Red).ToSection()&lt;br&gt;
.AddParagraph(“Omsk”).SetListBulleted(ListBullet.Dash,2).SetFontColor(Color.Red).ToSection()&lt;br&gt;
.AddParagraph(“Africa”).SetFontColor(Color.Green)&lt;br&gt;
.SetListBulleted().ToSection()&lt;br&gt;
.AddParagraph(“Nigeria”)&lt;br&gt;
.SetListNumbered(NumerationStyle.LowerLatin,1).SetFontColor(Color.Green).ToSection()&lt;br&gt;
.AddParagraph(“Kenya”)&lt;br&gt;
.SetListNumbered(NumerationStyle.LowerLatin,1).SetFontColor(Color.Green).ToSection()&lt;br&gt;
.AddParagraph(“North America”).SetFontColor(Color.Blue)&lt;br&gt;
.SetListBulleted().ToSection()&lt;br&gt;
.AddParagraph(“Canada”)&lt;br&gt;
.SetListNumbered(NumerationStyle.LowerRoman,1).SetFontColor(Color.Blue).ToSection()&lt;br&gt;
.AddParagraph(“USA”)&lt;br&gt;
.SetListNumbered(NumerationStyle.LowerRoman,1).SetFontColor(Color.Blue).ToSection()&lt;br&gt;
.ToDocument();&lt;br&gt;
The result will be like this:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--yx_QsZ0M--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oe2w8d24kcyvmjoadskk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--yx_QsZ0M--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oe2w8d24kcyvmjoadskk.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this tutorial, we considered how to create a simple PDF document from scratch and learned how to generate the most important and common PDF items using PDFFlow.&lt;/p&gt;

&lt;p&gt;You can find more useful examples on generating PDF docs on GitHub here: &lt;a href="https://go.pdfflow.io/exmpls-pdfflow"&gt;https://go.pdfflow.io/exmpls-pdfflow&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For more information about the PDFFlow library, check out the GS PDFFlow website: &lt;a href="https://go.pdfflow.io/lib-pdfflowhome"&gt;https://go.pdfflow.io/lib-pdfflowhome&lt;/a&gt;&lt;/p&gt;

</description>
      <category>csharp</category>
      <category>createpdf</category>
      <category>pdf</category>
      <category>dotnet</category>
    </item>
    <item>
      <title>C#: How to layout a PDF document with rowspan, colspan, and nested tables using PDFFlow
</title>
      <dc:creator>Sharnouby55</dc:creator>
      <pubDate>Thu, 26 Aug 2021 14:26:59 +0000</pubDate>
      <link>https://dev.to/mo_elsharnouby/c-how-to-layout-a-pdf-document-with-rowspan-colspan-and-nested-tables-using-pdfflow-28pd</link>
      <guid>https://dev.to/mo_elsharnouby/c-how-to-layout-a-pdf-document-with-rowspan-colspan-and-nested-tables-using-pdfflow-28pd</guid>
      <description>&lt;p&gt;This tutorial will show you when to use rowspan, colspan, and nested tables to get particular layouts and will guide you step-by-step to easily layout your PDF with the help of these options using the C# PDF generation library PDFFlow.&lt;/p&gt;

&lt;p&gt;Layouting using rowspans, colspans and nested tables allows you to arrange data into rows and columns. They are commonly used to display tabular data like product listings, customer details, financial reports, tickets, and so on.&lt;/p&gt;

&lt;p&gt;Use rowspan and colspan parameters if you need to get a table with cells spanning several rows and columns. Setting rowspan and colspan for table cells allow you to create simple well-organized layouts.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7nhqvoq14qg3oht5f9ws.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7nhqvoq14qg3oht5f9ws.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Use a nested table if you need to get a table with a more complex structure. For complex cases, you should divide your table into several logical parts and add them as nested tables. nested tables allow you to create bigger, more complex but well-ordered layouts.&lt;br&gt;
In the following example, the nested table is highlighted in blue.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhcgnfadek1mjv0w1v95k.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhcgnfadek1mjv0w1v95k.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Getting started&lt;br&gt;
First, we need to create a new console application project "MyLayoutingDoc" in Visual Studio and choose Language “C#”.&lt;br&gt;
Then we need to add the Gehtsoft.PDFFlowlib library to our project. You can download it as a NuGet package from NuGet Gallery | Gehtsoft.PDFFlowLib.&lt;br&gt;
You can also install it from the Visual Studio NuGet Package Manager: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;In Solution Explorer, right-click either References or a project and select Manage NuGet Packages.&lt;/li&gt;
&lt;li&gt;Go to the Browse tab and search for the Gehtsoft.PDFFlowLib package using the search box on the upper left. &lt;/li&gt;
&lt;li&gt;Select the package from the list and click Install.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;When to use rowspan&lt;br&gt;
When you want to place components in a single table cell vertically spanning several rows.&lt;br&gt;
How to create a layout with rowspan &lt;br&gt;
Let’s create a table with 4 rows and 4 columns and make the first cell span 4 rows.&lt;br&gt;
First, use the AddColumnToTable() method to define how many columns you need to create in your table, and the AddRow() method to define how many rows you need to create.&lt;br&gt;
To add cells to rows, you need to use the method AddCellToRow(), you can also add text to cells using the same method by adding your text in parentheses: AddCellToRow(“your text here”).&lt;br&gt;
Now to make one cell span 2 or more rows, first, you need to use the AddCell() method, then use the SetRowSpan(number of rows to span here) method, then the ToRow() method.&lt;br&gt;
Ex: .AddCell("row span").SetRowSpan(4).ToRow()&lt;/p&gt;

&lt;p&gt;Also, you can use the method AddCell and pass its 3 parameters Text, colspan, and rowspan to set how many rows and columns should the cell span.&lt;br&gt;
Ex:  .AddCell("text here", 1, 3).ToRow()&lt;br&gt;
The following code will create a table with 4 rows and 4 columns, with the first cell spanning 4 rows:&lt;/p&gt;

&lt;p&gt;DocumentBuilder.New().AddSection().AddTable()&lt;br&gt;
// add 4 columns to the table&lt;br&gt;
                                   .AddColumnToTable().AddColumnToTable()&lt;br&gt;
                                   .AddColumnToTable().AddColumnToTable()&lt;br&gt;
// add the first row&lt;br&gt;
                                   .AddRow()&lt;br&gt;
// add the first cell and make it span 4 rows&lt;br&gt;
                                       .AddCell("row span").SetRowSpan(4).ToRow()&lt;br&gt;
                                       .AddCellToRow()&lt;br&gt;
                                       .AddCellToRow()&lt;br&gt;
                                       .AddCellToRow().ToTable()&lt;br&gt;
// add the second row&lt;br&gt;
                                   .AddRow()&lt;br&gt;
                                       .AddCellToRow()&lt;br&gt;
                                       .AddCellToRow()&lt;br&gt;
                                       .AddCellToRow()&lt;br&gt;
                                       .AddCellToRow().ToTable()&lt;br&gt;
//  add the third row&lt;br&gt;
                                   .AddRow()&lt;br&gt;
                                       .AddCellToRow()&lt;br&gt;
                                       .AddCellToRow()&lt;br&gt;
                                       .AddCellToRow()&lt;br&gt;
                                       .AddCellToRow().ToTable()&lt;br&gt;
// add the fourth row&lt;br&gt;
                                   .AddRow()&lt;br&gt;
                                       .AddCellToRow()&lt;br&gt;
                                       .AddCellToRow()&lt;br&gt;
                                       .AddCellToRow()&lt;br&gt;
                                       .AddCellToRow()&lt;br&gt;
// generate the PDF document&lt;br&gt;
                           .ToDocument().Build("Result.pdf");&lt;br&gt;
The result should be similar to this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu03mcgii88ow3foq00cr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu03mcgii88ow3foq00cr.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When to use colspan&lt;br&gt;
When you want to place components in a single table cell horizontally spanning several columns.&lt;br&gt;
How to create a layout with colspan&lt;br&gt;
Using colspan is pretty much the same as using rowspan, you first need to use the AddColumnToTable() method to define how many columns you need, and the AddRow() method to define how many rows you need to create in your table. Here we are creating a table with 4 rows and 4 columns with the first cell spanning 4 columns.&lt;br&gt;
To add cells to a row, you need to use the method AddCellToRow().&lt;br&gt;
Now to make one cell span 4 columns, first, you need to use the AddCell() method, then use the SetColSpan(number of columns to span here) method, then the ToTable() method.&lt;br&gt;
Ex: .AddCell("col span").SetColSpan(4).ToTable()&lt;/p&gt;

&lt;p&gt;Or you can use the method AddCell and pass its 3 parameters Text, colspan, and rowspan.&lt;br&gt;
Ex:  .AddCell("text here", 1, 3).ToRow()&lt;br&gt;
The following code will create a table with 4 rows and 4 columns, with the first cell spanning 4 columns:&lt;/p&gt;

&lt;p&gt;DocumentBuilder.New().AddSection().AddTable()&lt;br&gt;
// create 4 columns&lt;br&gt;
                       .AddColumnToTable().AddColumnToTable()&lt;br&gt;
                       .AddColumnToTable().AddColumnToTable()&lt;br&gt;
// add the first cell to the first row and make it span 4 columns&lt;br&gt;
                       .AddRow()&lt;br&gt;
                           .AddCell("col span").SetColSpan(4).ToTable()&lt;br&gt;
// add the second row&lt;br&gt;
                       .AddRow()&lt;br&gt;
// create 4 cells in the second row&lt;br&gt;
                           .AddCellToRow()&lt;br&gt;
                           .AddCellToRow()&lt;br&gt;
                           .AddCellToRow()&lt;br&gt;
                           .AddCellToRow().ToTable()&lt;br&gt;
// add the third row&lt;br&gt;
                       .AddRow()&lt;br&gt;
                           .AddCellToRow()&lt;br&gt;
                           .AddCellToRow()&lt;br&gt;
                           .AddCellToRow()&lt;br&gt;
                           .AddCellToRow().ToTable()&lt;br&gt;
// add the fourth row&lt;br&gt;
                       .AddRow()&lt;br&gt;
                           .AddCellToRow()&lt;br&gt;
                           .AddCellToRow()&lt;br&gt;
                           .AddCellToRow()&lt;br&gt;
                           .AddCellToRow()&lt;br&gt;
//  generate the PDF document&lt;br&gt;
               .ToDocument().Build("Result.pdf");&lt;br&gt;
The result should be similar to this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcnq05d5fp5wbs159s117.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcnq05d5fp5wbs159s117.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Using rowspan and colspan together&lt;br&gt;
If you need to create a layout where cells span both several rows and several columns, you just need to use both methods SetRowSpan(#) and SetColSpan(#).&lt;br&gt;
 For example:&lt;br&gt;
                     .AddRow()&lt;br&gt;
                            .AddCell("row span").SetRowSpan(4).ToRow()&lt;br&gt;
                            .AddCell("col span").SetColSpan(3).ToTable()&lt;br&gt;
Notice that we set the number of columns to span to 3 because the first cell already spans 4 rows and only 3 columns are left to span.&lt;br&gt;
The following code will create a table with 4 rows and 4 columns, with the first cell spanning 4 rows and the second cell spanning 3 columns:&lt;br&gt;
             DocumentBuilder.New().AddSection().AddTable()&lt;br&gt;
// add 4 columns to the table&lt;br&gt;
                        .AddColumnToTable().AddColumnToTable()&lt;br&gt;
                        .AddColumnToTable().AddColumnToTable()&lt;br&gt;
// add the first row&lt;br&gt;
                        .AddRow()&lt;br&gt;
// make the first cell in the first row span 4 rows&lt;br&gt;
                            .AddCell("row span").SetRowSpan(4).ToRow()&lt;br&gt;
// make the second cell in the first row span 3 columns&lt;br&gt;
                            .AddCell("col span").SetColSpan(3).ToTable()&lt;br&gt;
// add the second row&lt;br&gt;
                        .AddRow()&lt;br&gt;
                            .AddCellToRow()&lt;br&gt;
                            .AddCellToRow()&lt;br&gt;
                            .AddCellToRow()&lt;br&gt;
                            .AddCellToRow().ToTable()&lt;br&gt;
// add the third row&lt;br&gt;
                        .AddRow()&lt;br&gt;
                            .AddCellToRow()&lt;br&gt;
                            .AddCellToRow()&lt;br&gt;
                            .AddCellToRow()&lt;br&gt;
                            .AddCellToRow().ToTable()&lt;br&gt;
// add the fourth row&lt;br&gt;
                        .AddRow()&lt;br&gt;
                            .AddCellToRow()&lt;br&gt;
                            .AddCellToRow()&lt;br&gt;
                            .AddCellToRow()&lt;br&gt;
                            .AddCellToRow()&lt;br&gt;
// generate the PDF document&lt;br&gt;
                .ToDocument().Build("Result.pdf");&lt;br&gt;
The result should be similar to this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp33mlv24dbsggpon7kg1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp33mlv24dbsggpon7kg1.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When to use nested tables&lt;br&gt;
You should use a nested table when you need to design a more complicated layout, for example, if you need to organize components in a tabular format within a single table cell.&lt;br&gt;
Here's an example of a complex real document created using the PDFFlow library.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fftpjfcqzse44g72hzk1l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fftpjfcqzse44g72hzk1l.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The area marked by a green rectangle is a table with a complex structure - it contains two nested tables - marked in red and dark-yellow. These tables are nested inside the cells of the main table that contains two columns (75% and 25% width respectively).&lt;br&gt;
But it is important to note that all elements in any cell are placed after one another in any case by using the break command “\n”. &lt;br&gt;
A clear example of this behavior is the placement of the texts: "Passenger name", "PAVEL REMPEL".&lt;br&gt;
How to create a layout with nested tables&lt;br&gt;
1- Decomposing the code into separate procedures&lt;br&gt;
To create several nested tables we should decompose the code into separate procedures. Create methods to generate each nested table, then call it when creating the main table, and pass the cell where we need to insert the nested table in the cell parameter.&lt;br&gt;
For example:&lt;br&gt;
           var row2 = table.AddRow().AddCellToRow();&lt;br&gt;
            AddTablePartToCell(row2.AddCell());&lt;br&gt;
// basically generate and insert a table inside a table cell&lt;br&gt;
        private static void AddTablePartToCell(TableCellBuilder cell)&lt;br&gt;
        {&lt;br&gt;
//  create a nested table with 2 rows and 2 columns&lt;br&gt;
            cell.AddTable().AddColumnToTable().AddColumnToTable()&lt;br&gt;&lt;br&gt;
                    .AddRow()&lt;br&gt;
                        .AddCellToRow("Row 1 Col 1").AddCellToRow("Row 1 Col 2").ToTable()&lt;br&gt;
                    .AddRow().AddCellToRow("Row 2 Col 1").AddCellToRow("Row 2 Col 2");&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In this example, the nested table is highlighted in yellow.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdc23oqyzvcudl1nxcir5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdc23oqyzvcudl1nxcir5.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;2- Using fluent style to create nested tables&lt;br&gt;
When you need to create a document using a table with nested tables, as an alternative to decomposing the code into separate procedures, you can get the same result using the fluent style to code the entire complex table. You can change the current fluent context to parent objects using the following To... methods: ToTable, ToRow, ToCell, ToSection. To avoid errors when creating nested tables, it is important to correctly specify changes of the current fluent context.&lt;br&gt;
Example of creating a simple table with a nested table using the fluent style:&lt;br&gt;
            DocumentBuilder.New()&lt;br&gt;
            .AddSection()&lt;br&gt;
            .AddTable()&lt;br&gt;
            .AddColumnToTable()&lt;br&gt;
            .AddRow()&lt;br&gt;
            .AddCell()&lt;br&gt;
                .AddTable()&lt;br&gt;
                    .AddColumnToTable()&lt;br&gt;
                    .AddRow()&lt;br&gt;
                    .AddCell("Nested table")&lt;br&gt;
                .ToTable()&lt;br&gt;
            .ToCell()&lt;br&gt;
                .ToTable()&lt;br&gt;
            .AddRow()&lt;br&gt;
                .AddCell("Parent table")&lt;br&gt;
                .ToDocument().Build("Result.pdf");&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0j3p7cxn210eurjf0ksx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0j3p7cxn210eurjf0ksx.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Also, you can set the width of each column in percent using the AddColumnPercent("", %) method. In our case, we will set the first column to 25% and the second column to 75%.&lt;br&gt;
For example:&lt;br&gt;
                        .AddColumnPercent("", 25).ToTable()&lt;br&gt;
                        .AddColumnPercent("", 75).ToTable()&lt;br&gt;
Create the first row and add 2 cells to it using the AddCell() method as follows:&lt;br&gt;
.AddRow()&lt;br&gt;
                        .AddCell("column 1,\nrow span", 1, 3).ToRow()&lt;br&gt;
                        .AddCell("column 2")&lt;br&gt;
Notice that the AddCell method here takes 3 parameters (text, colspan, and rowspan), also remember that using “\n” within the text will make a line break.&lt;br&gt;
To create the second row, we need to declare a new var and fill it with a new table cell, then call our method AddTablePartToCell() and pass the newly created cell in it, as shown in the following code:&lt;br&gt;
            var row2 = table.AddRow()&lt;br&gt;
                            .AddCellToRow();&lt;br&gt;
            AddTablePartToCell(row2.AddCell());&lt;/p&gt;

&lt;p&gt;The following code will create a table (main table) of 2 columns and 3 rows, span the first cell to 3 rows and insert another table (nested table) in the second column and the second row:&lt;br&gt;
        {&lt;br&gt;
//  create the main table&lt;br&gt;
            var table = DocumentBuilder.New()&lt;br&gt;
                .AddSection()&lt;br&gt;
                    .AddTable()&lt;br&gt;
// add 2 columns to the table with 25% and 75% width&lt;br&gt;
                        .AddColumnPercent("", 25).ToTable()&lt;br&gt;
                        .AddColumnPercent("", 75).ToTable()&lt;br&gt;
                        .AddRow()&lt;br&gt;
// \n will create a line-break inside the cell&lt;br&gt;
                        .AddCell("column 1,\nrow span", 1, 3).ToRow()&lt;br&gt;
                        .AddCell("column 2")&lt;br&gt;
                .ToTable();&lt;br&gt;
//  create the second row&lt;br&gt;
            var row2 = table.AddRow()&lt;br&gt;
                            .AddCellToRow();&lt;br&gt;
// here we call the method that generates the nested table and inserts it into &lt;br&gt;
// the cell of the main table&lt;br&gt;
            AddTablePartToCell(row2.AddCell());&lt;br&gt;
//  add the last row to the main table &lt;br&gt;
            table.AddRow()&lt;br&gt;
                            .AddCellToRow()&lt;br&gt;
                            .AddCellToRow()&lt;br&gt;
            .ToDocument().Build("Result.pdf");&lt;br&gt;
        }&lt;br&gt;
// create a method that generates the nested table and inserts it into the main table&lt;br&gt;
        private static void AddTablePartToCell(TableCellBuilder cell)&lt;br&gt;
        {&lt;br&gt;
// create the nested table&lt;br&gt;
            cell.AddTable()&lt;br&gt;
// create 4 columns&lt;br&gt;
                    .AddColumnToTable().AddColumnToTable()&lt;br&gt;
                    .AddColumnToTable().AddColumnToTable()&lt;br&gt;
                    .AddRow()&lt;br&gt;
// using \n to make a line-break &lt;br&gt;
// add the first cell and make it span 4 rows&lt;br&gt;
                        .AddCell("column 1,\nrow span", 1, 4).ToRow()&lt;br&gt;
                        .AddCellToRow("column 2")&lt;br&gt;
                        .AddCellToRow("column 3")&lt;br&gt;
                        .AddCell("column 4").ToTable()&lt;br&gt;
// add the second row&lt;br&gt;
                    .AddRow()&lt;br&gt;
                        .AddCellToRow().AddCellToRow()&lt;br&gt;
                        .AddCellToRow().AddCellToRow().ToTable()&lt;br&gt;
// add the third row&lt;br&gt;
                    .AddRow()&lt;br&gt;
                        .AddCellToRow().AddCellToRow()&lt;br&gt;
                        .AddCellToRow().AddCellToRow().ToTable()&lt;br&gt;
// add the fourth row&lt;br&gt;
                    .AddRow()&lt;br&gt;
                        .AddCellToRow().AddCellToRow()&lt;br&gt;
                        .AddCellToRow().AddCellToRow();&lt;br&gt;
        }&lt;br&gt;
The result should be similar to this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9qxpha75ma2ic63f9y48.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9qxpha75ma2ic63f9y48.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The nested table is highlighted in yellow and the main table is highlighted in grey.&lt;/p&gt;

&lt;p&gt;Create a complex table layout using nested tables, rowspan, and colspan&lt;br&gt;
Now let's try to create an even more complex layout containing 2 nested tables with spanned rows and columns.&lt;br&gt;
It’s very simple, all we have to do is to create a method for each nested table that generates and fills the nested table inside the main table’s cells.&lt;br&gt;
First, we need to create 2 methods that generate the 2 nested tables:&lt;br&gt;
//  notice that this method expects a TableCellBuilder parameter&lt;br&gt;&lt;br&gt;
private static void AddInnerTable1(TableCellBuilder cell)&lt;br&gt;
        {&lt;br&gt;
// let’s set the background of the first nested table to green&lt;br&gt;
            cell.AddTable().SetBackColor(Gehtsoft.PDFFlow.Models.Shared.Color.Green)&lt;br&gt;
// add two columns &lt;br&gt;
                    .AddColumnToTable().AddColumnToTable()&lt;br&gt;
// add the first row and the first cell with the text “Name”&lt;br&gt;
                    .AddRow()&lt;br&gt;
                        .AddCellToRow("Name")&lt;br&gt;
// add the second cell and make it span 4 rows&lt;br&gt;
                        .AddCell("photo", 1, 4)&lt;br&gt;
                        .SetHorizontalAlignment(HorizontalAlignment.Center)&lt;br&gt;
                        .ToTable()&lt;br&gt;
//  add the other 3 rows and fill their cells with texts&lt;br&gt;
                    .AddRow()&lt;br&gt;
                        .AddCellToRow("Surname")&lt;br&gt;
                        .AddCell().ToTable()&lt;br&gt;
                    .AddRow()&lt;br&gt;
                        .AddCellToRow("State")&lt;br&gt;
                        .AddCell().ToTable()&lt;br&gt;
                    .AddRow()&lt;br&gt;
                        .AddCellToRow("City")&lt;br&gt;
                        .AddCell().ToTable();&lt;br&gt;
        }&lt;br&gt;&lt;br&gt;
// now we create the second method that generates the second nested table&lt;br&gt;
private static void AddInnerTable2(TableCellBuilder cell)&lt;br&gt;
        {&lt;br&gt;
// let’s set the background of the second nested table to yellow&lt;br&gt;
            cell.AddTable().SetBackColor(Gehtsoft.PDFFlow.Models.Shared.Color.Yellow)&lt;br&gt;
// add two columns &lt;br&gt;
                    .AddColumnToTable().AddColumnToTable()&lt;br&gt;
// add the first row and the first cell with the text “Department”&lt;br&gt;
                    .AddRow()&lt;br&gt;
                        .AddCellToRow("Department")&lt;br&gt;
// add the second cell to the first row and make it span 2 rows&lt;br&gt;
                        .AddCell("image", 1, 2)&lt;br&gt;
                        .SetHorizontalAlignment(HorizontalAlignment.Center)&lt;br&gt;
                        .ToTable()&lt;br&gt;
// add the second row&lt;br&gt;
                    .AddRow()&lt;br&gt;
                        .AddCellToRow("Structure")&lt;br&gt;
                        .AddCell().ToTable()&lt;br&gt;
// add the third row&lt;br&gt;
                    .AddRow()&lt;br&gt;
                        .AddCellToRow("Floor")&lt;br&gt;
// add the second cell to the third row and make it span 2 rows&lt;br&gt;
                        .AddCell("qrcode", 1, 2)&lt;br&gt;
                        .SetHorizontalAlignment(HorizontalAlignment.Center)&lt;br&gt;
                        .ToTable()&lt;br&gt;
// add the fourth row&lt;br&gt;
                    .AddRow()&lt;br&gt;
                        .AddCellToRow("Room")&lt;br&gt;
                        .AddCell().ToTable();&lt;br&gt;
        }&lt;/p&gt;

&lt;p&gt;// now what is left is to create the main table, with 2 columns and 3 rows,&lt;br&gt;
// in the main class and call our 2 methods&lt;br&gt;
// that will insert the two nested tables in the main table&lt;br&gt;
{&lt;br&gt;
            var table = DocumentBuilder.New()&lt;br&gt;
                .AddSection().AddTable().AddColumnToTable().AddColumnToTable()&lt;br&gt;
// here we create the first row with 2 cells with the  center alignment &lt;br&gt;
                        .AddRow().AddCellToRow("Personal info").SetHorizontalAlignment(HorizontalAlignment.Center)&lt;br&gt;
                        .AddCellToRow("Department info").SetHorizontalAlignment(HorizontalAlignment.Center).ToTable();&lt;br&gt;
// now the magic happens,&lt;br&gt;
// here we create the second row of the main table&lt;br&gt;
// and call our 2 methods to insert the two nested tables in the &lt;br&gt;
// first and second cells of the second row&lt;br&gt;
            var row2 = table.AddRow();&lt;br&gt;
            AddInnerTable1(row2.AddCell());&lt;br&gt;
            AddInnerTable2(row2.AddCell());&lt;br&gt;
//  add the third row to the main table and make it span 2 columns&lt;br&gt;
            table.AddRow().AddCellToRow("Signature",2,1)&lt;br&gt;
            .ToDocument().Build("Result.pdf");&lt;br&gt;
}&lt;br&gt;
The result should be similar to this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frw53oqczuhutm5lt26k9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frw53oqczuhutm5lt26k9.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We’ve covered the most important tips on how to layout documents using rowspans, colspans, and nested tables on simple and complex examples. &lt;br&gt;
Now you know when to use colspan, rowspan, and nested tables and can create PDF documents with complex layouts using the instructions of this tutorial.&lt;/p&gt;

&lt;p&gt;You can find more useful examples on generating PDF docs on GitHub here: &lt;a href="https://go.pdfflow.io/lib_example" rel="noopener noreferrer"&gt;https://go.pdfflow.io/lib_example&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For more information about the PDFFlow library, check out the GS PDFFlow website: &lt;a href="https://go.pdfflow.io/library_home" rel="noopener noreferrer"&gt;https://go.pdfflow.io/library_home&lt;/a&gt;&lt;/p&gt;

</description>
      <category>csharp</category>
      <category>dotnet</category>
    </item>
  </channel>
</rss>
