将页眉和页脚添加到XPS打印输出

时间:2016-05-24 19:07:27

标签: wpf documentviewer xpsdocument

从磁盘

加载

XpsDocument

通过FixedDocumentSequence

创建XpsDocument.GetFixedDocumentSequence Method ()

通过DocumentViewer

显示

想要添加页眉和页脚来打印
我不需要将页眉和页脚添加回XPS文档 - 仅限于打印输出

我尝试实施DocumentPaginator但无法使尺寸或展示位置正常工作

与此footer to FlowDocument不同 我在那里使用FlowDocuments的解决方案就好了 Convert XAML Flow Document to XPS with Style

在FixedDocumentSequence上,大小来自文档(我猜)
例如,可以混合风景和肖像 我无法弄清楚如何勾选大小并为页眉和页脚腾出空间 我可以在上面盖章,但它在页面的顶部 景观页面被切断

当来自FixedDocumentSequence时,无法分配PageSize 如链接中所述,它是建议的页面大小
DocumentPage。尺寸为只读

即使我使用DocumentPaginator.GetPage加载页面时创建了我想要的大小的DocumentPage,那么大小也会被覆盖

更糟糕的是,DocumentPaginator似乎没有正确地意识到尺寸,因为它没有正确处理混合风景和肖像。风景是打印的肖像,只是在页面上运行。

有人要我发布代码
也可能有一个更好的方法 请参阅footer to FlowDocument了解如何使用它 这是它破坏的地方 //这将从GetPage获取页面大小 - 忽略

之上的大小
public class DocumentPaginatorWrapperXPS : DocumentPaginator
{
    System.Windows.Size m_PageSize;
    System.Windows.Size m_Margin;
    DocumentPaginator m_Paginator;
    FixedDocumentSequence fixedDocumentSequence;
    Typeface m_Typeface;
    private string printHeader;
    private int? sID;
    private string docID;

