使用 WPF XPS 查看器打印 XPS 时的默认布局方向

发布于 2024-07-12 07:37:58 字数 211 浏览 12 评论 0原文

使用 WPF XPS 查看器打印 XPS 时是否可以设置默认布局方向?

我的固定文档 XPS 将其页面方向设置为横向,页面介质尺寸的宽度大于其高度,并且在查看器中正确显示为横向。 只是当您点击打印按钮时,“打印对话框”首选项默认为“纵向”,并且按此方式打印。

我宁愿不必更改用户的默认打印设置,如果 XPS 查看器能够按设计打印的方式打印 XPS,我会更喜欢它。

Is there a way to set the Default Layout Orientation when printing XPSs using the WPF XPS Viewer?

My fixed document XPS has its page orientation set to Landscape, the Page Media Size has a width that is longer that its height and it displays correctly in the Viewer as Landscape.
Its just that when you hit the print button the Print Dialog preferences are defaulted to Portrait and it prints as such.

I'd rather not have to alter the users default print settings I'd much prefer it if the XPS Viewer would print the XPS as it was designed to be printed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

守不住的情 2024-07-19 07:37:58

填写 PrintTicket 的字段:

  PrintDialog pd = new PrintDialog();
  PrintTicket pt = new PrintTicket();
  pt.PageOrientation = PageOrientation.Landscape;
  pd.PrintTicket = pd.PrintQueue.MergeAndValidatePrintTicket(pd.PrintQueue.DefaultPrintTicket, pt).ValidatedPrintTicket;
  if (pd.ShowDialog() == true)
  {
    ...
  }

Fill the field of the PrintTicket:

  PrintDialog pd = new PrintDialog();
  PrintTicket pt = new PrintTicket();
  pt.PageOrientation = PageOrientation.Landscape;
  pd.PrintTicket = pd.PrintQueue.MergeAndValidatePrintTicket(pd.PrintQueue.DefaultPrintTicket, pt).ValidatedPrintTicket;
  if (pd.ShowDialog() == true)
  {
    ...
  }
时光无声 2024-07-19 07:37:58

我相信创建固定文档时执行此操作的正确方法是,当尺寸高于宽度时,在页面内容上设置 RenderTransform = RotateTransform(90)。
示例:

var visualContent = new Image
            {
                Source = image,
                Stretch = Stretch.Uniform
            };
visualContent.RenderTransformOrigin = new Point(0.5, 0.5);    
visualContent.RenderTransform = new RotateTransform(90);
FixedPage fixedPage = new FixedPage();
fixedPage.Children.Add(visualContent);
var pageContent = new PageContent
{
    Child = fixedPage
};

但是不确定这是否对预先存在的 XPS 文档有帮助。

I believe the correct way to do this when creating a FixedDocument, is set the RenderTransform = RotateTransform(90) on the page content when the dimensions are higher than they are wide.
Example:

var visualContent = new Image
            {
                Source = image,
                Stretch = Stretch.Uniform
            };
visualContent.RenderTransformOrigin = new Point(0.5, 0.5);    
visualContent.RenderTransform = new RotateTransform(90);
FixedPage fixedPage = new FixedPage();
fixedPage.Children.Add(visualContent);
var pageContent = new PageContent
{
    Child = fixedPage
};

Not sure if that helps with a pre-existing XPS document however.

萌吟 2024-07-19 07:37:58

这实际上并不是 MXDW 的问题,而是驱动程序在 Windows 上工作方式的问题。 用户选择被保存用于特定会话。 这意味着您可以在第一次打印和退出应用程序之间进行打印时重复使用第一次打印设置。 大多数打印机都以这种方式运行,除非有人想出一种方法将该信息保存在某处并让用户在会话中重复使用它。

因此,我尝试破解 GPD 文件(通常存储打印机的打印信息)。 方向有两个可能的值:PORTRAIT 和 LANDSCAPE_CC270,默认设置为 PORTRAIT。 见下文:

*%********************************************* **********************************
*% 方向
*%**************************************************** **********************************
*特点:定向
{
*rcNameID:=ORIENTATION_DISPLAY
*DefaultOption: PORTRAIT

*Option: PORTRAIT
{
    *rcNameID: =PORTRAIT_DISPLAY
}

*Option: LANDSCAPE_CC270
{
    *rcNameID: =LANDSCAPE_DISPLAY
}

}

