// Helper method drawing a filled box with text: private void DrawBox(string text, GcGraphics g, RectangleF box) { g.FillRectangle(box, Color.FromArgb(80, 0, 184, 204)); g.DrawRectangle(box, Color.FromArgb(0, 193, 213), 1); box.Inflate(-6, -6); g.DrawString(text, new TextFormat() { Font = StandardFonts.Times, FontSize = 14, }, box); }
// Helper method to draw a caption beneath a shape: private void DrawCaption(GcGraphics g, PointF center, float r, string caption) { g.DrawString(caption, new TextFormat() { Font = StandardFonts.Times, FontSize = 10, }, new RectangleF(center.X - r, center.Y + r, r * 2, 24), TextAlignment.Center, ParagraphAlignment.Center, false); }
// Simple table drawing method. Returns the array of table cell rectangles. private RectangleF[,] DrawTable(PointF loc, float[] widths, float[] heights, GcGraphics g, Pen p) { if (widths.Length == 0 || heights.Length == 0) { throw new Exception("Table must have some columns and rows."); } RectangleF[,] cells = new RectangleF[widths.Length, heights.Length]; var r = new RectangleF(loc, new SizeF(widths.Sum(), heights.Sum())); // Draw left borders (except for 1st one): float x = loc.X; for (int i = 0; i < widths.Length; ++i) { for (int j = 0; j < heights.Length; ++j) { cells[i, j].X = x; cells[i, j].Width = widths[i]; } if (i > 0) { g.DrawLine(x, r.Top, x, r.Bottom, p); } x += widths[i]; } // Draw top borders (except for 1st one): float y = loc.Y; for (int j = 0; j < heights.Length; ++j) { for (int i = 0; i < widths.Length; ++i) { cells[i, j].Y = y; cells[i, j].Height = heights[j]; } if (j > 0) { g.DrawLine(r.Left, y, r.Right, y, p); } y += heights[j]; } // Draw outer border: g.DrawRectangle(r, p); // Done: return(cells); }
// Utility method which pre-pends numbers to all paragraphs in a TextLayout. private void AddBullets(GcGraphics g, PointF pt, TextLayout tl, ref int itemNo) { var tlBullet = g.CreateTextLayout(); tlBullet.DefaultFormat.Font = StandardFonts.Times; tlBullet.DefaultFormat.FontSize = 12; foreach (var line in tl.Lines) { if (line.FirstLineInParagraph) { tlBullet.Clear(); tlBullet.Append($"{itemNo++})"); tlBullet.PerformLayout(true); g.DrawTextLayout(tlBullet, new PointF(pt.X, pt.Y + line.Position + line.LineGap)); } } }
// This method shows how to create a graphics path that may be used // to fill or draw arbitrary shapes on a GcGraphics. private IPath MakeRoundRect(GcGraphics g, RectangleF rc, float rx, float ry) { var path = g.CreatePath(); var sz = new SizeF(rx, ry); // Start from horizontal top left: path.BeginFigure(new PointF(rc.Left + rx, rc.Top)); path.AddLine(new PointF(rc.Right - rx, rc.Top)); path.AddArc(new ArcSegment() { Point = new PointF(rc.Right, rc.Top + ry), SweepDirection = SweepDirection.Clockwise, Size = sz }); path.AddLine(new PointF(rc.Right, rc.Bottom - ry)); path.AddArc(new ArcSegment() { Point = new PointF(rc.Right - rx, rc.Bottom), SweepDirection = SweepDirection.Clockwise, Size = sz }); path.AddLine(new PointF(rc.Left + rx, rc.Bottom)); path.AddArc(new ArcSegment() { Point = new PointF(rc.Left, rc.Bottom - ry), SweepDirection = SweepDirection.Clockwise, Size = sz }); path.AddLine(new PointF(rc.Left, rc.Top + ry)); path.AddArc(new ArcSegment() { Point = new PointF(rc.Left + rx, rc.Top), SweepDirection = SweepDirection.Clockwise, Size = sz }); path.EndFigure(FigureEnd.Closed); return path; }
// Helper method to draw a polygon and a caption beneath it. // Can also be used to just calculate the points without actual drawing. // startAngle is for the first point, clockwise from (1,0). private PointF[] DrawPolygon(GcGraphics g, PointF center, float r, int n, float startAngle, Pen pen, string caption = null) { PointF[] pts = new PointF[n]; for (int i = 0; i < n; ++i) { pts[i] = new PointF(center.X + (float)(r * Math.Cos(startAngle + 2 * Math.PI * i / n)), center.Y + (float)(r * Math.Sin(startAngle + 2 * Math.PI * i / n))); } if (pen != null) { g.DrawPolygon(pts, pen); } if (!string.IsNullOrEmpty(caption)) { DrawCaption(g, center, r, caption); } return(pts); }
public void CreatePDF(Stream stream) { GcPdfDocument doc = new GcPdfDocument(); Page page = null; GcGraphics g = null; const float margin = 72 / 2; const float pad = 4; const float gap = 10; var ip = new PointF(margin, margin); Action newPage = () => { page = doc.NewPage(); g = page.Graphics; ip = new PointF(margin, margin); }; newPage(); var tfCaption = new TextFormat() { Font = StandardFonts.Times, FontSize = 12, }; var tfBarcode = new TextFormat() { Font = StandardFonts.Helvetica, FontSize = 9, }; GcBarcode barcode = new GcBarcode() { TextFormat = tfBarcode, ScaleFactor = 1.5f, }; barcode.Options.CaptionPosition = BarCodeCaptionPosition.Below; barcode.Options.SizeOptions.NarrowWideRatio = 0; Action <CodeType, string, string> drawBarcode = (ct_, txt_, txt2_) => { var caption = $"{ct_}:\r\n{txt_}"; if (string.IsNullOrEmpty(txt2_)) { barcode.Options.GS1Composite.Type = GS1CompositeType.None; } else { caption += $"\r\nDependent CCA: {txt2_}"; barcode.Options.GS1Composite.Type = GS1CompositeType.CCA; barcode.Options.GS1Composite.Value = txt2_; } barcode.Options.CheckSumEnabled = ct_ != CodeType.Code25intlv && ct_ != CodeType.Code_2_of_5 && ct_ != CodeType.Matrix_2_of_5; var csize = g.MeasureString(caption, tfCaption); barcode.CodeType = ct_; barcode.Text = txt_; var size = g.MeasureBarcode(barcode); size.Height = Math.Max(size.Height, csize.Height); var border = new RectangleF(ip, new SizeF(page.Size.Width - margin * 2, size.Height + pad * 2)); if (ip.Y + border.Height > page.Size.Height - margin) { newPage(); border = new RectangleF(ip, border.Size); } g.DrawRectangle(border, Color.Gray); g.DrawString(caption, tfCaption, new PointF(border.Left + pad, border.Top + pad)); g.DrawBarcode(barcode, new RectangleF(border.Right - size.Width - pad, border.Top + pad, size.Width, size.Height)); ip.Y = border.Bottom + gap; }; // drawBarcode(CodeType.Ansi39, "*GCBARCODE*", null); drawBarcode(CodeType.Ansi39x, "*GcPdf*", null); drawBarcode(CodeType.Codabar, "A12041961D", null); drawBarcode(CodeType.Code25intlv, "1234567890", null); // Interleaved 2 of 5 (ITF) drawBarcode(CodeType.Code39, "*GCBARCODE*", null); drawBarcode(CodeType.Code39x, "*GcPdf*", null); drawBarcode(CodeType.Code49, "GcBarcode+GcPdf", null); drawBarcode(CodeType.Code93x, "GcBarcode+GcPdf", null); drawBarcode(CodeType.Code_93, "GCBARCODE", null); drawBarcode(CodeType.Code_128_A, "GCPDF-2017", null); drawBarcode(CodeType.Code_128_B, "GcPdf-2017", null); drawBarcode(CodeType.Code_128_C, "1234567890", null); drawBarcode(CodeType.Code_128auto, "GcPdf-2017", null); drawBarcode(CodeType.Code_2_of_5, "1234567890", null); drawBarcode(CodeType.DataMatrix, "GcBarcode+GcPdf", null); drawBarcode(CodeType.QRCode, "GcBarcode+GcPdf", null); drawBarcode(CodeType.EAN_8, "1234567", null); drawBarcode(CodeType.EAN_13, "469" + "87654" + "3210", null); drawBarcode(CodeType.EAN128FNC1, "GcBarcode\nGcPdf", null); drawBarcode(CodeType.IntelligentMail, "00300999999000000001", null); drawBarcode(CodeType.JapanesePostal, "TOKYO-10CC-09-1978", null); drawBarcode(CodeType.PostNet, "152063949", null); drawBarcode(CodeType.RM4SCC, "SE17PB9Z", null); drawBarcode(CodeType.Matrix_2_of_5, "1234567890", null); drawBarcode(CodeType.MSI, "1234567890", null); drawBarcode(CodeType.MicroPDF417, "GcPdf", null); drawBarcode(CodeType.Pdf417, "GcPdf", null); drawBarcode(CodeType.RSS14, "1234567890", null); drawBarcode(CodeType.RSS14Stacked, "1234567890", null); drawBarcode(CodeType.RSS14Stacked, "1234567890", "12345"); drawBarcode(CodeType.RSS14StackedOmnidirectional, "1234567890", null); drawBarcode(CodeType.RSS14Truncated, "1234567890", null); drawBarcode(CodeType.RSSExpanded, "12345678901234", null); drawBarcode(CodeType.RSSExpandedStacked, "12345678901234", null); drawBarcode(CodeType.RSSLimited, "1234567890", null); drawBarcode(CodeType.RSSLimited, "1234567890", "12345"); drawBarcode(CodeType.UCCEAN128, "GcBarcode+GcPdf", null); drawBarcode(CodeType.UPC_A, "123456789012", null); drawBarcode(CodeType.UPC_E0, "123456789012", null); drawBarcode(CodeType.UPC_E1, "123456789012", null); // doc.Save(stream); }