Example 2: Formatting a paragraph with different font stylesc# using iTextSharp.text; using iTextSharp.text.pdf; // Create a new PDF document Document doc = new Document(); PdfWriter.GetInstance(doc, new FileStream("example.pdf", FileMode.Create)); // Open the document doc.Open(); // Create a new font for the paragraph Font font = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD); // Create a new paragraph with the specified text and font Paragraph paragraph = new Paragraph("This is a formatted paragraph.", font); // Add different font style to different section of the paragraph paragraph.Add(new Chunk(" Bold Text", new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD))); paragraph.Add(new Chunk(" Italic Text", new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.ITALIC))); // Add the paragraph to the document doc.Add(paragraph); // Close the document doc.Close(); ``` In both examples, the iTextSharp.text package is used to create a new PDF document and format the text using fonts and different font styles. The Paragraph Add method is used to add the text to the document.
public Add ( IElement o ) : bool | ||
o | IElement | the object to add |
return | bool |