/// <summary> /// Draws content on a page using colors created from a calibrated RGB colorspace. /// </summary> /// <param name="page">The page to draw to.</param> private static void DrawCalRgbColorSpace(PDFPage page) { PDFRgbColor black = new PDFRgbColor(0, 0, 0); PDFBrush blackBrush = new PDFBrush(black); //PDF4NET v5: PDFFont font = new PDFFont(PDFFontFace.Helvetica, 16); PDFStandardFont font = new PDFStandardFont(PDFStandardFontFace.Helvetica, 16); //PDF4NET v5: page.Canvas.DrawText("CalRGB colorspace", font, null, blackBrush, 50, 50); page.Canvas.DrawString("CalRGB colorspace", font, blackBrush, 50, 50); PDFCalRgbColorSpace calRgbCS = new PDFCalRgbColorSpace(); PDFCalRgbColor red = new PDFCalRgbColor(calRgbCS); red.Red = 1; red.Green = 0; red.Blue = 0; PDFCalRgbColor blue = new PDFCalRgbColor(calRgbCS); blue.Red = 0; blue.Green = 0; blue.Blue = 1; PDFBrush blueBrush = new PDFBrush(blue); PDFPen redPen = new PDFPen(red, 5); page.Canvas.DrawRoundRectangle(redPen, blueBrush, 50, 100, 400, 150, 20, 20); }
/// <summary> /// Draws content on a page using colors created from a calibrated ICC colorspace. /// </summary> /// <param name="page">The page to draw to.</param> private static void DrawICCColorSpace(PDFPage page) { PDFRgbColor black = new PDFRgbColor(0, 0, 0); PDFBrush blackBrush = new PDFBrush(black); //PDF4NET v5: PDFFont font = new PDFFont(PDFFontFace.Helvetica, 16); PDFStandardFont font = new PDFStandardFont(PDFStandardFontFace.Helvetica, 16); //PDF4NET v5: page.Canvas.DrawText("ICC colorspace", font, null, blackBrush, 50, 50); page.Canvas.DrawString("ICC colorspace", font, blackBrush, 50, 50); // Read the ICC profile from disk. FileStream fs = new FileStream("..\\..\\..\\..\\..\\SupportFiles\\rgb.icc", FileMode.Open, FileAccess.Read); byte[] profileData = new byte[fs.Length]; fs.Read(profileData, 0, profileData.Length); fs.Close(); //PDF4NET v5: PDFICCColorSpace iccCS = new PDFICCColorSpace(); PDFIccColorSpace iccCS = new PDFIccColorSpace(); iccCS.IccProfile = profileData; //PDF4NET v5: PDFICCColor red = new PDFICCColor(iccCS); PDFIccColor red = new PDFIccColor(iccCS); red.ColorComponents = new double[] { 192, 0, 0 }; //PDF4NET v5: PDFICCColor blue = new PDFICCColor(iccCS); PDFIccColor blue = new PDFIccColor(iccCS); blue.ColorComponents = new double[] { 0, 0, 192 }; PDFBrush blueBrush = new PDFBrush(blue); PDFPen redPen = new PDFPen(red, 5); page.Canvas.DrawRoundRectangle(redPen, blueBrush, 50, 100, 400, 150, 20, 20); }
/// <summary> /// Extracts text fragments from the 2nd page and highlights the glyphs in the fragment. /// </summary> /// <param name="document"></param> private static void ExtractTextAndHighlightGlyphs(PDFFixedDocument document) { PDFRgbColor penColor = new PDFRgbColor(); PDFPen pen = new PDFPen(penColor, 0.5); Random rnd = new Random(); byte[] rgb = new byte[3]; PDFContentExtractor ce = new PDFContentExtractor(document.Pages[1]); PDFTextRunCollection trc = ce.ExtractTextRuns(); PDFTextRun tr = trc[1]; for (int i = 0; i < tr.Glyphs.Count; i++) { rnd.NextBytes(rgb); penColor.R = rgb[0]; penColor.G = rgb[1]; penColor.B = rgb[2]; PDFPath boundingPath = new PDFPath(); boundingPath.StartSubpath(tr.Glyphs[i].GlyphCorners[0].X, tr.Glyphs[i].GlyphCorners[0].Y); boundingPath.AddLineTo(tr.Glyphs[i].GlyphCorners[1].X, tr.Glyphs[i].GlyphCorners[1].Y); boundingPath.AddLineTo(tr.Glyphs[i].GlyphCorners[2].X, tr.Glyphs[i].GlyphCorners[2].Y); boundingPath.AddLineTo(tr.Glyphs[i].GlyphCorners[3].X, tr.Glyphs[i].GlyphCorners[3].Y); boundingPath.CloseSubpath(); document.Pages[1].Canvas.DrawPath(pen, boundingPath); } }
/// <summary> /// Draws content on a page using colors created from an indexed colorspace. /// </summary> /// <param name="page">The page to draw to.</param> private static void DrawIndexedColorSpace(PDFPage page) { PDFRgbColor black = new PDFRgbColor(0, 0, 0); PDFBrush blackBrush = new PDFBrush(black); //PDF4NET v5: PDFFont font = new PDFFont(PDFFontFace.Helvetica, 16); PDFStandardFont font = new PDFStandardFont(PDFStandardFontFace.Helvetica, 16); //PDF4NET v5: page.Canvas.DrawText("Indexed colorspace with 2 colors", font, null, blackBrush, 50, 50); page.Canvas.DrawString("Indexed colorspace with 2 colors", font, blackBrush, 50, 50); // Create an indexed colorspace with 2 RGB colors, 24bits (3*8bytes) per color. PDFIndexedColorSpace ixCS = new PDFIndexedColorSpace(); //PDF4NET v5: ixCS.MaxIndex = 1; ixCS.ColorCount = 2; ixCS.ColorTable = new byte[] { 255, 0, 0, 192, 0, 255 }; PDFIndexedColor red = new PDFIndexedColor(ixCS); red.ColorIndex = 0; PDFIndexedColor violet = new PDFIndexedColor(ixCS); violet.ColorIndex = 1; PDFBrush redBrush = new PDFBrush(red); PDFPen violetPen = new PDFPen(violet, 5); page.Canvas.DrawRoundRectangle(violetPen, redBrush, 50, 100, 400, 150, 20, 20); }
/// <summary> /// Draws content on a page using colors created from a calibrated Lab colorspace. /// </summary> /// <param name="page">The page to draw to.</param> private static void DrawLabColorSpace(PDFPage page) { PDFRgbColor black = new PDFRgbColor(0, 0, 0); PDFBrush blackBrush = new PDFBrush(black); //PDF4NET v5: PDFFont font = new PDFFont(PDFFontFace.Helvetica, 16); PDFStandardFont font = new PDFStandardFont(PDFStandardFontFace.Helvetica, 16); //PDF4NET v5: page.Canvas.DrawText("L*a*b* colorspace", font, null, blackBrush, 50, 50); page.Canvas.DrawString("L*a*b* colorspace", font, blackBrush, 50, 50); PDFLabColorSpace labCS = new PDFLabColorSpace(); PDFLabColor labClr1 = new PDFLabColor(labCS); labClr1.L = 90; labClr1.A = 20; labClr1.B = -10; PDFLabColor labClr2 = new PDFLabColor(labCS); labClr2.L = 10; labClr2.A = 70; labClr2.B = -50; PDFBrush brush = new PDFBrush(labClr1); PDFPen pen = new PDFPen(labClr2, 5); page.Canvas.DrawRoundRectangle(pen, brush, 50, 100, 400, 150, 20, 20); }
private static void DrawBezierCurves(PDFPage page, PDFFont titleFont, PDFFont sectionFont) { PDFBrush brush = new PDFBrush(); PDFPen blackPen = new PDFPen(PDFRgbColor.Black, 1); PDFPen redPen = new PDFPen(PDFRgbColor.Red, 1); PDFBrush blueBrush = new PDFBrush(PDFRgbColor.DarkBlue); PDFRgbColor randomPenColor = new PDFRgbColor(); PDFPen randomPen = new PDFPen(randomPenColor, 1); page.Canvas.DrawString("Bezier curves", titleFont, brush, 20, 50); page.Canvas.DrawLine(blackPen, 20, 210, 600, 210); page.Canvas.DrawLine(blackPen, 306, 70, 306, 350); page.Canvas.DrawRectangle(blueBrush, 39, 339, 2, 2); page.Canvas.DrawRectangle(blueBrush, 279, 79, 2, 2); page.Canvas.DrawRectangle(blueBrush, 499, 299, 2, 2); page.Canvas.DrawRectangle(blueBrush, 589, 69, 2, 2); page.Canvas.DrawBezier(redPen, 40, 340, 280, 80, 500, 300, 590, 70); page.Canvas.DrawString("Random bezier curves clipped to view", sectionFont, brush, 20, 385); PDFPath rectPath = new PDFPath(); rectPath.AddRectangle(20, 400, 570, 300); page.Canvas.SaveGraphicsState(); page.Canvas.SetClip(rectPath); Random rnd = new Random(); for (int i = 0; i < 100; i++) { randomPenColor.R = (byte)rnd.Next(256); randomPenColor.G = (byte)rnd.Next(256); randomPenColor.B = (byte)rnd.Next(256); double x1 = rnd.NextDouble() * page.Width; double y1 = 380 + rnd.NextDouble() * 350; double x2 = rnd.NextDouble() * page.Width; double y2 = 380 + rnd.NextDouble() * 350; double x3 = rnd.NextDouble() * page.Width; double y3 = 380 + rnd.NextDouble() * 350; double x4 = rnd.NextDouble() * page.Width; double y4 = 380 + rnd.NextDouble() * 350; page.Canvas.DrawBezier(randomPen, x1, y1, x2, y2, x3, y3, x4, y4); } page.Canvas.RestoreGraphicsState(); blackPen.DashPattern = null; page.Canvas.DrawPath(blackPen, rectPath); page.Canvas.CompressAndClose(); }
private static void DrawFormXObjects(PDFPage page, PDFFont titleFont, PDFFont sectionFont) { PDFBrush brush = new PDFBrush(); PDFPen blackPen = new PDFPen(PDFRgbColor.Black, 1); PDFRgbColor randomPenColor = new PDFRgbColor(); PDFPen randomPen = new PDFPen(randomPenColor, 1); PDFRgbColor randomBrushColor = new PDFRgbColor(); PDFBrush randomBrush = new PDFBrush(randomBrushColor); page.Canvas.DrawString("Form XObjects", titleFont, brush, 20, 50); page.Canvas.DrawString("Scaling", sectionFont, brush, 20, 70); // Create the XObject content - random rectangles PDFFormXObject xobject = new PDFFormXObject(300, 300); Random rnd = new Random(); for (int i = 0; i < 100; i++) { randomPenColor.R = (byte)rnd.Next(256); randomPenColor.G = (byte)rnd.Next(256); randomPenColor.B = (byte)rnd.Next(256); randomBrushColor.R = (byte)rnd.Next(256); randomBrushColor.G = (byte)rnd.Next(256); randomBrushColor.B = (byte)rnd.Next(256); double left = rnd.NextDouble() * xobject.Width; double top = rnd.NextDouble() * xobject.Height; double width = rnd.NextDouble() * xobject.Width; double height = rnd.NextDouble() * xobject.Height; double orientation = rnd.Next(360); xobject.Canvas.DrawRectangle(randomPen, randomBrush, left, top, width, height, orientation); } xobject.Canvas.DrawRectangle(blackPen, 0, 0, xobject.Width, xobject.Height); xobject.Canvas.CompressAndClose(); // Draw the form XObject 3 times on the page at different sizes. page.Canvas.DrawFormXObject(xobject, 3, 90, 100, 100); page.Canvas.DrawFormXObject(xobject, 106, 90, 200, 200); page.Canvas.DrawFormXObject(xobject, 309, 90, 300, 300); page.Canvas.DrawString("Flipping", sectionFont, brush, 20, 420); page.Canvas.DrawFormXObject(xobject, 20, 440, 150, 150); page.Canvas.DrawFormXObject(xobject, 200, 440, 150, 150, 0, PDFFlipDirection.VerticalFlip); page.Canvas.DrawFormXObject(xobject, 20, 620, 150, 150, 0, PDFFlipDirection.HorizontalFlip); page.Canvas.DrawFormXObject(xobject, 200, 620, 150, 150, 0, PDFFlipDirection.VerticalFlip | PDFFlipDirection.HorizontalFlip); page.Canvas.CompressAndClose(); }
static void Main(string[] args) { // Create the pdf document //PDF4NET v5: PDFDocument pdfDoc = new PDFDocument(); PDFFixedDocument pdfDoc = new PDFFixedDocument(); // Create first page //PDF4NET v5: PDFPage pdfPage1 = pdfDoc.AddPage(); PDFPage pdfPage1 = pdfDoc.Pages.Add(); // Create the graphic objects //PDF4NET v5: PDFFont fontText = new PDFFont(PDFFontFace.Helvetica, 12); PDFStandardFont fontText = new PDFStandardFont(PDFStandardFontFace.Helvetica, 12); PDFBrush blackBrush = new PDFBrush(new PDFRgbColor()); Random rnd = new Random(); for (int i = 0; i < 50; i++) { // Create random colors for drawing the spline PDFColor penColor = new PDFRgbColor((byte)rnd.Next(256), (byte)rnd.Next(256), (byte)rnd.Next(256)); // Create the pen to draw the border PDFPen randomPen = new PDFPen(penColor, 1); // Generate random control points float x1 = rnd.Next((int)pdfPage1.Width); float y1 = rnd.Next((int)pdfPage1.Height); float x2 = rnd.Next((int)pdfPage1.Width); float y2 = rnd.Next((int)pdfPage1.Height); float x3 = rnd.Next((int)pdfPage1.Width); float y3 = rnd.Next((int)pdfPage1.Height); float x4 = rnd.Next((int)pdfPage1.Width); float y4 = rnd.Next((int)pdfPage1.Height); // Draw the Bezier spline pdfPage1.Canvas.DrawBezier(randomPen, x1, y1, x2, y2, x3, y3, x4, y4); } // Draw a label //PDF4NET v5: pdfPage1.Canvas.DrawText("Random Bezier splines", fontText, null, blackBrush, 20, 20, 0, PDFTextAlign.TopLeft); pdfPage1.Canvas.DrawString("Random Bezier splines", fontText, blackBrush, 20, 20); // Save the document to disk pdfDoc.Save("Sample_Bezier.pdf"); }
/// <summary> /// Main method for running the sample. /// </summary> public static SampleOutputInfo[] Run(Stream output) { PDFFixedDocument document = new PDFFixedDocument(); // Init the save operation document.BeginSave(output); PDFRgbColor color = new PDFRgbColor(); PDFBrush brush = new PDFBrush(color); Random rnd = new Random(); for (int i = 0; i < 3; i++) { PDFPage page = document.Pages.Add(); page.Width = 1000; page.Height = 1000; for (int y = 1; y <= page.Height; y++) { for (int x = 0; x < page.Width; x++) { color.R = (byte)rnd.Next(256); color.G = (byte)rnd.Next(256); color.B = (byte)rnd.Next(256); page.Canvas.DrawRectangle(brush, x, y - 1, 1, 1); } if ((y % 100) == 0) { // Compress the graphics that have been drawn so far and save them. page.Canvas.Compress(); page.SaveGraphics(); } } // Close the page graphics and save the page. page.Canvas.CompressAndClose(); page.Save(); } // Finish the document. document.EndSave(); return(null); }
/// <summary> /// Draws content on a page using colors created from a separation colorspace. /// </summary> /// <param name="page">The page to draw to.</param> private static void DrawSeparationColorSpace(PDFPage page) { PDFRgbColor black = new PDFRgbColor(0, 0, 0); PDFBrush blackBrush = new PDFBrush(black); //PDF4NET v5: PDFFont font = new PDFFont(PDFFontFace.Helvetica, 16); PDFStandardFont font = new PDFStandardFont(PDFStandardFontFace.Helvetica, 16); //PDF4NET v5: page.Canvas.DrawText("Separation colorspace", font, null, blackBrush, 50, 50); page.Canvas.DrawString("Separation colorspace", font, blackBrush, 50, 50); //PDF4NET v5: PDFType2Function tintTransform = new PDFType2Function(); PDFExponentialFunction tintTransform = new PDFExponentialFunction(); tintTransform.C0 = new double[] { 0, 0, 0, 0 }; tintTransform.C1 = new double[] { 0, 0.53f, 1, 0 }; tintTransform.Exponent = 1; tintTransform.Domain = new double[] { 0, 1 }; tintTransform.Range = new double[] { 0, 1, 0, 1, 0, 1, 0, 1 }; PDFSeparationColorSpace separationCS = new PDFSeparationColorSpace(); separationCS.Colorant = "PANTONE Orange 021 C"; separationCS.TintTransform = tintTransform; PDFSeparationColor orange = new PDFSeparationColor(separationCS); orange.Tint = 0.9; //PDF4NET v5: tintTransform = new PDFType2Function(); tintTransform = new PDFExponentialFunction(); tintTransform.C0 = new double[] { 0, 0, 0, 0 }; tintTransform.C1 = new double[] { 0, 0.75f, 0.9f, 0 }; tintTransform.Exponent = 1; tintTransform.Domain = new double[] { 0, 1 }; tintTransform.Range = new double[] { 0, 1, 0, 1, 0, 1, 0, 1 }; separationCS = new PDFSeparationColorSpace(); separationCS.Colorant = "PANTONE Warm Red C"; separationCS.TintTransform = tintTransform; PDFSeparationColor warmRed = new PDFSeparationColor(separationCS); warmRed.Tint = 0.4; PDFBrush orangeBrush = new PDFBrush(orange); PDFPen warmRedPen = new PDFPen(warmRed, 5); page.Canvas.DrawRoundRectangle(warmRedPen, orangeBrush, 50, 100, 400, 150, 20, 20); }
/// <summary> /// Creates a new bookmark and adds it at the end of the collection. /// </summary> /// <param name="title">the title of the bookmark</param> /// <param name="bookmarkColor">a <see cref="PDFRgbColor"/> object specifying /// the color used to draw the bookmark in the bookmark tree.</param> /// <param name="visualStyle">a combination of <see cref="PDFOutlineItemVisualStyle"/> /// enumeration members, specifying the font style used to draw /// the bookmark title.</param> /// <param name="destinationPage">a <see cref="PDFPage"/> object /// representing the destination of the bookmark.</param> /// <returns>a <see cref="PDFBookmark"/> object representing the /// newly created bookmark.</returns> public static PDFOutlineItem CreateBookmark(string title, PDFRgbColor bookmarkColor, PDFOutlineItemVisualStyle visualStyle, PDFPage destinationPage) { //PDF4NET 5: PDFBookmark bookmark = new PDFBookmark(); PDFOutlineItem bookmark = new PDFOutlineItem(); bookmark.Title = title; bookmark.Color = bookmarkColor; //PDF4NET 5: bookmark.DisplayStyle = displayStyle; bookmark.VisualStyle = visualStyle; PDFGoToAction gotoAction = new PDFGoToAction(); //PDF4NET 5: gotoAction.Destination = new PDFPageDestination(); gotoAction.Destination = new PDFPageDirectDestination(); //PDF4NET 5: gotoAction.Destination.Page = destinationPage; (gotoAction.Destination as PDFPageDirectDestination).Page = destinationPage; bookmark.Action = gotoAction; return(bookmark); }
/// <summary> /// Draws content on a page using colors created from a device colorspace. /// </summary> /// <param name="page">The page to draw to.</param> /// <remarks>RGB, CMYK and Gray are device colorspaces. /// The PDFRgbColor, PDFCmykColor and PDFGrayColor classes create automatically the /// required colorspaces.</remarks> private static void DrawDeviceColorSpace(PDFPage page) { PDFRgbColor black = new PDFRgbColor(0, 0, 0); PDFBrush blackBrush = new PDFBrush(black); //PDF4NET v5: PDFFont font = new PDFFont(PDFFontFace.Helvetica, 16); PDFStandardFont font = new PDFStandardFont(PDFStandardFontFace.Helvetica, 16); //PDF4NET v5: page.Canvas.DrawText("Device colorspaces: RGB, CMYK and Gray", font, null, blackBrush, 50, 50); page.Canvas.DrawString("Device colorspaces: RGB, CMYK and Gray", font, blackBrush, 50, 50); // Draw RGB content. //PDF4NET v5: page.Canvas.DrawText("RGB", font, null, blackBrush, 50, 80); page.Canvas.DrawString("RGB", font, blackBrush, 50, 80); PDFRgbColor red = new PDFRgbColor(255, 0, 0); PDFRgbColor blue = new PDFRgbColor(0, 0, 255); PDFBrush redBrush = new PDFBrush(red); PDFPen bluePen = new PDFPen(blue, 5); page.Canvas.DrawRoundRectangle(bluePen, redBrush, 50, 100, 400, 150, 20, 20); // Draw CMYK content. //PDF4NET v5: page.Canvas.DrawText("CMYK", font, null, blackBrush, 50, 280); page.Canvas.DrawString("CMYK", font, blackBrush, 50, 280); PDFCmykColor cyan = new PDFCmykColor(1, 0, 0, 0); PDFCmykColor magenta = new PDFCmykColor(0, 1, 0, 0); PDFBrush cyanBrush = new PDFBrush(cyan); PDFPen magentaPen = new PDFPen(magenta, 5); page.Canvas.DrawRoundRectangle(magentaPen, cyanBrush, 50, 300, 400, 150, 20, 20); // Draw Gray content. //PDF4NET v5: page.Canvas.DrawText("Gray", font, null, blackBrush, 50, 480); page.Canvas.DrawString("Gray", font, blackBrush, 50, 480); PDFGrayColor lightGray = new PDFGrayColor(0.85); PDFGrayColor darkGray = new PDFGrayColor(0.1); PDFBrush lgBrush = new PDFBrush(lightGray); PDFPen dgPen = new PDFPen(darkGray, 5); page.Canvas.DrawRoundRectangle(dgPen, lgBrush, 50, 500, 400, 150, 20, 20); }
/// <summary> /// Draws content on a page using colors created from a calibrated gray colorspace. /// </summary> /// <param name="page">The page to draw to.</param> private static void DrawCalGrayColorSpace(PDFPage page) { PDFRgbColor black = new PDFRgbColor(0, 0, 0); PDFBrush blackBrush = new PDFBrush(black); //PDF4NET v5: PDFFont font = new PDFFont(PDFFontFace.Helvetica, 16); PDFStandardFont font = new PDFStandardFont(PDFStandardFontFace.Helvetica, 16); //PDF4NET v5: page.Canvas.DrawText("CalGray colorspace", font, null, blackBrush, 50, 50); page.Canvas.DrawString("CalGray colorspace", font, blackBrush, 50, 50); PDFCalGrayColorSpace calGrayCS = new PDFCalGrayColorSpace(); PDFCalGrayColor dkGray = new PDFCalGrayColor(calGrayCS); dkGray.Gray = 0.1; PDFCalGrayColor ltGray = new PDFCalGrayColor(calGrayCS); ltGray.Gray = 0.8; PDFBrush dkBrush = new PDFBrush(dkGray); PDFPen ltPen = new PDFPen(ltGray, 5); page.Canvas.DrawRoundRectangle(ltPen, dkBrush, 50, 100, 400, 150, 20, 20); }
private static void DrawArcsAndPies(PDFPage page, PDFFont titleFont, PDFFont sectionFont) { PDFBrush brush = new PDFBrush(); PDFPen blackPen = new PDFPen(PDFRgbColor.Black, 1); PDFPen redPen = new PDFPen(PDFRgbColor.Red, 1); PDFRgbColor randomPenColor = new PDFRgbColor(); PDFPen randomPen = new PDFPen(randomPenColor, 1); PDFRgbColor randomBrushColor = new PDFRgbColor(); PDFBrush randomBrush = new PDFBrush(randomBrushColor); page.Canvas.DrawString("Arcs", titleFont, brush, 20, 50); page.Canvas.DrawString("Pies", titleFont, brush, 310, 50); page.Canvas.DrawLine(blackPen, 20, 210, 300, 210); page.Canvas.DrawLine(blackPen, 160, 70, 160, 350); page.Canvas.DrawLine(blackPen, 310, 210, 590, 210); page.Canvas.DrawLine(blackPen, 450, 70, 450, 350); blackPen.DashPattern = new double[] { 2, 2 }; page.Canvas.DrawLine(blackPen, 20, 70, 300, 350); page.Canvas.DrawLine(blackPen, 20, 350, 300, 70); page.Canvas.DrawLine(blackPen, 310, 70, 590, 350); page.Canvas.DrawLine(blackPen, 310, 350, 590, 70); page.Canvas.DrawArc(redPen, 30, 80, 260, 260, 0, 135); page.Canvas.DrawPie(redPen, 320, 80, 260, 260, 45, 270); page.Canvas.DrawString("Random arcs and pies clipped to view", sectionFont, brush, 20, 385); PDFPath rectPath = new PDFPath(); rectPath.AddRectangle(20, 400, 570, 300); page.Canvas.SaveGraphicsState(); page.Canvas.SetClip(rectPath); Random rnd = new Random(); for (int i = 0; i < 100; i++) { randomPenColor.R = (byte)rnd.Next(256); randomPenColor.G = (byte)rnd.Next(256); randomPenColor.B = (byte)rnd.Next(256); randomBrushColor.R = (byte)rnd.Next(256); randomBrushColor.G = (byte)rnd.Next(256); randomBrushColor.B = (byte)rnd.Next(256); int mode = rnd.Next(4); double left = rnd.NextDouble() * page.Width; double top = 380 + rnd.NextDouble() * 350; double width = rnd.NextDouble() * page.Width; double height = rnd.NextDouble() * 250; double startAngle = rnd.Next(360); double sweepAngle = rnd.Next(360); switch (mode) { case 0: // Stroke arc outline page.Canvas.DrawArc(randomPen, left, top, width, height, startAngle, sweepAngle); break; case 1: // Stroke pie outline page.Canvas.DrawPie(randomPen, left, top, width, height, startAngle, sweepAngle); break; case 2: // Fill pie interior page.Canvas.DrawPie(randomBrush, left, top, width, height, startAngle, sweepAngle); break; case 3: // Stroke and fill pie page.Canvas.DrawPie(randomPen, randomBrush, left, top, width, height, startAngle, sweepAngle); break; } } page.Canvas.RestoreGraphicsState(); blackPen.DashPattern = null; page.Canvas.DrawPath(blackPen, rectPath); page.Canvas.CompressAndClose(); }
static void Main(string[] args) { // Create the pdf document //PDF4NET v5: PDFDocument pdfDoc = new PDFDocument(); PDFFixedDocument pdfDoc = new PDFFixedDocument(); // Create first page //PDF4NET v5: PDFPage pdfPage1 = pdfDoc.AddPage(); PDFPage pdfPage1 = pdfDoc.Pages.Add(); // Create the graphic objects //PDF4NET v5: PDFFont fontText = new PDFFont(PDFFontFace.Helvetica, 12); PDFStandardFont fontText = new PDFStandardFont(PDFStandardFontFace.Helvetica, 12); PDFBrush blackBrush = new PDFBrush(new PDFRgbColor()); PDFBrush greenBrush = new PDFBrush(new PDFRgbColor(0, 255, 0)); PDFPen pen = new PDFPen(PDFRgbColor.Orange, 1); // Draw an ellipse that has a border //PDF4NET v5: pdfPage1.Canvas.DrawText("This ellipse is drawn by drawing its border", fontText, null, blackBrush, 20, 20, 0, PDFTextAlign.TopLeft); pdfPage1.Canvas.DrawString("This ellipse is drawn by drawing its border", fontText, blackBrush, 20, 20); pdfPage1.Canvas.DrawEllipse(pen, null, 20, 35, 200, 100); // Draw an ellipse that has no border and its interior is filled //PDF4NET v5: pdfPage1.Canvas.DrawText("This ellipse is drawn by filling its interior", fontText, null, blackBrush, 300, 20, 0, PDFTextAlign.TopLeft); pdfPage1.Canvas.DrawString("This ellipse is drawn by filling its interior", fontText, blackBrush, 300, 20); pdfPage1.Canvas.DrawEllipse(null, greenBrush, 300, 35, 300, 150); // Draw an ellipse that has a border and its interior is filled //PDF4NET v5: pdfPage1.Canvas.DrawText("This ellipse has a border and a filled interior", fontText, null, blackBrush, 20, 200, 0, PDFTextAlign.TopLeft); pdfPage1.Canvas.DrawString("This ellipse has a border and a filled interior", fontText, blackBrush, 20, 200); pdfPage1.Canvas.DrawEllipse(pen, greenBrush, 20, 215, 200, 150); // Draw a circle //PDF4NET v5: pdfPage1.Canvas.DrawText("This is a circle", fontText, null, blackBrush, 300, 200, 0, PDFTextAlign.TopLeft); pdfPage1.Canvas.DrawString("This is a circle", fontText, blackBrush, 300, 200); pdfPage1.Canvas.DrawEllipse(pen, greenBrush, 300, 215, 200, 200); // Create the second page //PDF4NET v5: PDFPage pdfPage2 = pdfDoc.AddPage(); PDFPage pdfPage2 = pdfDoc.Pages.Add(); Random rnd = new Random(); for (int i = 0; i < 50; i++) { // Create random colors for the border and the interior PDFRgbColor brushColor = new PDFRgbColor((byte)rnd.Next(256), (byte)rnd.Next(256), (byte)rnd.Next(256)); PDFRgbColor penColor = new PDFRgbColor((byte)rnd.Next(256), (byte)rnd.Next(256), (byte)rnd.Next(256)); // Create the brush to fill the interior PDFBrush randomBrush = new PDFBrush(brushColor); // Create the pen to draw the border PDFPen randomPen = new PDFPen(penColor, 1); // Generate random positions float left = rnd.Next((int)pdfPage2.Width); float top = rnd.Next((int)pdfPage2.Height); // Generate random sizes float width = rnd.Next((int)(pdfPage2.Width - left)); // try to keep the rectangle float height = rnd.Next((int)(pdfPage2.Height - top)); // within the page // Draw the ellipse pdfPage2.Canvas.DrawEllipse(randomPen, randomBrush, left, top, width, height); } // Draw a label //PDF4NET v5: pdfPage2.Canvas.DrawText("Random ellipses", fontText, null, blackBrush, 20, 350, 0, PDFTextAlign.TopLeft); pdfPage2.Canvas.DrawString("Random ellipses", fontText, blackBrush, 20, 350); // Save the document to disk pdfDoc.Save("Sample_Ellipses.pdf"); }
private static void DrawLines(PDFPage page, PDFFont titleFont, PDFFont sectionFont) { PDFBrush brush = new PDFBrush(); PDFPen blackPen = new PDFPen(PDFRgbColor.Black, 1); PDFPen bluePen = new PDFPen(PDFRgbColor.LightBlue, 16); page.Canvas.DrawString("Lines", titleFont, brush, 20, 50); page.Canvas.DrawString("Line styles:", sectionFont, brush, 20, 70); page.Canvas.DrawString("Solid", sectionFont, brush, 20, 90); page.Canvas.DrawLine(blackPen, 100, 95, 400, 95); page.Canvas.DrawString("Dashed", sectionFont, brush, 20, 110); blackPen.DashPattern = new double[] { 3, 3 }; page.Canvas.DrawLine(blackPen, 100, 115, 400, 115); page.Canvas.DrawString("Line cap styles:", sectionFont, brush, 20, 150); page.Canvas.DrawString("Flat", sectionFont, brush, 20, 175); page.Canvas.DrawLine(bluePen, 100, 180, 400, 180); blackPen.DashPattern = null; page.Canvas.DrawLine(blackPen, 100, 180, 400, 180); page.Canvas.DrawString("Square", sectionFont, brush, 20, 195); bluePen.LineCap = PDFLineCap.Square; page.Canvas.DrawLine(bluePen, 100, 200, 400, 200); blackPen.DashPattern = null; page.Canvas.DrawLine(blackPen, 100, 200, 400, 200); page.Canvas.DrawString("Round", sectionFont, brush, 20, 215); bluePen.LineCap = PDFLineCap.Round; page.Canvas.DrawLine(bluePen, 100, 220, 400, 220); blackPen.DashPattern = null; page.Canvas.DrawLine(blackPen, 100, 220, 400, 220); page.Canvas.DrawString("Line join styles:", sectionFont, brush, 20, 250); page.Canvas.DrawString("Miter", sectionFont, brush, 20, 280); PDFPath miterPath = new PDFPath(); miterPath.StartSubpath(150, 320); miterPath.AddLineTo(250, 260); miterPath.AddLineTo(350, 320); bluePen.LineCap = PDFLineCap.Flat; bluePen.LineJoin = PDFLineJoin.Miter; page.Canvas.DrawPath(bluePen, miterPath); page.Canvas.DrawString("Bevel", sectionFont, brush, 20, 360); PDFPath bevelPath = new PDFPath(); bevelPath.StartSubpath(150, 400); bevelPath.AddLineTo(250, 340); bevelPath.AddLineTo(350, 400); bluePen.LineCap = PDFLineCap.Flat; bluePen.LineJoin = PDFLineJoin.Bevel; page.Canvas.DrawPath(bluePen, bevelPath); page.Canvas.DrawString("Round", sectionFont, brush, 20, 440); PDFPath roundPath = new PDFPath(); roundPath.StartSubpath(150, 480); roundPath.AddLineTo(250, 420); roundPath.AddLineTo(350, 480); bluePen.LineCap = PDFLineCap.Flat; bluePen.LineJoin = PDFLineJoin.Round; page.Canvas.DrawPath(bluePen, roundPath); page.Canvas.DrawString("Random lines clipped to rectangle", sectionFont, brush, 20, 520); PDFPath clipPath = new PDFPath(); clipPath.AddRectangle(20, 550, 570, 230); page.Canvas.SaveGraphicsState(); page.Canvas.SetClip(clipPath); PDFRgbColor randomColor = new PDFRgbColor(); PDFPen randomPen = new PDFPen(randomColor, 1); Random rnd = new Random(); for (int i = 0; i < 100; i++) { randomColor.R = (byte)rnd.Next(256); randomColor.G = (byte)rnd.Next(256); randomColor.B = (byte)rnd.Next(256); page.Canvas.DrawLine(randomPen, rnd.NextDouble() * page.Width, 550 + rnd.NextDouble() * 250, rnd.NextDouble() * page.Width, 550 + rnd.NextDouble() * 250); } page.Canvas.RestoreGraphicsState(); page.Canvas.DrawPath(blackPen, clipPath); page.Canvas.CompressAndClose(); }
private static void DrawEllipses(PDFPage page, PDFFont titleFont, PDFFont sectionFont) { PDFBrush brush = new PDFBrush(); PDFPen blackPen = new PDFPen(PDFRgbColor.Black, 1); PDFPen redPen = new PDFPen(PDFRgbColor.Red, 1); PDFRgbColor randomPenColor = new PDFRgbColor(); PDFPen randomPen = new PDFPen(randomPenColor, 1); PDFRgbColor randomBrushColor = new PDFRgbColor(); PDFBrush randomBrush = new PDFBrush(randomBrushColor); page.Canvas.DrawString("Ellipses", titleFont, brush, 20, 50); page.Canvas.DrawLine(blackPen, 20, 150, 300, 150); page.Canvas.DrawLine(blackPen, 80, 70, 80, 350); page.Canvas.DrawEllipse(redPen, 80, 150, 180, 100); page.Canvas.DrawLine(blackPen, 320, 150, 600, 150); page.Canvas.DrawLine(blackPen, 380, 70, 380, 350); page.Canvas.DrawEllipse(redPen, 380, 150, 180, 100, 30); page.Canvas.DrawString("Random ellipses clipped to view", sectionFont, brush, 20, 385); PDFPath ellipsePath = new PDFPath(); ellipsePath.AddEllipse(20, 400, 570, 300); page.Canvas.SaveGraphicsState(); page.Canvas.SetClip(ellipsePath); Random rnd = new Random(); for (int i = 0; i < 100; i++) { randomPenColor.R = (byte)rnd.Next(256); randomPenColor.G = (byte)rnd.Next(256); randomPenColor.B = (byte)rnd.Next(256); randomBrushColor.R = (byte)rnd.Next(256); randomBrushColor.G = (byte)rnd.Next(256); randomBrushColor.B = (byte)rnd.Next(256); int mode = rnd.Next(3); double left = rnd.NextDouble() * page.Width; double top = 380 + rnd.NextDouble() * 350; double width = rnd.NextDouble() * page.Width; double height = rnd.NextDouble() * 250; double orientation = rnd.Next(360); switch (mode) { case 0: // Stroke ellipse outline page.Canvas.DrawEllipse(randomPen, left, top, width, height, orientation); break; case 1: // Fill ellipse interior page.Canvas.DrawEllipse(randomBrush, left, top, width, height, orientation); break; case 2: // Stroke and fill ellipse page.Canvas.DrawEllipse(randomPen, randomBrush, left, top, width, height, orientation); break; } } page.Canvas.RestoreGraphicsState(); page.Canvas.DrawPath(blackPen, ellipsePath); page.Canvas.CompressAndClose(); }
private static void DrawShadings(PDFPage page, PDFFont titleFont, PDFFont sectionFont) { PDFBrush brush = new PDFBrush(); PDFPen blackPen = new PDFPen(PDFRgbColor.Black, 1); PDFRgbColor randomPenColor = new PDFRgbColor(); PDFPen randomPen = new PDFPen(randomPenColor, 1); PDFRgbColor randomBrushColor = new PDFRgbColor(); PDFBrush randomBrush = new PDFBrush(randomBrushColor); page.Canvas.DrawString("Shadings", titleFont, brush, 20, 50); page.Canvas.DrawString("Horizontal", sectionFont, brush, 25, 70); PDFAxialShading horizontalShading = new PDFAxialShading(); horizontalShading.StartColor = new PDFRgbColor(255, 0, 0); horizontalShading.EndColor = new PDFRgbColor(0, 0, 255); horizontalShading.StartPoint = new PDFPoint(25, 90); horizontalShading.EndPoint = new PDFPoint(175, 90); // Clip the shading to desired area. PDFPath hsArea = new PDFPath(); hsArea.AddRectangle(25, 90, 150, 150); page.Canvas.SaveGraphicsState(); page.Canvas.SetClip(hsArea); page.Canvas.DrawShading(horizontalShading); page.Canvas.RestoreGraphicsState(); page.Canvas.DrawString("Vertical", sectionFont, brush, 225, 70); PDFAxialShading verticalShading = new PDFAxialShading(); verticalShading.StartColor = new PDFRgbColor(255, 0, 0); verticalShading.EndColor = new PDFRgbColor(0, 0, 255); verticalShading.StartPoint = new PDFPoint(225, 90); verticalShading.EndPoint = new PDFPoint(225, 240); // Clip the shading to desired area. PDFPath vsArea = new PDFPath(); vsArea.AddRectangle(225, 90, 150, 150); page.Canvas.SaveGraphicsState(); page.Canvas.SetClip(vsArea); page.Canvas.DrawShading(verticalShading); page.Canvas.RestoreGraphicsState(); page.Canvas.DrawString("Diagonal", sectionFont, brush, 425, 70); PDFAxialShading diagonalShading = new PDFAxialShading(); diagonalShading.StartColor = new PDFRgbColor(255, 0, 0); diagonalShading.EndColor = new PDFRgbColor(0, 0, 255); diagonalShading.StartPoint = new PDFPoint(425, 90); diagonalShading.EndPoint = new PDFPoint(575, 240); // Clip the shading to desired area. PDFPath dsArea = new PDFPath(); dsArea.AddRectangle(425, 90, 150, 150); page.Canvas.SaveGraphicsState(); page.Canvas.SetClip(dsArea); page.Canvas.DrawShading(diagonalShading); page.Canvas.RestoreGraphicsState(); page.Canvas.DrawString("Extended shading", sectionFont, brush, 25, 260); PDFAxialShading extendedShading = new PDFAxialShading(); extendedShading.StartColor = new PDFRgbColor(255, 0, 0); extendedShading.EndColor = new PDFRgbColor(0, 0, 255); extendedShading.StartPoint = new PDFPoint(225, 280); extendedShading.EndPoint = new PDFPoint(375, 280); extendedShading.ExtendStart = true; extendedShading.ExtendEnd = true; // Clip the shading to desired area. PDFPath esArea = new PDFPath(); esArea.AddRectangle(25, 280, 550, 30); page.Canvas.SaveGraphicsState(); page.Canvas.SetClip(esArea); page.Canvas.DrawShading(extendedShading); page.Canvas.RestoreGraphicsState(); page.Canvas.DrawPath(blackPen, esArea); page.Canvas.DrawString("Limited shading", sectionFont, brush, 25, 330); PDFAxialShading limitedShading = new PDFAxialShading(); limitedShading.StartColor = new PDFRgbColor(255, 0, 0); limitedShading.EndColor = new PDFRgbColor(0, 0, 255); limitedShading.StartPoint = new PDFPoint(225, 350); limitedShading.EndPoint = new PDFPoint(375, 350); limitedShading.ExtendStart = false; limitedShading.ExtendEnd = false; // Clip the shading to desired area. PDFPath lsArea = new PDFPath(); lsArea.AddRectangle(25, 350, 550, 30); page.Canvas.SaveGraphicsState(); page.Canvas.SetClip(lsArea); page.Canvas.DrawShading(limitedShading); page.Canvas.RestoreGraphicsState(); page.Canvas.DrawPath(blackPen, lsArea); page.Canvas.DrawString("Multi-stop shading", sectionFont, brush, 25, 400); // Multi-stop shadings use a stitching function to combine the functions that define each gradient part. // Function for red to blue shading. PDFExponentialFunction redToBlueFunc = new PDFExponentialFunction(); // Linear function redToBlueFunc.Exponent = 1; redToBlueFunc.Domain = new double[] { 0, 1 }; // Red color for start redToBlueFunc.C0 = new double[] { 1, 0, 0 }; // Blue color for start redToBlueFunc.C1 = new double[] { 0, 0, 1 }; // Function for blue to green shading. PDFExponentialFunction blueToGreenFunc = new PDFExponentialFunction(); // Linear function blueToGreenFunc.Exponent = 1; blueToGreenFunc.Domain = new double[] { 0, 1 }; // Blue color for start blueToGreenFunc.C0 = new double[] { 0, 0, 1 }; // Green color for start blueToGreenFunc.C1 = new double[] { 0, 1, 0 }; //Stitching function for the shading. PDFStitchingFunction shadingFunction = new PDFStitchingFunction(); shadingFunction.Functions.Add(redToBlueFunc); shadingFunction.Functions.Add(blueToGreenFunc); shadingFunction.Domain = new double[] { 0, 1 }; shadingFunction.Encode = new double[] { 0, 1, 0, 1 }; // Entire shading goes from 0 to 1 (100%). // We set the first shading (red->blue) to cover 30% (0 - 0.3) and // the second shading to cover 70% (0.3 - 1). shadingFunction.Bounds = new double[] { 0.3 }; // The multistop shading PDFAxialShading multiStopShading = new PDFAxialShading(); multiStopShading.StartPoint = new PDFPoint(25, 420); multiStopShading.EndPoint = new PDFPoint(575, 420); // The colorspace must match the colors specified in C0 & C1 multiStopShading.ColorSpace = new PDFRgbColorSpace(); multiStopShading.Function = shadingFunction; // Clip the shading to desired area. PDFPath mssArea = new PDFPath(); mssArea.AddRectangle(25, 420, 550, 30); page.Canvas.SaveGraphicsState(); page.Canvas.SetClip(mssArea); page.Canvas.DrawShading(multiStopShading); page.Canvas.RestoreGraphicsState(); page.Canvas.DrawPath(blackPen, lsArea); page.Canvas.DrawString("Radial shading", sectionFont, brush, 25, 470); PDFRadialShading rs1 = new PDFRadialShading(); rs1.StartColor = new PDFRgbColor(0, 255, 0); rs1.EndColor = new PDFRgbColor(255, 0, 255); rs1.StartCircleCenter = new PDFPoint(50, 500); rs1.StartCircleRadius = 10; rs1.EndCircleCenter = new PDFPoint(500, 570); rs1.EndCircleRadius = 100; page.Canvas.DrawShading(rs1); PDFRadialShading rs2 = new PDFRadialShading(); rs2.StartColor = new PDFRgbColor(0, 255, 0); rs2.EndColor = new PDFRgbColor(255, 0, 255); rs2.StartCircleCenter = new PDFPoint(80, 600); rs2.StartCircleRadius = 10; rs2.EndCircleCenter = new PDFPoint(110, 690); rs2.EndCircleRadius = 100; page.Canvas.DrawShading(rs2); page.Canvas.CompressAndClose(); }