private static void drawShot(Shot shot, XGraphics it, int xLoc, int yLoc, int targetSize, decimal zoomFactor) { //transform shot coordinates to imagebox coordinates XPoint x = transform((float)getShotX(shot), (float)getShotY(shot), targetSize, zoomFactor, xLoc, yLoc); //draw shot on target XBrush b = new XSolidBrush(XColor.FromArgb(200, 135, 206, 250)); //semitransparent shots XPen p = new XPen(XColor.FromKnownColor(XKnownColor.Blue), 0.5); XBrush bText = new XSolidBrush(XColor.FromKnownColor(XKnownColor.Blue)); it.SmoothingMode = XSmoothingMode.AntiAlias; float peletSize = getDimension(targetSize, ISSF.pelletCaliber, zoomFactor); x.X -= peletSize / 2; x.Y -= peletSize / 2; it.DrawEllipse(b, new XRect(x, new XSize(peletSize, peletSize))); it.DrawEllipse(p, new XRect(x, new XSize(peletSize, peletSize))); XStringFormat format = new XStringFormat(); format.LineAlignment = (XLineAlignment)XStringAlignment.Center; format.Alignment = XStringAlignment.Center; Font f = new Font("Arial", peletSize / 3, GraphicsUnit.World); it.DrawString((shot.index + 1).ToString(), f, bText, new XRect(x, new XSize(peletSize, peletSize)), format); }
private static void DrawCropMarks(PdfPage page, XGraphics gfx, XUnit xunitsBetweenTrimAndMediaBox) { XPoint upperLeftTrimBoxCorner = page.TrimBox.ToXRect().TopLeft; XPoint upperRightTrimBoxCorner = page.TrimBox.ToXRect().TopRight; XPoint lowerLeftTrimBoxCorner = page.TrimBox.ToXRect().BottomLeft; XPoint lowerRightTrimBoxCorner = page.TrimBox.ToXRect().BottomRight; //while blue would look nicer, then if they make color separations, the marks wouldn't show all all of them. //Note that in InDesign, there is a "registration color" which looks black but is actually 100% of all each //sep color, so it always prints. But I don't see a way to do that in PDF. //.25 is a standard width var pen = new XPen(XColor.FromKnownColor(XKnownColor.Black), .25); var gapLength = XUnit.FromMillimeter(3.175); // this 3.175 is the industry standard gfx.DrawLine(pen, upperLeftTrimBoxCorner.X - gapLength, upperLeftTrimBoxCorner.Y, upperLeftTrimBoxCorner.X - xunitsBetweenTrimAndMediaBox, upperLeftTrimBoxCorner.Y); gfx.DrawLine(pen, upperLeftTrimBoxCorner.X, upperLeftTrimBoxCorner.Y - gapLength, upperLeftTrimBoxCorner.X, upperLeftTrimBoxCorner.Y - xunitsBetweenTrimAndMediaBox); gfx.DrawLine(pen, upperRightTrimBoxCorner.X + gapLength, upperRightTrimBoxCorner.Y, upperRightTrimBoxCorner.X + xunitsBetweenTrimAndMediaBox, upperLeftTrimBoxCorner.Y); gfx.DrawLine(pen, upperRightTrimBoxCorner.X, upperRightTrimBoxCorner.Y - gapLength, upperRightTrimBoxCorner.X, upperLeftTrimBoxCorner.Y - xunitsBetweenTrimAndMediaBox); gfx.DrawLine(pen, lowerLeftTrimBoxCorner.X - gapLength, lowerLeftTrimBoxCorner.Y, lowerLeftTrimBoxCorner.X - xunitsBetweenTrimAndMediaBox, lowerLeftTrimBoxCorner.Y); gfx.DrawLine(pen, lowerLeftTrimBoxCorner.X, lowerLeftTrimBoxCorner.Y + gapLength, lowerLeftTrimBoxCorner.X, lowerLeftTrimBoxCorner.Y + xunitsBetweenTrimAndMediaBox); gfx.DrawLine(pen, lowerRightTrimBoxCorner.X + gapLength, lowerRightTrimBoxCorner.Y, lowerRightTrimBoxCorner.X + xunitsBetweenTrimAndMediaBox, lowerRightTrimBoxCorner.Y); gfx.DrawLine(pen, lowerRightTrimBoxCorner.X, lowerRightTrimBoxCorner.Y + gapLength, lowerRightTrimBoxCorner.X, lowerRightTrimBoxCorner.Y + xunitsBetweenTrimAndMediaBox); }
public void DrawHeader(XGraphics gfx) { XRect userDetailsRect = new XRect(20, page.Height * 0.05, quaterWidth, page.Height * 0.075); gfx.DrawRoundedRectangle(new XSolidBrush(XColor.FromKnownColor(XKnownColor.Gray)), userDetailsRect, new XSize(20, 20)); XRect innerRect = new XRect(userDetailsRect.X + 10, userDetailsRect.Y + 10, userDetailsRect.Height - 20, userDetailsRect.Height - 20); gfx.DrawRoundedRectangle(new XSolidBrush(XColor.FromKnownColor(XKnownColor.LightGray)), innerRect, new XSize(10, 10)); gfx.DrawString("Name : " + userProfile.Name, new XFont("Arial", 10), XBrushes.Black, 35, innerRect.Y + 10); gfx.DrawString("RB ID : " + userProfile.RBID.Replace(",", ""), new XFont("Arial", 10), XBrushes.Black, 35, innerRect.Y + 30); XImage headerImage = XImage.FromFile(@"C:\Users\kevin\Desktop\PDFsharp\samples\Samples C#\Based on WPF\HelloWorld\Content\Redback 3D logo.png"); gfx.DrawImage(headerImage, new XPoint((quaterWidth * 2.5) - (headerImage.PixelWidth / 2), page.Height * 0.075)); XSolidBrush brush = new XSolidBrush(); double score = Convert.ToDouble(userProfile.Score); if (score <= 33) { brush = new XSolidBrush(XColors.Red); } else if (score > 33 && score <= 66) { brush = new XSolidBrush(XColor.FromArgb(255, 255, 190, 0)); } else { brush = new XSolidBrush(XColors.Green); } XRect scoreRect = new XRect((quaterWidth * 3) - 20, page.Height * 0.05, quaterWidth, page.Height * 0.075); gfx.DrawRoundedRectangle(new XSolidBrush(XColor.FromKnownColor(XKnownColor.Gray)), scoreRect, new XSize(20, 20)); XRect innerRectScore = new XRect(scoreRect.X + (scoreRect.Width - 50), scoreRect.Y + 10, scoreRect.Height - 20, scoreRect.Height - 20); gfx.DrawRoundedRectangle(brush, innerRectScore, new XSize(10, 10)); gfx.DrawString("Overhead Squat : ", new XFont("Arial", 10), XBrushes.Black, scoreRect.X + 10, scoreRect.Y + 35); gfx.DrawString(userProfile.Score + "%", new XFont("Arial", 10), XBrushes.Black, innerRectScore.X + 5, innerRectScore.Y + 25); }
private void AddPage() { page = doc.AddPage(); gfx = XGraphics.FromPdfPage(page); gfx.MUH = PdfFontEncoding.Unicode; fontOptions = new XPdfFontOptions(PdfFontEncoding.Unicode); defaultFont = new XFont(InvoiceConfig.FontFamily, InvoiceConfig.FontSize, XFontStyle.Regular, fontOptions); defaultPen = new XPen(XColor.FromKnownColor(XKnownColor.Black)); }
private void DrawThemeLayer(XGraphics gfx, Map map, double scaleFactor, VectorLayer lyr) { TransformsProvider tp = new TransformsProvider(gfx); BoundingBox geombox = map.GetExtents(); FeatureDataSet fds = new FeatureDataSet(); try { lyr.DataSource.ExecuteIntersectionQuery(geombox, fds); FeatureDataTable fdt = fds.Tables[0]; for (int i = 0; i < fdt.Count; i++) { FeatureDataRow fdr = fdt[i]; VectorStyle s = (VectorStyle)lyr.Theme.GetStyle(fdr); Geometry geom = fdr.Geometry; XPen xp = new XPen(XColor.FromArgb(s.Outline.Color), s.Outline.Width); xp.DashStyle = (XDashStyle)s.Outline.DashStyle; XSolidBrush xb = null; if (geom is MultiLineString) { XGraphicsPath g = tp.TransformGeom(scaleFactor, (MultiLineString)geom, geombox); this._geomCache.Add(new PDFGeom(g, xp, null)); gfx.DrawPath(xp, g); } if (geom is MultiPolygon) { XGraphicsPath g = tp.TransformGeom(scaleFactor, (MultiPolygon)geom, geombox); if (s.Fill != null) { if ((s.Fill) is SolidBrush) { xb = new XSolidBrush(XColor.FromArgb((s.Fill as SolidBrush).Color)); } else { xb = new XSolidBrush(XColor.FromKnownColor(KnownColor.Transparent)); } this._geomCache.Add(new PDFGeom(g, xp, xb)); gfx.DrawPath(xp, xb, g); } else { this._geomCache.Add(new PDFGeom(g, xp, null)); gfx.DrawPath(xp, g); } } } } catch { //Do nothing, no render } }
void Fill() { Items.Add(new ColorItem(XColor.Empty, "custom")); XKnownColor[] knownColors = XColorResourceManager.GetKnownColors(false); int count = knownColors.Length; for (int idx = 0; idx < knownColors.Length; idx++) { XKnownColor color = knownColors[idx]; Items.Add(new ColorItem(XColor.FromKnownColor(color), _crm.ToColorName(color))); } }
public void drawLine(int pageNumber) { nowY[pageNumber - 1] = nowY[pageNumber - 1] + 5; XPen pen = new XPen(XColor.FromKnownColor(XKnownColor.Black)); PdfPage page = pages[pageNumber - 1]; // Get an XGraphics object for drawing XGraphics gfx = gfxes[pageNumber - 1]; gfx.DrawLine(pen, new Point(10, nowY[pageNumber - 1]), new Point(page.Width - 10, nowY[pageNumber - 1])); nowY[pageNumber - 1] = nowY[pageNumber - 1] + 8; }
/// <summary> /// Generates a new welcome title. /// </summary> /// <param name="y">The y position.</param> /// <param name="x">The x position.</param> /// <param name="gfx">The used graphic object.</param> /// <param name="userName">The specific username</param> /// <param name="titleFont">The font for the title.</param> /// <param name="bigFont">The font for the secondary title.</param> /// <param name="format">The used string format.</param> private static void CreateWelcomeTitle(double y, double x, XGraphics gfx, string userName, XFont titleFont, XFont bigFont, XStringFormat format) { // Welcome rectangle XRect rectWelcome = new XRect(x, y, 490, 60); gfx.DrawRectangle(XPens.Transparent, rectWelcome); // Welcome title format.LineAlignment = XLineAlignment.Near; format.Alignment = XStringAlignment.Center; gfx.DrawString($"Hello {userName}!", titleFont, new XSolidBrush(XColor.FromKnownColor(XKnownColor.DarkRed)), rectWelcome, format); format.LineAlignment = XLineAlignment.Center; gfx.DrawString("Your Jack the Clipper news arrived", bigFont, new XSolidBrush(XColor.FromKnownColor(XKnownColor.Gray)), rectWelcome, format); }
public async Task DrawImages(List <XImage> images) { while (IsDrawing) { await Task.Delay(1); } IsDrawing = true; if (images.Count + ImagesDrawn > CardsPerPage) { throw new InvalidOperationException($"Attempted to draw {images.Count + ImagesDrawn} to a page with a maximum of {Rows * Columns} images possible."); } var gutterPen = new XPen(XColor.FromKnownColor(CutLineColor), GutterThickness); foreach (XImage image in images) { // Do stuff int column = ImagesDrawn % Columns; int row = ImagesDrawn / Columns; XRect imagePlacement = await Task.Run(() => GetCardPlacement(row, column)); if (HasCutLines) { var verticalLinePlacement = new XRect( new XPoint(imagePlacement.Left - GutterThickness / 2, imagePlacement.Top - GutterThickness), new XPoint(imagePlacement.Right + GutterThickness / 2, imagePlacement.Bottom + GutterThickness)); var horizontalLinePlacement = new XRect( new XPoint(imagePlacement.Left - GutterThickness, imagePlacement.Top - GutterThickness / 2), new XPoint(imagePlacement.Right + GutterThickness, imagePlacement.Bottom + GutterThickness / 2)); await Task.Run(() => Gfx.DrawLine(gutterPen, horizontalLinePlacement.TopLeft, horizontalLinePlacement.TopRight)); await Task.Run(() => Gfx.DrawLine(gutterPen, verticalLinePlacement.TopRight, verticalLinePlacement.BottomRight)); await Task.Run(() => Gfx.DrawLine(gutterPen, horizontalLinePlacement.BottomRight, horizontalLinePlacement.BottomLeft)); await Task.Run(() => Gfx.DrawLine(gutterPen, verticalLinePlacement.BottomLeft, verticalLinePlacement.TopLeft)); } await Task.Run(() => Gfx.DrawImage(image, imagePlacement)); ImagesDrawn += 1; } IsDrawing = false; }
internal override void Render(IRenderData renderData) { if (IsNotVisible(renderData)) { return; } var bounds = GetBounds(renderData.ParentBounds); System.Drawing.Image imageData = null; try { imageData = GetImage(renderData.DocumentData, bounds); renderData.ElementBounds = GetImageBounds(imageData, bounds); if (renderData.IncludeBackground || !IsBackground) { using (var image = XImage.FromGdiPlusImage(imageData)) { renderData.Graphics.DrawImage(image, renderData.ElementBounds); } } } catch (Exception e) { var f = new Font(); var font = new XFont(f.GetName(renderData.Section), f.GetSize(renderData.Section) / 1.5, f.GetStyle(renderData.Section)); var brush = new XSolidBrush(XColor.FromKnownColor(KnownColor.Transparent)); renderData.Graphics.DrawRectangle(new XPen(f.GetColor(renderData.Section)), brush, bounds); var textBrush = new XSolidBrush(XColor.FromArgb(f.GetColor(renderData.Section))); try { var nextTop = OutputText(renderData, e.Message, font, textBrush, new XPoint(bounds.Left, bounds.Top), bounds.Width); if (e.Data.Contains("source")) { OutputText(renderData, e.Data["source"].ToString(), font, textBrush, new XPoint(bounds.Left, bounds.Top + nextTop), bounds.Width); } } catch (Exception exception) { renderData.Graphics.DrawString(exception.Message, font, brush, new XPoint(bounds.Left, bounds.Top), XStringFormats.TopLeft); } } finally { imageData?.Dispose(); } }
public void setBrush(string KnownColour) { XColor xcol = XColor.FromKnownColor((XKnownColor)Enum.Parse(typeof(XKnownColor), KnownColour)); Color col = Color.Parse(KnownColour); if (xcol != null && col != null) { xcolour = xcol; colour = col; } else { xcolour = defaultXColour; colour = defaultColour; } }
public void DrawBarCharts(XGraphics gfx) { XPoint backgroundPoint = new XPoint(20, page.Height * 0.585); XRect backgroundRect = new XRect(backgroundPoint.X, backgroundPoint.Y, page.Width - 40, page.Height * 0.41); gfx.DrawRoundedRectangle(new XSolidBrush(XColor.FromKnownColor(XKnownColor.Gray)), backgroundRect, new XSize(40, 40)); DoubleBar ShoulderFlexionBar = new DoubleBar(userParameters["LEFT Shoulder Flexion"], userParameters["RIGHT Shoulder Flexion"], gfx); ShoulderFlexionBar.Draw(new XPoint(quaterWidth * 0.5 - 10, backgroundPoint.Y + 20), backgroundRect, XImage.FromFile(@"C:\Users\kevin\Desktop\PDFsharp\samples\Samples C#\Based on WPF\HelloWorld\Content\Shoulder Flexion.png")); DoubleBar hipFlexionBar = new DoubleBar(userParameters["LEFT Hip Flexion"], userParameters["RIGHT Hip Flexion"], gfx); hipFlexionBar.Draw(new XPoint(quaterWidth * 2 - (quaterWidth * 0.5) - 10, backgroundPoint.Y + 20), backgroundRect, XImage.FromFile(@"C:\Users\kevin\Desktop\PDFsharp\samples\Samples C#\Based on WPF\HelloWorld\Content\Hip Flexion.png")); DoubleBar kneeFlexionBar = new DoubleBar(userParameters["LEFT Knee Flexion"], userParameters["RIGHT Knee Flexion"], gfx); kneeFlexionBar.Draw(new XPoint(quaterWidth * 3 - (quaterWidth * 0.5) - 10, backgroundPoint.Y + 20), backgroundRect, XImage.FromFile(@"C:\Users\kevin\Desktop\PDFsharp\samples\Samples C#\Based on WPF\HelloWorld\Content\Knee Flexion.png")); DoubleBar ankleFlexionBar = new DoubleBar(userParameters["LEFT Ankle Flexion"], userParameters["RIGHT Ankle Flexion"], gfx); ankleFlexionBar.Draw(new XPoint(quaterWidth * 4 - (quaterWidth * 0.5) - 10, backgroundPoint.Y + 20), backgroundRect, XImage.FromFile(@"C:\Users\kevin\Desktop\PDFsharp\samples\Samples C#\Based on WPF\HelloWorld\Content\Ankle Flexion.png")); XPoint top = new XPoint(backgroundPoint.X, backgroundPoint.Y + backgroundRect.Height * 0.2); XPoint bottom = new XPoint(backgroundPoint.X, backgroundPoint.Y + backgroundRect.Height * 0.8); for (int i = 11; i > 0; i--) { float increment = -i * 0.1f; XPoint percentagePoint = Interpolate(top, bottom, increment); gfx.DrawString(((11 - i) * 10).ToString() + "%", new XFont("Arial", 8), XBrushes.Black, percentagePoint + new XPoint(5, 0)); gfx.DrawLine(XPens.Black, percentagePoint, percentagePoint + new XPoint(backgroundRect.Width, 0)); } }
public override async Task <Stream> Execute(int request) { Data.Entities.Exam exam = await _dbContext.Exams .Include(e => e.ExamCategory) .Include(e => e.ExamTakers) .ThenInclude(et => et.User) .FirstOrDefaultAsync(e => e.Id == request); if (exam == null) { throw new KeyNotFoundException(); } if (exam.ExamTakers.Count() > 15) { throw new ValidationFailedException() { ValidationResult = new ValidationResult("Karty egzaminacyjne nie są dostępne gdy egzaminy ma więcej niż 15 uczestników") }; } using (PdfDocument outputDocument = new PdfDocument()) { outputDocument.Info.Title = $"IdCard_{request}"; outputDocument.Info.Author = "RecruitMe"; PdfPage page = outputDocument.AddPage(); XGraphics gfx = XGraphics.FromPdfPage(page); gfx.DrawRectangle(new XPen(XColor.FromKnownColor(XKnownColor.Black), 1.5), 10, 10, page.Width - 20, page.Height - 20); PrintHeader(gfx, exam); var examTakers = exam.ExamTakers.OrderBy(et => et.UserId).ToList(); for (int i = 0; i < examTakers.Count; i++) { AddOmrRow(i, examTakers[i], gfx); } DrawQrCode(examTakers, gfx, (int)page.Width - 130, (int)page.Height - 130, 100, 100); var stream = new MemoryStream(); outputDocument.Save(stream, false); stream.Seek(0, SeekOrigin.Begin); return(stream); } }
public void DrawAppearance(XGraphics gfx, XRect rect) { var backColor = XColor.Empty; var defaultText = string.Format("Signed by: {0}\nLocation: {1}\nReason: {2}\nDate: {3}", Signer, Location, Reason, DateTime.Now); XFont font = new XFont("Verdana", 7, XFontStyle.Regular); XTextFormatter txtFormat = new XTextFormatter(gfx); var currentPosition = new XPoint(0, 0); txtFormat.DrawString(defaultText, font, new XSolidBrush(XColor.FromKnownColor(XKnownColor.Black)), new XRect(currentPosition.X, currentPosition.Y, rect.Width - currentPosition.X, rect.Height), XStringFormats.TopLeft); }
private static void drawMeanGroup(XGraphics it, decimal currentTargetSize, decimal zoomFactor, List <Shot> shotsList, decimal rBar, decimal xBar, decimal yBar, int xLoc, int yLoc) { if (shotsList.Count >= 2) { float circle = getDimension(currentTargetSize, rBar * 2, zoomFactor); XPoint x = transform((float)xBar, (float)yBar, (float)currentTargetSize, zoomFactor, xLoc, yLoc); XPen p = new XPen(XColor.FromKnownColor(XKnownColor.Red), 0.5); it.DrawEllipse(p, x.X - (circle / 2), x.Y - (circle / 2), circle, circle); float cross = 4; // center of group cross is always the same size - 5 pixels it.DrawLine(p, x.X - cross, x.Y, x.X + cross, x.Y); it.DrawLine(p, x.X, x.Y - cross, x.X, x.Y + cross); } }
public void DrawAppearance(XGraphics gfx, XRect rect) { XColor empty = XColor.Empty; string text = "Signed by Napoleon \nLocation: Paris \nDate: " + DateTime.Now.ToString(); XFont font = new XFont("Verdana", 7.0, XFontStyle.Regular); XTextFormatter xTextFormatter = new XTextFormatter(gfx); int num = this.Image.PixelWidth / this.Image.PixelHeight; XPoint xPoint = new XPoint(0.0, 0.0); bool flag = this.Image != null; if (flag) { gfx.DrawImage(this.Image, xPoint.X, xPoint.Y, rect.Width / 4.0, rect.Width / 4.0 / (double)num); xPoint = new XPoint(rect.Width / 4.0, 0.0); } xTextFormatter.DrawString(text, font, new XSolidBrush(XColor.FromKnownColor(XKnownColor.Black)), new XRect(xPoint.X, xPoint.Y, rect.Width - xPoint.X, rect.Height), XStringFormats.TopLeft); }
private void AddOmrRow(int i, ExamTaker examTaker, XGraphics gfx) { int y = 70 + 50 * i; var text = $"{i + 1}. {examTaker.User.Name} {examTaker.User.Surname}"; XFont font = new XFont("Arial", 12, XFontStyle.Regular); gfx.DrawString(text, font, XBrushes.Gray, new XPoint(15, y)); XFont smallFont = new XFont("Arial", 5, XFontStyle.BoldItalic); for (int points = 0; points < 10; points++) { string pointsPlusOne = (points + 1).ToString(); var pointX = 15 + points * 20; var pointY = y + 15; gfx.DrawEllipse(new XPen(XColor.FromKnownColor(XKnownColor.Black), 2), pointX, pointY, 10, 10); gfx.DrawString(pointsPlusOne, smallFont, XBrushes.LightGray, new XRect(pointX, pointY, 10, 10), XStringFormats.Center); } }
private void HideAllPages(PdfDocument doc) { foreach (PdfPage page in doc.Pages) { var gfx = XGraphics.FromPdfPage(page); SquareCircleAnnotation BigWhiteBox = new SquareCircleAnnotation(true); BigWhiteBox.Opacity = 1; //BigWhiteBox.Elements["/BS"] = new PdfBorderStyle(1, PdfSharp.Pdf.BorderStyle.Solid); // Convert rectangle from world space to page space. This is necessary because the annotation is // placed relative to the bottom left corner of the page with units measured in point. BigWhiteBox.Rectangle = new PdfRectangle(gfx.Transformer.WorldToDefaultPage(new XRect(new PdfSharp.Drawing.XPoint(0, 0), gfx.PageSize))); BigWhiteBox.Color = XColor.FromKnownColor(XKnownColor.White); BigWhiteBox.FillColor = XColor.FromKnownColor(XKnownColor.White); BigWhiteBox.Flags = PdfAnnotationFlags.Print | PdfAnnotationFlags.Locked | PdfAnnotationFlags.ReadOnly; BigWhiteBox.Contents = "ScreenProtector"; page.Annotations.Add(BigWhiteBox); } }
private void button1_Click(object sender, EventArgs e) { // Create a new PDF document PdfDocument document = new PdfDocument(); document.Info.Title = "Created with PDFsharp"; // Create an empty page PdfPage page = document.AddPage(); // Get an XGraphics object for drawing XGraphics gfx = XGraphics.FromPdfPage(page); // Create a font XFont font = new XFont("Verdana", 20, XFontStyle.BoldItalic); // Draw the text gfx.DrawString("Hello, World!", font, XBrushes.Black, new XRect(0, 0, page.Width, page.Height), XStringFormats.Center); // Draw the line, it could used to draw table XPen pen = new XPen(XColor.FromKnownColor(XKnownColor.DeepPink), 1.0); gfx.DrawLine(pen, 45, 250, 45, 703); gfx.DrawLine(pen, 87, 250, 87, 703); gfx.DrawLine(pen, 150, 250, 150, 703); gfx.DrawLine(pen, 291, 250, 291, 703); gfx.DrawLine(pen, 381, 250, 381, 703); gfx.DrawLine(pen, 461, 250, 461, 703); gfx.DrawLine(pen, 571, 250, 571, 703); // Save the document... const string filename = "d:\\HelloWorld.pdf"; document.Save(filename); // ...and start a viewer. System.Diagnostics.Process.Start(filename); }
public override async Task <Stream> Execute(int request) { var user = await _dbContext.Users .Include(u => u.PersonalData) .ThenInclude(pd => pd.ProfilePictureFile) .FirstOrDefaultAsync(u => u.Id == request); if (user == null) { throw new KeyNotFoundException(); } using (PdfDocument outputDocument = new PdfDocument()) { outputDocument.Info.Title = $"IdCard_{request}"; outputDocument.Info.Author = "RecruitMe"; PdfPage page = outputDocument.AddPage(); XGraphics gfx = XGraphics.FromPdfPage(page); //Draw outline gfx.DrawRectangle(new XPen(XColor.FromKnownColor(XKnownColor.Gray), 0.7), 0, 0, 300, 400); DrawProfilePicture(user, gfx); //Draw name surname var displayName = user.Name + " " + user.Surname; XFont font = new XFont("Arial", GetFontSize(displayName.Length), XFontStyle.BoldItalic); gfx.DrawString(displayName, font, XBrushes.Black, new XRect(0, 220, 300, 20), XStringFormats.Center); DrawQrCode(user, gfx); var stream = new MemoryStream(); outputDocument.Save(stream, false); stream.Seek(0, SeekOrigin.Begin); return(stream); } }
public IActionResult GradientDocs() { var document = new PdfDocument(); using (document = new PdfDocument()) { var page = document.AddPage(); var graphics = XGraphics.FromPdfPage(page, XGraphicsPdfPageOptions.Append); var bounds = new XRect(graphics.PageOrigin, graphics.PageSize); var state = graphics.Save(); graphics.DrawRectangle( new XLinearGradientBrush(bounds, XColor.FromKnownColor(XKnownColor.Red), XColor.FromKnownColor(XKnownColor.White), XLinearGradientMode.ForwardDiagonal), bounds ); graphics.Restore(state); graphics.DrawString("Hello world", new XFont("Arial", 20), XBrushes.Black, bounds.Center, XStringFormat.Center); XPen pen = new XPen(XColors.DarkBlue, 2.5); graphics.DrawPie(pen, 10, 0, 100, 90, -120, 75); document.Save("test.pdf"); document.Close(); } MemoryStream stream = new MemoryStream(); document.Save(stream, false); stream.Position = 0; return(File(stream, "application/pdf", "test.pdf")); }
/// <summary> /// initialise the fonts and pens. /// this can only happen when the Graphics and GraphicsUnit are known. /// </summary> private void InitFontsAndPens() { FXBlackPen = new XPen(XColor.FromKnownColor(XKnownColor.Black), Cm(0.05f)); XPdfFontOptions options = new XPdfFontOptions(PdfFontEncoding.Unicode, PdfFontEmbedding.Always); // the fonts need to be a little bit bigger so that they have the same size as the GfxPrinter? FXSmallPrintFont = new XFont("Arial", 0.12, XFontStyle.Regular, options); // Point(6 + XFONTSIZE) FXDefaultFont = new XFont("Arial", 0.14, XFontStyle.Regular, options); // Point(8 + XFONTSIZE) FXDefaultBoldFont = new XFont("Arial", 0.14, XFontStyle.Bold, options); // Point(8 + XFONTSIZE) FXHeadingFont = new XFont("Arial", 0.16, XFontStyle.Bold, options); // Point(10 + XFONTSIZE) // using GPL Font Code 128 from Grand Zebu http://grandzebu.net/ FXBarCodeFont = new XFont("Code 128", 0.45, XFontStyle.Regular, options); // Point(10 + XFONTSIZE) FXBiggestLastUsedFont = FXDefaultFont; FXRight = new XStringFormat(); FXRight.Alignment = XStringAlignment.Far; FXLeft = new XStringFormat(); FXLeft.Alignment = XStringAlignment.Near; FXCenter = new XStringFormat(); FXCenter.Alignment = XStringAlignment.Center; }
void FillFields(PdfDocument document, LetterFieldsDto fields, PdfSettingDto settings, string countryInSettings, bool isEndYear = false) { PdfAcroForm form = document.AcroForm; if (form == null || fields == null) { return; } var acroFields = form.Fields; var names = acroFields.DescendantNames; if (!names.Any()) { names = acroFields.Names; } foreach (string name in names) { var t = form.Fields[name]; var text = GetValueByName(name, fields, settings, countryInSettings); t.Value = new PdfString(text); ((PdfTextField)t).BackColor = XColor.FromKnownColor(XKnownColor.White); t.ReadOnly = true; } //Ensure the new values are displayed if (document.AcroForm.Elements.ContainsKey("/NeedAppearances") == false) { document.AcroForm.Elements.Add("/NeedAppearances", new PdfBoolean(true)); } else { document.AcroForm.Elements["/NeedAppearances"] = new PdfBoolean(true); } }
/// <summary> /// Renders the content of the page. /// </summary> public void Render(XGraphics gfx) { XRect rect; XPen pen; double x = 50, y = 100; XFont fontH1 = new XFont("Times", 18, XFontStyle.Bold); XFont font = new XFont("Times", 12); XFont fontItalic = new XFont("Times", 12, XFontStyle.BoldItalic); double ls = font.GetHeight(gfx); // Draw some text gfx.DrawString("Create PDF on the fly with PDFsharp", fontH1, XBrushes.Black, x, x); gfx.DrawString("With PDFsharp you can use the same code to draw graphic, " + "text and images on different targets.", font, XBrushes.Black, x, y); y += ls; gfx.DrawString("The object used for drawing is the XGraphics object.", font, XBrushes.Black, x, y); y += 2 * ls; // Draw an arc pen = new XPen(XColors.Red, 4); pen.DashStyle = XDashStyle.Dash; gfx.DrawArc(pen, x + 20, y, 100, 60, 150, 120); // Draw a star XGraphicsState gs = gfx.Save(); gfx.TranslateTransform(x + 140, y + 30); for (int idx = 0; idx < 360; idx += 10) { gfx.RotateTransform(10); gfx.DrawLine(XPens.DarkGreen, 0, 0, 30, 0); } gfx.Restore(gs); // Draw a rounded rectangle rect = new XRect(x + 230, y, 100, 60); pen = new XPen(XColors.DarkBlue, 2.5); XColor color1 = XColor.FromKnownColor(KnownColor.DarkBlue); XColor color2 = XColors.Red; XLinearGradientBrush lbrush = new XLinearGradientBrush(rect, color1, color2, XLinearGradientMode.Vertical); gfx.DrawRoundedRectangle(pen, lbrush, rect, new XSize(10, 10)); // Draw a pie pen = new XPen(XColors.DarkOrange, 1.5); pen.DashStyle = XDashStyle.Dot; gfx.DrawPie(pen, XBrushes.Blue, x + 360, y, 100, 60, -130, 135); // Draw some more text y += 60 + 2 * ls; gfx.DrawString("With XGraphics you can draw on a PDF page as well as " + "on any System.Drawing.Graphics object.", font, XBrushes.Black, x, y); y += ls * 1.1; gfx.DrawString("Use the same code to", font, XBrushes.Black, x, y); x += 10; y += ls * 1.1; gfx.DrawString("• draw on a newly created PDF page", font, XBrushes.Black, x, y); y += ls; gfx.DrawString("• draw above or beneath of the content of an existing PDF page", font, XBrushes.Black, x, y); y += ls; gfx.DrawString("• draw in a window", font, XBrushes.Black, x, y); y += ls; gfx.DrawString("• draw on a printer", font, XBrushes.Black, x, y); y += ls; gfx.DrawString("• draw in a bitmap image", font, XBrushes.Black, x, y); x -= 10; y += ls * 1.1; gfx.DrawString("You can also import an existing PDF page and use it like " + "an image, e.g. draw it on another PDF page.", font, XBrushes.Black, x, y); y += ls * 1.1 * 2; gfx.DrawString("Imported PDF pages are neither drawn nor printed; create a " + "PDF file to see or print them!", fontItalic, XBrushes.Firebrick, x, y); y += ls * 1.1; gfx.DrawString("Below this text is a PDF form that will be visible when " + "viewed or printed with a PDF viewer.", fontItalic, XBrushes.Firebrick, x, y); y += ls * 1.1; XGraphicsState state = gfx.Save(); XRect rcImage = new XRect(100, y, 100, 100 * Math.Sqrt(2)); gfx.DrawRectangle(XBrushes.Snow, rcImage); gfx.DrawImage(XPdfForm.FromFile("../../../../../PDFs/SomeLayout.pdf"), rcImage); gfx.Restore(state); }
private static void printSeriesData(PdfPage page, int x, int y, int width, int height, List <Shot> shotList, int index, Session session) { XGraphics gfx = XGraphics.FromPdfPage(page); gfx.IntersectClip(new XRect(x, y, width, height)); XPen penBlack = new XPen(XColor.FromKnownColor(XKnownColor.Black), 0.5); XBrush brushBlack = new XSolidBrush(XColor.FromKnownColor(XKnownColor.Black)); XTextFormatter tf = new XTextFormatter(gfx); tf.Alignment = XParagraphAlignment.Left; Font fMedium = new Font("Arial", 10, GraphicsUnit.World); Font fMediumBold = new Font("Arial", 10, FontStyle.Bold, GraphicsUnit.World); XPoint point = new XPoint(x, y); point.Offset(5, 5); String s = "Series " + index; XSize rect = gfx.MeasureString(s, fMediumBold); tf.DrawString(s, fMediumBold, brushBlack, new XRect(point, rect)); //new line point = new XPoint(x + 5, point.Y + rect.Height + 5); s = "Score:"; rect = gfx.MeasureString(s, fMediumBold); tf.DrawString(s, fMediumBold, brushBlack, new XRect(point, rect)); int X; point.Offset(rect.Width + 2, 0); decimal score = sumScores(session, shotList, out X); if (session.decimalScoring) { s = score.ToString("F1", CultureInfo.InvariantCulture) + " - " + X + "x"; } else { s = score.ToString("F0", CultureInfo.InvariantCulture) + " - " + X + "x"; } rect = gfx.MeasureString(s, fMedium); tf.DrawString(s, fMedium, brushBlack, new XRect(point, rect)); //new line point = new XPoint(x + 5, point.Y + rect.Height + 5); s = "Shots:"; rect = gfx.MeasureString(s, fMediumBold); tf.DrawString(s, fMediumBold, brushBlack, new XRect(point, rect)); point.Offset(rect.Width + 2, 0); s = ""; foreach (Shot shot in shotList) { string t = shot.innerTen ? "*" : ""; if (session.decimalScoring) { s += shot.decimalScore.ToString("F1", CultureInfo.InvariantCulture) + t + " "; } else { s += shot.score + t + " "; } } rect = gfx.MeasureString(s, fMedium); tf.DrawString(s, fMedium, brushBlack, new XRect(point, rect)); //new line point = new XPoint(x + 5, point.Y + rect.Height + 5); s = "Average score:"; rect = gfx.MeasureString(s, fMediumBold); tf.DrawString(s, fMediumBold, brushBlack, new XRect(point, rect)); point.Offset(rect.Width + 1, 0); decimal avg = score / shotList.Count; s = avg.ToString("F2", CultureInfo.InvariantCulture); rect = gfx.MeasureString(s, fMedium); tf.DrawString(s, fMedium, brushBlack, new XRect(point, rect)); decimal localRbar; decimal localXbar; decimal localYbar; computeMeans(out localRbar, out localXbar, out localYbar, shotList); //new line point = new XPoint(x + 5, point.Y + rect.Height + 5); s = "MPI: "; rect = gfx.MeasureString(s, fMediumBold); tf.DrawString(s, fMediumBold, brushBlack, new XRect(point, rect)); point.Offset(rect.Width + 1, 0); string W, E; if (localXbar < 0) { W = "\u2190"; } else { W = "\u2192"; } if (localYbar < 0) { E = "\u2193"; } else { E = "\u2191"; } s = W + Math.Abs(localXbar).ToString("F2", CultureInfo.InvariantCulture) + "mm" + " " + E + Math.Abs(localYbar).ToString("F2", CultureInfo.InvariantCulture) + "mm"; rect = gfx.MeasureString(s, fMedium); tf.DrawString(s, fMedium, brushBlack, new XRect(point, rect)); point.Offset(rect.Width + 5, 0); s = "MR:"; rect = gfx.MeasureString(s, fMediumBold); tf.DrawString(s, fMediumBold, brushBlack, new XRect(point, rect)); point.Offset(rect.Width + 1, 0); s = localRbar.ToString("F2", CultureInfo.InvariantCulture) + "mm"; rect = gfx.MeasureString(s, fMedium); tf.DrawString(s, fMedium, brushBlack, new XRect(point, rect)); point.Offset(rect.Width + 5, 0); s = "Group size:"; rect = gfx.MeasureString(s, fMediumBold); tf.DrawString(s, fMediumBold, brushBlack, new XRect(point, rect)); point.Offset(rect.Width + 1, 0); s = calculateMaxSpread(shotList).ToString("F2", CultureInfo.InvariantCulture) + "mm"; rect = gfx.MeasureString(s, fMedium); tf.DrawString(s, fMedium, brushBlack, new XRect(point, rect)); //new line point = new XPoint(x + 5, point.Y + rect.Height + 5); s = "First shot:"; rect = gfx.MeasureString(s, fMediumBold); tf.DrawString(s, fMediumBold, brushBlack, new XRect(point, rect)); point.Offset(rect.Width + 1, 0); s = shotList[0].timestamp.ToString("hh:mm:ss"); rect = gfx.MeasureString(s, fMedium); tf.DrawString(s, fMedium, brushBlack, new XRect(point, rect)); point.Offset(rect.Width + 5, 0); s = "Last shot:"; rect = gfx.MeasureString(s, fMediumBold); tf.DrawString(s, fMediumBold, brushBlack, new XRect(point, rect)); point.Offset(rect.Width + 1, 0); s = shotList[shotList.Count - 1].timestamp.ToString("hh:mm:ss"); rect = gfx.MeasureString(s, fMedium); tf.DrawString(s, fMedium, brushBlack, new XRect(point, rect)); TimeSpan shortest; TimeSpan longest; TimeSpan avgTime; calculateTimeStats(out shortest, out longest, out avgTime, shotList); //new line point = new XPoint(x + 5, point.Y + rect.Height + 5); s = "Avg Shot time:"; rect = gfx.MeasureString(s, fMediumBold); tf.DrawString(s, fMediumBold, brushBlack, new XRect(point, rect)); point.Offset(rect.Width + 1, 0); s = avgTime.TotalSeconds.ToString("F2", CultureInfo.InvariantCulture) + "s"; rect = gfx.MeasureString(s, fMedium); tf.DrawString(s, fMedium, brushBlack, new XRect(point, rect)); point.Offset(rect.Width + 5, 0); s = "Shortest:"; rect = gfx.MeasureString(s, fMediumBold); tf.DrawString(s, fMediumBold, brushBlack, new XRect(point, rect)); point.Offset(rect.Width + 1, 0); s = shortest.TotalSeconds.ToString("F2", CultureInfo.InvariantCulture) + "s"; rect = gfx.MeasureString(s, fMedium); tf.DrawString(s, fMedium, brushBlack, new XRect(point, rect)); point.Offset(rect.Width + 5, 0); s = "Longest:"; rect = gfx.MeasureString(s, fMediumBold); tf.DrawString(s, fMediumBold, brushBlack, new XRect(point, rect)); point.Offset(rect.Width + 1, 0); s = longest.TotalSeconds.ToString("F2", CultureInfo.InvariantCulture) + "s"; rect = gfx.MeasureString(s, fMedium); tf.DrawString(s, fMedium, brushBlack, new XRect(point, rect)); gfx.DrawRectangle(penBlack, x, y, width, height); gfx.Dispose(); }
private static void drawBreakdownGraph(XGraphics gfx, double x, double y, double width, double height, Session session) { XPen penBlack = new XPen(XColor.FromKnownColor(XKnownColor.Black), 0.5); XBrush brushBlack = new XSolidBrush(XColor.FromKnownColor(XKnownColor.Black)); gfx.DrawRectangle(penBlack, x, y, width, height); Font f1 = new Font("Arial", 8, GraphicsUnit.World); Font f2 = new Font("Arial", 5, GraphicsUnit.World); List <int> breakdown = new List <int> { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; foreach (Shot s in session.Shots) { if (s.score == 10) { if (s.innerTen) { breakdown[11]++; } else { breakdown[s.score]++; } } else { breakdown[s.score]++; } } int bigest = -100; double offset = x + 7; for (int i = breakdown.Count - 1; i >= 0; i--) { int c = breakdown[i]; if (c > bigest) { bigest = c; } XPoint point = new XPoint(offset, y + height - 10); string cs = c.ToString(); if (c == 0) { cs = "-"; } gfx.DrawString(cs, f1, brushBlack, point); XPoint point2 = new XPoint(offset + 1, y + height - 3); cs = i.ToString(); if (i == 11) { cs = "X"; } gfx.DrawString(cs, f2, brushBlack, point2); offset += width / 12; } double tallest = height - 30; offset = x + 7; for (int i = breakdown.Count - 1; i >= 0; i--) { int c = breakdown[i]; double h = (c * tallest) / bigest; gfx.DrawRectangle(brushBlack, new XRect(new XPoint(offset - 2, y + height - 25), new XPoint(offset + 8, y + height - 25 - h))); offset += width / 12; } }
private static void printSessionData(PdfPage page, int x, int y, int width, int height, Session session) { XGraphics gfx = XGraphics.FromPdfPage(page); gfx.IntersectClip(new XRect(x, y, width, height)); XPen penBlack = new XPen(XColor.FromKnownColor(XKnownColor.Black), 0.5); XBrush brushBlack = new XSolidBrush(XColor.FromKnownColor(XKnownColor.Black)); XTextFormatter tf = new XTextFormatter(gfx); tf.Alignment = XParagraphAlignment.Left; Font fLarge = new Font("Arial", 12, FontStyle.Bold, GraphicsUnit.World); Font fMedium = new Font("Arial", 10, GraphicsUnit.World); Font fMediumBold = new Font("Arial", 10, FontStyle.Bold, GraphicsUnit.World); Font fSmall = new Font("Arial", 8, GraphicsUnit.World); String s = "Match summary"; XPoint point = new XPoint(x, y); point.Offset(5, 5); s = "Session ID:"; XSize rect = gfx.MeasureString(s, fMediumBold); tf.DrawString(s, fMediumBold, brushBlack, new XRect(point, rect)); point.Offset(rect.Width + 5, 0); s = session.id.ToString(); rect = gfx.MeasureString(s, fMedium); tf.DrawString(s, fMedium, brushBlack, new XRect(point, rect)); //new line point = new XPoint(x, y); point.Offset(5, 5); point.Offset(0, rect.Height + 5); s = "Event:"; rect = gfx.MeasureString(s, fMediumBold); tf.DrawString(s, fMediumBold, brushBlack, new XRect(point, rect)); point.Offset(rect.Width + 5, 0); s = session.eventType.Name; if (session.numberOfShots > 0) { s += " " + session.numberOfShots; } rect = gfx.MeasureString(s, fMedium); tf.DrawString(s, fMedium, brushBlack, new XRect(point, rect)); //new line point = new XPoint(x + 5, point.Y + rect.Height + 5); s = "Shooter:"; rect = gfx.MeasureString(s, fMediumBold); tf.DrawString(s, fMediumBold, brushBlack, new XRect(point, rect)); point.Offset(rect.Width + 5, 0); s = session.user; rect = gfx.MeasureString(s, fMedium); tf.DrawString(s, fMedium, brushBlack, new XRect(point, rect)); //new line point = new XPoint(x + 5, point.Y + rect.Height + 5); s = "Start Time: "; rect = gfx.MeasureString(s, fMediumBold); tf.DrawString(s, fMediumBold, brushBlack, new XRect(point, rect)); point.Offset(rect.Width, 0); s = session.startTime.ToString("yyyy-MM-dd hh:mm:ss"); rect = gfx.MeasureString(s, fMedium); tf.DrawString(s, fMedium, brushBlack, new XRect(point, rect)); //new line point = new XPoint(x + 5, point.Y + rect.Height + 5); s = "End Time: "; rect = gfx.MeasureString(s, fMediumBold); tf.DrawString(s, fMediumBold, brushBlack, new XRect(point, rect)); point.Offset(rect.Width, 0); s = session.endTime.ToString("yyyy-MM-dd hh:mm:ss"); rect = gfx.MeasureString(s, fMedium); tf.DrawString(s, fMedium, brushBlack, new XRect(point, rect)); point.Offset(rect.Width + 15, 0); s = "Duration:"; rect = gfx.MeasureString(s, fMediumBold); tf.DrawString(s, fMediumBold, brushBlack, new XRect(point, rect)); point.Offset(rect.Width + 1, 0); s = (session.endTime - session.startTime).TotalMinutes.ToString("F2", CultureInfo.InvariantCulture) + " mins"; rect = gfx.MeasureString(s, fMedium); tf.DrawString(s, fMedium, brushBlack, new XRect(point, rect)); //new line point = new XPoint(x + 5, point.Y + rect.Height + 5); s = "Avg Shot time:"; rect = gfx.MeasureString(s, fMediumBold); tf.DrawString(s, fMediumBold, brushBlack, new XRect(point, rect)); point.Offset(rect.Width + 1, 0); s = session.averageTimePerShot.TotalSeconds.ToString("F2", CultureInfo.InvariantCulture) + "s"; rect = gfx.MeasureString(s, fMedium); tf.DrawString(s, fMedium, brushBlack, new XRect(point, rect)); point.Offset(rect.Width + 5, 0); s = "Shortest:"; rect = gfx.MeasureString(s, fMediumBold); tf.DrawString(s, fMediumBold, brushBlack, new XRect(point, rect)); point.Offset(rect.Width + 1, 0); s = session.shortestShot.TotalSeconds.ToString("F2", CultureInfo.InvariantCulture) + "s"; rect = gfx.MeasureString(s, fMedium); tf.DrawString(s, fMedium, brushBlack, new XRect(point, rect)); point.Offset(rect.Width + 5, 0); s = "Longest:"; rect = gfx.MeasureString(s, fMediumBold); tf.DrawString(s, fMediumBold, brushBlack, new XRect(point, rect)); point.Offset(rect.Width + 1, 0); s = session.longestShot.TotalSeconds.ToString("F2", CultureInfo.InvariantCulture) + "s"; rect = gfx.MeasureString(s, fMedium); tf.DrawString(s, fMedium, brushBlack, new XRect(point, rect)); //new line point = new XPoint(x + 5, point.Y + rect.Height + 5); s = "MPI: "; rect = gfx.MeasureString(s, fMediumBold); tf.DrawString(s, fMediumBold, brushBlack, new XRect(point, rect)); point.Offset(rect.Width + 1, 0); string W, E; if (session.xbar < 0) { W = "\u2190"; } else { W = "\u2192"; } if (session.ybar < 0) { E = "\u2193"; } else { E = "\u2191"; } s = W + Math.Abs(session.xbar).ToString("F2", CultureInfo.InvariantCulture) + "mm" + " " + E + Math.Abs(session.ybar).ToString("F2", CultureInfo.InvariantCulture) + "mm"; rect = gfx.MeasureString(s, fMedium); tf.DrawString(s, fMedium, brushBlack, new XRect(point, rect)); point.Offset(rect.Width + 5, 0); s = "MR:"; rect = gfx.MeasureString(s, fMediumBold); tf.DrawString(s, fMediumBold, brushBlack, new XRect(point, rect)); point.Offset(rect.Width + 1, 0); s = session.rbar.ToString("F2", CultureInfo.InvariantCulture) + "mm"; rect = gfx.MeasureString(s, fMedium); tf.DrawString(s, fMedium, brushBlack, new XRect(point, rect)); point.Offset(rect.Width + 5, 0); s = "Group size:"; rect = gfx.MeasureString(s, fMediumBold); tf.DrawString(s, fMediumBold, brushBlack, new XRect(point, rect)); point.Offset(rect.Width + 1, 0); s = session.groupSize.ToString("F2", CultureInfo.InvariantCulture) + "mm"; rect = gfx.MeasureString(s, fMedium); tf.DrawString(s, fMedium, brushBlack, new XRect(point, rect)); //new line point = new XPoint(x + 5, point.Y + rect.Height + 5); s = "Total Score:"; rect = gfx.MeasureString(s, fMediumBold); tf.DrawString(s, fMediumBold, brushBlack, new XRect(point, rect)); point.Offset(rect.Width + 5, 0); if (session.decimalScoring) { s = session.decimalScore.ToString("F1", CultureInfo.InvariantCulture) + " - " + session.innerX + "x"; } else { s = session.score.ToString() + " - " + session.innerX + "x"; } rect = gfx.MeasureString(s, fMedium); tf.DrawString(s, fMedium, brushBlack, new XRect(point, rect)); point.Offset(rect.Width + 10, 0); s = "Average score:"; rect = gfx.MeasureString(s, fMediumBold); tf.DrawString(s, fMediumBold, brushBlack, new XRect(point, rect)); point.Offset(rect.Width + 1, 0); s = session.averageScore.ToString("F2", CultureInfo.InvariantCulture); rect = gfx.MeasureString(s, fMedium); tf.DrawString(s, fMedium, brushBlack, new XRect(point, rect)); //new line point = new XPoint(x + 5, point.Y + rect.Height + 5); s = "Number of shots in session:"; rect = gfx.MeasureString(s, fMediumBold); tf.DrawString(s, fMediumBold, brushBlack, new XRect(point, rect)); point.Offset(rect.Width + 2, 0); s = session.actualNumberOfShots.ToString(); rect = gfx.MeasureString(s, fMedium); tf.DrawString(s, fMedium, brushBlack, new XRect(point, rect)); //new line point = new XPoint(x + 5, point.Y + rect.Height + 5); s = "Series:"; rect = gfx.MeasureString(s, fMediumBold); tf.DrawString(s, fMediumBold, brushBlack, new XRect(point, rect)); point.Offset(rect.Width + 2, 0); s = ""; int X; foreach (List <Shot> shots in session.AllSeries) { if (session.decimalScoring) { s += sumScores(session, shots, out X).ToString("F1", CultureInfo.InvariantCulture) + " "; } else { s += sumScores(session, shots, out X).ToString("F0", CultureInfo.InvariantCulture) + " "; } } rect = gfx.MeasureString(s, fMedium); tf.DrawString(s, fMedium, brushBlack, new XRect(point, rect)); //new line point = new XPoint(x + 5, point.Y + rect.Height + 5); drawBreakdownGraph(gfx, point.X, point.Y, width - 10, height - point.Y + 15, session); gfx.DrawRectangle(penBlack, x, y, width, height); gfx.Dispose(); }
public static void generateAndSavePDF(Session session, string savePath, Stream imageStream) { //prepare session (load shots into series) session.AllSeries.Clear(); foreach (Shot s in session.Shots) { session.addLoadedShot(s); } PdfDocument document = new PdfDocument(); document.Info.Title = "Session " + session.id; PdfPage firstPage = document.AddPage(); int pageNo = 1; //dimensions: 595 x 792 - good both for A4 and for Letter int blackRings = 0; bool solidInner = false; decimal[] rings = null; if (session.targetType == Session.TargetType.Pistol) { blackRings = ISSF.pistolBlackRings; rings = ISSF.ringsPistol; solidInner = false; } else if (session.targetType == Session.TargetType.Rifle) { blackRings = ISSF.rifleBlackRings; rings = ISSF.ringsRifle; solidInner = true; } else { Console.WriteLine("WTF"); } XPen penBlack = new XPen(XColor.FromKnownColor(XKnownColor.Black), 0.75); XBrush brushBlack = new XSolidBrush(XColor.FromKnownColor(XKnownColor.Black)); Font f = new Font("Arial", 12, GraphicsUnit.World); //session target if (session.targetType == Session.TargetType.Pistol) { paintTarget(250, 20, 20, blackRings, rings, 1, solidInner, session.Shots, firstPage); } else { paintTarget(250, 20, 20, blackRings, rings, 0.29m, solidInner, session.Shots, firstPage); } //session data printSessionData(firstPage, 280, 20, 295, 250, session); //draw series header XGraphics gfx = XGraphics.FromPdfPage(firstPage); gfx.DrawLine(penBlack, 20, 290, 575, 290); gfx.DrawString("Series (" + session.AllSeries.Count + ")", f, brushBlack, 25, 285); //draw footer XBrush brushLight = new XSolidBrush(XColor.FromKnownColor(XKnownColor.Linen)); gfx.DrawRectangle(brushLight, 20, 770, 555, 10); Font f2 = new Font("Arial", 6, GraphicsUnit.World); gfx.DrawString("Page " + pageNo, f2, brushBlack, new XPoint(550, 777)); XImage img = XImage.FromStream(imageStream); gfx.DrawImage(img, new XPoint(22, 771)); gfx.Dispose(); //series 1 if (session.AllSeries.Count > 0) { int y = 295; decimal zoom2 = getZoom(session.AllSeries[0], session.targetType); paintTarget(150, 20, y, blackRings, rings, zoom2, solidInner, session.AllSeries[0], firstPage); printSeriesData(firstPage, 180, y, 395, 150, session.AllSeries[0], 1, session); } //series 2 if (session.AllSeries.Count > 1) { int y = 455; decimal zoom2 = getZoom(session.AllSeries[1], session.targetType); paintTarget(150, 20, y, blackRings, rings, zoom2, solidInner, session.AllSeries[1], firstPage); printSeriesData(firstPage, 180, y, 395, 150, session.AllSeries[1], 2, session); } //series 3 if (session.AllSeries.Count > 2) { int y = 615; decimal zoom2 = getZoom(session.AllSeries[2], session.targetType); paintTarget(150, 20, y, blackRings, rings, zoom2, solidInner, session.AllSeries[2], firstPage); printSeriesData(firstPage, 180, y, 395, 150, session.AllSeries[2], 3, session); } if (session.AllSeries.Count > 3) { //more pages needed PdfPage page = null; int y = 20; for (int p = 3; p < session.AllSeries.Count; p++) { if ((p + 1) % 4 == 0) { page = document.AddPage(); pageNo++; y = 20; } decimal zoom2 = getZoom(session.AllSeries[p], session.targetType); paintTarget(150, 20, y, blackRings, rings, zoom2, solidInner, session.AllSeries[p], page); printSeriesData(page, 180, y, 395, 150, session.AllSeries[p], p + 1, session); y += 170; //draw footer gfx = XGraphics.FromPdfPage(page); gfx.DrawRectangle(brushLight, 20, 770, 555, 10); gfx.DrawString("Page " + pageNo, f2, brushBlack, new XPoint(550, 777)); gfx.DrawImage(img, new XPoint(22, 771)); gfx.Dispose(); } } PdfPage notes = document.AddPage(); pageNo++; gfx = XGraphics.FromPdfPage(notes); RichTextBox r = new RichTextBox(); r.Rtf = session.diaryEntry; gfx.DrawLine(penBlack, 20, 30, 575, 30); gfx.DrawString("Diary", f, brushBlack, 30, 25); XTextFormatter tf = new XTextFormatter(gfx); tf.Alignment = XParagraphAlignment.Left; f = new Font("Arial", 12, GraphicsUnit.World); tf.DrawString(r.Text, f, brushBlack, new XRect(25, 40, 575, 782)); gfx.DrawRectangle(brushLight, 20, 770, 555, 10); gfx.DrawString("Page " + pageNo, f2, brushBlack, new XPoint(550, 777)); gfx.DrawImage(img, new XPoint(22, 771)); gfx.Dispose(); string filename = savePath + Path.DirectorySeparatorChar + "Session " + session.id + ".pdf"; try { document.Save(filename); Process.Start(filename); } catch (Exception ex) { MessageBox.Show("Error saving pdf! " + Environment.NewLine + ex.Message, "PDF Export Error", MessageBoxButtons.OK, MessageBoxIcon.Error); Console.WriteLine("Error saving pdf " + ex.Message); } }
private static void paintTarget(int dimension, int xLoc, int yLoc, int blackRingCutoff, decimal[] rings, decimal zoomFactor, bool solidInner, List <Shot> shotsList, PdfPage page) { XPen penBlack = new XPen(XColor.FromKnownColor(XKnownColor.Black), 0.5); XPen penWhite = new XPen(XColor.FromKnownColor(XKnownColor.Linen), 0.5); XBrush brushBlack = new XSolidBrush(XColor.FromKnownColor(XKnownColor.Black)); XBrush brushWhite = new XSolidBrush(XColor.FromKnownColor(XKnownColor.Linen)); XGraphics gfx = XGraphics.FromPdfPage(page); gfx.IntersectClip(new XRect(xLoc, yLoc, dimension, dimension)); gfx.SmoothingMode = XSmoothingMode.AntiAlias; gfx.DrawRectangle(brushWhite, xLoc, yLoc, dimension, dimension); int r = 1; for (int i = 0; i < rings.Length; i++) { XPen p; XBrush b; XBrush bText; if (r < blackRingCutoff) { p = penBlack; b = brushWhite; bText = brushBlack; } else { p = penWhite; b = brushBlack; bText = brushWhite; } float circle = getDimension(dimension, rings[i], zoomFactor); float centerX = (dimension / 2) + xLoc; float centerY = (dimension / 2) + yLoc; float x = centerX - (circle / 2); float y = centerY - (circle / 2); float xInvers = centerX + (circle / 2); float yInvers = centerY + (circle / 2); if (solidInner && i == rings.Length - 1) //rifle target - last ring (10) is a solid dot { gfx.DrawEllipse(brushWhite, x, y, circle, circle); } else { gfx.DrawEllipse(b, x, y, circle, circle); gfx.DrawEllipse(p, x, y, circle, circle); } if (r < 9) //for ring 9 and after no text is displayed { float nextCircle = getDimension(dimension, rings[i + 1], zoomFactor); float diff = circle - nextCircle; float fontSize = diff / 6f; Font f = new Font("Arial", fontSize, GraphicsUnit.World); XStringFormat format = new XStringFormat(); format.LineAlignment = (XLineAlignment)XStringAlignment.Center; format.Alignment = XStringAlignment.Center; gfx.DrawString(r.ToString(), f, bText, centerX, y + (diff / 4), format); gfx.DrawString(r.ToString(), f, bText, centerX, yInvers - (diff / 4), format); gfx.DrawString(r.ToString(), f, bText, x + (diff / 4), centerY, format); gfx.DrawString(r.ToString(), f, bText, xInvers - (diff / 4), centerY, format); } r++; } gfx.DrawRectangle(penBlack, xLoc, yLoc, dimension, dimension); foreach (Shot shot in shotsList) { drawShot(shot, gfx, xLoc, yLoc, dimension, zoomFactor); } decimal localRbar; decimal localXbar; decimal localYbar; computeMeans(out localRbar, out localXbar, out localYbar, shotsList); drawMeanGroup(gfx, dimension, zoomFactor, shotsList, localRbar, localXbar, localYbar, xLoc, yLoc); gfx.Dispose(); }
// Draw information and products onto PDF. public void DrawPDF(XGraphics gfx, string dateAndNumber) { // Create fonts. XFont fontTitle = new XFont("Verdana", 15, XFontStyle.Bold); XFont fontText = new XFont("Verdana", 10, XFontStyle.Regular); XFont fontBoldText = new XFont("Verdana", 10, XFontStyle.Bold); XPen pen = new XPen(XColor.FromKnownColor(KnownColor.Black)); // Draw outlines. gfx.DrawString(dateAndNumber, fontBoldText, XBrushes.Black, 60, 40); gfx.DrawLine(pen, new Point(40, 60), new Point(555, 60)); gfx.DrawLine(pen, new Point(40, 280), new Point(555, 280)); gfx.DrawLine(pen, new Point(297, 60), new Point(297, 280)); gfx.DrawString("Offerte", fontTitle, XBrushes.Black, 267, 300); gfx.DrawLine(pen, new Point(40, 310), new Point(555, 310)); gfx.DrawString("Producten", fontTitle, XBrushes.Black, 60, 335); gfx.DrawString("Naam", fontBoldText, XBrushes.Black, 60, 355); gfx.DrawString("Aantal", fontBoldText, XBrushes.Black, 350, 355); gfx.DrawString("Prijs", fontBoldText, XBrushes.Black, 425, 355); gfx.DrawString("Totaal", fontBoldText, XBrushes.Black, 500, 355); // Draw customer info. gfx.DrawString("Klant:", fontTitle, XBrushes.Black, 60, 90); gfx.DrawString(customer.companyName, fontText, XBrushes.Black, 60, 110); gfx.DrawString(customer.name, fontText, XBrushes.Black, 60, 130); gfx.DrawString(customer.postalCode, fontText, XBrushes.Black, 60, 150); gfx.DrawString(customer.location, fontText, XBrushes.Black, 60, 170); gfx.DrawString(customer.adres, fontText, XBrushes.Black, 60, 190); gfx.DrawString(customer.phonenumber, fontText, XBrushes.Black, 60, 210); gfx.DrawString(customer.email, fontText, XBrushes.Black, 60, 230); gfx.DrawString(customer.kvkNumber, fontText, XBrushes.Black, 60, 250); gfx.DrawString(customer.iban, fontText, XBrushes.Black, 60, 270); // Draw user info. gfx.DrawString("Vertegenwoordiger:", fontTitle, XBrushes.Black, 317, 90); gfx.DrawString(representative.companyName, fontText, XBrushes.Black, 317, 110); gfx.DrawString(representative.postalCode, fontText, XBrushes.Black, 317, 130); gfx.DrawString(representative.location, fontText, XBrushes.Black, 317, 150); gfx.DrawString(representative.phonenumber.ToString(), fontText, XBrushes.Black, 317, 170); gfx.DrawString(representative.emailadres, fontText, XBrushes.Black, 317, 190); gfx.DrawString(representative.kvkNumber.ToString(), fontText, XBrushes.Black, 317, 210); gfx.DrawString(representative.iban, fontText, XBrushes.Black, 317, 230); // Draw products. if (productList != null) { var sorted = productList.GroupBy(x => x.productId) .Select(g => new { Value = g.Key, Count = g.Count(), Name = productList.Find(v => v.productId == g.Key).name, Price = productList.Find(v => v.productId == g.Key).price }) .OrderByDescending(x => x.Count); int productY = 375; double totPrijs = 0; foreach (var p in sorted) { // Draw name. gfx.DrawString(p.Name, fontText, XBrushes.Black, 60, productY); // Draw number. gfx.DrawString(p.Count.ToString(), fontText, XBrushes.Black, 350, productY); // Draw price. gfx.DrawString("€" + p.Price, fontText, XBrushes.Black, 425, productY); // Draw total. gfx.DrawString("€" + (p.Count * p.Price), fontText, XBrushes.Black, 500, productY); productY += 20; totPrijs += p.Count * p.Price; } if (room != null) { // Draw floor. double squareM = 0; // Calculate square meters of room if (room.shape == "Circle") { squareM = Math.Round((Math.PI * (room.width / 2) * (room.width / 2) / 10000), 0); } else { squareM = (room.width / 100) * (room.height / 100); } // Calculate price. double prijs = Math.Round((squareM * room.floorType.price), 2); // Add to total price. totPrijs += prijs; gfx.DrawString("Vloer", fontBoldText, XBrushes.Black, 60, productY + 20); gfx.DrawString("Aantal m2", fontBoldText, XBrushes.Black, 350, productY + 20); gfx.DrawString("Prijs/m2", fontBoldText, XBrushes.Black, 425, productY + 20); gfx.DrawString("Totaal", fontBoldText, XBrushes.Black, 500, productY + 20); gfx.DrawString(room.floorType.name, fontText, XBrushes.Black, 60, productY + 40); gfx.DrawString(squareM.ToString(), fontText, XBrushes.Black, 350, productY + 40); gfx.DrawString("€" + room.floorType.price.ToString(), fontText, XBrushes.Black, 425, productY + 40); gfx.DrawString("€" + prijs.ToString(), fontText, XBrushes.Black, 500, productY + 40); } //Draw total price. gfx.DrawLine(pen, new Point(40, productY + 50), new Point(555, productY + 50)); gfx.DrawString(String.Format("Totaalprijs: €{0}", totPrijs), fontTitle, XBrushes.Black, 385, productY + 70); } }