Печать WPF FlowDocument

33
задан Jason 5 December 2008 в 20:22
поделиться

3 ответа

yes, make a copy of the FlowDocument before printing it. This is because the pagination and margins will be different. This works for me.

    private void DoThePrint(System.Windows.Documents.FlowDocument document)
    {
        // Clone the source document's content into a new FlowDocument.
        // This is because the pagination for the printer needs to be
        // done differently than the pagination for the displayed page.
        // We print the copy, rather that the original FlowDocument.
        System.IO.MemoryStream s = new System.IO.MemoryStream();
        TextRange source = new TextRange(document.ContentStart, document.ContentEnd);
        source.Save(s, DataFormats.Xaml);
        FlowDocument copy = new FlowDocument();
        TextRange dest = new TextRange(copy.ContentStart, copy.ContentEnd);
        dest.Load(s, DataFormats.Xaml);

        // Create a XpsDocumentWriter object, implicitly opening a Windows common print dialog,
        // and allowing the user to select a printer.

        // get information about the dimensions of the seleted printer+media.
        System.Printing.PrintDocumentImageableArea ia = null;
        System.Windows.Xps.XpsDocumentWriter docWriter = System.Printing.PrintQueue.CreateXpsDocumentWriter(ref ia);

        if (docWriter != null && ia != null)
        {
            DocumentPaginator paginator = ((IDocumentPaginatorSource)copy).DocumentPaginator;

            // Change the PageSize and PagePadding for the document to match the CanvasSize for the printer device.
            paginator.PageSize = new Size(ia.MediaSizeWidth, ia.MediaSizeHeight);
            Thickness t = new Thickness(72);  // copy.PagePadding;
            copy.PagePadding = new Thickness(
                             Math.Max(ia.OriginWidth, t.Left),
                               Math.Max(ia.OriginHeight, t.Top),
                               Math.Max(ia.MediaSizeWidth - (ia.OriginWidth + ia.ExtentWidth), t.Right),
                               Math.Max(ia.MediaSizeHeight - (ia.OriginHeight + ia.ExtentHeight), t.Bottom));

            copy.ColumnWidth = double.PositiveInfinity;
            //copy.PageWidth = 528; // allow the page to be the natural with of the output device

            // Send content to the printer.
            docWriter.Write(paginator);
        }

    }
38
ответ дан 27 November 2019 в 17:02
поделиться

Можно использовать код от URL ниже, он переносит документ потока в фиксированный документ и печать, которой, большое преимущество состоит в том, что можно использовать его для добавления поля, заголовков и нижних колонтитулов.

http://blogs.msdn.com/fyuan/archive/2007/03/10/convert-xaml-flow-document-to-xps-with-style-multiple-page-page-size-header-margin.aspx

7
ответ дан 27 November 2019 в 17:02
поделиться

Я также генерирую отчет WPF на основе документа Flow, но я намеренно использую документ Flow в качестве экрана предварительного просмотра печати. Я хочу, чтобы поля были одинаковыми. Вы можете прочитать о том, как я это сделал здесь.

В вашем сценарии я думаю, почему бы просто не сделать копию настроек, а не всего документа потока. Затем вы сможете повторно применить настройки, если захотите вернуть документ в исходное состояние.

0
ответ дан 27 November 2019 в 17:02
поделиться
Другие вопросы по тегам:

Похожие вопросы: