How to Start Convert PDF Read PDF Edit PDF PDF Report Builder Work with PDF Modules PDF Document PDF Pages Text Image Graph & Path Annotation, Markup & Drawing Redaction Security Digital Signature Forms Watermark Bookmark Link File Attachment File Metadata Printing Work with Other SDKs Barcode read Barcode create OCR Twain

C# PDF Report Generator Library
How to add a paragraph of text to PDF file using C#


C# Demo Code to add a text paragraph to adobe pdf file





In this C# tutorial, you will learn how to create a new PDF report file from scratch, and add text paragraph content to the new PDF file using C# in ASP.NET MVC Web, Windows applications.

  • Add paragraph text to PDF
  • Adjust indentation, margin space, alignment to the text paragraph in PDF
  • Adjust font style, text underline style to the text paragraph on PDF





Set paragraph indentation


In this C# example code, you will learn how to create a new PDF file, and add text paragraph with alignment and identation applied.

  • Utilize PDFBuildHandler.Create() to create a new PDF file
  • Call method Document.Open() to start adding text content to the PDF document
  • Create a new Paragraph object "p1", with text content defined
  • Set Paragraph.Alignment to "Alignment.ALIGN_JUSTIFIED"
  • Set Paragraph.FirstLineIndent to 0F
  • Set Paragraph.IndentationLeft to 0F
  • Set Paragraph.IndentationRight to 0F
  • Add the paragraph to the PDF document
  • Use the same properties to add two more paragraph text to PDF document
  • Call method Document.Close() to apply the content changes



String outputFilePath = Program.RootPath + "\\" + "Sample6.pdf";

Document document = new Document();
PDFBuildHandler.Create(document, outputFilePath);

//  open document
document.Open();

Paragraph p1 = new Paragraph("This is a sample paragraph that the first line indentation is 0 pt., the left indentation is 0 pt.and the right indentation it 0 pt.");
p1.Alignment = Alignment.ALIGN_JUSTIFIED;
p1.FirstLineIndent = 0F;
p1.IndentationLeft = 0F;
p1.IndentationRight = 0F;
document.Add(p1);

Paragraph p2 = new Paragraph("This is a sample paragraph that the first line indentation is -20 pt., the left indentation is 50 pt.and the right indentation it 50 pt.");
p2.Alignment = Alignment.ALIGN_JUSTIFIED;
p2.FirstLineIndent = -20F;
p2.IndentationLeft = 50F;
p2.IndentationRight = 50F;
document.Add(p2);

Paragraph p3 = new Paragraph("This is a sample paragraph that the first line indentation is 20 pt., the left indentation is 50 pt.and the right indentation it 50 pt.");
p3.Alignment = Alignment.ALIGN_JUSTIFIED;
p3.FirstLineIndent = 20F;
p3.IndentationLeft = 50F;
p3.IndentationRight = 50F;
document.Add(p3);

//  close document and save to the output file
document.Close();




Set paragraph before and after space


String outputFilePath = Program.RootPath + "\\" + "Sample7.pdf";

Document document = new Document();
PDFBuildHandler.Create(document, outputFilePath);

//  open document
document.Open();

Paragraph p1 = new Paragraph("This is a sample paragraph that the before paragraph spacing is 0 pt. and the after paragraph spacing is 0 pt.");
p1.SpaceBefore = 0;
p1.SpaceAfter = 0;
document.Add(p1);

Paragraph p2 = new Paragraph("This is a sample paragraph that the before paragraph spacing is 20 pt. and the after paragraph spacing is 100 pt.");
p2.SpaceBefore = 20;
p2.SpaceAfter = 100;
document.Add(p2);

Paragraph p3 = new Paragraph("This is a sample paragraph that the before paragraph spacing is 0 pt. and the after paragraph spacing is 0 pt.");
p3.SpaceBefore = 0;
p3.SpaceAfter = 0;
document.Add(p3);

//  close document and save to the output file
document.Close();




Set line space in the paragraph


String outputFilePath = Program.RootPath + "\\" + "Sample8.pdf";
String content = "The Solar System is the gravitationally bound system comprising the Sun and the objects that orbit it, either directly or indirectly. Of those objects that orbit the Sun directly, the largest eight are the planets, with the remainder being significantly smaller objects, such as dwarf planets and small Solar System bodies. Of the objects that orbit the Sun indirectly, the moons, two are larger than the smallest planet, Mercury.";

Document document = new Document();
PDFBuildHandler.Create(document, outputFilePath);

//  open document
document.Open();

Paragraph p1 = new Paragraph(content);
p1.SetLeading(20, 1);
document.Add(p1);

Paragraph p2 = new Paragraph(content);
p2.SetLeading(0, 2);
document.Add(p2);

Paragraph p3 = new Paragraph(content);
p3.SetLeading(50, 2);
document.Add(p3);

//  close document and save to the output file
document.Close();




Set paragraph alignment


