// ---------------------------------- // Using Declarations // ---------------------------------- using System.Collections.Generic; using System.Drawing; using System.IO; using System; // ---------------------------------- // DemoFunction // ---------------------------------- public delegate void DemoFunction(PDF p); // ---------------------------------- // Complex // ---------------------------------- public class Complex { public Complex(double x, double y) { X = x; Y = y; } public double X { get; private set; } public double Y { get; private set; } public Complex Multiply(Complex other) { return(new Complex(X * other.X - Y * other.Y, other.X * Y + other.Y * X)); } public Complex Add(Complex other) { return(new Complex(X + other.X, Y + other.Y)); } } // ---------------------------------- // Test // ---------------------------------- public class Test { public static void DrawBoundedText( string theText, int x, int y, PDF.Font theFont, int fontSize, PDF p ) { p.SetFont(theFont, fontSize); p.ShowTextXY(theText, x, y); int width = PDF.StringWidth(theFont, fontSize, theText); int offset = (int)Math.Round(0.4 * fontSize); p.DrawRect(x, y - offset, width, fontSize + offset); } static double DegreesToRadians(int degrees) { return(degrees * Math.PI / 180.0); } public static void DemoOne(PDF pdf) { pdf.SetFont(PDF.Font.HELVETICA, 12); string s = " \t\t fee \r\nxxxxxxxxxxxxx\r\n\"fi\"fo fum"; s += " a aa aaa aaaa bbb bb b c cc ccc dddd eeeee "; s += " foo bar baz foo bar baz "; s += " mairzy doats and doazy doats and little lambsey divey"; s += " a kiddley divey too, wouldn't you? "; s += " a b c d e f g h i jj kkk llll mmmmmm nnnnnnn oooooooooooo "; s += "----------------------- -------------------------- "; s += "$$$ $$$$$$$$$$$$$$ $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ "; s += "******************** ************************ ***** "; List sv = pdf.WrapText(s, 100, true); for(int i = 0; i < sv.Count; i ++) pdf.ShowTextXY(sv[i], 80, 745 - 20 * i); pdf.DrawLine(80, 760, 80, 300); pdf.DrawLine(180, 760, 180, 300); pdf.DrawEllipse(400, 550, 150, 75); pdf.SetFillColor(255, 255, 0); pdf.FillEllipse(400, 550, 40, 65); pdf.DrawEllipse(400, 550, 40, 65); for(int i = 0; i < 12; i ++) { byte value = (byte)(20 * i); pdf.SetFillColor(value, value, value); pdf.FillCircle(480, 310 + 10 * i, 2 * i); pdf.DrawCircle(480, 310 + 10 * i, 3 * i); } pdf.SetLineColor(255, 0, 0); pdf.SetFillColor(0, 0, 255); int iValue = 0; foreach(PDF.Font theFont in Enum.GetValues(typeof(PDF.Font))) { if(theFont != PDF.Font.NONE) { int fontSize = 9; int x = 10; int y = 36 + (2 * fontSize) * iValue++; string ss = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" + "!@#$%^&*()_-=+[]{};:<>,./?"; DrawBoundedText(ss, x, y, theFont, fontSize, pdf); } } pdf.FillRect(300, 700, 40, 40); pdf.DrawRect(400, 700, 40, 40); pdf.NewPage(); pdf.SetLineColor(255, 0, 0); iValue = 0; foreach(PDF.Font theFont in Enum.GetValues(typeof(PDF.Font))) { if(theFont != PDF.Font.NONE) { pdf.SetFont(theFont, 20); pdf.ShowTextXY(PDF.mFonts[iValue], 100, 100 + 40 * iValue); iValue++; } } pdf.NewPage(); pdf.SetLineColor(255, 0, 0); for(int i = 0; i < 20; i ++) { pdf.DrawLine(0, 0, 30 * (1 + i), 500 - 15 * i); pdf.SetLineWidth(i/2 + 1); } var points = new List(); points.Add(new Point(300, 500)); points.Add(new Point(325, 550)); points.Add(new Point(350, 480)); points.Add(new Point(375, 570)); points.Add(new Point(400, 460)); points.Add(new Point(425, 590)); points.Add(new Point(450, 450)); pdf.SetLineColor(0, 255, 0); pdf.DrawLine(points); points.Clear(); points.Add(new Point(100, 540)); points.Add(new Point(150, 700)); points.Add(new Point( 80, 700)); pdf.SetFillColor(200, 200, 200); pdf.FillPolygon(points); pdf.SetLineColor(0, 0, 255); pdf.DrawPolygon(points); pdf.NewPage(); // Make an image var row1 = new List(); var row2 = new List(); row1.Add(new RGB(255, 255, 0)); row1.Add(new RGB(255, 0, 255)); row1.Add(new RGB( 0, 255, 0)); row2.Add(new RGB( 0, 0, 255)); row2.Add(new RGB(255, 0, 0)); row2.Add(new RGB( 0, 0, 255)); var anImage = new List>(); anImage.Add(row1); anImage.Add(row2); ImageInfo info = pdf.ProcessImage(anImage); double scale = 50.0; for(int i = 0; i < 10; i ++) { int xValue = 120 + 30 * i; int yValue = 220 + 40 * i; pdf.ShowImage(info, xValue, yValue, scale); pdf.DrawRect( xValue, yValue, (int)Math.Round(scale * info.Width), (int)Math.Round(scale * info.Height) ); } pdf.NewPage(); pdf.SetLineWidth(20); pdf.SetFillColor(255, 0, 0); pdf.DrawLine(100, 100, 200, 200); pdf.FillCircle(100, 100, 2); pdf.FillCircle(200, 200, 2); pdf.SetLineCap(PDF.LineCap.BUTT_CAP); pdf.DrawLine(150, 100, 250, 200); pdf.FillCircle(150, 100, 2); pdf.FillCircle(250, 200, 2); pdf.SetLineCap(PDF.LineCap.ROUND_CAP); pdf.DrawLine(200, 100, 300, 200); pdf.FillCircle(200, 100, 2); pdf.FillCircle(300, 200, 2); pdf.SetLineCap(PDF.LineCap.PROJECTING_SQUARE_CAP); pdf.DrawLine(250, 100, 350, 200); pdf.FillCircle(250, 100, 2); pdf.FillCircle(350, 200, 2); // Back to the default pdf.SetLineCap(PDF.LineCap.BUTT_CAP); pdf.DrawLine(100, 400, 200, 500, 200, 700); pdf.SetLineJoin(PDF.LineJoin.MITER_JOIN); pdf.DrawLine(150, 400, 250, 500, 250, 700); pdf.SetLineJoin(PDF.LineJoin.ROUND_JOIN); pdf.DrawLine(200, 400, 300, 500, 300, 700); pdf.SetLineJoin(PDF.LineJoin.BEVEL_JOIN); pdf.DrawLine(250, 400, 350, 500, 350, 700); var thePoints = new List() { new Point(450, 500), new Point(500, 450), new Point(450, 400), new Point(500, 350), new Point(450, 300), new Point(500, 250), new Point(450, 200), new Point(500, 150) }; pdf.SetLineCap(PDF.LineCap.ROUND_CAP); pdf.SetLineJoin(PDF.LineJoin.ROUND_JOIN); pdf.DrawLine(thePoints); pdf.NewPage(); pdf.SetLineWidth(20); pdf.SetLineJoin(PDF.LineJoin.MITER_JOIN); pdf.DrawLine(100, 100, 100, 200, 200, 200); pdf.DrawLine(300, 100, 300, 200, 400, 210); pdf.DrawLine(500, 100, 500, 200, 600, 190); // convert miters to bevels for angles // less than about 90 degrees pdf.SetMiterLimit(1.45); pdf.DrawLine(100, 200 + 100, 100, 200 + 200, 200, 200 + 200); pdf.DrawLine(300, 200 + 100, 300, 200 + 200, 400, 200 + 210); pdf.DrawLine(500, 200 + 100, 500, 200 + 200, 600, 200 + 190); pdf.NewPage(); pdf.SetLineWidth(3); pdf.SetDash(new int[] { 3 }); pdf.DrawLine(100, 100, 200, 200); pdf.SetDash(new int[] { 4 }, 2); pdf.DrawLine(150, 100, 250, 200); pdf.SetDash(new int[] { 3, 10 }, 6); pdf.DrawLine(200, 100, 300, 200); pdf.NewPage(); pdf.SetFont(PDF.Font.HELVETICA, 12); pdf.ShowTextXY("hello world", 100, 100); pdf.SetTextCharSpace(10); pdf.ShowTextXY("hello world", 100, 200); pdf.SetTextCharSpace(20); pdf.ShowTextXY("hello world", 100, 300); pdf.SetTextCharSpace(0); pdf.ShowTextXY("hello world", 100, 400); pdf.SetTextWordSpace(10); pdf.ShowTextXY("hello world", 100, 500); pdf.SetTextWordSpace(20); pdf.ShowTextXY("hello world", 100, 600); pdf.SetTextWordSpace(0); pdf.ShowTextXY("hello world", 400, 100); pdf.SetTextHorizontalScaling(200); pdf.ShowTextXY("hello world", 400, 200); pdf.SetTextHorizontalScaling(300); pdf.ShowTextXY("hello world", 400, 300); } public static void DemoTwo(PDF pdf) { int xc = pdf.GetWidth() / 2; int yc = pdf.GetHeight() / 2; int smaller = (xc < yc ? xc : yc); int radius = (int)(.9 * smaller); int step = 15; for(int i = 0; i < 360; i += step) { double angle = DegreesToRadians(i); int x0 = xc + (int)Math.Round(radius * Math.Cos(angle)); int y0 = yc + (int)Math.Round(radius * Math.Sin(angle)); for(int j = 0; j < 360; j += step) { if(j != i) { double theAngle = DegreesToRadians(j); int x1 = xc + (int)Math.Round(radius * Math.Cos(theAngle)); int y1 = yc + (int)Math.Round(radius * Math.Sin(theAngle)); pdf.DrawLine(x0, y0, x1, y1); } } } } // begin: DemoThree static void DemoThree(PDF p) { // Create an image, 500 pixels square int width = 500; int height = 500; var anImage = new List>(); for(int i = 0; i < height; i ++) { var row = new List(); for(int j = 0; j < width; j ++) row.Add(new RGB(0, 0, 0)); anImage.Add(row); } double yStart = -2.0; double yStop = 2.0; double yStep = (yStop - yStart) / (height - 1); double xStart = -2.0; double xStop = 2.0; double xStep = (xStop - xStart) / (width - 1); int maxIterations = 25; double maxDistance = 1000.0; int iValue = 0; for(double y = yStart; y <= yStop; y += yStep) { int jValue = 0; for(double x = xStart; x <= xStop; x += xStep) { Complex z = new Complex(0.0, 0.0); Complex c = new Complex(x, y); int iterations = 0; while( iterations < maxIterations && Math.Sqrt(z.X * z.X + z.Y * z.Y) < maxDistance ) { z = z.Multiply(z).Add(c); iterations ++; } double v1 = (double)iterations / maxIterations; double v2 = Math.Sqrt(v1); double v3 = Math.Sqrt(v2); v1 *= 255.0; v2 *= 255.0; v3 *= 255.0; byte red = (byte)Math.Round(v1); byte green = (byte)Math.Round(v2); byte blue = (byte)Math.Round(v3); var theColor = new RGB(red, green, blue); anImage[iValue][jValue] = theColor; //std::cout << iValue << " " << jValue << "\n"; jValue++; } iValue++; } // Place the image, centered ImageInfo info = p.ProcessImage(anImage); int xValue = (p.GetWidth() - width) / 2; int yValue = (p.GetHeight() - height) / 2; p.ShowImage(info, xValue, yValue, 1.0); p.NewPage(); string[] lines = File.ReadAllLines(GetCurrentFileName()); const int FONTSIZE = 8; const int MARGIN = 36; const int YSTART = 750; int yCurrent = YSTART; bool showLine = false; // Avoid false positive by building our // markerBegin and markerEnd strings up dynamically string tag = "DemoThree"; string markerBegin = "// begin: " + tag; string markerEnd = "// end: " + tag; bool needSetFont = true; for(int i = 0; i < lines.Length; i ++) { if(!showLine) { if(lines[i].Contains(markerBegin)) showLine = true; } else { if(lines[i].Contains(markerEnd)) showLine = false; if(showLine) { if(needSetFont) { p.SetFont(PDF.Font.COURIER, FONTSIZE); needSetFont = false; } p.ShowTextXY(lines[i], MARGIN, yCurrent); yCurrent -= FONTSIZE; if(yCurrent <= MARGIN) { p.NewPage(); needSetFont = true; yCurrent = YSTART; } } } } } // end: DemoThree static void DemoFour(PDF p) { const int MIN_SIZE = 200; string[] list = { "files\\spock.jpg", "files\\ant-wearing-sneakers.png", "files\\fractal.tif", "files\\dogs-playing.bmp" }; bool first = true; foreach(string fileName in list) { if(first) first = false; else p.NewPage(); ImageInfo info = p.ProcessImage(fileName); // Start at full size (1.0 scale), reduce to 80% of // that size as many times as are needed to fit // inside our pdf width and height double scale = 1.0; double w = info.Width; double h = info.Height; while(w > p.GetWidth() || h > p.GetHeight()) { scale *= .8; w *= .8; h *= .8; } // If our image is small, display it multiple times to // exercise our image checksum feature if(w < MIN_SIZE && h < MIN_SIZE) { for(int i = 0; i < 3; i ++) p.ShowImage(info, MIN_SIZE * i + 36, MIN_SIZE * i + 36, scale, scale); } else { int x = (int)(Math.Round(p.GetWidth() - w) / 2.0); int y = (int)(Math.Round(p.GetHeight() - h) / 2.0); p.ShowImage(info, x, y, scale, scale); } } } private static string GetCurrentFileName([System.Runtime.CompilerServices.CallerFilePath] string file = "") { return(file); } public static void Main() { DemoFunction[] functions = { DemoOne, DemoTwo, DemoThree, DemoFour }; for(int i = 0; i < functions.Length; i ++) { string fileName = "example-" + i + ".pdf"; Console.WriteLine("------------------------------------"); Console.WriteLine("Creating File: [" + fileName + "]"); Console.WriteLine("------------------------------------\n"); var pdf = new PDF(); functions[i](pdf); pdf.WriteToFile(fileName); } } }