现在,如果我将交换默认值更改为 LANDSCAPE_CC270,打印首选项将停止出现(任何打印都会失败)。 事实上,指定任何其他值似乎都会保留默认值 PORTRAIT。 毫无疑问,MS是
进行某种检查以防止我们破解该驱动程序。 MS好像没有
希望任何人篡改它的设置:(

但是你可以尝试更多地调动 GPD 值,看看是否会出现你喜欢的东西。将继续进行更多的黑客攻击。

警告:GPD 文件不应该被篡改,如果你不知道你在做什么。
还是想继续备份!

提示:它们存储在 %WINDOWS%system32\spool\drivers\w32x86\3 文件夹中。

This is not really a problem with MXDW but one with the way drivers work on Windows. The user choice(s) is/are saved for a particular session. This means that you can reuse firs-print settings when printing between the first print and quitting the application. Most printers behave this way, until unless one comes up with a way to save this information somewhere and let the user re-use it across sessions.

So, I tried hacking the GPD file (where printing information for a printer is typically stored). The orientation has two possible values: PORTRAIT and LANDSCAPE_CC270 with the default being set to PORTRAIT. See below:

*%******************************************************************************
*% Orientation
*%******************************************************************************
*Feature: Orientation
{
*rcNameID: =ORIENTATION_DISPLAY
*DefaultOption: PORTRAIT

*Option: PORTRAIT
{
    *rcNameID: =PORTRAIT_DISPLAY
}

*Option: LANDSCAPE_CC270
{
    *rcNameID: =LANDSCAPE_DISPLAY
}

}

Now, if I were to change swap the default value to LANDSCAPE_CC270, the printing preferences stop coming up (and any print would fail). In fact, it appears, that specifying any other value keeps the default to PORTRAIT. Definitely, MS is
doing some sort of check to prevent us from hacking this driver. Looks like MS doesn't
want anyone to tamper with its settings :(

But you could try flirting with the GPD values a bit more and see if something of your liking comes up. Will keep hacking a bit more.

Caveat: GPD files shouldn't be tampered with if you don't know what you are doing. If you
still want to go ahead make a backup!

Hint: They are stored in %WINDOWS%system32\spool\drivers\w32x86\3 folder.

马蹄踏│碎落叶 2024-07-19 07:37:58
<Grid Margin="0,0,-8,-8">
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="50"/>
    </Grid.RowDefinitions>

    <FlowDocumentScrollViewer Name="printpanel" HorizontalAlignment="Left" Width="959" FontFamily="Arial" Margin="0,-10,0,10">
        <FlowDocument x:Name="FD">

            <BlockUIContainer>

                <Canvas>
                    <Label x:Name="lblReceipt" Visibility="Visible" Content="Receipt No." HorizontalAlignment="Left" VerticalAlignment="Top" Canvas.Top="178" FontSize="12" Canvas.Left="60"/>
                    <Label x:Name="txtReceiptNo" BorderThickness="2" BorderBrush="Black" HorizontalAlignment="Left"  Padding="10,3,3,0" Height="23"   VerticalAlignment="Top" Width="200" FontSize="12" Canvas.Left="187" Canvas.Top="177" FontFamily="Arial"/>
                    <Label x:Name="lblmemNo" Visibility="Visible" Content="Membership No." HorizontalAlignment="Left" VerticalAlignment="Top" FontSize="12" Canvas.Left="462" Canvas.Top="177"/>
                    <Label x:Name="txtMembershipNo"  BorderThickness="2" BorderBrush="Black" HorizontalAlignment="Left" Padding="10,3,3,0" Height="23"  VerticalAlignment="Top" Width="177" FontSize="12" Canvas.Left="604" Canvas.Top="177" FontFamily="Arial">

                    </Label>
                    <Label x:Name="lblAuthCentr" Visibility="Visible" Content="Authorised Center." HorizontalAlignment="Left" VerticalAlignment="Top" Canvas.Left="60" Canvas.Top="221" FontSize="12"/>
                    <TextBox x:Name="txtAuthCentr"   HorizontalAlignment="Left" TextWrapping="WrapWithOverflow" Padding="10,3,3,0" Height="38"  VerticalAlignment="Top" Width="219" FontSize="12" Canvas.Left="238" Canvas.Top="219" FontFamily="Arial"/>
                    <Label x:Name="lblSector" Visibility="Visible" Content="Sector." HorizontalAlignment="Left" VerticalAlignment="Top" FontSize="12" Canvas.Left="492" Canvas.Top="220"/>
                    <Label x:Name="txtSector" BorderThickness="2" BorderBrush="Black"  HorizontalAlignment="Left" Padding="10,3,3,0" Height="23"  VerticalAlignment="Top" Width="115" FontSize="12" Canvas.Left="567" Canvas.Top="220" FontFamily="Arial"/>
                   

                </Canvas>
            </BlockUIContainer>

        </FlowDocument>
    </FlowDocumentScrollViewer>

    <Button Name="btnOk" Content="Print" Height="30" Grid.Row="1" Click="btnOk_Click" Margin="355,0,404,0"></Button>
</Grid>

只需设置 FlowDocument 高度和宽度

set FD.PageWidth = 1100; FD.PageHeight = 600;

 
 private void btnOk_Click(object sender, RoutedEventArgs e)
        {
          

            if (File.Exists("printPreview.xps"))
            {
                File.Delete("printPreview.xps");
            }
            var xpsDocument = new XpsDocument("printPreview.xps", FileAccess.ReadWrite);
            XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(xpsDocument);
            DocumentPaginator docPage;
            FD.PageWidth = 1100; // set FlowDocument Width
            FD.PageHeight = 600; // set FlowDocument Height
            docPage = ((IDocumentPaginatorSource)FD).DocumentPaginator;
            writer.Write(docPage);
            Document = xpsDocument.GetFixedDocumentSequence();
            this.Close();
            xpsDocument.Close();
            var windows = new PrintWindow(Document);
            windows.ShowDialog();

        }

<Grid Margin="0,0,-8,-8">
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="50"/>
    </Grid.RowDefinitions>

    <FlowDocumentScrollViewer Name="printpanel" HorizontalAlignment="Left" Width="959" FontFamily="Arial" Margin="0,-10,0,10">
        <FlowDocument x:Name="FD">

            <BlockUIContainer>

                <Canvas>
                    <Label x:Name="lblReceipt" Visibility="Visible" Content="Receipt No." HorizontalAlignment="Left" VerticalAlignment="Top" Canvas.Top="178" FontSize="12" Canvas.Left="60"/>
                    <Label x:Name="txtReceiptNo" BorderThickness="2" BorderBrush="Black" HorizontalAlignment="Left"  Padding="10,3,3,0" Height="23"   VerticalAlignment="Top" Width="200" FontSize="12" Canvas.Left="187" Canvas.Top="177" FontFamily="Arial"/>
                    <Label x:Name="lblmemNo" Visibility="Visible" Content="Membership No." HorizontalAlignment="Left" VerticalAlignment="Top" FontSize="12" Canvas.Left="462" Canvas.Top="177"/>
                    <Label x:Name="txtMembershipNo"  BorderThickness="2" BorderBrush="Black" HorizontalAlignment="Left" Padding="10,3,3,0" Height="23"  VerticalAlignment="Top" Width="177" FontSize="12" Canvas.Left="604" Canvas.Top="177" FontFamily="Arial">

                    </Label>
                    <Label x:Name="lblAuthCentr" Visibility="Visible" Content="Authorised Center." HorizontalAlignment="Left" VerticalAlignment="Top" Canvas.Left="60" Canvas.Top="221" FontSize="12"/>
                    <TextBox x:Name="txtAuthCentr"   HorizontalAlignment="Left" TextWrapping="WrapWithOverflow" Padding="10,3,3,0" Height="38"  VerticalAlignment="Top" Width="219" FontSize="12" Canvas.Left="238" Canvas.Top="219" FontFamily="Arial"/>
                    <Label x:Name="lblSector" Visibility="Visible" Content="Sector." HorizontalAlignment="Left" VerticalAlignment="Top" FontSize="12" Canvas.Left="492" Canvas.Top="220"/>
                    <Label x:Name="txtSector" BorderThickness="2" BorderBrush="Black"  HorizontalAlignment="Left" Padding="10,3,3,0" Height="23"  VerticalAlignment="Top" Width="115" FontSize="12" Canvas.Left="567" Canvas.Top="220" FontFamily="Arial"/>
                   

                </Canvas>
            </BlockUIContainer>

        </FlowDocument>
    </FlowDocumentScrollViewer>

    <Button Name="btnOk" Content="Print" Height="30" Grid.Row="1" Click="btnOk_Click" Margin="355,0,404,0"></Button>
</Grid>

Just set FlowDocument Height and width

set FD.PageWidth = 1100; FD.PageHeight = 600;

 
 private void btnOk_Click(object sender, RoutedEventArgs e)
        {
          

            if (File.Exists("printPreview.xps"))
            {
                File.Delete("printPreview.xps");
            }
            var xpsDocument = new XpsDocument("printPreview.xps", FileAccess.ReadWrite);
            XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(xpsDocument);
            DocumentPaginator docPage;
            FD.PageWidth = 1100; // set FlowDocument Width
            FD.PageHeight = 600; // set FlowDocument Height
            docPage = ((IDocumentPaginatorSource)FD).DocumentPaginator;
            writer.Write(docPage);
            Document = xpsDocument.GetFixedDocumentSequence();
            this.Close();
            xpsDocument.Close();
            var windows = new PrintWindow(Document);
            windows.ShowDialog();

        }

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文