none
WPF xps打印预览问题 RRS feed

  • 问题

  • 怎么能让所有的列都显示,实现预览打印!

    我的代码实现:

    第一步:

    public class FileHelper
        {
            #region GetXPSFromDialog
            /// <summary>
            /// GetXPSFromDialog
            /// </summary>
            /// <param name="isSaved"></param>
            /// <returns></returns>
            public static string GetXPSFromDialog(bool isSaved)
            {
                if (isSaved)
                {
                    SaveFileDialog saveFileDialog = new SaveFileDialog();

                    saveFileDialog.Filter = "XPS Document files (*.xps)|*.xps|Txt Document files(*.txt)|*.txt";
                    saveFileDialog.FilterIndex = 1;

                    if (saveFileDialog.ShowDialog() == DialogResult.OK)
                    {
                        return saveFileDialog.FileName;
                    }
                    else
                    {
                        return null;
                    }
                }
                else return string.Format("{0}\\temp.xps", Environment.CurrentDirectory);//制造一个临时存储
            }
            #endregion

            #region 打开XPS文件
            /// <summary>
            /// 打开XPS文件
            /// </summary>
            /// <returns></returns>
            public static string OpenXPSFileFromDialog()
            {
                OpenFileDialog openFileDialog = new OpenFileDialog();

                openFileDialog.Filter = "XPS Document Files(*.xps)|*.xps";

                if (openFileDialog.ShowDialog() == DialogResult.OK)

                    return openFileDialog.FileName;

                else

                    return null;

            }
            #endregion

            #region 保存XPS文件
            /// <summary>
            /// 保存XPS文件
            /// </summary>
            /// <param name="page"></param>
            /// <param name="isSaved"></param>
            /// <returns></returns>
            public static bool SaveXPS(FixedPage page, bool isSaved)
            {

                FixedDocument fixedDoc = new FixedDocument();//创建一个文档 fixedDoc.DocumentPaginator.PageSize = new Size(96 * 8.5, 96 * 11);
                fixedDoc.DocumentPaginator.PageSize = new Size(96 * 8.5, 96 * 11);

                PageContent pageContent = new PageContent();
                ((IAddChild)pageContent).AddChild(page);
                fixedDoc.Pages.Add(pageContent);//将对象到当前文档中

                string containerName = GetXPSFromDialog(isSaved);

                if (containerName != null)
                {
                    try
                    {
                        File.Delete(containerName);
                    }
                    catch (Exception e)
                    {
                        MessageBox.Show(e.Message);
                    }
                    XpsDocument _xpsDocument = new XpsDocument(containerName, FileAccess.Write);
                    XpsDocumentWriter xpsdw = XpsDocument.CreateXpsDocumentWriter(_xpsDocument);
                    xpsdw.Write(fixedDoc);//写入XPS文件
                    _xpsDocument.Close();
                    return true;
                }
                else return false;

            }
            #endregion

            #region 装入到显示容器
            static XpsDocument xpsPackage = null;
            /// <summary>
            /// 装入到显示容器
            /// </summary>
            /// <param name="xpsFileName"></param>
            /// <param name="viewer"></param>
            public static void LoadDocumentViewer(string xpsFileName, DocumentViewer viewer)
            {

                XpsDocument oldXpsPackage = xpsPackage;//保存原来的XPS

                xpsPackage = new XpsDocument(xpsFileName, FileAccess.Read, CompressionOption.NotCompressed);//从文件中读取文档

                FixedDocumentSequence fixedDocumentSequence = xpsPackage.GetFixedDocumentSequence();//从XPS文档对象得到FixedDocumentSequence

                viewer.Document = fixedDocumentSequence as IDocumentPaginatorSource;

                if (oldXpsPackage != null)

                    oldXpsPackage.Close();

                xpsPackage.Close();

            }
            #endregion

        }

    第二步:

    <FixedPage xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
               xmlns:extVisifireChart="clr-namespace:Visifire.Charts;assembly=WPFVisifire.Charts"
                        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
        <Grid x:Name="LayoutRoot" >
            <!--<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="820" Background="Yellow" Canvas.Top="40" Name="grid_showData">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition/>
                </Grid.RowDefinitions>
                <Grid Grid.Row="1">
                    <extVisifireChart:Chart Name="visifire_Times" Margin="20,20,0,20" HorizontalAlignment="Left" VerticalAlignment="Stretch" Width="260" Height="400"/>
                    <extVisifireChart:Chart Name="visifire_Dosage" Margin="320,20,20,20"  HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
                </Grid>
                <Grid Grid.Row="2">
                    <extVisifireChart:Chart Name="visifire_PerbeddayUsage" Margin="20,0,20,20" Height="400" HorizontalAlignment="Stretch"  VerticalAlignment="Stretch"/>
                </Grid>
            </Grid>-->
            <Grid x:Name="frame" Margin="0,0,0,0" >
                <Canvas Margin="0,0,0,8">
                    <!--<Rectangle  Margin="0"  Stroke="Black" Fill="#FF4B4B9F" Opacity="0.4" RadiusX="5" RadiusY="5" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Canvas.Left="3" Canvas.Top="2"/>-->
                    <Label Width="530" FontSize="20" HorizontalContentAlignment="Center" HorizontalAlignment="Center" Margin="164,6,153,0" Name="lbl_showDataTitle" VerticalAlignment="Top" />
                    <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="Yellow" Canvas.Top="40" Name="grid_showData">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition/>
                        </Grid.RowDefinitions>
                        <DataGrid Name="dgShowData" AutoGenerateColumns="True" ScrollViewer.HorizontalScrollBarVisibility="Disabled" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" CanUserAddRows="False" HeadersVisibility="Column" GridLinesVisibility="Horizontal" >
                            <DataGrid.ColumnHeaderStyle>
                                <Style TargetType="DataGridColumnHeader">
                                    <Setter Property="HorizontalContentAlignment" Value="Center"></Setter>
                                    <Setter Property="FontSize" Value="18"></Setter>
                                </Style>
                            </DataGrid.ColumnHeaderStyle>
                        </DataGrid>
                    </Grid>
                </Canvas>
            </Grid>
        </Grid>
    </FixedPage>

    第三步:

    <Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="Workpiece.PrintWindow"
    x:Name="Window"
    Title="PrintWindow"
    UseLayoutRounding="True"
    Width="640" Height="480" Loaded="Window_Loaded">

    <Grid x:Name="LayoutRoot">
            <DocumentViewer x:Name="docViewer"  />
    </Grid>
    </Window>

    public PrintWindow()
    {
    this.InitializeComponent();

    // Insert code required on object creation below this point.
    }

            //传递一个公共的数据类
            public string fixedDocFile;
            private void Window_Loaded(object sender, RoutedEventArgs e)
            {
               FileHelper.LoadDocumentViewer(fixedDocFile, docViewer);
            }

    第四步:

     //打印
            private void button1_Click(object sender, RoutedEventArgs e)
            {
                Uri printViewUri = new Uri("/Print/FixPage.xaml", UriKind.Relative);
                FixedPage printPage = (FixedPage)Application.LoadComponent(printViewUri);
                DataGrid dg = printPage.FindName("dgShowData") as DataGrid;
                dg.ItemsSource = GetDataList().Tables[0].DefaultView;
                Label lbl = printPage.FindName("lbl_showDataTitle") as Label;
                lbl.Content = "T_Users";

                Grid mygrid = printPage.FindName("grid_showData") as Grid;

                FileHelper.SaveXPS(printPage, false);
                string xpsFileName = FileHelper.GetXPSFromDialog(false);    //得到临时的文件存储
                PrintWindow window = new PrintWindow();     //调用显示容器
                window.fixedDocFile = xpsFileName;
                printPage.Width = dg.ActualWidth;
                MessageBox.Show(mygrid.ActualWidth.ToString() + "___" + dg.ActualWidth + "___" + printPage.ActualWidth);
                window.Show();

    }

    麻烦大侠指导!


    wy926

    2012年11月28日 7:17

全部回复

  • 主要是这个窗体宽高的问题!可是遇到一个问题是,我要是给定FixedPage的宽高,则会显示,可是这个太固化了;我想做到灵活改变窗体大小,我使用了绑定,【

    Width="{Binding Path=ActualWidth,ElementName=dgShowData}"  Height="{Binding Path=ActualHeight,ElementName=dgShowData}"】可是这样写效果也不对,如图:


    wy926

    2012年11月29日 1:15