BSG
BSG

Reputation: 1

Not able to print fixed document with large user control element in wpf

I am trying to print an user control. I am passing the user control as framework element in fixed document. When the user control is large it does not get printed fully only one page gets printed and remaining contents are truncated. I am not getting the remaining pages. I need to print fixed document continuously in multiple pages without truncating the content.

  public void PrintAllReceipts(object datatoprint, string resourceNameToUserControl)
         {

             var assembly = Assembly.GetEntryAssembly();
             string resourceName = resourceNameToUserControl;

             FrameworkElement elem;
             using (Stream stream = assembly.GetManifestResourceStream(resourceName))
             {
                 using (StreamReader reader = new StreamReader(stream))
                 {
                     elem = XamlReader.Parse(reader.ReadToEnd()) as FrameworkElement;
                 }
             }

             var data = datatoprint;

             elem.DataContext = data;


             //var pageSize = new Size(8.26 * 96, 11.69 * 96); // A4 page, at 96 dpi
             //FixedDocument fixedDoc = new FixedDocument();
             //PageContent pageContent = new PageContent();
             //FixedPage fixedPage = new FixedPage();

             //fixedDoc.DocumentPaginator.PageSize = pageSize;
             //fixedPage.Width = pageSize.Width;
             //fixedPage.Height = pageSize.Height;
             //Create first page of document
             //fixedPage.Children.Add(elem);
             //fixedPage.Measure(pageSize);
             //fixedPage.Arrange(new Rect(new Point(), pageSize));
             //fixedPage.UpdateLayout();
             //((System.Windows.Markup.IAddChild)pageContent).AddChild(fixedPage);
             //fixedDoc.Pages.Add(pageContent);

        PrintDialog dial = new PrintDialog();
        PrintContinuous(elem);
           //List<FixedDocument > docss = GetFixedDocument(elem, dial);
           //foreach (FixedDocument docs in docss)
           //{
           //    dial.PrintDocument(docs.DocumentPaginator, "");
           //}

          //   ShowPrintPreview(docss);

         }



public static void PrintContinuous( FrameworkElement fe)
         {
             PrintDialog pd = new PrintDialog();

             bool? result = pd.ShowDialog();

             if (!result.HasValue || !result.Value) return;

             fe.Dispatcher.Invoke(new Action(() =>
             {
                 fe.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity));
                 fe.Arrange(new Rect(fe.DesiredSize));
                 fe.UpdateLayout();
             }), System.Windows.Threading.DispatcherPriority.Render);

             pd.PrintVisual(fe, ((String.IsNullOrWhiteSpace
             (fe.Name) ? "Temp" : fe.Name) + " PRINT"));
         }

Upvotes: 1

Views: 1156

Answers (1)

Glen Thomas
Glen Thomas

Reputation: 10744

You need to paginate the visual.

You could take the control and convert it into a bitmap and then chop the bitmap up into pieces that will fit on a page, add the pages to a FixedDocument and send that to the printer using PrintDialog.PrintDocument.

See this fairly lengthy CodeProject article which fully covers the process and code required: Printing-large-WPF-UserControls

Upvotes: 2

Related Questions