Могу ли я сделать xpsDocument из строки - для printpreview? - PullRequest
0 голосов
/ 26 октября 2019

Мне удалось с помощью stackowerlow, сделать простой printpreviewControl, который печатает окно wpf - но я хочу создать динамический xaml в коде.

У меня есть следующее:

Окно, которое я печатаю: (пример из stackoverflow)

<Window x:Class="ExpressTicket.Dialogs.printTest"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:ExpressTicket.Dialogs"
    mc:Ignorable="d"
    Title="printTest" Height="450" Width="800">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    <FlowDocumentScrollViewer>
        <FlowDocument x:Name="FD">
            <Paragraph>
                <Image Source="http://www.wpf-tutorial.com/images/logo.png" Width="90" Height="90" 
             Margin="0,0,30,0" />
                <Run FontSize="120">WPF</Run>
            </Paragraph>

            <Paragraph>
                WPF, which stands for
                <Bold>Windows Presentation Foundation</Bold> ,
            is Microsoft's latest approach to a GUI framework, used with the .NET framework.
            Some advantages include:
            </Paragraph>

            <List>
                <ListItem>
                    <Paragraph>
                        It's newer and thereby more in tune with current standards
                    </Paragraph>
                </ListItem>
                <ListItem>
                    <Paragraph>
                        Microsoft is using it for a lot of new applications, e.g. Visual Studio
                    </Paragraph>
                </ListItem>
                <ListItem>
                    <Paragraph>
                        <Run Text="It's more flexible, so you can do more things wi"/>
                        <Run Text="thout ha"/>
                        <InlineUIContainer>
                            <Button Content="Button" Width="75"/>
                        </InlineUIContainer>
                        <Run Text="ving to write or buy new controls"/>
                    </Paragraph>
                </ListItem>
            </List>

        </FlowDocument>
    </FlowDocumentScrollViewer>
    <Button Content="Print" Grid.Row="1" Click="Button_Click"></Button>
 </Grid>
</Window>

Код этого окна: (Я не хочу отображать окно в этом тесте - и я хочу заменить это окно чистой строкой xamlчто я создаю динамический. (поэтому я не хочу это окно)

public printTest()
{
    InitializeComponent();
    Hide();
    //To do - tempfile name - del*.xps
    try
    {
        File.Delete(Helpers.Utils.GetBinCatalogFilePath() + "*.xps");
    }
    catch (Exception)
    {

    }

    string fileName = System.IO.Path.GetRandomFileName(); 
        var xpsDocument = new XpsDocument(fileName, FileAccess.ReadWrite);
    XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(xpsDocument);
    writer.Write(((IDocumentPaginatorSource)FD).DocumentPaginator);
    Document = xpsDocument.GetFixedDocumentSequence();
    xpsDocument.Close();
    var windows = new PrintWindow(Document);
    windows.ShowDialog();
    Loaded += PrintTest_Loaded;
}

private void PrintTest_Loaded(object sender, RoutedEventArgs e)
{
    Hide();
}

Окончательно окно предварительного просмотра печати:

   <Window x:Class="ExpressTicket.Dialogs.PrintWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:ExpressTicket.Dialogs"
    mc:Ignorable="d"
    Title="PrintWindow" Height="450" Width="800">
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="1.5*"/>
    </Grid.ColumnDefinitions>
    <StackPanel>
        <Button Content="Print" Click="Button_Click"></Button>
        <!--Other print operations-->
    </StackPanel>
    <DocumentViewer  Grid.Column="1" x:Name="PreviewD">
    </DocumentViewer>
</Grid>
</Window>

Код:

 private FixedDocumentSequence _document;
        public PrintWindow(FixedDocumentSequence document)
        {



            _document = document;
            InitializeComponent();
            PreviewD.Document =document;
        }

Так что яДругими словами, я хочу создать в коде строку AXML или динамический документ xps, а затем использовать XpsDocumentWriter (XpsDocument.CreateXpsDocumentWriter (xpsDocument)), чтобы загрузить окно для печати из строки. Одним из решений является динамическая загрузка элементов управления втестовое окно, но я не думаю, что оно такое чистое, я хочу написать xaml в коде.

...