English 中文(简体)
• 如何向XPS提供Xaml流程文件时,与页头和脚注者建立Xaml流动文件?
原标题:How to create a Xaml FlowDocument with Page Headers and Footers when rendered to XPS?

我是一份没有任何密码的《电子格式》和《电子数据流动文件》的“光线”概念。 只需要从C#中正确显示S-PS。

最佳回答

I had the same problem a few months ago, and found these links very helpful: WPF Multipage Reports Part IV Pagination http://www.codeproject.com/KB/WPF/PimpedDocumentPaginator.aspx

我采用的基本技术是,从衍生出一种习俗pa:

internal class HeaderFooterPaginator<THeaderModel, TFooterModel> : DynamicDocumentPaginator where THeaderModel : PageNumberModel, new() where TFooterModel : PageNumberModel, new()
{
...
}

在我的案件中,<代码>THeaderFooter Model和TFooterModel是<代码>的子类。 我需要头盔或脚步才能显示目前的页数。

public class PageNumberModel
{
    public int PageNumber { get; set; }
}

习俗的缔造者代表到原XPS的缔造者中去做大部分工作,因此在构造者中不做。

<代码>THeaderModel和>TFooterModel 类型允许假肢者检索XAMLDataTemplates每种类型,即允许你在XAML中具体说明头盔和脚.的布局,而不采用习惯图谱法。

In my code, the header and footer are of a fixed size, so when the paginator is created it retrieves the header and footer templates to determine how much space to reserve.

In the example code in the links provided, the technique they use to reserve space for the header and footer is to use a scale transform to shrink the original content. Instead, I tell the original paginator to use a reduced page size and then add the page the original paginator generates to a ContainerVisual and set its Offset. You probably couldn t do this if the size of the headers and footers was dynamic because the page count would keep changing.

我可以回顾的唯一其它复杂情况是,在增加头盔和脚步时,你需要使用<代码>Dissuer(见以下编码AddHeaderOrFooter TotainerAsync)。 具有约束性的数据没有工作。 我们正在稍微改变世界森林论坛使这一模式发挥作用。

This would all be quite hard to explain without including the code so I ve attached the custom renderer code below. I ve stripped out some irrelevant stuff so if it doesn t compile as is that s probably why :-)

请注意,这一页数被冲抵是因为我们的XPS文件由多个流程文件科组成,而电话码则跟踪当前总页数。

希望这一帮助!

internal class HeaderFooterPaginator<THeaderModel, TFooterModel> : DynamicDocumentPaginator where THeaderModel : PageNumberModel, new() where TFooterModel : PageNumberModel, new()
{
    private readonly double _footerHeight;
    private readonly DataTemplate _footerTemplate;
    private readonly double _headerHeight;
    private readonly DataTemplate _headerTemplate;
    private Size _newPageSize;
    private Size _originalPageSize;
    private readonly DynamicDocumentPaginator _originalPaginator;
    private readonly int _pageNumberOffset;
    private readonly FlowDocument _paginatorSource;
    private const double HeaderAndFooterMarginHeight = 5;
    private const double HeaderAndFooterMarginWidth = 10;

    public HeaderFooterPaginator(int pageNumberOffset, FlowDocument document)
    {
        if (document == null)
        {
            throw new ArgumentNullException("document");
        }

        _paginatorSource = document;

        if (_paginatorSource == null)
        {
            throw new InvalidOperationException("Could not create a clone of the document being paginated.");
        }

        _originalPaginator = ((IDocumentPaginatorSource) _paginatorSource).DocumentPaginator as DynamicDocumentPaginator;

        if (_originalPaginator == null)
        {
            throw new InvalidOperationException("The paginator must be a DynamicDocumentPaginator.");
        }

        _headerTemplate = GetModelDataTemplate(typeof (THeaderModel));
        _footerTemplate = GetModelDataTemplate(typeof (TFooterModel));

        var headerSize = GetModelContentSize(new THeaderModel { PageNumber = int.MaxValue }, _headerTemplate, _originalPaginator.PageSize);
        var footerSize = GetModelContentSize(new TFooterModel { PageNumber = int.MaxValue }, _footerTemplate, _originalPaginator.PageSize);

        _headerHeight = double.IsInfinity(headerSize.Height) ? 0 : headerSize.Height;
        _footerHeight = double.IsInfinity(footerSize.Height) ? 0 : footerSize.Height;

        _pageNumberOffset = pageNumberOffset;

        SetPageSize(new Size(document.PageWidth, document.PageHeight));
    }