    public DocumentPaginatorWrapperXPS(FixedDocumentSequence FixedDocumentSequence, System.Windows.Size margin, string PrintHeader, int? SID, string DOCID)
    {
        //m_PageSize = pageSize;
        //m_PageSize = new Size(800, 600);

        fixedDocumentSequence = FixedDocumentSequence;

        m_Margin = margin;
        m_Paginator = fixedDocumentSequence.DocumentPaginator;
        //m_Paginator.PageSize = new System.Windows.Size(m_PageSize.Width - margin.Width * 2,
        //                                               //m_PageSize.Height - margin.Width * 2);
        printHeader = PrintHeader;
        sID = SID;
        docID = DOCID;
    }
    Rect Move(Rect rect)
    {
        if (rect.IsEmpty)
        {
            return rect;
        }
        else
        {
            return new Rect(rect.Left + m_Margin.Width, rect.Top + m_Margin.Height,
                            rect.Width, rect.Height);
        }
    }
    private Size sizeXPS;
    public override DocumentPage GetPage(int pageNumber)
    {
        System.Windows.Documents.DocumentPage page = m_Paginator.GetPage(pageNumber);
        Debug.WriteLine(page.Size.Width + " " + page.Size.Height);


        m_Paginator.PageSize = new System.Windows.Size(page.Size.Width/2 - m_Margin.Width * 2, page.Size.Height/2 - m_Margin.Height * 2);

        page = m_Paginator.GetPage(pageNumber);  // this get the page size from GetPage - ignores size above
        Debug.WriteLine(page.Size.Width + " " + page.Size.Height);

        DynamicDocumentPaginator paginatorPage = fixedDocumentSequence.DocumentPaginator as DynamicDocumentPaginator;
        paginatorPage.PageSize = new System.Windows.Size(page.Size.Width / 2 - m_Margin.Width * 2, page.Size.Height / 2 - m_Margin.Height * 2);
        sizeXPS = new System.Windows.Size(page.Size.Width / 2 - m_Margin.Width * 2, page.Size.Height / 2 - m_Margin.Height * 2);
        page = paginatorPage.GetPage(pageNumber);  // still does not change the page size
        Debug.WriteLine(page.Size.Width + " " + page.Size.Height);

        //page.PageSize = new System.Windows.Size(m_PageSize.Width - margin.Width * 2,
        //m_PageSize.Height - margin.Width * 2);
        //page.Size = new System.Windows.Size(page.Size.Width - m_Margin.Width * 2, page.Size.Height - m_Margin.Height * 2);
        //page.Size.Width = page.Size.Width - m_Margin.Width * 2;
        // Create a wrapper visual for transformation and add extras

        ContainerVisual newpage = new ContainerVisual();
        DrawingVisual title = new DrawingVisual();
        using (DrawingContext ctx = title.RenderOpen())
        {
            if (m_Typeface == null)
            {
                m_Typeface = new Typeface("Segoe UI Symbol");  //Ariel
            }
            //FormattedText text = new FormattedText("Attorney eyes only  \uE18B  \u1F440                Page " + (pageNumber + 1), 
            //                                        System.Globalization.CultureInfo.CurrentCulture, FlowDirection.LeftToRight, 
            //                                        m_Typeface, 14, System.Windows.Media.Brushes.Black);

            string pageS = "m_PageSize.Width " + m_PageSize.Width + "  m_Margin.Width " + m_Margin.Width + "  m_PageSize.Height " + m_PageSize.Height + "  m_Margin.Height " + m_Margin.Height + Environment.NewLine +
                            "page.Size.Width " + page.Size.Width + "  page.Size.Height " + page.Size.Height;
            FormattedText header = new FormattedText("\u1F440 " + printHeader + Environment.NewLine + pageS,
                                                    System.Globalization.CultureInfo.CurrentCulture, FlowDirection.LeftToRight,
                                                    m_Typeface, 14, System.Windows.Media.Brushes.Black);
            ctx.DrawText(header, new System.Windows.Point(96 / 2, -96 / 4)); // 1/4 inch above page content
            //text = new FormattedText(pageS, System.Globalization.CultureInfo.CurrentCulture, FlowDirection.LeftToRight,
            //                         m_Typeface, 14, System.Windows.Media.Brushes.Black);

            string goGabe = string.Empty;
            if (sID != null)
            {
                goGabe += Environment.NewLine + "Gabriel Docs™ sysDocID: " + sID;
                if (!string.IsNullOrEmpty(docID))
                    goGabe += "  docID: " + docID;
            }
            goGabe += Environment.NewLine + "Page: " + (pageNumber + 1) + "   Date: " + DateTime.Now.ToString() + "  User: " + App.StaticGabeLib.CurUserP.UserID;
            FormattedText footer = new FormattedText(goGabe
                                                    , System.Globalization.CultureInfo.CurrentCulture, FlowDirection.LeftToRight,
                                                        m_Typeface, 14, System.Windows.Media.Brushes.Black);
            ctx.DrawText(footer, new System.Windows.Point(96 / 2, page.Size.Height - m_Margin.Height));
            //ctx.DrawText(footer, new System.Windows.Point(96 / 2, m_Paginator.PageSize.Height - m_Margin.Height));
        }
        DrawingVisual background = new DrawingVisual();
        using (DrawingContext ctx = background.RenderOpen())
        {
            ctx.DrawRectangle(new SolidColorBrush(System.Windows.Media.Color.FromRgb(240, 240, 240)), null, page.ContentBox);
        }
        newpage.Children.Add(background); // Scale down page and center
        ContainerVisual smallerPage = new ContainerVisual();
        smallerPage.Children.Add(page.Visual);
        smallerPage.Transform = new MatrixTransform(0.8, 0, 0, 0.8, 0.1 * page.ContentBox.Width, 0.1 * page.ContentBox.Height);
        newpage.Children.Add(smallerPage);
        newpage.Children.Add(title);
        newpage.Transform = new TranslateTransform(m_Margin.Width, m_Margin.Height);
        return new DocumentPage(newpage, m_PageSize, Move(page.BleedBox), Move(page.ContentBox));
    }
    public override bool IsPageCountValid
    {
        get
        {
            return m_Paginator.IsPageCountValid;
        }
    }
    public override int PageCount
    {
        get
        {
            return m_Paginator.PageCount;
        }
    }
    public override System.Windows.Size PageSize
    {
        get
        {
            Debug.WriteLine("PageSize " + sizeXPS.Width + " " + sizeXPS.Height);
            return sizeXPS;  // this is not called

            //m_Paginator.PageSize;
        }
        set
        {
            sizeXPS = value;
            // m_Paginator.PageSize = value;
        }
    }
    public override IDocumentPaginatorSource Source
    {
        get
        {
            return m_Paginator.Source;
        }
    }
}

0 个答案:

没有答案