C# XpsDocument忽略我的高度设置
当我试图在XPS文档上声明页面高度时,我遇到了一个问题 到目前为止,我的代码如下所示:C# XpsDocument忽略我的高度设置,c#,wpf,printing,preview,xpsdocument,C#,Wpf,Printing,Preview,Xpsdocument,当我试图在XPS文档上声明页面高度时,我遇到了一个问题 到目前为止,我的代码如下所示: private FixedDocumentSequence GetDocument(DocumentPaginator paginator) { FixedDocumentSequence document = null; string tempFileName = System.IO.Path.GetTempFileName(); File.Delete(tempFileName)
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;
}
问题是我声明的页面大小高度为1122,这是我从以下代码得到的:
var pd = new PrintDialog();
var sz = new Size(pd.PrintableAreaWidth, pd.PrintableAreaHeight);
但当我调查这片土地时
d.DocumentPaginator.PageSize.Height
我可以看到高度是1056,这个高度恰好是北美字母页面格式高度的高度,我已经用一个显式的PageMediaSize指定了一个特定的打印票证还有什么问题
编辑:
这是我编辑过的代码,但结果相同:
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;
}
我查看了使用第一个using块创建的文件,仍然可以看到页面的高度是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">
现在,第二个构造函数:
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());
}
问题是您希望
xpsDocument
具有与您创建的文件相同的维度
创建XpsDocument
时,它将具有默认的FixedDocumentSequence
。由于您正在创建XpsDocument,而不是打开XpsDocument,因此将获得默认的固定文档序列,这就是为什么您将1056
视为高度--默认值是默认值
但是,如果您查看的底部的代码示例,您将看到LoadDocumentViewer
,其中它将旧的XpsDocument
替换为新的
当您调用writer.Write(分页器、打印票据)时
它将使用您提供的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()
编辑:
在您上次编辑中找到了很好的结果。我使用MS Word打印了一份XPS文档,设置为A4大小,所有页面显示为1056 x 816。然后,我创建了一个使用A3大小的XPS文档,最终显示为1587.36 x 1122.56。显然,内部正在发生一些我们看不见的事情;我的猜测是,因为页面大小在维度上相似,所以它只使用1056x816维度?谁知道呢。。。我建议向微软提交一份bug报告
同时,可以尝试更改
d.DocumentPaginator.PageSize
或甚至d.PrintTicket
对象,并尝试以这种方式更改页面大小。如果有办法枚举FixedPage
,您也可以手动更改它们的设置 @RandRandom添加了一个已编辑的。。。我想你已经发现了一些非常有趣的事情。我以前在Win7上作为32位应用程序运行时,不必对我的应用程序执行这些操作。我现在在Win8.1上,我的打印开始变得愚蠢——打印景观,但结果是打印了肖像。我回到了这篇文章,使用了重载构造函数,现在我的结果和预期的一样。谢谢@网景没问题当我看到你是回答这个问题的人时,我不得不哈哈大笑,很高兴看到你能从我的经历中获益。谢谢。你救了我的命!
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()