public Example_37() { PDF pdf = new PDF(new BufferedStream( new FileStream("Example_37.pdf", FileMode.Create))); FileStream fis = new FileStream("data/testPDFs/wirth.pdf", FileMode.Open); SortedDictionary <Int32, PDFobj> objects = pdf.Read(fis); fis.Close(); Font f1 = new Font( objects, new FileStream("fonts/OpenSans/OpenSans-Regular.ttf.stream", FileMode.Open, FileAccess.Read), Font.STREAM); f1.SetSize(72f); TextLine line = new TextLine(f1, "This is a test!"); line.SetLocation(50f, 350f); line.SetColor(Color.peru); List <PDFobj> pages = pdf.GetPageObjects(objects); foreach (PDFobj pageObj in pages) { GraphicsState gs = new GraphicsState(); gs.Set_CA(0.75f); // Stroking alpha gs.Set_ca(0.75f); // Nonstroking alpha pageObj.SetGraphicsState(gs, objects); Page page = new Page(pdf, pageObj); page.AddResource(f1, objects); page.SetBrushColor(Color.blue); page.DrawString(f1, "Hello, World!", 50f, 200f); line.DrawOn(page); page.Complete(objects); // The graphics stack is unwinded automatically } pdf.AddObjects(objects); /* * List<Image> images = new List<Image>(); * foreach (PDFobj obj in objects.Values) { * if (obj.GetValue("/Subtype").Equals("/Image")) { * float w = float.Parse(obj.GetValue("/Width")); * float h = float.Parse(obj.GetValue("/Height")); * if (w > 500f && h > 500f) { * images.Add(new Image(pdf, obj)); * } * } * } * * Font f1 = new Font(pdf, CoreFont.HELVETICA); * f1.SetSize(72f); * * Page page = null; * foreach (Image image in images) { * page = new Page(pdf, A4.PORTRAIT); * * GraphicsState gs = new GraphicsState(); * gs.Set_CA(0.7f); // Stroking alpha * gs.Set_ca(0.7f); // Nonstroking alpha * page.SetGraphicsState(gs); * * image.ResizeToFit(page, true); * * // image.FlipUpsideDown(true); * // image.SetLocation(0f, -image.GetHeight()); * * // image.SetRotate(ClockWise._180_degrees); * // image.SetLocation(0f, 0f); * * image.DrawOn(page); * * TextLine text = new TextLine(f1, "Hello, World!"); * text.SetColor(Color.blue); * text.SetLocation(150f, 150f); * text.DrawOn(page); * * page.SetGraphicsState(new GraphicsState()); * } */ pdf.Close(); }
public Example_16() { PDF pdf = new PDF(new BufferedStream( new FileStream("Example_16.pdf", FileMode.Create))); Font f1 = new Font(pdf, CoreFont.HELVETICA); f1.SetSize(14f); Page page = new Page(pdf, Letter.PORTRAIT); Box bg_flag = new Box(); bg_flag.SetLocation(50f, 50f); bg_flag.SetSize(200f, 120f); bg_flag.SetColor(Color.black); bg_flag.SetLineWidth(1.5f); bg_flag.DrawOn(page); float stripe_width = 120f / 3; Line white_stripe = new Line(0.0f, 0.0f, 200.0f, 0.0f); white_stripe.SetWidth(stripe_width); white_stripe.SetColor(Color.white); white_stripe.PlaceIn(bg_flag, 0.0f, stripe_width / 2); white_stripe.DrawOn(page); Line green_stripe = new Line(0.0f, 0.0f, 200.0f, 0.0f); green_stripe.SetWidth(stripe_width); green_stripe.SetColor(0x00966e); green_stripe.PlaceIn(bg_flag, 0.0f, (3 * stripe_width) / 2); green_stripe.DrawOn(page); Line red_stripe = new Line(0.0f, 0.0f, 200.0f, 0.0f); red_stripe.SetWidth(stripe_width); red_stripe.SetColor(0xd62512); red_stripe.PlaceIn(bg_flag, 0.0f, (5 * stripe_width) / 2); red_stripe.DrawOn(page); Dictionary <String, Int32> colors = new Dictionary <String, Int32>(); colors["Lorem"] = Color.blue; colors["ipsum"] = Color.red; colors["dolor"] = Color.green; colors["ullamcorper"] = Color.gray; f1.SetSize(72f); GraphicsState gs = new GraphicsState(); gs.Set_CA(0.5f); // Stroking alpha gs.Set_ca(0.5f); // Nonstroking alpha page.SetGraphicsState(gs); TextLine text = new TextLine(f1, "Hello, World"); text.SetLocation(50f, 300f); text.DrawOn(page); f1.SetSize(14f); StringBuilder buf = new StringBuilder(); buf.Append(" Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla elementum interdum elit, quis vehicula urna interdum quis. Phasellus gravida ligula quam, nec blandit nulla. Sed posuere, lorem eget feugiat placerat, ipsum nulla euismod nisi, in semper mi nibh sed elit. Mauris libero est, sodales dignissim congue sed, pulvinar non ipsum. Sed risus nisi, ultrices nec eleifend at, viverra sed neque. Integer vehicula massa non arcu viverra ullamcorper. Ut id tellus id ante mattis commodo. Donec dignissim aliquam tortor, eu pharetra ipsum ullamcorper in. Vivamus ultrices imperdiet iaculis.\n\n"); buf.Append(" Donec a urna ac ipsum fringilla ultricies non vel diam. Morbi vitae lacus ac elit luctus dignissim. Quisque rutrum egestas facilisis. Curabitur tempus, tortor ac fringilla fringilla, libero elit gravida sem, vel aliquam leo nibh sed libero. Proin pretium, augue quis eleifend hendrerit, leo libero auctor magna, vitae porttitor lorem urna eget urna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut tincidunt venenatis odio in dignissim. Ut cursus egestas eros, ac blandit nisi ullamcorper a."); TextBox textBox = new TextBox(f1, buf.ToString()); textBox.SetLocation(50f, 200f); textBox.SetWidth(400f); textBox.SetHeight(400f); // If no height is specified the height will be calculated based on the text. // textBox.SetHeight(363f); // textBox.SetHeight(362f); // textBox.SetVerticalAlignment(Align.TOP); // textBox.SetVerticalAlignment(Align.BOTTOM); // textBox.SetVerticalAlignment(Align.CENTER); textBox.SetBgColor(Color.whitesmoke); textBox.SetTextColors(colors); // Find x and y without actually drawing the text box. // float[] xy = textBox.DrawOn(page, false); float[] xy = textBox.DrawOn(page); page.SetGraphicsState(new GraphicsState()); // Reset GS Box box = new Box(); box.SetLocation(xy[0], xy[1]); box.SetSize(20f, 20f); box.DrawOn(page); pdf.Close(); }
public Example_31() { PDF pdf = new PDF(new BufferedStream( new FileStream("Example_31.pdf", FileMode.Create))); Page page = new Page(pdf, Letter.PORTRAIT); Font f1 = new Font(pdf, new FileStream("fonts/Noto/NotoSansDevanagari-Regular.ttf", FileMode.Open, FileAccess.Read)); f1.SetSize(15f); Font f2 = new Font(pdf, new FileStream("fonts/Droid/DroidSans.ttf", FileMode.Open, FileAccess.Read)); f2.SetSize(15f); StringBuilder buf = new StringBuilder(); StreamReader reader = new StreamReader( new FileStream("data/marathi.txt", FileMode.Open, FileAccess.Read)); String line = null; while ((line = reader.ReadLine()) != null) { buf.Append(line + "\n"); } reader.Close(); TextBox textBox = new TextBox(f1, buf.ToString(), 500f, 300f); textBox.SetFallbackFont(f2); textBox.SetLocation(50f, 50f); textBox.SetNoBorders(); textBox.DrawOn(page); String str = "असम के बाद UP में भी CM कैंडिडेट का ऐलान करेगी BJP?"; TextLine textLine = new TextLine(f1, str); textLine.SetFallbackFont(f2); textLine.SetLocation(50f, 175f); textLine.DrawOn(page); page.SetPenColor(Color.blue); page.SetBrushColor(Color.blue); page.FillRect(50f, 200f, 200f, 200f); GraphicsState gs = new GraphicsState(); gs.Set_CA(0.5f); // The stroking alpha constant gs.Set_ca(0.5f); // The nonstroking alpha constant page.SetGraphicsState(gs); page.SetPenColor(Color.green); page.SetBrushColor(Color.green); page.FillRect(100f, 250f, 200f, 200f); page.SetPenColor(Color.red); page.SetBrushColor(Color.red); page.FillRect(150, 300, 200f, 200f); // Reset the parameters to the default values page.SetGraphicsState(new GraphicsState()); page.SetPenColor(Color.orange); page.SetBrushColor(Color.orange); page.FillRect(200, 350, 200f, 200f); page.SetBrushColor(0x00003865); page.FillRect(50, 550, 200f, 200f); pdf.Close(); }