Sivajith
Sivajith

Reputation: 1211

Set margin to printing document while wpf control is printing

I am trying to print a treeview control in wpf using the following code

PrintDialog printDlg = new PrintDialog();
printDlg.PrintVisual(trv, "GTree Printing.");

The treeview is printing, but it prints to right side and some data is cutting off. How do I set margin in this case? Also It won't print the entire tree structure, only the visible portion is printing. Please do advise on this.

This is in desktop application

Thanks, Sivajith

Upvotes: 1

Views: 3668

Answers (1)

Valera Scherbakov
Valera Scherbakov

Reputation: 488

    public static void Print(IEnumerable<UIElement> dataForPrint, string printerName)
    {
        try
        {
            var printDialog = new PrintDialog();
            using (var printQueue = new PrintQueue(new PrintServer(), printerName))
            {
                printDialog.PrintQueue = printQueue;
                var area = printDialog.PrintQueue.GetPrintCapabilities();
                if (area.PageImageableArea == null) throw new Exception("Failed to load printer settings.");
                var flowDocument = new FlowDocument
                {
                    PagePadding = new Thickness(area.PageImageableArea.OriginWidth, 0, 0, 0),
                    PageWidth = area.PageImageableArea.ExtentWidth + area.PageImageableArea.OriginWidth
                };

                foreach (var uiElement in dataForPrint)
                {
                    flowDocument.Blocks.Add(new BlockUIContainer(uiElement));
                }

                var paginator = ((IDocumentPaginatorSource) flowDocument).DocumentPaginator;
                printDialog.PrintDocument(paginator, "A Flow Document");
            }
        }
        catch (NotSupportedException)
        {

        }
        catch (Exception e)
        {
            Log(e);
        }
    }

If you show Printer Dialog you haven't to create PrintQueue. Just access to PrintDialog.PrintQueue property

Upvotes: 1

Related Questions