Click here to Skip to main content
15,891,976 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello all,

I have a grid with some control (Textblock), I have also two button to navigate on the page.

When I change the page the content of Textblock change.

I would like to print all the page in pdf or printer. But I doesn't arrive. the page seems to not refresh correctly.

The contains of all pages is always the same.

If necessary I can do a test project.

Thank you for your assistance.

What I have tried:

/// <summary>
/// Print the current view to PDF or printer
/// </summary>
/// <param name="sender"></param>
/// <param name="re"></param>
private void Print_Click(object sender, RoutedEventArgs re)
{

    const double CMTOPIXEL = 37.79527559055;
    // size expected = 18 cm * 10.3 cm
    Size ExpectedSize = new Size(18 * CMTOPIXEL, 10.3 * CMTOPIXEL);

    System.Windows.FrameworkElement e = printPanel as System.Windows.FrameworkElement;
    if (e == null)
        return;
    // if all ECU doesn't contains something => not print
    if (affectedPinByECU.Count == 0)
        return;

    // select printer and get printer settings
    PrintDialog pd = new PrintDialog();
    pd.SelectedPagesEnabled = true;
    pd.PageRangeSelection = PageRangeSelection.SelectedPages;

    // if windows is close without print
    if (pd.ShowDialog() != true) return;

    List<int> toPrint = new List<int>();
    if (pd.PageRangeSelection == PageRangeSelection.AllPages)
    {
        for (int i = 0; i < MaxNumberOfECU; i++)
            toPrint.Add(i);
    }
    else
    {
        foreach (var usedpage in affectedPinByECU)
        {
            toPrint.Add(usedpage.Key);
        }
    }
    // create a document
    FixedDocument document = new FixedDocument();
    document.DocumentPaginator.PageSize = new Size(pd.PrintableAreaWidth, pd.PrintableAreaHeight);
    PrintCapabilities capabilities = pd.PrintQueue.GetPrintCapabilities(pd.PrintTicket);
    Thickness margin = new Thickness(10);

    double scalex = ExpectedSize.Width / e.ActualWidth;
    double scaley = ExpectedSize.Height / e.ActualHeight;

    e.RenderTransform = new ScaleTransform(scalex, scaley);

    Size visibleSize = new Size(capabilities.PageImageableArea.ExtentWidth - margin.Left - margin.Right, capabilities.PageImageableArea.ExtentHeight - margin.Top - margin.Bottom);

    //update the layout of the visual to the printer page size.
    e.Measure(visibleSize);
    e.Arrange(new Rect(visibleSize));
                   e.UpdateLayout();

    // for each page to pring
    foreach (var usedpage in toPrint)
    {
        FixedPage page1 = new FixedPage();
        page1.Width = document.DocumentPaginator.PageSize.Width;
        page1.Height = document.DocumentPaginator.PageSize.Height;

        Canvas canvas = UpdateECUpages(e, usedpage, visibleSize, capabilities.PageImageableArea, margin);

        page1.Children.Add(canvas);

        PageContent page1Content = new PageContent();
        ((IAddChild)page1Content).AddChild(page1);

        document.Pages.Add(page1Content);
    }

    // go back to the current ecu;

    vUpdateECU(indexECU);
    // and print
    pd.PrintDocument(document.DocumentPaginator, "My first document");

    e.RenderTransform = new ScaleTransform(1, 1);
}

private Canvas UpdateECUpages(FrameworkElement e, int i, Size visibleSize, PageImageableArea PageImageableArea, Thickness margin)
{

    vUpdateECU(i);

    // clone the element
    Canvas newCanvas = new Canvas();
    DockPanel dk = new DockPanel();

    // Add text at the begin
    TextBlock page1Text = new TextBlock();
    if (i == 0)
        page1Text.Text = "Master";
    else
        page1Text.Text = "Slave " + i;
    page1Text.FontSize = 40;
    page1Text.Margin = new Thickness(10);
    page1Text.HorizontalAlignment = HorizontalAlignment.Center;
    DockPanel.SetDock(page1Text, Dock.Top);
    dk.Children.Add(page1Text);

    newCanvas.Children.Add(dk);

    e.UpdateLayout();
    VisualBrush vb = new VisualBrush(e);


    vb.Stretch = Stretch.None;
    vb.AlignmentX = AlignmentX.Center;
    vb.AlignmentY = AlignmentY.Top;
    vb.ViewboxUnits = BrushMappingMode.Absolute;
    vb.TileMode = TileMode.None;
    vb.Viewbox = new Rect(0, 0, visibleSize.Width, visibleSize.Height);

    FixedPage.SetLeft(newCanvas, PageImageableArea.OriginWidth);
    FixedPage.SetTop(newCanvas, PageImageableArea.OriginHeight);

    newCanvas.Width = visibleSize.Width;
    newCanvas.Height = visibleSize.Height;
    newCanvas.Background = vb;
    newCanvas.Margin = margin;
    return newCanvas;
}
Posted
Updated 27-Jul-18 3:15am

1 solution

Hi,Your approach is not familiar to me but I can see at least one problem within your example. The method
private Canvas UpdateECUpages(FrameworkElement e, int i, Size visibleSize, PageImageableArea PageImageableArea, Thickness margin)
receiving FrameworElement e but returns new Canvas object. You may want to assign a new content (preserving the reference) to the e element as newCanvas and return e, or you can change the method signature to
private void UpdateECUpages(ref FrameworkElement e, int i, Size visibleSize, PageImageableArea PageImageableArea, Thickness margin)
and in the method assign a new value to e. e = newCanvas; and then use the e object in your following code. Another option is to use a different approach, see the accepted solution here [^]
Sorry for my English and not full ansver.

Best wishes,
Roman
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900