private void button1_Click(object sender, EventArgs e) { //Create a pdf document PdfDocument doc = new PdfDocument(); //Add a new page PdfPageBase page = doc.Pages.Add(); //Create a rectangle Rectangle rect = new Rectangle(new Point(0, 0), new Size(300, 100)); //Create a brush with gradient PdfLinearGradientBrush brush = new PdfLinearGradientBrush(rect, Color.Red, Color.Blue, PdfLinearGradientMode.Horizontal); //Create a true type font with size 20f, underline style PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 20, FontStyle.Underline)); //Draw text page.Canvas.DrawString("Welcome to E-iceblue!", font, brush, new Point(0, 100)); String result = "DrawWithGradient-result.pdf"; //Save to file doc.SaveToFile(result); //Launch the Pdf file PDFDocumentViewer(result); }
private void button1_Click(object sender, EventArgs e) { //Create a pdf document. PdfDocument doc = new PdfDocument(); //margin PdfUnitConvertor unitCvtr = new PdfUnitConvertor(); PdfMargins margin = new PdfMargins(); margin.Top = unitCvtr.ConvertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point); margin.Bottom = margin.Top; margin.Left = unitCvtr.ConvertUnits(3.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point); margin.Right = margin.Left; // Create one page PdfPageBase page = doc.Pages.Add(PdfPageSize.A4, margin); float y = 10; //title PdfBrush brush1 = PdfBrushes.Black; PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 16f, FontStyle.Bold), true); PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Center); page.Canvas.DrawString("Categories List", font1, brush1, page.Canvas.ClientSize.Width / 2, y, format1); y = y + font1.MeasureString("Categories List", format1).Height; y = y + 5; RectangleF rctg = new RectangleF(new PointF(0, 0), page.Canvas.ClientSize); PdfLinearGradientBrush brush = new PdfLinearGradientBrush(rctg, Color.Navy, Color.OrangeRed, PdfLinearGradientMode.Vertical); PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 12f, PdfFontStyle.Bold); String formatted = "Beverages\nCondiments\nConfections\nDairy Products\nGrains/Cereals\nMeat/Poultry\nProduce\nSeafood"; PdfList list = new PdfList(formatted); list.Font = font; list.Indent = 8; list.TextIndent = 5; list.Brush = brush; PdfLayoutResult result = list.Draw(page, 0, y); y = result.Bounds.Bottom; PdfSortedList sortedList = new PdfSortedList(formatted); sortedList.Font = font; sortedList.Indent = 8; sortedList.TextIndent = 5; sortedList.Brush = brush; sortedList.Draw(page, 0, y); //Save pdf file. doc.SaveToFile("SimpleList.pdf"); doc.Close(); //Launching the Pdf file. PDFDocumentViewer("SimpleList.pdf"); }
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(); //margin PdfUnitConvertor unitCvtr = new PdfUnitConvertor(); PdfMargins margin = new PdfMargins(); margin.Top = unitCvtr.ConvertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point); margin.Bottom = margin.Top; margin.Left = unitCvtr.ConvertUnits(3.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point); margin.Right = margin.Left; PdfSection section = doc.Sections.Add(); section.PageSettings.Margins = margin; section.PageSettings.Size = PdfPageSize.A4; // Create one page PdfPageBase page = section.Pages.Add(); float y = 10; PdfBrush brush1 = PdfBrushes.Black; PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 12f, FontStyle.Bold), true); RectangleF rctg = new RectangleF(new PointF(0, 0), page.Canvas.ClientSize); PdfLinearGradientBrush brush2 = new PdfLinearGradientBrush(rctg, Color.Navy, Color.OrangeRed, PdfLinearGradientMode.Vertical); //draw Codabar PdfTextWidget text = new PdfTextWidget(); text.Font = font1; text.Text = "Codabar:"; PdfLayoutResult result = text.Draw(page, 0, y); page = result.Page; y = result.Bounds.Bottom + 2; PdfCodabarBarcode barcode1 = new PdfCodabarBarcode("00:12-3456/7890"); barcode1.BarcodeToTextGapHeight = 1f; barcode1.EnableCheckDigit = true; barcode1.ShowCheckDigit = true; barcode1.TextDisplayLocation = TextLocation.Bottom; barcode1.TextColor = Color.Blue; barcode1.Draw(page, new PointF(0, y)); y = barcode1.Bounds.Bottom + 5; //draw Code11Barcode text.Text = "Code11:"; result = text.Draw(page, 0, y); page = result.Page; y = result.Bounds.Bottom + 2; PdfCode11Barcode barcode2 = new PdfCode11Barcode("123-4567890"); barcode2.BarcodeToTextGapHeight = 1f; barcode2.TextDisplayLocation = TextLocation.Bottom; barcode2.TextColor = Color.Blue; barcode2.Draw(page, new PointF(0, y)); y = barcode2.Bounds.Bottom + 5; //draw Code128-A text.Text = "Code128-A:"; result = text.Draw(page, 0, y); page = result.Page; y = result.Bounds.Bottom + 2; PdfCode128ABarcode barcode3 = new PdfCode128ABarcode("HELLO 00-123"); barcode3.BarcodeToTextGapHeight = 1f; barcode3.TextDisplayLocation = TextLocation.Bottom; barcode3.TextColor = Color.Blue; barcode3.Draw(page, new PointF(0, y)); y = barcode3.Bounds.Bottom + 5; //draw Code128-B text.Text = "Code128-B:"; result = text.Draw(page, 0, y); page = result.Page; y = result.Bounds.Bottom + 2; PdfCode128BBarcode barcode4 = new PdfCode128BBarcode("Hello 00-123"); barcode4.BarcodeToTextGapHeight = 1f; barcode4.TextDisplayLocation = TextLocation.Bottom; barcode4.TextColor = Color.Blue; barcode4.Draw(page, new PointF(0, y)); y = barcode4.Bounds.Bottom + 5; //draw Code32 text.Text = "Code32:"; result = text.Draw(page, 0, y); page = result.Page; y = result.Bounds.Bottom + 2; PdfCode32Barcode barcode5 = new PdfCode32Barcode("16273849"); barcode5.BarcodeToTextGapHeight = 1f; barcode5.TextDisplayLocation = TextLocation.Bottom; barcode5.TextColor = Color.Blue; barcode5.Draw(page, new PointF(0, y)); y = barcode5.Bounds.Bottom + 5; page = section.Pages.Add(); y = 10; //draw Code39 text.Text = "Code39:"; result = text.Draw(page, 0, y); page = result.Page; y = result.Bounds.Bottom + 2; PdfCode39Barcode barcode6 = new PdfCode39Barcode("16-273849"); barcode6.BarcodeToTextGapHeight = 1f; barcode6.TextDisplayLocation = TextLocation.Bottom; barcode6.TextColor = Color.Blue; barcode6.Draw(page, new PointF(0, y)); y = barcode6.Bounds.Bottom + 5; //draw Code39-E text.Text = "Code39-E:"; result = text.Draw(page, 0, y); page = result.Page; y = result.Bounds.Bottom + 2; PdfCode39ExtendedBarcode barcode7 = new PdfCode39ExtendedBarcode("16-273849"); barcode7.BarcodeToTextGapHeight = 1f; barcode7.TextDisplayLocation = TextLocation.Bottom; barcode7.TextColor = Color.Blue; barcode7.Draw(page, new PointF(0, y)); y = barcode7.Bounds.Bottom + 5; //draw Code93 text.Text = "Code93:"; result = text.Draw(page, 0, y); page = result.Page; y = result.Bounds.Bottom + 2; PdfCode93Barcode barcode8 = new PdfCode93Barcode("16-273849"); barcode8.BarcodeToTextGapHeight = 1f; barcode8.TextDisplayLocation = TextLocation.Bottom; barcode8.TextColor = Color.Blue; barcode8.QuietZone.Bottom = 5; barcode8.Draw(page, new PointF(0, y)); y = barcode8.Bounds.Bottom + 5; //draw Code93-E text.Text = "Code93-E:"; result = text.Draw(page, 0, y); page = result.Page; y = result.Bounds.Bottom + 2; PdfCode93ExtendedBarcode barcode9 = new PdfCode93ExtendedBarcode("16-273849"); barcode9.BarcodeToTextGapHeight = 1f; barcode9.TextDisplayLocation = TextLocation.Bottom; barcode9.TextColor = Color.Blue; barcode9.Draw(page, new PointF(0, y)); y = barcode9.Bounds.Bottom + 5; //Save pdf file. doc.SaveToFile("Barcode.pdf"); doc.Close(); //Launching the Pdf file. PDFDocumentViewer("Barcode.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(); //margin PdfUnitConvertor unitCvtr = new PdfUnitConvertor(); PdfMargins margin = new PdfMargins(); margin.Top = unitCvtr.ConvertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point); margin.Bottom = margin.Top; margin.Left = unitCvtr.ConvertUnits(3.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point); margin.Right = margin.Left; // Create one page PdfPageBase page = doc.Pages.Add(PdfPageSize.A4, margin); float y = 10; //title PdfBrush brush1 = PdfBrushes.Black; PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 16f, FontStyle.Bold), true); PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Center); page.Canvas.DrawString("Categories List", font1, brush1, page.Canvas.ClientSize.Width / 2, y, format1); y = y + font1.MeasureString("Categories List", format1).Height; y = y + 5; RectangleF rctg = new RectangleF(new PointF(0, 0), page.Canvas.ClientSize); PdfLinearGradientBrush brush2 = new PdfLinearGradientBrush(rctg, Color.Navy, Color.OrangeRed, PdfLinearGradientMode.Vertical); PdfLinearGradientBrush brush3 = new PdfLinearGradientBrush(rctg, Color.OrangeRed, Color.Navy, PdfLinearGradientMode.Vertical); PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Arial", 10f), true); PdfTrueTypeFont font3 = new PdfTrueTypeFont(new Font("Arial", 8f), true); PdfOrderedMarker marker1 = new PdfOrderedMarker(PdfNumberStyle.LowerRoman, new PdfFont(PdfFontFamily.Helvetica, 10f)); PdfOrderedMarker marker2 = new PdfOrderedMarker(PdfNumberStyle.Numeric, new PdfFont(PdfFontFamily.Helvetica, 8f)); PdfSortedList vendorList = new PdfSortedList(font2); vendorList.Indent = 0; vendorList.TextIndent = 5; vendorList.Brush = brush2; vendorList.Marker = marker1; using (OleDbConnection conn = new OleDbConnection()) { conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\..\..\..\..\..\Data\demo.mdb"; OleDbCommand command = new OleDbCommand(); command.CommandText = " select VendorNo, VendorName from vendors "; command.Connection = conn; OleDbCommand command2 = new OleDbCommand(); command2.CommandText = " select Description from parts where VendorNo = @VendorNo"; command2.Connection = conn; OleDbParameter param = new OleDbParameter("@VendorNo", OleDbType.Double); command2.Parameters.Add(param); conn.Open(); OleDbDataReader reader = command.ExecuteReader(); while (reader.Read()) { double id = reader.GetDouble(0); PdfListItem item = vendorList.Items.Add(reader.GetString(1)); PdfSortedList subList = new PdfSortedList(font3); subList.Marker = marker2; subList.Brush = brush3; item.SubList = subList; subList.TextIndent = 20; command2.Parameters[0].Value = id; using (OleDbDataReader reader2 = command2.ExecuteReader()) { while (reader2.Read()) { subList.Items.Add(reader2.GetString(0)); } } String maxNumberLabel = Convert.ToString(subList.Items.Count); subList.Indent = 30 - font3.MeasureString(maxNumberLabel).Width; } } PdfTextLayout textLayout = new PdfTextLayout(); textLayout.Break = PdfLayoutBreakType.FitPage; textLayout.Layout = PdfLayoutType.Paginate; vendorList.Draw(page, new PointF(0, y), textLayout); //Save pdf file. doc.SaveToFile("List.pdf"); doc.Close(); //Launching the Pdf file. PDFDocumentViewer("List.pdf"); }
private void TekenHelePyramideOpPapier(PdfPageBase page, int[,] pyramide, int basislengte, int papierhoogte) { PdfSolidBrush blackBrush = new PdfSolidBrush(Color.Black); PdfFont helv = new PdfFont(PdfFontFamily.Helvetica, 15f); Random rnd = new Random(); int xOffset = 50; // draw the rectangles for (int rij = 0; rij <= basislengte - 1; rij++) { for (int aantal = 0; aantal <= (basislengte - 1 - rij); aantal++) { int x = aantal * 50 + 25 * rij+xOffset; int y = papierhoogte - rij * 25; int rechthook = 50; RectangleF rectangleFLine = new RectangleF(x - 6, y - 6, rechthook + 3, 25); page.Canvas.DrawRectangle(blackBrush, rectangleFLine); RectangleF rectangleF = new RectangleF(x - 4, y - 4, rechthook - 1, 22); PdfLinearGradientBrush gradBrush = new PdfLinearGradientBrush(rectangleF, new PdfRGBColor(200, 200, 200), new PdfRGBColor(200, 200, 200), PdfLinearGradientMode.Horizontal); page.Canvas.DrawRectangle(gradBrush, rectangleF); } } // draw all the numbers on the paper for (int rij = 0; rij <= basislengte - 1; rij++) { for (int aantal = 0; aantal <= (basislengte - rij - 1); aantal++) { Console.Write(pyramide[rij, aantal].ToString() + " "); int x = aantal * 50 + 25 * rij + 5 +xOffset; int y = papierhoogte - rij * 25; if (DezeCelTekenen(basislengte, rnd, rij, aantal)) { page.Canvas.DrawString(pyramide[rij, aantal].ToString(), helv, blackBrush, x, y); } } Console.WriteLine(); } }