using System; using System.IO; using System.Collections;using System.Collections.Generic; using PDFjet.NET; /** * Example_09.cs * */ public class Example_09 { private Exception e = null; public Example_09() { PDF pdf = new PDF(); Font f1 = new Font(pdf, "Helvetica-Bold"); f1.SetSize(10); Font f2 = new Font(pdf, "Helvetica-Bold"); f2.SetSize(8); Font f3 = new Font(pdf, "Helvetica-Bold"); f3.SetSize(7); Font f4 = new Font(pdf, "Helvetica"); f4.SetSize(7); Page page = new Page(pdf, Letter.PORTRAIT); Chart chart = new Chart(f1, f2); chart.SetTitle("World View - Communications"); chart.SetXAxisTitle("Cell phones per capita"); chart.SetYAxisTitle("Internet users % of the population"); chart.SetData(GetData("data/world-communications.txt", "|")); addTrendLine(chart); chart.SetPosition(70, 50); chart.SetSize(500, 300); chart.DrawOn(page); addTableToChart(page, chart, f3, f4); pdf.Wrap(); pdf.Save("Example_09.pdf"); } public void addTrendLine(Chart chart) { List points = chart.GetData()[0]; double m = chart.Slope(points); double b = chart.Intercept(points, m); List trendline = new List(); double x = 0.0; double y = m * x + b; Point p1 = new Point(x, y); p1.SetDrawLineTo(true); p1.SetColor(RGB.BLUE); p1.SetShape(Point.INVISIBLE); x = 1.5; y = m * x + b; Point p2 = new Point(x, y); p2.SetDrawLineTo(true); p2.SetColor(RGB.BLUE); p2.SetShape(Point.INVISIBLE); trendline.Add(p1); trendline.Add(p2); chart.GetData().Add(trendline); } public void addTableToChart( Page page, Chart chart, Font f3, Font f4) { Table table = new Table(f3, f4); List> tableData = new List>(); List points = chart.GetData()[0]; for (int i = 0; i < points.Count; i++) { Point point = points[i]; if (point.GetShape() != Point.CIRCLE) { List tableRow = new List(); Cell cell = new Cell(f4); cell.SetPoint(point); tableRow.Add(cell); cell = new Cell(f4); cell.SetText(point.GetText()); tableRow.Add(cell); cell = new Cell(f4); cell.SetText(point.GetURIAction()); tableRow.Add(cell); tableData.Add(tableRow); } } table.SetData(tableData); table.AutoAdjustColumnWidths(); table.SetLineWidth(0.2); table.SetPosition(70.0, 360.0); table.DrawOn(page); } public List> GetData( String fileName, String delimiter) { List> chartData = new List>(); StreamReader reader = new StreamReader(fileName); List points = new List(); String line = null; while ((line = reader.ReadLine()) != null) { 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"); } Point point = new Point(); try { double population = Double.Parse(cols[1].Replace(",", "")); point.SetText(cols[0].Trim()); String country_name = point.GetText(); country_name = country_name.Replace(" ", "_"); country_name = country_name.Replace("'", "_"); country_name = country_name.Replace(",", "_"); country_name = country_name.Replace("(", "_"); country_name = country_name.Replace(")", "_"); point.SetURIAction( "http://pdfjet.com/country/" + country_name + ".txt"); point.SetX(Double.Parse( cols[5].Replace(",", "")) / population); point.SetY(Double.Parse( cols[7].Replace(",", "")) / population * 100); point.SetRadius(2.0); if (point.GetX() > 1.25) { point.SetShape(Point.RIGHT_ARROW); } if (point.GetY() > 80) { point.SetShape(Point.UP_ARROW); } if (point.GetText().Equals("France")) { point.SetShape(Point.MULTIPLY); } if (point.GetText().Equals("Canada")) { point.SetShape(Point.BOX); } if (point.GetText().Equals("United States")) { point.SetShape(Point.STAR); } points.Add(point); } catch (Exception e) { // Don't print or log the exceptions and // prevent csc from generating warnings this.e = e; } } reader.Close(); chartData.Add(points); return chartData; } public static void Main(String[] args) { try { new Example_09(); } catch (Exception e) { Console.WriteLine(e.StackTrace); } } } // End of Example_09.cs