String outputFilePath = Program.RootPath + "\\" + "Sample9.pdf";
String content = "The Solar System is the gravitationally bound system comprising the Sun and the objects that orbit it, either directly or indirectly. Of those objects that orbit the Sun directly, the largest eight are the planets, with the remainder being significantly smaller objects, such as dwarf planets and small Solar System bodies. Of the objects that orbit the Sun indirectly, the moons, two are larger than the smallest planet, Mercury.";

Document document = new Document();
PDFBuildHandler.Create(document, outputFilePath);

//  open document
document.Open();

//  right alignment
Paragraph p1 = new Paragraph(content);
p1.Alignment = Alignment.ALIGN_RIGHT;
document.Add(p1);

//  left alignment
Paragraph p2 = new Paragraph(content);
p2.Alignment = Alignment.ALIGN_LEFT;
document.Add(p2);

//  center alignment
Paragraph p3 = new Paragraph(content);
p3.Alignment = Alignment.ALIGN_CENTER;
document.Add(p3);

//  justified
Paragraph p4 = new Paragraph(content);
p4.Alignment = Alignment.ALIGN_JUSTIFIED;
document.Add(p4);

//  close document and save to the output file
document.Close();




Create a paragraph by the specified font


String outputFilePath = Program.RootPath + "\\" + "Sample10.pdf";

Document document = new Document();
PDFBuildHandler.Create(document, outputFilePath);

//  open document
document.Open();

Paragraph p1 = new Paragraph("Font: Helvetica, 36, Regular ", new Font(Font.FontFamily.Helvetica, 36F, Font.FontStyle.Regular));
document.Add(p1);
Paragraph p2 = new Paragraph("Font: TimesRoman, 36, Regular ", new Font(Font.FontFamily.TimesRoman, 36F, Font.FontStyle.Regular));
document.Add(p2);
Paragraph p3 = new Paragraph("Font: Courier, 36, Regular ", new Font(Font.FontFamily.Courier, 36F, Font.FontStyle.Regular));
document.Add(p3);
Paragraph p4 = new Paragraph("Font: Helvetica, 12, Bold ", new Font(Font.FontFamily.Helvetica, 12F, Font.FontStyle.Bold));
document.Add(p4);
Paragraph p5 = new Paragraph("Font: Helvetica, 12, Italic ", new Font(Font.FontFamily.Helvetica, 12F, Font.FontStyle.Italic));
document.Add(p5);

//  close document and save to the output file
document.Close();




Create a paragraph with different fonts


String outputFilePath = Program.RootPath + "\\" + "Sample11.pdf";

Document document = new Document();
PDFBuildHandler.Create(document, outputFilePath);

//  open document
document.Open();

Paragraph p = new Paragraph();
p.SetLeading(64, 0);

Chunk c1 = new TextChunk("This is the first chunk. ", new Font(Font.FontFamily.Helvetica, 36F, Font.FontStyle.Regular, new Color(255, 0, 0)));
p.AddChunk(c1);
Chunk c2 = new TextChunk("This is the 2nd chunk. ", new Font(Font.FontFamily.Helvetica, 36F, Font.FontStyle.Italic, new Color(0, 255, 0)));
p.AddChunk(c2);
Chunk c3 = new TextChunk("This is the third chunk.", new Font(Font.FontFamily.Helvetica, 18F, Font.FontStyle.BoldItalic | Font.FontStyle.Underline, new Color(255, 0, 128)));
p.AddChunk(c3);

//  add content to the document
document.Add(p);

//  close document and save to the output file
document.Close();




Apply default underline to text


String outputFilePath = Program.RootPath + "\\" + "Sample12.pdf";

Document document = new Document();
PDFBuildHandler.Create(document, outputFilePath);

//  open document
document.Open();

Paragraph p = new Paragraph();

Font textFont = new Font(Font.FontFamily.Helvetica, 16F, Font.FontStyle.Regular);

Chunk c1 = new TextChunk("This is a sample code to apply ", textFont);
p.AddChunk(c1);
TextChunk c2 = new TextChunk("underline", textFont);
c2.SetUnderline();
p.AddChunk(c2);
Chunk c3 = new TextChunk(" to the text.", textFont);
p.AddChunk(c3);

document.Add(p);

//  close document and save to the output file
document.Close();




Apply different underline to text


String outputFilePath = Program.RootPath + "\\" + "Sample13.pdf";

Document document = new Document();
PDFBuildHandler.Create(document, outputFilePath);

//  open document
document.Open();

Font textFont = new Font(Font.FontFamily.Helvetica, 16F, Font.FontStyle.Regular);

TextChunk c1 = new TextChunk("Underline thickness 1 pt; Y position: 0 pt.", textFont);
c1.SetUnderline(1F, 0F);
document.Add(new Paragraph(c1));

TextChunk c2 = new TextChunk("Underline thickness 1.5 pt; Y position: 5 pt.", textFont);
c2.SetUnderline(1.5F, 5F);
document.Add(new Paragraph(c2));

TextChunk c3 = new TextChunk("Underline thickness 0.5 pt; Y position: -2 pt.", textFont);
c3.SetUnderline(0.5F, -2F);
document.Add(new Paragraph(c3));

//  close document and save to the output file
document.Close();