当我试图在XPS文档上声明PageHeight时,我面临着一个问题。
到目前为止,我的代码如下:
private FixedDocumentSequence GetDocument(DocumentPaginator paginator)
{
FixedDocumentSequence document = null;
string tempFileName = System.IO.Path.GetTempFileName();
File.Delete(tempFileName);
using (var xpsDocument = new XpsDocument(tempFileName, FileAccess.ReadWrite, CompressionOption.NotCompressed))
{
var writer = XpsDocument.CreateXpsDocumentWriter(xpsDocument);
var printTicket = new PrintTicket
{
PageMediaSize = new PageMediaSize(PageMediaSizeName.ISOA4, paginator.PageSize.Width, paginator.PageSize.Height),
PageBorderless = PageBorderless.Borderless,
PageMediaType = PageMediaType.None,
};
writer.Write(paginator, printTicket);
//paginator.PageSize.Height = 1122
var d = xpsDocument.GetFixedDocumentSequence();
//d.DocumentPaginator.PageSize.Height = 1056
document = d;
}
return document;
}问题是,我声明的PageSize高度为1122,这是从下面的代码得到的:
var pd = new PrintDialog();
var sz = new Size(pd.PrintableAreaWidth, pd.PrintableAreaHeight);但当我调查财产的时候
d.DocumentPaginator.PageSize.Height我可以看到,这个高度是1056,这个高度恰好是NorthAmericanLetter页面格式的高度,我已经用显式PageMediaSize指定了一个特定的printTicket,还有什么问题呢?
编辑:
下面是我编辑的代码,但结果是相同的:
private FixedDocumentSequence GetDocument(DocumentPaginator paginator)
{
FixedDocumentSequence document = null;
string oldTempFileName = System.IO.Path.GetTempFileName();
File.Delete(oldTempFileName);
XpsDocument oldXpsDocument;
using (oldXpsDocument = new XpsDocument(oldTempFileName, FileAccess.ReadWrite, CompressionOption.NotCompressed))
{
var writer = XpsDocument.CreateXpsDocumentWriter(oldXpsDocument);
var printTicket = new PrintTicket
{
PageMediaSize = new PageMediaSize(PageMediaSizeName.ISOA4, paginator.PageSize.Width, paginator.PageSize.Height),
PageBorderless = PageBorderless.Borderless,
PageMediaType = PageMediaType.None,
};
writer.Write(paginator, printTicket);
}
//string newTempFileName = System.IO.Path.GetTempFileName();
//File.Delete(newTempFileName);
using (var newXpsDocument = new XpsDocument(oldTempFileName, FileAccess.Read, CompressionOption.NotCompressed))
{
var d = newXpsDocument.GetFixedDocumentSequence();
document = d;
}
return document;
}我查看了使用第一个使用块创建的文件,仍然可以看到页面的高度为1056,即1.fpage文件的输出:
<FixedPage
xmlns="http://schemas.microsoft.com/xps/2005/06" xmlns:x="http://schemas.microsoft.com/xps/2005/06/resourcedictionary-key"
xml:lang="und"
Width="816" Height="1056">
<FixedPage.Resources>
<ResourceDictionary>
<LinearGradientBrush x:Key="b0" StartPoint="33,0" EndPoint="33,23" ColorInterpolationMode="SRgbLinearInterpolation" MappingMode="Absolute" SpreadMethod="Pad">Edit2:
找到了解决我问题的方法。在我的DocumentPaginator中,我必须重写GetPage方法,在那里我返回一个新的DocumentPage(可视),这个构造函数有一个重载,只有在正确设置它的PageSizes时,才允许我设置它。
http://msdn.microsoft.com/en-us/library/system.windows.documents.documentpage(v=vs.110).aspx http://msdn.microsoft.com/en-us/library/ms597306(v=vs.110).aspx
我以前的代码:
public override DocumentPage GetPage(int pageNumber)
{
// create page element (PageTemplate is a custom usercontrol that can hold content)
var page = new PageTemplate(this, pageNumber + 1);
// arrange the elements on the page
page.Arrange(new Rect(0, 0, PageSize.Width, PageSize.Height));
// return new document page
return new DocumentPage(page);
}现在是第二个修理工:
public override DocumentPage GetPage(int pageNumber)
{
// create page element (PageTemplate is a custom usercontrol that can hold content)
var page = new PageTemplate(this, pageNumber + 1);
// arrange the elements on the page
page.Arrange(new Rect(0, 0, PageSize.Width, PageSize.Height));
// return new document page
return new DocumentPage(page, PageSize, new Rect(0, 0, 10, 10), new Rect());
}发布于 2014-07-17 18:14:05
问题是,您期望xpsDocument具有与您创建的文件相同的维度。
创建XpsDocument时,它将有一个默认的FixedDocumentSequence。由于您正在创建一个XpsDocument,而不是打开一个,所以您将得到默认的固定文档序列,这就是为什么您将1056视为默认的高度-默认值。
但是,如果您查看FixedDocumentSequence底部的代码示例,您将看到LoadDocumentViewer,它用新的XpsDocument替换旧的XpsDocument。
当您调用writer.Write(paginator, printTicket);时,它将向您使用所提供的PrintTicket指定的文件写入一个XpsDocument。现在您需要使用该文件路径创建一个XpsDocument,当您打开它时,您可以获得该文档的FixedDocumentSequence以及与PrintTicket属性匹配的大小。所以在伪代码中,您应该这样做:
string fileName = ...
using Create XpsDocument at fileName
Create XpsDocumentWriter
Create and setup PrintTicket
Write to the file using your DocumentPaginator and PrintTicket
using Create XpsDocument reading from fileName
document = opened XpsDocument GetFixedDocumentSequence()编辑:
很好的发现在你的上一次编辑。我使用带有A4大小设置的MS打印了一个XPS文档,所有的页面都是1056x816。然后,我使用A3大小创建了一个XPS文档,其大小为1587.36 x 1122.56。很明显,内部存在一些我们看不到的东西;我猜想,由于页面大小在维度上是相似的,所以它只使用1056x816维?谁真的知道..。我建议向微软提交一份错误报告。
同时,可以尝试更改d.DocumentPaginator.PageSize或甚至d.PrintTicket对象,并尝试以这种方式更改页面大小。如果有一种方法来枚举FixedPage,您也可以手动更改它们的设置。
https://stackoverflow.com/questions/24808436
复制相似问题