I am trying to save an XPS document with a FixedDocument . 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.
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; the page in the resulting XPS document retains its default paper size.
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
.
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
property works and provide an example which does the same as what I've tried. (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. 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. 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
. 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. The height and width of FixedPage can be controlled. Somewhat like this:
FixedPage pageOne = new FixedPage();
pageOne.Height = 20;
pageOne.Width = 10;
or in XAML:
Height="20" Width="10"
I believe a FixedDocument will only print at the size of its pages. Even when loading a FixedDocument into a DocumentViewer, changing the printer settings' page size when you click the print button will have no effect. A FixedDocument by its very definition preserves the fidelity of the its contents exactly.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.