private void DrawPath(PdfPageBase page) { PointF[] points = new PointF[5]; for (int i = 0; i < points.Length; i++) { float x = (float)Math.Cos(i * 2 * Math.PI / 5); float y = (float)Math.Sin(i * 2 * Math.PI / 5); points[i] = new PointF(x, y); } PdfPath path = new PdfPath(); path.AddLine(points[2], points[0]); path.AddLine(points[0], points[3]); path.AddLine(points[3], points[1]); path.AddLine(points[1], points[4]); path.AddLine(points[4], points[2]); //save graphics state PdfGraphicsState state = page.Canvas.Save(); PdfPen pen = new PdfPen(Color.DeepSkyBlue, 0.02f); PdfBrush brush1 = new PdfSolidBrush(Color.CadetBlue); page.Canvas.ScaleTransform(50f, 50f); page.Canvas.TranslateTransform(5f, 1.2f); page.Canvas.DrawPath(pen, path); page.Canvas.TranslateTransform(2f, 0f); path.FillMode = PdfFillMode.Alternate; page.Canvas.DrawPath(pen, brush1, path); page.Canvas.TranslateTransform(2f, 0f); path.FillMode = PdfFillMode.Winding; page.Canvas.DrawPath(pen, brush1, path); PdfLinearGradientBrush brush2 = new PdfLinearGradientBrush(new PointF(-2, 0), new PointF(2, 0), Color.Red, Color.Blue); page.Canvas.TranslateTransform(-4f, 2f); path.FillMode = PdfFillMode.Alternate; page.Canvas.DrawPath(pen, brush2, path); PdfRadialGradientBrush brush3 = new PdfRadialGradientBrush(new PointF(0f, 0f), 0f, new PointF(0f, 0f), 1f, Color.Red, Color.Blue); page.Canvas.TranslateTransform(2f, 0f); path.FillMode = PdfFillMode.Winding; page.Canvas.DrawPath(pen, brush3, path); PdfTilingBrush brush4 = new PdfTilingBrush(new RectangleF(0, 0, 4f, 4f)); brush4.Graphics.DrawRectangle(brush2, 0, 0, 4f, 4f); page.Canvas.TranslateTransform(2f, 0f); path.FillMode = PdfFillMode.Winding; page.Canvas.DrawPath(pen, brush4, path); //restor graphics page.Canvas.Restore(state); }
public ActionResult DrawingShapes(string InsideBrowser) { // Create a new PDF document. PdfDocument doc = new PdfDocument(); int i; // Create a new page. PdfPage page = doc.Pages.Add(); // Obtain PdfGraphics object. PdfGraphics g = page.Graphics; PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 14, PdfFontStyle.Bold); #region Polygon PdfPen pen = new PdfPen(Color.Black); pen.Width = 3; PointF p1 = new PointF(05, 10); PointF p2 = new PointF(05, 10); PointF p3 = new PointF(60, 70); PointF p4 = new PointF(40, 70); PointF[] points = { p1, p2, p3, p4 }; int pointNum = 16; points = new PointF[pointNum]; double f = 360.0 / pointNum * Math.PI / 180.0; const double r = 100; PointF center = new PointF(140, 140); for (i = 0; i < pointNum; ++i) { PointF p = new PointF(); double theta = i * f; p.Y = (float)(Math.Sin(theta) * r + center.Y); p.X = (float)(Math.Cos(theta) * r + center.X); points[i] = p; } PdfSolidBrush brush = new PdfSolidBrush(Color.Green); pen.Color = Color.Brown; pen.Width = 10; pen.LineJoin = PdfLineJoin.Round; g.DrawString("Polygon", font, PdfBrushes.DarkBlue, new PointF(50, 0)); g.DrawPolygon(pen, brush, points); #endregion #region Pie RectangleF rect = new RectangleF(200, 50, 200, 200); brush.Color = Color.Green; pen.Color = Color.Brown; pen.Width = 10; rect.Location = new PointF(20, 280); pen.LineJoin = PdfLineJoin.Round; g.DrawString("Pie shape", font, PdfBrushes.DarkBlue, new PointF(50, 250)); g.DrawPie(pen, brush, rect, 180, 60); g.DrawPie(pen, brush, rect, 360 - 60, 60); g.DrawPie(pen, brush, rect, 60, 60); #endregion #region Arc g.DrawString("Arcs", font, PdfBrushes.DarkBlue, new PointF(330, 0)); pen = new PdfPen(Color.Black); pen.Width = 11; pen.LineCap = PdfLineCap.Round; pen.Color = Color.Brown; rect = new RectangleF(310, 40, 200, 200); g.DrawArc(pen, rect, 0, 90); pen.Color = Color.DarkGreen; rect.X -= 10; g.DrawArc(pen, rect, 90, 90); pen.Color = Color.Brown; rect.Y -= 10; g.DrawArc(pen, rect, 180, 90); pen.Color = Color.DarkGreen; rect.X += 10; g.DrawArc(pen, rect, 270, 90); #endregion #region Rectangle rect = new RectangleF(310, 280, 200, 100); brush.Color = Color.Green; pen.Color = Color.Brown; g.DrawString("Simple Rectangle", font, PdfBrushes.DarkBlue, new PointF(310, 255)); g.DrawRectangle(pen, brush, rect); #endregion #region ellipse pen = new PdfPen(Color.Black); rect = new RectangleF(270, 400, 160, 1000); g.DrawString("Shape with pagination", font, PdfBrushes.DarkBlue, new PointF(300, 390)); PdfEllipse ellipse = new PdfEllipse(rect); PdfLayoutFormat format = new PdfLayoutFormat(); format.Break = PdfLayoutBreakType.FitPage; format.Layout = PdfLayoutType.Paginate; ellipse.Brush = PdfBrushes.Brown; ellipse.Draw(page, 20, 20, format); brush = new PdfSolidBrush(Color.Black); ellipse.Brush = PdfBrushes.DarkGreen; ellipse.Draw(page, 40, 40, format); #endregion #region Transaparency page = doc.Pages[1]; g = page.Graphics; g.DrawString("Transparent Rectangles", font, PdfBrushes.DarkBlue, new PointF(50, 80)); PdfBrush gBrush; rect = new RectangleF(10, 150, 100, 100); pen = new PdfPen(Color.Black); gBrush = new PdfSolidBrush(Color.DarkGreen); g.DrawRectangle(pen, gBrush, rect); gBrush = new PdfSolidBrush(Color.Brown); rect.X += 20; rect.Y += 20; pen = new PdfPen(Color.Brown); g.SetTransparency(0.7f); g.DrawRectangle(pen, gBrush, rect); rect.X += 20; rect.Y += 20; gBrush = new PdfLinearGradientBrush(rect, Color.DarkGreen, Color.Brown, PdfLinearGradientMode.BackwardDiagonal); g.SetTransparency(0.5f); g.DrawRectangle(pen, gBrush, rect); rect.X += 20; rect.Y += 20; pen = new PdfPen(Color.Blue); gBrush = new PdfSolidBrush(Color.Gray); g.SetTransparency(0.25f); g.DrawRectangle(pen, gBrush, rect); rect.X += 20; rect.Y += 20; pen = new PdfPen(Color.Black); gBrush = new PdfSolidBrush(Color.Green); g.SetTransparency(0.1f); g.DrawRectangle(pen, gBrush, rect); #endregion #region Rectangle with Color space PointF location = new PointF(10, 50); page = doc.Pages.Add(); g = page.Graphics; doc.ColorSpace = (PdfColorSpace)i; // SolidBrush gBrush = new PdfSolidBrush(Color.Red); DrawRectangles(location, g, font, gBrush, pen, doc); // LinearGradient location = new PointF(180, 50); PointF point2 = location; point2.X += 180; gBrush = new PdfLinearGradientBrush(location, point2, Color.Blue, Color.Red); DrawRectangles(location, g, font, gBrush, pen, doc); // Raidal Gradient location = new PointF(360, 50); point2 = location; point2 = new PointF(location.X + 50, location.Y + 50); //point2.Y += 250; //point2.X = 150; gBrush = new PdfRadialGradientBrush(point2, 0, point2, 100, Color.Blue, Color.Red); (gBrush as PdfRadialGradientBrush).Extend = PdfExtend.Both; DrawRectangles(location, g, font, gBrush, pen, doc); g.DrawString("Rectangle with color spaces", font, PdfBrushes.DarkBlue, new PointF(150, 0)); #endregion //Save the PDF to the MemoryStream MemoryStream ms = new MemoryStream(); doc.Save(ms); //If the position is not set to '0' then the PDF will be empty. ms.Position = 0; //Close the PDF document. doc.Close(true); //Download the PDF document in the browser. FileStreamResult fileStreamResult = new FileStreamResult(ms, "application/pdf"); fileStreamResult.FileDownloadName = "Shapes.pdf"; return(fileStreamResult); }
private void button1_Click(object sender, EventArgs e) { //Create a pdf document PdfDocument doc = new PdfDocument(); doc.LoadFromFile(@"..\..\..\..\..\..\Data\Font.pdf"); //Create one page PdfPageBase page = doc.Pages[0]; //Draw the text float l = page.Canvas.ClientSize.Width / 2; PointF center = new PointF(l, l); float r = (float)Math.Sqrt(2 * l * l); PdfRadialGradientBrush brush = new PdfRadialGradientBrush(center, 0f, center, r, Color.Blue, Color.Red); PdfFontFamily[] fontFamilies = (PdfFontFamily[])Enum.GetValues(typeof(PdfFontFamily)); float y = 200; for (int i = 0; i < fontFamilies.Length; i++) { String text = String.Format("Font Family: {0}", fontFamilies[i]); float x1 = 40; y = 200 + i * 16; PdfFont font1 = new PdfFont(PdfFontFamily.Courier, 14f); PdfFont font2 = new PdfFont(fontFamilies[i], 14f); float x2 = x1 + 10 + font1.MeasureString(text).Width; page.Canvas.DrawString(text, font1, brush, x1, y); page.Canvas.DrawString(text, font2, brush, x2, y); } //True type font - embedded System.Drawing.Font font = new System.Drawing.Font("Arial", 15f, FontStyle.Bold); PdfTrueTypeFont trueTypeFont = new PdfTrueTypeFont(font); page.Canvas.DrawString("Font Family: Arial - Embedded", trueTypeFont, brush, 40, (y = y + 26f)); //Right to left String arabicText = "\u0627\u0644\u0630\u0647\u0627\u0628\u0021\u0020" + "\u0628\u062F\u0648\u0631\u0647\u0020\u062D\u0648\u0644\u0647\u0627\u0021\u0020" + "\u0627\u0644\u0630\u0647\u0627\u0628\u0021\u0020" + "\u0627\u0644\u0630\u0647\u0627\u0628\u0021\u0020" + "\u0627\u0644\u0630\u0647\u0627\u0628\u0021"; trueTypeFont = new PdfTrueTypeFont(font, true); RectangleF rctg = new RectangleF(new PointF(40, (y = y + 26f)), page.Canvas.ClientSize); PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Right); format.RightToLeft = true; page.Canvas.DrawString(arabicText, trueTypeFont, brush, rctg, format); //True type font - not embedded font = new System.Drawing.Font("Batang", 14f, FontStyle.Italic); trueTypeFont = new PdfTrueTypeFont(font); page.Canvas.DrawString("Font Family: Batang - Not Embedded", trueTypeFont, brush, 40, (y = y + 16f)); //Font file String fontFileName = @"..\..\..\..\..\..\Data\Hawaii_Killer.ttf"; trueTypeFont = new PdfTrueTypeFont(fontFileName, 20f); page.Canvas.DrawString("Hawaii Killer Font", trueTypeFont, brush, 40, (y = y + 36f)); page.Canvas.DrawString("Hawaii Killer Font, from http://www.1001freefonts.com", new PdfFont(PdfFontFamily.Helvetica, 8f), brush, 40, (y = y + 40f)); //Cjk font PdfCjkStandardFont cjkFont = new PdfCjkStandardFont(PdfCjkFontFamily.MonotypeHeiMedium, 14f); page.Canvas.DrawString("How to say 'Font' in Chinese? \u5B57\u4F53", cjkFont, brush, 40, (y = y + 36f)); cjkFont = new PdfCjkStandardFont(PdfCjkFontFamily.HanyangSystemsGothicMedium, 14f); page.Canvas.DrawString("How to say 'Font' in Japanese? \u30D5\u30A9\u30F3\u30C8", cjkFont, brush, 40, (y = y + 36f)); cjkFont = new PdfCjkStandardFont(PdfCjkFontFamily.HanyangSystemsShinMyeongJoMedium, 14f); page.Canvas.DrawString("How to say 'Font' in Korean? \uAE00\uAF34", cjkFont, brush, 40, (y = y + 36f)); //Save the document doc.SaveToFile("Font.pdf"); doc.Close(); //Launch the Pdf file PDFDocumentViewer("Font.pdf"); }
public ActionResult DrawingShapes(string InsideBrowser) { // Create a new PDF document. PdfDocument doc = new PdfDocument(); int i; // Create a new page. PdfPage page = doc.Pages.Add(); // Obtain PdfGraphics object. PdfGraphics g = page.Graphics; PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 14, PdfFontStyle.Bold); #region Polygon PdfPen pen = new PdfPen(Color.Black); pen.Width = 3; PointF p1 = new PointF(05, 10); PointF p2 = new PointF(05, 10); PointF p3 = new PointF(60, 70); PointF p4 = new PointF(40, 70); PointF[] points = { p1, p2, p3, p4 }; int pointNum = 16; points = new PointF[pointNum]; double f = 360.0 / pointNum * Math.PI / 180.0; const double r = 100; PointF center = new PointF(140, 140); for (i = 0; i < pointNum; ++i) { PointF p = new PointF(); double theta = i * f; p.Y = (float)(Math.Sin(theta) * r + center.Y); p.X = (float)(Math.Cos(theta) * r + center.X); points[i] = p; } PdfSolidBrush brush = new PdfSolidBrush(Color.Green); pen.Color = Color.Brown; pen.Width = 10; pen.LineJoin = PdfLineJoin.Round; g.DrawString("Polygon", font, PdfBrushes.DarkBlue, new PointF(50, 0)); g.DrawPolygon(pen, brush, points); #endregion #region Pie RectangleF rect = new RectangleF(200, 50, 200, 200); brush.Color = Color.Green; pen.Color = Color.Brown; pen.Width = 10; rect.Location = new PointF(20, 280); pen.LineJoin = PdfLineJoin.Round; g.DrawString("Pie shape", font, PdfBrushes.DarkBlue, new PointF(50, 250)); g.DrawPie(pen, brush, rect, 180, 60); g.DrawPie(pen, brush, rect, 360 - 60, 60); g.DrawPie(pen, brush, rect, 60, 60); #endregion #region Arc g.DrawString("Arcs", font, PdfBrushes.DarkBlue, new PointF(330, 0)); pen = new PdfPen(Color.Black); pen.Width = 11; pen.LineCap = PdfLineCap.Round; pen.Color = Color.Brown; rect = new RectangleF(310, 40, 200, 200); g.DrawArc(pen, rect, 0, 90); pen.Color = Color.DarkGreen; rect.X -= 10; g.DrawArc(pen, rect, 90, 90); pen.Color = Color.Brown; rect.Y -= 10; g.DrawArc(pen, rect, 180, 90); pen.Color = Color.DarkGreen; rect.X += 10; g.DrawArc(pen, rect, 270, 90); #endregion #region Rectangle rect = new RectangleF(310, 280, 200, 100); brush.Color = Color.Green; pen.Color = Color.Brown; g.DrawString("Simple Rectangle", font, PdfBrushes.DarkBlue, new PointF(310, 255)); g.DrawRectangle(pen, brush, rect); #endregion #region ellipse // Draw a simple ellipse. pen = new PdfPen(Color.Black); rect = new RectangleF(80, 520, 100, 200); //page = doc.Pages[0]; //g = page.Graphics; PdfLinearGradientBrush lgBrush = new PdfLinearGradientBrush(rect, Color.DarkGreen, Color.White, 90); brush = new PdfSolidBrush(Color.Black); brush.Color = Color.DarkGreen; pen.Color = Color.Brown; pen.Width = 10.0f; g.DrawString("Ellipse with Gradient", font, PdfBrushes.DarkBlue, new PointF(50, 490)); g.DrawEllipse(pen, lgBrush, rect); rect = new RectangleF(270, 400, 160, 1000); g.DrawString("Shape with pagination", font, PdfBrushes.DarkBlue, new PointF(300, 390)); PdfEllipse ellipse = new PdfEllipse(rect); PdfLayoutFormat format = new PdfLayoutFormat(); format.Break = PdfLayoutBreakType.FitPage; format.Layout = PdfLayoutType.Paginate; ellipse.Brush = PdfBrushes.Brown; ellipse.Draw(page, 20, 20, format); brush = new PdfSolidBrush(Color.Black); ellipse.Brush = PdfBrushes.DarkGreen; ellipse.Draw(page, 40, 40, format); #endregion #region Transaparency page = doc.Pages[1]; g = page.Graphics; g.DrawString("Transparent Rectangles", font, PdfBrushes.DarkBlue, new PointF(50, 80)); PdfBrush gBrush; rect = new RectangleF(10, 150, 100, 100); pen = new PdfPen(Color.Black); gBrush = new PdfSolidBrush(Color.DarkGreen); g.DrawRectangle(pen, gBrush, rect); gBrush = new PdfSolidBrush(Color.Brown); rect.X += 20; rect.Y += 20; pen = new PdfPen(Color.Brown); g.SetTransparency(0.7f); g.DrawRectangle(pen, gBrush, rect); rect.X += 20; rect.Y += 20; gBrush = new PdfLinearGradientBrush(rect, Color.DarkGreen, Color.Brown, PdfLinearGradientMode.BackwardDiagonal); g.SetTransparency(0.5f); g.DrawRectangle(pen, gBrush, rect); rect.X += 20; rect.Y += 20; pen = new PdfPen(Color.Blue); gBrush = new PdfSolidBrush(Color.Gray); g.SetTransparency(0.25f); g.DrawRectangle(pen, gBrush, rect); rect.X += 20; rect.Y += 20; pen = new PdfPen(Color.Black); gBrush = new PdfSolidBrush(Color.Green); g.SetTransparency(0.1f); g.DrawRectangle(pen, gBrush, rect); #endregion #region Rectangle with Color space PointF location = new PointF(10, 50); page = doc.Pages.Add(); g = page.Graphics; doc.ColorSpace = (PdfColorSpace)i; // SolidBrush gBrush = new PdfSolidBrush(Color.Red); DrawRectangles(location, g, font, gBrush, pen, doc); // LinearGradient location = new PointF(180, 50); PointF point2 = location; point2.X += 100; gBrush = new PdfLinearGradientBrush(location, point2, Color.Blue, Color.Red); DrawRectangles(location, g, font, gBrush, pen, doc); // Raidal Gradient location = new PointF(360, 50); point2 = location; point2.Y += 250; point2.X = 150; gBrush = new PdfRadialGradientBrush(point2, 210, point2, 400, Color.Blue, Color.Red); (gBrush as PdfRadialGradientBrush).Extend = PdfExtend.End; DrawRectangles(location, g, font, gBrush, pen, doc); g.DrawString("Rectangle with color spaces", font, PdfBrushes.DarkBlue, new PointF(150, 0)); #endregion //Stream the output to the browser. if (InsideBrowser == "Browser") { return(doc.ExportAsActionResult("sample.pdf", HttpContext.ApplicationInstance.Response, HttpReadType.Open)); } else { return(doc.ExportAsActionResult("sample.pdf", HttpContext.ApplicationInstance.Response, HttpReadType.Save)); } }
private void button1_Click(object sender, EventArgs e) { //Create a pdf document. PdfDocument doc = new PdfDocument(); // Create one page PdfPageBase page = doc.Pages.Add(); //Draw the text float l = page.Canvas.ClientSize.Width / 2; PointF center = new PointF(l, l); float r = (float)Math.Sqrt(2 * l * l); PdfRadialGradientBrush brush = new PdfRadialGradientBrush(center, 0f, center, r, Color.Blue, Color.Red); PdfFontFamily[] fontFamilies = (PdfFontFamily[])Enum.GetValues(typeof(PdfFontFamily)); float y = 10; for(int i = 0; i < fontFamilies.Length; i++) { String text = String.Format("Font Family: {0}", fontFamilies[i]); float x1 = 0; y = 10 + i * 16; PdfFont font1 = new PdfFont(PdfFontFamily.Courier, 14f); PdfFont font2 = new PdfFont(fontFamilies[i], 14f); float x2 = x1 + 10 + font1.MeasureString(text).Width; page.Canvas.DrawString(text, font1, brush, x1, y); page.Canvas.DrawString(text, font2, brush, x2, y); } //true type font - embedded. System.Drawing.Font font = new System.Drawing.Font("Arial", 14f, FontStyle.Bold); PdfTrueTypeFont trueTypeFont = new PdfTrueTypeFont(font); page.Canvas.DrawString("Font Family: Arial - Embedded", trueTypeFont, brush, 0, (y = y + 16f)); //right to left String arabicText = "\u0627\u0644\u0630\u0647\u0627\u0628\u0021\u0020" + "\u0628\u062F\u0648\u0631\u0647\u0020\u062D\u0648\u0644\u0647\u0627\u0021\u0020" + "\u0627\u0644\u0630\u0647\u0627\u0628\u0021\u0020" + "\u0627\u0644\u0630\u0647\u0627\u0628\u0021\u0020" + "\u0627\u0644\u0630\u0647\u0627\u0628\u0021"; trueTypeFont = new PdfTrueTypeFont(font, true); RectangleF rctg = new RectangleF(new PointF(0, (y = y + 16f)), page.Canvas.ClientSize); PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Right); format.RightToLeft = true; page.Canvas.DrawString(arabicText, trueTypeFont, brush, rctg, format); //true type font - not embedded font = new System.Drawing.Font("Batang", 14f, FontStyle.Italic); trueTypeFont = new PdfTrueTypeFont(font); page.Canvas.DrawString("Font Family: Batang - Not Embedded", trueTypeFont, brush, 0, (y = y + 16f)); //font file String fontFileName = @"..\..\..\..\..\..\Data\Hawaii_Killer.ttf"; trueTypeFont = new PdfTrueTypeFont(fontFileName, 20f); page.Canvas.DrawString("Hawaii Killer Font", trueTypeFont, brush, 0, (y = y + 16f)); page.Canvas.DrawString("Hawaii Killer Font, from http://www.1001freefonts.com", new PdfFont(PdfFontFamily.Helvetica, 8f), brush, 10, (y = y + 20f)); //cjk font PdfCjkStandardFont cjkFont = new PdfCjkStandardFont(PdfCjkFontFamily.MonotypeHeiMedium, 14f); page.Canvas.DrawString("How to say 'Font' in Chinese? \u5B57\u4F53", cjkFont, brush, 0, (y = y + 16f)); cjkFont = new PdfCjkStandardFont(PdfCjkFontFamily.HanyangSystemsGothicMedium, 14f); page.Canvas.DrawString("How to say 'Font' in Japanese? \u30D5\u30A9\u30F3\u30C8", cjkFont, brush, 0, (y = y + 16f)); cjkFont = new PdfCjkStandardFont(PdfCjkFontFamily.HanyangSystemsShinMyeongJoMedium, 14f); page.Canvas.DrawString("How to say 'Font' in Korean? \uAE00\uAF34", cjkFont, brush, 0, (y = y + 16f)); //Save pdf file. doc.SaveToFile("Font.pdf"); doc.Close(); //Launching the Pdf file. PDFDocumentViewer("Font.pdf"); }