[英]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: 我找到了各种在线资源,但尚未解决我的问题:
PageSize
property works, but it does not as far as I can tell. PageSize
属性是有效的,但它并没有我能说的那么多。 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.) 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? DocumentPaginator
property only ever once, so there is no "calling (...).DocumentPaginator again" for which I could save an instance. DocumentPaginator
属性一次,因此没有“再次调用(...)。DocumentPaginator”,我可以保存一个实例。 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
属性(此处设置为默认大小), 本教程使用FixedPage
的Width
和Height
属性。 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. 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.