C# tutorial: add chunk and phrase to PDD document


Add Chunk and Phrase to PDF document

A Chunk is the smallest piece of text that can be added to a PDF document. You can apply font name, size, color, and style to the text wrapped in the Chunk object by using a Font object. The Font object is instantiated from the Font class. Font name, size, style, and color have to be passed to the constructor of the Font class when an Font object is created. If you want to make the text underlined, rised, and to change background of the text, you need to use the setUnderline, setTextRise, and setBacground method respectively. You should note that when Chunk objects are added to the document, a new line is not recognized. Thus, the text might look messy. This problem can be solved by placing all Chunk objects in a Phrase object. Then add the Phrase object to the document. A Phrase is simply a collection of Chunk objects. Generally, a Chunk object is not directly added to the document.

In the examplde code below, the information of instutitions that contain identification numbers (id) and names are read from the Institution table of testdb database in SQL Server by using LINQ. The information is added to the PDF document by using Chunk and Phrase objects. Below is the Institution table with some sample data.

table

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Text;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
using System.Drawing.Imaging;
namespace createpdf
{

class Program
{

static void Main(string[] args)
{
//create DataContext object to get data from SQL Server database
DataContext db = new DataContext("Server=ASPIREV5\\SQLEXPRESS; Initial Catalog=testdb; Integrated Security=SSPI");

  //create a document object
  var doc = new Document(PageSize.A4);
  //get the current directory
  string path = Environment.CurrentDirectory;
  //create PdfWriter object
  PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(path + "/pdfdoc.pdf", FileMode.Create));

  //open the document for writing
  doc.Open();

  Chunk n = new Chunk("N", new Font(Font.FontFamily.COURIER, 12, Font.BOLD, BaseColor.WHITE));
  n.SetBackground(BaseColor.LIGHT_GRAY);
  n.SetUnderline(1, 1);
  Chunk o = new Chunk("o", new Font(Font.FontFamily.COURIER, 9, Font.BOLD, BaseColor.BLACK));
  o.SetTextRise(6);
  Chunk name = new Chunk("Name", new Font(Font.FontFamily.COURIER,12,Font.BOLD,BaseColor.WHITE));
  name.SetBackground(BaseColor.LIGHT_GRAY);
  name.SetUnderline(1, 1);
  Phrase header = new Phrase();
  header.Add(n);
  header.Add(o);
  header.Add(new Chunk(" "));
  header.Add(name);
  header.Add(Chunk.NEWLINE);
  doc.Add(header);

  var x = from st in db.GetTable<Institution>()
  select st;
  foreach (var s in x)
  {
    Chunk chid = new Chunk(s.id,new Font(Font.FontFamily.COURIER,11,Font.ITALIC,new BaseColor(0,100,200)));
    Chunk chname = new Chunk(s.name.Trim(), new Font(Font.FontFamily.COURIER,11,Font.NORMAL,BaseColor.BLACK));
    Phrase ph = new Phrase();
    ph.Add(chid);
    header.Add(new Chunk(" "));
    ph.Add(chname);

    ph.Add(Chunk.NEWLINE);
    doc.Add(ph);


  }
doc.Close();
//view the result pdf file
System.Diagnostics.Process.Start(path + "/pdfdoc.pdf");

    }

  }
}
[Table(Name = "Institution")]
class Institution
{
[Column(IsPrimaryKey = true)]
public string id { get; set; }
[Column(Name = "name")]
public string name { get; set; }

}
}

output

 


Comments

Lio comment

 Lio

That is very nice tutorial. It is simple to understand.


2015-01-14



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.