Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# iTextSharp multi fonts in a single cell

Tags:

c#

itextsharp

First off I'm not that great with C# and it's been a while since I've worked with it..

I'm making a windows form for a friend that delivers packages. So I want to transfer his current paper form, into a .pdf with the library iTextSharp. He still needs to print the form to get the customer signature and so on.

What I need: I want the table to have a little headline, "Company name" for example, the text should be a little smaller than the text input from the windows form(richTextBox1)

Currently I'm using cells and was wondering if I can use 2 different font sizes within the same cell?

What I have:

table.AddCell("Static headline" + Chunk.NEWLINE + richTextBox1.Text);

What I "want":

var normalFont = FontFactory.GetFont(FontFactory.HELVETICA, 9);

var boldFont = FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 12);

table.AddCell("Static headline", boldFont + Chunk.NEWLINE + richTextBox1.Text, normalFont);
like image 946
Frederik Kiel Avatar asked Feb 13 '14 09:02

Frederik Kiel


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


1 Answers

You're passing a String and a Font to the AddCell() method. That's not going to work. You need the AddCell() method that takes a Phrase object or a PdfPCell object as parameter.

A Phrase is an object that consists of different Chunks, and the different Chunks can have different font sizes. Please read chapter 2 of my book for more info about this object.

Phrase phrase = new Phrase();
phrase.Add(
    new Chunk("Some BOLD text",  new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD))
);
phrase.Add(new Chunk(", some normal text", new Font()));
table.AddCell(phrase);

A PdfPCell is an object to which you can add different objects, such as Phrases, Paragraphs, Images,...

PdfPCell cell = new PdfPCell();
cell.AddElement(new Paragraph("Hello"));
cell.AddElement(list);
cell.AddElement(image);

In this snippet list is of type List and image is of type Image.

The first snippet uses text mode; the second snippet uses composite mode. Cells behave very differently depending on the mode you use.

This is all explained in the documentation; you can find hundreds of C# examples here.

like image 167
Bruno Lowagie Avatar answered Sep 26 '22 01:09

Bruno Lowagie