/* * Example_14.cs * * Copyright (c) 2026 PDFjet Software * Licensed under the MIT License. See LICENSE file in the project root. */ using System; using System.IO; using System.Collections.Generic; using System.Diagnostics; using PDFjet.NET; /** * Example_14.java */ public class Example_14 { public Example_14() { PDF pdf = new PDF(new BufferedStream( new FileStream("Example_14.pdf", FileMode.Create))); pdf.SetCompliance(Compliance.PDF_UA_1); pdf.SetTitle("Paragraphs in a Text Frame"); Font f1 = new Font(pdf, IBMPlexSans.Regular); f1.SetSize(10f); Font f2 = new Font(pdf, IBMPlexSans.Bold); f2.SetSize(10f); Font f3 = new Font(pdf, IBMPlexSans.Italic); f3.SetSize(10f); Page page = new Page(pdf, Letter.PORTRAIT); List paragraphs = new List(); Paragraph paragraph = new Paragraph() .Add(new TextLine(f1, "The small business centres offer practical resources, from step-by-step info on setting up your business to sample business plans to a range of business-related articles and books in our resource libraries.") .SetUnderline(true)) .Add(new TextLine(f2, "This text is bold!").SetTextColor(Color.blue)); paragraphs.Add(paragraph); paragraph = new Paragraph() .Add(new TextLine(f1, "The centres also offer free one-on-one consultations with business advisors who can review your business plan and make recommendations to improve it.") .SetUnderline(true)) .Add(new TextLine(f3, "This text is using italic font.").SetTextColor(Color.green)); paragraphs.Add(paragraph); // The two paragraphs are a numbered list. The number of a paragraph // is its label, which the text frame draws to the left of it and // reads before it. int paragraphNumber = 1; foreach (Paragraph p in paragraphs) { p.SetListLabel(new TextLine(f2, paragraphNumber.ToString() + "."), 15f); paragraphNumber++; } TextFrame text = new TextFrame(paragraphs); text.SetLocation(70f, 50f); text.SetWidth(500f); text.SetBorders(true); text.SetBorderColor(Color.blue); text.DrawOn(page); Dictionary colorMap = new Dictionary(); colorMap["Physics"] = Color.firebrick; colorMap["physics"] = Color.firebrick; colorMap["Experimentation"] = Color.sienna; colorMap["science"] = Color.blue; paragraphs = Paragraph.ParagraphsFromFile(f1, "data/physics.txt"); foreach (Paragraph p in paragraphs) { if (p.StartsWith("**")) { p.SetStructureType(StructElem.H1); p.GetTextLines()[0].SetFont(f2).SetFontSize(24f); p.GetTextLines()[0].SetTextColor(Color.navy); paragraphNumber = 1; } else { p.SetTextColor(Color.dimgray); p.SetHighlightColors(colorMap); p.SetListLabel(new TextLine(f2, paragraphNumber.ToString() + "."), 15f); paragraphNumber++; } } text = new TextFrame(paragraphs); text.SetLocation(70f, 150f); text.SetWidth(500f); text.SetBorders(true); text.SetBorderColor(Color.blue); text.DrawOn(page); // The rule beside each paragraph reaches from its top to its bottom, // which the frame knows only after it has drawn it. foreach (Paragraph p in paragraphs) { if (!p.StartsWith("**")) { new Line(p.GetX1() - 3f, p.GetY1(), p.GetX1() - 3f, p.GetY2()) .SetStrokeColor(Color.navy) .SetStrokeWidth(1f).DrawOn(page); } } pdf.Complete(); } public static void Main(String[] args) { Stopwatch sw = Stopwatch.StartNew(); long time0 = sw.ElapsedMilliseconds; new Example_14(); long time1 = sw.ElapsedMilliseconds; sw.Stop(); Console.WriteLine($"Example_14 => {time1 - time0,4} ms"); } } // End of Example_14.cs