开发者

Print Grid which generated dynamically in wpf

开发者 https://www.devze.com 2023-03-06 21:25 出处:网络
i want to print a grid which is generated dynamically. Means, in the click event of the Print Button, i m generating a grid and then i want to print that grid.

i want to print a grid which is generated dynamically.

Means, in the click event of the Print Button, i m generating a grid and then i want to print that grid.

here is my code,

private void btnPrint_Click(object sender, RoutedEventArgs e)
    {
        PrintDialog Objprint = new System.Windows.Controls.PrintDialog();
        if (Objprint.ShowDialog() == true)
        {

            System.Printing.PrintCapabilities capabilities = Objprint.PrintQueue.GetPrintCapabilities(Objprint.PrintTicket);

            double scale = Math.Min(capabilities.PageImageableArea.ExtentWidth / this.ActualWidth, capabilities.PageImageableArea.ExtentHeight / this.ActualHeight);

            #region "Make a grid For Printing"
            Grid objgrid = new Grid();
            objgrid.Name = "GridForPrinting";
            objgrid.Width = 1000;
            objgrid.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
            objgrid.VerticalAlignment = System.Windows.Vertic开发者_运维技巧alAlignment.Top;

            objgrid.RowDefinitions.Add(new RowDefinition());

            TextBlock objtext = new TextBlock();
            objtext.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
            objtext.VerticalAlignment = System.Windows.VerticalAlignment.Center;
            objtext.Text = "SUPERIOR COURT OF CALIF COUNTY OF SAN BERNARDINO";
            Grid.SetRow(objtext, 0);
            objgrid.Children.Add(objtext);

            #endregion

            Objprint.PrintVisual(objgrid, "Case Summary");
        }
    }

this code give me blank page to print.

how can i do that?


here i get answer, from the below code i get what i want to do...

void PrintOnClick(object sender, RoutedEventArgs args)
    {
        PrintDialog dlg = new PrintDialog();

        if ((bool)dlg.ShowDialog().GetValueOrDefault())
        {
            // Create Grid panel.
            Grid grid = new Grid();

            // Define 5 auto-sized rows and columns.
            for (int i = 0; i < 5; i++)
            {
                ColumnDefinition coldef = new ColumnDefinition();
                coldef.Width = GridLength.Auto;
                grid.ColumnDefinitions.Add(coldef);

                RowDefinition rowdef = new RowDefinition();
                rowdef.Height = GridLength.Auto;
                grid.RowDefinitions.Add(rowdef);
            }

            // Give the Grid a gradient brush.
            grid.Background = 
                new LinearGradientBrush(Colors.Black, Colors.White,
                                        new Point(0, 0), new Point(1, 1));

            // Every program needs some randomness.
            Random rand = new Random();

            // Fill the Grid with 25 buttons.
            for (int i = 0; i < 25; i++)
            {
                Button btn = new Button();
                btn.FontSize = 12 + rand.Next(8);
                btn.Content = "Button No. " + (i + 1);
                btn.HorizontalAlignment = HorizontalAlignment.Center;
                btn.VerticalAlignment = VerticalAlignment.Center;
                btn.Margin = new Thickness(6);
                grid.Children.Add(btn);
                Grid.SetRow(btn, i % 5);
                Grid.SetColumn(btn, i / 5);
            }

            // Size the Grid.
            grid.Measure(new Size(Double.PositiveInfinity,
                                  Double.PositiveInfinity));

            Size sizeGrid = grid.DesiredSize;

            // Determine point for centering Grid on page.
            Point ptGrid =
                new Point((dlg.PrintableAreaWidth - sizeGrid.Width) / 2,
                          (dlg.PrintableAreaHeight - sizeGrid.Height) / 2);

            // Layout pass.
            grid.Arrange(new Rect(ptGrid, sizeGrid));

            // Now print it.
            dlg.PrintVisual(grid, Title);
        }
    }


The PrintVisual print a Visual object. That means, by using the PrintVisual method, we can print any control, container, Window or user control that is in the visualtree.You cannot print a control that is not in the visualtree

0

精彩评论

暂无评论...
验证码 换一张
取 消