Beispiel #1
0
        public PdfDocument GenerateDocument(DashPlaqueViewModel viewModel)
        {
            PdfDocument document = new PdfDocument();

            // Create an empty page
            PdfPage page = document.AddPage();

            page.Orientation = PageOrientation.Landscape;

            // Get an XGraphics object for drawing
            XGraphics gfx = XGraphics.FromPdfPage(page);

            AddImage(gfx, @".\wwwroot\images\FPER_17_DreamCruise_4C_R01.png");

            // Get the FontResolver and load fonts.
            var fontResolver = FontResolver.Get;

            // Assign it to PDFsharp.
            GlobalFontSettings.FontResolver = fontResolver;

            fontResolver.AddFont("Ubuntu", XFontStyle.Regular, @".\wwwroot\fonts\ubuntufontfamily0.80\Ubuntu-C.ttf", true, true);
            XFont labelFont = new XFont("Ubuntu", 40, XFontStyle.BoldItalic);

            //X offset, Y offset, image width, image height
            gfx.DrawString(viewModel.Year.ToString() ?? "Year", labelFont, XBrushes.Black, new XRect(100, (page.Height / 2) + 50, page.Width, page.Height), XStringFormat.TopLeft);
            gfx.DrawString(viewModel.BodyStyle ?? "Body Style", labelFont, XBrushes.Black, new XRect((page.Width - (280 + (viewModel.BodyStyle.Length * 2))), (page.Height / 2) + 50, page.Width, page.Height), XStringFormat.TopLeft);
            gfx.DrawString(viewModel.Owner ?? "Owner", labelFont, XBrushes.Black, new XRect(0, 0, page.Width - 150, page.Height - 40), XStringFormat.BottomCenter);

            return(document);
        }
        public MemoryStream Generate(DashPlaqueViewModel model)
        {
            using (var stream = new MemoryStream())
            {
                var pdf = documentService.GenerateDocument(model);

                pdf.Save(stream);
                var pdfStreamData = stream.ToArray();
                stream.Close();
                stream.Dispose();

                return(new MemoryStream(pdfStreamData, 0, pdfStreamData.Length));
            }
        }