C# Wpf xps размер экспорта - PullRequest
0 голосов
/ 15 апреля 2020

Я пытаюсь экспортировать визуал (Grid) с его содержимым в XPS >> PDF. К сожалению, XPSDocumentWriter, который преобразует его, принимает размер окна вместо размера визуала. Я попытался установить PageMediaSize вручную, но это не удалось.

Может быть, кто-нибудь может помочь. Спасибо!

<Window x:Class="WpfApp10.MainWindow"
    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:WpfApp10"
    mc:Ignorable="d"
    Title="MainWindow" Height="300" Width="400">
<Grid Background="LightBlue">
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition Height="50"/>
    </Grid.RowDefinitions>
    <Grid x:Name="Visual" Height="100" Width="200" Background="LightGray">
        <Ellipse Width="50" Height="50" Fill="Red"/>
        <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Text="PDF" FontSize="60"/>
    </Grid>
    <Button Grid.Row="1" Content="Start" PreviewMouseDown="Button_PreviewMouseDown"/>
</Grid>

private void Button_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
    Grid vs = Visual;

    //Doesnt work
    PrintTicket pt = new PrintTicket();
    pt.PageMediaSize = new PageMediaSize(200, 100);

    MemoryStream lMemoryStream = new MemoryStream();
    Package package = Package.Open(lMemoryStream, FileMode.Create);
    XpsDocument doc = new XpsDocument(package);
    XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(doc);
    writer.Write(vs);
    doc.Close();
    package.Close();

    var pdfXpsDoc = PdfSharp.Xps.XpsModel.XpsDocument.Open(lMemoryStream);
    Console.WriteLine(pdfXpsDoc.Documents.First().Pages.First().Width + " " + vs.Width);
    PdfSharp.Xps.XpsConverter.Convert(pdfXpsDoc, @"C:\File.pdf", 0);
}
...