简体   繁体   English

如何设置Xps文档的页面大小?

[英]How to set the Page Size for an Xps Document?

I am trying to save an XPS document with a FixedDocument . 我试图用FixedDocument保存XPS文档。 So far, I have failed to change the page size. 到目前为止,我没有改变页面大小。 How can I change it to a custom size? 如何将其更改为自定义尺寸?

I have based my code on the first answer to this question , more specifically, I am using this code: 我已将代码基于此问题的第一个答案 ,更具体地说,我使用此代码:

        if (File.Exists(filename)) {
            File.Delete(filename);
        }

        var oldParent = LogicalTreeHelper.GetParent(this) as ContentControl;
        try {
            oldParent.Content = null;

            FixedDocument doc = new FixedDocument();

            PageContent pageCnt = new PageContent();
            FixedPage page = new FixedPage();

            page.Children.Add(this);
            try {
                ((System.Windows.Markup.IAddChild)pageCnt).AddChild(page);
                doc.Pages.Add(pageCnt);

                XpsDocument xpsDoc = new XpsDocument(filename, FileAccess.ReadWrite);
                try {
                    var writer = XpsDocument.CreateXpsDocumentWriter(xpsDoc);
                    writer.Write(doc);
                }
                finally {
                    xpsDoc.Close();
                }
            }
            finally {
                page.Children.Clear();
            }
        }
        finally {
            ((ContentControl)oldParent).Content = this;
        }

It copies a user control into an XPS document and does so successfully, but, as I said, uses a default paper size. 它将用户控件复制到XPS文档中并成功完成,但正如我所说,它使用默认的纸张大小。

I have tried to use the DocumentPaginator.PageSize property to set a new page size (after instantiating FixedDocument ), but whatever I assign to that property seems to be ignored; 我曾尝试使用DocumentPaginator.PageSize属性来设置新的页面大小(在实例化FixedDocument ),但是我分配给该属性的任何内容似乎都被忽略了; the page in the resulting XPS document retains its default paper size. 生成的XPS文档中的页面保留其默认纸张大小。

When executing stepwise, I can see that the value of the PageSize property has really changed, so it's not like the new value somehow isn't accepted by the DocumentPaginator . 当逐步执行时,我可以看到PageSize属性的值确实已经改变,因此它不像DocumentPaginator不接受新值。

I have found various online resources, none of which has solved my problem as yet: 我找到了各种在线资源,但尚未解决我的问题:

  • This forum posting at MS Social insists that setting the PageSize property works, but it does not as far as I can tell. 在MS Social发布的这个论坛坚持认为设置PageSize属性是有效的,但它并没有我能说的那么多。
  • The docs claim that setting the PageSize property works and provide an example which does the same as what I've tried. 文档声称设置PageSize属性是有效的,并提供了一个与我尝试过的相同的示例。 (Other than that, based on this docs page I can't even tell the unit of the numbers to use.) (除此之外,基于此文档页面,我甚至无法告诉单位使用的数字。)
  • The docs also point to the DocumentPage.Size property , however that property cannot be publicly changed. 文档还指向DocumentPage.Size属性 ,但该属性不能公开更改。 Do I really have to override some page class before adding the page to the document just to get a different page size? 在将页面添加到文档之前,我是否真的必须覆盖某些页面类才能获得不同的页面大小?
  • This forum posting describes the same problem, but the answer seems nonsensical to me. 这个论坛帖子描述了同样的问题,但答案对我来说似乎是荒谬的。 I am using the DocumentPaginator property only ever once, so there is no "calling (...).DocumentPaginator again" for which I could save an instance. 我只使用DocumentPaginator属性一次,因此没有“再次调用(...)。DocumentPaginator”,我可以保存一个实例。
  • This question sounds promising, but it is actually not about the page size, but about the scale of an image on a given page. 这个问题听起来很有希望,但实际上并不是关于页面大小,而是关于给定页面上图像的比例。
  • Aside from the aforementioned PageSize property (which is set to what seems to be the default size anyway here), this tutorial uses the Width and Height properties of a FixedPage . 除了前面提到的PageSize属性(此处设置为默认大小), 本教程使用FixedPageWidthHeight属性。 However, assigning some positive random values to these for a quick test would result in my XPS document apparently being corrupted and XPS Viewer displaying an error message when opening it. 但是,为这些快速测试分配一些正的随机值会导致我的XPS文档显然已损坏,并且XPS Viewer在打开时会显示错误消息。

FixedDocuments have fixed pages. FixedDocuments有固定的页面。 The height and width of FixedPage can be controlled. 可以控制FixedPage的高度和宽度。 Somewhat like this: 有点像这样:

        FixedPage pageOne = new FixedPage();
        pageOne.Height = 20;
        pageOne.Width = 10;

or in XAML: 或者在XAML中:

Height="20" Width="10" 高度=“20”宽度=“10”

I believe a FixedDocument will only print at the size of its pages. 我相信FixedDocument只会以其页面大小打印。 Even when loading a FixedDocument into a DocumentViewer, changing the printer settings' page size when you click the print button will have no effect. 即使将FixedDocument加载到DocumentViewer中,单击打印按钮时更改打印机设置的页面大小也无效。 A FixedDocument by its very definition preserves the fidelity of the its contents exactly. 一个FixedDocument按其定义保留了其内容的保真度。

The only way to modify it is to create a derived DocumentPaginator which calls the FixedDocument.DocumentPaginator's functions internally and modifies the return values accordingly. 修改它的唯一方法是创建一个派生的DocumentPaginator,它在内部调用FixedDocument.DocumentPaginator的函数并相应地修改返回值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM