【问题标题】:How to get FixedDocument from PageDocuments [duplicate]如何从 PageDocuments 中获取 FixedDocument [重复]
【发布时间】:2015-01-02 12:00:15
【问题描述】:

我正在使用 WPF 进行打印。
我实现了一个继承自DocumentPaginator 类的类

public class ReportPaginator : DocumentPaginator
{
    private double pageUpperLimit;

    private double pageDownLimit;

    private Model.Report printedReport;

    private int pageCount;

    public override IDocumentPaginatorSource Source
    {
        get
        {
            return null;
        }
    }

    public override bool IsPageCountValid
    {
        get { return true; }
    }

    public override int PageCount
    {
        get { return pageCount; }
    }

    public override Size PageSize
    {
        get
        {
            return new Size(printedReport.PageSize.Width, printedReport.PageSize.Height);
        }
        set
        {
            // validate the value.
            if (value != null)
            {
                throw new ArgumentException("page size can not be null");
            }

            // TODO: here we have to validate if the page is A3, A4, A5. this is to set the SizeName.
            // if you did not set the sizeName here we will get an exception.
            printedReport.PageSize = new Model.PageSize { Height = value.Height, Width = value.Width };
            CalculatesPage();
            // if we have to set the PageSize (I do not think so), do not forget to call the PaginateData method.
        }
    }

    public ReportPaginator( Model.Report report)
    {
        printedReport = report;
        CalculatesPage();
    }

    public override DocumentPage GetPage(int pageNumber)
    {
        // validate the argument.
        if (pageNumber < 0)
        {
            throw new ArgumentException("pageNumber parameter could not be negative number", "pageNumber");
        }

        // if the argument is outside of the available pages, return 
        if (pageNumber > pageCount - 1)
        {
            return DocumentPage.Missing;
        }

        // specify the start pixel and end pixel of the page according to the height of the report.
        pageUpperLimit = pageNumber * PageSize.Height;
        pageDownLimit = (pageNumber + 1) * PageSize.Height;

        // create DrawingVisual for this DocumentPage
        DrawingVisual visual = new DrawingVisual();

        // get the DrawingContext for this DrawingVisual for this page.
        using (DrawingContext pageContext = visual.RenderOpen())
        {
            // drawing operations for elements and sections go here.

            // we will use the for loop instead of the foreach loop to enumerate the sections
            // because we will need the know in which section we draw.
            for (int sectionIndex = 0; sectionIndex < printedReport.Sections.Count; sectionIndex++)
            {
                PrintSection(pageContext, sectionIndex);

                foreach (Model.BaseReportControl control in printedReport.Sections[sectionIndex].Controls)
                {
                    PrintControl(pageContext, sectionIndex, control);
                }
            }
        }  // close the DrawingContext.

        return new DocumentPage(visual);
    }
}

有更多的辅助方法来完成这些工作。

我想创建 FixedDocument 对象以从 Source 属性返回它 使用从GetPage 方法返回的DocumentPage 对象。

我需要从源属性返回一个FixedDocument 因为我想在打印前预览文件

ReportPreviewerWindow window = new ReportPreviewerWindow();
window.previewer.Document = new ReportPaginator(printedSurface.ModelReport).Source;
window.ShowDialog();

预览器是DocumentViewer 对象存在于ReportPreviewerWindow

<Window x:Class="TanmiaGrp.Report.Designer.ReportPreviewerWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Report Previewer" >
   <Grid>
      <DocumentViewer x:Name="previewer"/>
   </Grid>
</Window>

【问题讨论】:

    标签: wpf printing fixeddocument documentpage


    【解决方案1】:

    在我问了这个问题后,我发现有些东西对我来说非常正确。 如果您遇到上述问题,请参考此链接

    How to Print Preview when using a DocumentPaginator to print?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-17
      • 2016-10-25
      • 2014-05-01
      • 2016-03-03
      • 1970-01-01
      相关资源
      最近更新 更多