PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, September 15, 2022

[FIXED] How to print multiple WPF pages in one document

 September 15, 2022     printing, vb.net, wpf     No comments   

Issue

I want to create a document with several pages and with one wpf window depicted on each page.

I managed to print one window using PrintDialog and PrintVisual. However this seem to only work with one single page? Any ideas how I can build a document with several pages and print the complete set.

Can I insert that visual (referred in code) and insert it as a page in a document and print it after that?

Is approach really bad? Is there som other way to solve this problem?

      Sub Print (Dim ele As FrameWorkElement)
            Dim margin As Double = 30
            Dim titlePadding As Double = 10

            Dim printDlg As PrintDialog = New PrintDialog()
            printDlg.PrintTicket.PageOrientation = PageOrientation.Landscape
            If (printDlg.ShowDialog() <> True) Then Return

            Dim formattedText As FormattedText = New FormattedText(Name, CultureInfo.GetCultureInfo("en-us"),
                                                            FlowDirection.LeftToRight, New Typeface("Verdana"), 25, Brushes.Black)

            formattedText.MaxTextWidth = printDlg.PrintableAreaWidth

            Dim scale As Double = Math.Min(printDlg.PrintableAreaWidth / (ele.ActualWidth + (Margin * 2)),
                                    (printDlg.PrintableAreaHeight - (formattedText.Height + titlePadding)) / (ele.ActualHeight + (Margin * 2)))

            Dim visual As DrawingVisual = New DrawingVisual()
            Using context As DrawingContext = visual.RenderOpen()

                Dim brush As VisualBrush = New VisualBrush(ele)
                context.DrawRectangle(brush, Nothing, New Rect(New Point(margin, margin + formattedText.Height + titlePadding),
                                                            New Size(ele.ActualWidth, ele.ActualHeight)))

                context.DrawText(formattedText, New Point(margin, margin))
            End Using
            visual.Transform = New ScaleTransform(scale, scale)
            printDlg.PrintVisual(visual, "")
     End Sub

Solution

You want to create a FixedDocument with multiple FixedPages.

Example:

How to create a new document and single page.

var pageSize = new Size(8.26 * 96, 11.69 * 96); // A4 page, at 96 dpi
var document = new FixedDocument();
document.DocumentPaginator.PageSize = pageSize;

// Create FixedPage
var fixedPage = new FixedPage();
fixedPage.Width = pageSize.Width;
fixedPage.Height = pageSize.Height;
// Add visual, measure/arrange page.
fixedPage.Children.Add((UIElement)visual);
fixedPage.Measure(pageSize);
fixedPage.Arrange(new Rect(new Point(), pageSize));
fixedPage.UpdateLayout();

// Add page to document
var pageContent = new PageContent();
((IAddChild)pageContent).AddChild(fixedPage);
document.Pages.Add(pageContent);

// Send to the printer.
var pd = new PrintDialog();
pd.PrintDocument(document.DocumentPaginator, "My Document");

Written in C#, however you should be able to convert to VB.

HTH,



Answered By - Dennis
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing