/* * Example_23.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_23.cs */ public class Example_23 { public Example_23() { PDF pdf = new PDF(new BufferedStream( new FileStream("Example_23.pdf", FileMode.Create))); pdf.SetCompliance(Compliance.PDF_A_1A); pdf.SetTitle("World Communications"); Font f1 = new Font(pdf, IBMPlexSans.Bold); Font f2 = new Font(pdf, IBMPlexSans.Regular); Font f3 = new Font(pdf, IBMPlexSans.BoldItalic); f1.SetSize(7f); f2.SetSize(7f); f3.SetSize(7f); Table table = new Table(); List> tableData = GetData( "data/world-communications.txt", "|", 2, f1, f2); Point p1 = new Point(); p1.SetShape(Shape.CIRCLE); p1.SetRadius(2f); p1.SetStrokeColor(Color.darkolivegreen); p1.SetURIAction("https://en.wikipedia.org/wiki/India"); tableData[4][3].SetMarker(p1, Alignment.RIGHT); p1 = new Point(); p1.SetShape(Shape.DIAMOND); p1.SetRadius(2.5f); p1.SetStrokeColor(Color.blue); p1.SetURIAction("https://en.wikipedia.org/wiki/European_Union"); tableData[5][3].SetMarker(p1, Alignment.RIGHT); p1 = new Point(); p1.SetShape(Shape.STAR); p1.SetRadius(3f); p1.SetStrokeColor(Color.red); p1.SetURIAction("https://en.wikipedia.org/wiki/United_States"); tableData[6][3].SetMarker(p1, Alignment.RIGHT); table.SetTableData(tableData, 2); table.SetBottomMargin(15f); // The table is about 555 points wide: at x = 30 it fits on the page. table.SetLocation(30f, 30f); table.SetTextColorInRow(6, Color.blue); table.SetTextColorInRow(39, Color.firebrick); table.SetFontInRow(26, f3); table.RemoveLineBetweenRows(0, 1); table.AutoAdjustColumnWidths(); table.SetColumnWidth(0, 50f); table.SetColumnWidth(3, 80f); table.RightAlignNumbers(); List pages = new List(); table.DrawOn(pdf, pages, Letter.PORTRAIT); for (int i = 0; i < pages.Count; i++) { Page page = pages[i]; page.AddFooter(new TextLine(f1, "Page " + (i + 1) + " of " + pages.Count)); pdf.AddPage(page); } pdf.Complete(); } public List> GetData( String fileName, String delimiter, int numOfHeaderRows, Font f1, Font f2) { List> tableData = new List>(); int currentRow = 0; StreamReader reader = new StreamReader(fileName); String line = null; while ((line = reader.ReadLine()) != null) { List row = new List(); String[] cols = null; if (delimiter.Equals("|")) { cols = line.Split(new Char[] {'|'}); } else if (delimiter.Equals("\t")) { cols = line.Split(new Char[] {'\t'}); } else { throw new Exception( "Only pipes and tabs can be used as delimiters"); } for (int i = 0; i < cols.Length; i++) { String text = cols[i].Trim(); Cell cell = null; if (currentRow < numOfHeaderRows) { cell = new Cell(f1, text); } else { cell = new Cell(f2, text); } cell.SetTopPadding(2f); cell.SetBottomPadding(2f); cell.SetLeftPadding(2f); if (i == 3) { cell.SetRightPadding(10f); } else { cell.SetRightPadding(2f); } row.Add(cell); } tableData.Add(row); currentRow++; } reader.Close(); AppendMissingCells(tableData, f2); return tableData; } private void AppendMissingCells(List> tableData, Font f2) { List firstRow = tableData[0]; int numOfColumns = firstRow.Count; for (int i = 0; i < tableData.Count; i++) { List dataRow = tableData[i]; int dataRowColumns = dataRow.Count; if (dataRowColumns < numOfColumns) { for (int j = 0; j < (numOfColumns - dataRowColumns); j++) { dataRow.Add(new Cell(f2)); } dataRow[dataRowColumns - 1].SetColSpan( (numOfColumns - dataRowColumns) + 1); } } } public static void Main(String[] args) { Stopwatch sw = Stopwatch.StartNew(); long time0 = sw.ElapsedMilliseconds; new Example_23(); long time1 = sw.ElapsedMilliseconds; sw.Stop(); Console.WriteLine($"Example_23 => {time1 - time0,4} ms"); } } // End of Example_23.cs