    private void AddHeaderOrFooterToContainerAsync<THeaderOrFooter>(ContainerVisual container, double areaWidth, double areaHeight, Vector areaOffset, FrameworkTemplate template, int displayPageNumber)  where THeaderOrFooter : PageNumberModel, new()
    {
        if (template == null)
        {
            return;
        }
        var visual = GetModelContent(new THeaderOrFooter { PageNumber = displayPageNumber }, template);

        if (visual != null)
        {
            Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle, new Action(() =>
            {
                visual.Measure(_originalPageSize);
                visual.Arrange(new Rect(0, 0, areaWidth, areaHeight));
                visual.UpdateLayout();

                var headerContainer = new ContainerVisual { Offset = areaOffset };
                headerContainer.Children.Add(visual);
                container.Children.Add(headerContainer);
            }));
        }
    }

    public override void ComputePageCount()
    {
        _originalPaginator.ComputePageCount();
    }

    private static void FlushDispatcher()
    {
        Dispatcher.CurrentDispatcher.Invoke(DispatcherPriority.ApplicationIdle, new DispatcherOperationCallback(delegate { return null; }), null);
    }

    private static FrameworkElement GetModelContent(object dataModel, FrameworkTemplate modelTemplate)
    {
        if (modelTemplate == null)
        {
            return null;
        }

        var content = modelTemplate.LoadContent() as FrameworkElement;
        if (content == null)
        {
            return null;
        }

        content.DataContext = dataModel;

        return content;
    }

    private static Size GetModelContentSize(object dataModel, FrameworkTemplate modelTemplate, Size availableSize)
    {
        var content = GetModelContent(dataModel, modelTemplate);
        if (content == null)
        {
            return Size.Empty;
        }

        FlushDispatcher();
        content.Measure(availableSize);
        return content.DesiredSize;
    }

    private DataTemplate GetModelDataTemplate(Type modelType)
    {
        var key = new DataTemplateKey(modelType);
        return _paginatorSource.TryFindResource(key) as DataTemplate;
    }

    public override ContentPosition GetObjectPosition(object value)
    {
        return _originalPaginator.GetObjectPosition(value);
    }

    public override DocumentPage GetPage(int pageNumber)
    {
        if (!_originalPaginator.IsPageCountValid)
        {
            ComputePageCount();
        }

        var originalPage = _originalPaginator.GetPage(pageNumber);

        var newPage = new ContainerVisual();

        var displayPageNumber = _pageNumberOffset + pageNumber;
        var internalWidth = _originalPageSize.Width - 2*HeaderAndFooterMarginWidth;
        AddHeaderOrFooterToContainerAsync<THeaderModel>(newPage, internalWidth, _headerHeight, new Vector(HeaderAndFooterMarginWidth, HeaderAndFooterMarginHeight), _headerTemplate, displayPageNumber);

        var smallerPage = new ContainerVisual();
        smallerPage.Children.Add(originalPage.Visual);
        smallerPage.Offset = new Vector(HeaderAndFooterMarginWidth, HeaderAndFooterMarginHeight + _headerHeight);
        newPage.Children.Add(smallerPage);

        AddHeaderOrFooterToContainerAsync<TFooterModel>(newPage, internalWidth, _footerHeight, new Vector(HeaderAndFooterMarginWidth, _originalPageSize.Height - HeaderAndFooterMarginHeight - _footerHeight), _footerTemplate, displayPageNumber);

        return new DocumentPage(newPage, _originalPageSize, originalPage.BleedBox, originalPage.ContentBox);
    }

    public override int GetPageNumber(ContentPosition contentPosition)
    {
        return _originalPaginator.GetPageNumber(contentPosition);
    }

    public override ContentPosition GetPagePosition(DocumentPage page)
    {
        return _originalPaginator.GetPagePosition(page);
    }

    private void SetPageSize(Size pageSize)
    {
        _originalPageSize = pageSize;

        // Decrease the available page size by the height of the header and footer. The page is offset by the header height later on.
        var sizeRect = new Rect(pageSize);
        sizeRect.Inflate(-HeaderAndFooterMarginWidth, -(HeaderAndFooterMarginHeight + _footerHeight/2 + _headerHeight/2));

        _originalPaginator.PageSize = _newPageSize = sizeRect.Size;

        // Change page size of the document to the size of the content area
        _paginatorSource.PageHeight = _newPageSize.Height;
        _paginatorSource.PageWidth = _newPageSize.Width;
    }

    public override bool IsPageCountValid
    {
        get { return _originalPaginator.IsPageCountValid; }
    }

    public override int PageCount
    {
        get { return _originalPaginator.PageCount; }
    }

    public override Size PageSize
    {
        get { return _newPageSize; }
        set { SetPageSize(value); }
    }

    public override IDocumentPaginatorSource Source
    {
        get { return _originalPaginator.Source; }
    }
}
问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签