示例#1
0
        private async void TableFeaturesSample()
        {
            #region Field Definitions
            //Load product data.
            IEnumerable <Products> products = DataProvider.GetProducts();

            //Create a new PDF standard font
            PdfStandardFont font = new PdfStandardFont(PdfFontFamily.TimesRoman, 8f);

            PdfStandardFont smallFont = new PdfStandardFont(font, 5f);

            PdfFont bigFont = new PdfStandardFont(font, 16f);

            //Create a new PDF solid brush
            PdfBrush orangeBrush = new PdfSolidBrush(new PdfColor(247, 148, 29));

            PdfBrush grayBrush = new PdfSolidBrush(new PdfColor(170, 171, 171));

            //Create a new PDF pen
            PdfPen borderPen = new PdfPen(PdfBrushes.DarkGray, .3f);

            borderPen.LineCap = PdfLineCap.Square;

            PdfPen transparentPen = new PdfPen(PdfBrushes.Transparent, .3f);

            transparentPen.LineCap = PdfLineCap.Square;

            float margin = 40f;
            #endregion

            MemoryStream stream = new MemoryStream();

            //Create a new PDF document
            using (PdfDocument document = new PdfDocument())
            {
                //Set the margins
                document.PageSettings.Margins.All = 0;

                //Add a new PDF page
                PdfPage page = document.Pages.Add();

                //Add the document header
                AddHeader(document, "Syncfusion Essential PDF");

                //Add the document footer
                AddFooter(document, font, grayBrush);

                //Draw the text to the PDF page
                page.Graphics.DrawString("What You Get with Syncfusion", bigFont, orangeBrush, new PointF(10, 10));

                #region PdfGrid
                //Create a new PDF grid
                PdfGrid pdfGrid = new PdfGrid();

                IEnumerable <Report> report = DataProvider.GetReport();
                pdfGrid.DataSource = report;
                pdfGrid.Headers.Clear();
                pdfGrid.Columns[0].Width  = 80;
                pdfGrid.Style.Font        = font;
                pdfGrid.Style.CellSpacing = 15f;

                for (int i = 0; i < pdfGrid.Rows.Count; i++)
                {
                    if (i % 2 == 0)
                    {
                        PdfGridCell cell = pdfGrid.Rows[i].Cells[0];

                        cell.RowSpan = 2;

                        Stream imgStream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("SampleBrowser.Samples.PDF.Assets." + cell.Value.ToString().ToLower() + ".jpg");

                        //Set cell background image
                        cell.Style.BackgroundImage = new PdfBitmap(imgStream);

                        cell.Value = "";

                        cell = pdfGrid.Rows[i].Cells[1];

                        cell.Style.Font = bigFont;
                    }
                    for (int j = 0; j < pdfGrid.Columns.Count; j++)
                    {
                        pdfGrid.Rows[i].Cells[j].Style.Borders.All = transparentPen;
                    }
                }

                //Create a PDF grid layout format
                PdfGridLayoutFormat gridLayoutFormat = new PdfGridLayoutFormat();

                //Set pagination
                gridLayoutFormat.Layout = PdfLayoutType.Paginate;

                //Draw the grid to the PDF document
                pdfGrid.Draw(page, new RectangleF(new PointF(margin, 75), new SizeF(page.Graphics.ClientSize.Width - (2 * margin), page.Graphics.ClientSize.Height - margin)), gridLayoutFormat);
                #endregion

                //Save the PDF document
                document.Save(stream);
            }

            stream.Position = 0;

            if (IsToggled)
            {
                PdfViewerUI pdfViewer = new SampleBrowser.PdfViewerUI();
                pdfViewer.PdfDocumentStream = stream;
                if (Device.Idiom != TargetIdiom.Phone && Device.OS == TargetPlatform.Windows)
                {
                    await PDFViewModel.Navigation.PushModalAsync(new NavigationPage(pdfViewer));
                }
                else
                {
                    await PDFViewModel.Navigation.PushAsync(pdfViewer);
                }
            }
            else
            {
                if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
                {
                    Xamarin.Forms.DependencyService.Get <ISaveWindowsPhone>().Save("TableFeatures.pdf", "application/pdf", stream);
                }
                else
                {
                    Xamarin.Forms.DependencyService.Get <ISave>().Save("TableFeatures.pdf", "application/pdf", stream);
                }
            }
        }