动态可打印内容区域

时间:2014-01-29 12:04:50

标签: wpf paging flowdocument

我正在使用WPF Flowdocument在带有页眉和页脚的表中打印内容。

但是,当数据仅占据表格中的几行时,页脚部分仍然会反映在页面的最后一页,整个页面都会被打印,从而使页面的一半空白。

如果数据少于一半,我可以预期内容(标题+内容+页脚)只占据页面的一半,如果数据超过半页,我可以期待一页整页(A4 / Letter页面)吗?如果数据超过整页,它也应该跨越第二页。

谢谢..

1 个答案:

答案 0 :(得分:0)

我可以通过覆盖DocumentViewer的OnPrintCommand

来解决这个问题
public class CustomDocumentViewer : DocumentViewer
{
    protected override void OnPrintCommand()
    {
        // get a print dialog, defaulted to default printer and default printer's preferences.
        PrintDialog printDialog = new PrintDialog();
        printDialog.PrintQueue = LocalPrintServer.GetDefaultPrintQueue();
        printDialog.PrintTicket = printDialog.PrintQueue.DefaultPrintTicket;

        // get a reference to the FixedDocumentSequence for the viewer.
        FixedDocumentSequence docSeq = this.Document as FixedDocumentSequence;

        if (Globals.PRINT_COMMAND_PAGEHEIGHT == "FULL")
            printDialog.PrintTicket.PageMediaSize = new PageMediaSize(960, 1152);
        else
            printDialog.PrintTicket.PageMediaSize = new PageMediaSize(960, 576);

        if (printDialog.ShowDialog() == true)
        {
            // set the print ticket for the document sequence and write it to the printer.
            docSeq.PrintTicket = printDialog.PrintTicket;

            XpsDocumentWriter writer = PrintQueue.CreateXpsDocumentWriter(printDialog.PrintQueue);
            writer.WriteAsync(docSeq, printDialog.PrintTicket);
        }
    }
}

Window_Activated功能:

            //Set Page Height and Width dynamically

            MemoryStream mem = new MemoryStream();
            byte[] buf = Encoding.UTF8.GetBytes(strXmlData);
            mem.Write(buf, 0, buf.Length);
            mem.Position = 0;
            FlowDocument res = XamlReader.Load(mem) as FlowDocument;

            //1152 = 12in * 96 DPI

            if (Utils.GetPageHeight(strOrderNo.Substring(0, 2)) == "FULL")
            {
                res.PageHeight = Globals.FULL_PAGE_HEIGHT;
                Globals.PRINT_COMMAND_PAGEHEIGHT = "FULL";
            }
            else
            {
                res.PageHeight = Globals.HALF_PAGE_HEIGHT;
                Globals.PRINT_COMMAND_PAGEHEIGHT = "HALF";
            }
            res.PageWidth = Globals.PAGE_WIDTH;

            reportDocument.XamlData = XamlWriter.Save(res);
DocumentViewer xaml文件中的

<Grid>
    <spu:CustomDocumentViewer x:Name="documentViewer" />
</Grid>

并在Flowdocument Template中:

PageHeight="0.0cm" PageWidth="0.0cm" ColumnWidth="21.0cm"

谢谢, 〜拉维