简体   繁体   中英

XpsDocument ignores my height settings

I am facing a problem when I try to declare a PageHeight on an XPS Document.

My code so far looks like this:

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;
}

The Problem is that I am declaring a PageSize Height of 1122 which I am getting from this code:

var pd = new PrintDialog();
var sz = new Size(pd.PrintableAreaWidth, pd.PrintableAreaHeight);

but when I look into the property

d.DocumentPaginator.PageSize.Height

I can see that the Height is 1056 , this height happens to be the Height of the NorthAmericanLetter Page format height, I am already specifying a specific printTicket with an explicit PageMediaSize what else could be wrong?

Edit:

Here is my edited code, but this give the same result:

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;
}

I looked into the file that got created with the first using block and I still can see that the height of the pages is 1056, output of the 1.fpage file:

<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:

Found the a solution for my problem. In my DocumentPaginator I had to override the GetPage Method and there I return a new DocumentPage(visual), this constructor has an overload that lets me set the PageSize only if I set it there its setting the PageSizes correctly.

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

My previous code:

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);
}

Now with the second contructor:

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());
}

The problem is that you're expecting the xpsDocument to have the same dimensions of the file that you created.

When you create an XpsDocument , its going to have a default FixedDocumentSequence . Since you're creating an XpsDocument, and not opening one, you're getting the default fixed document sequence, which is why you are seeing 1056 as the height--default that is the default.

However, if you look at the code example at the bottom of FixedDocumentSequence , you'll see LoadDocumentViewer , where it replaces an old XpsDocument with a new one.

When you call writer.Write(paginator, printTicket); it will write an XpsDocument to the file you specified using the PrintTicket you provided. Now you need to create an XpsDocument using that file path, and when you open it, you can then get the FixedDocumentSequence of that document and the sizes with match the PrintTicket properties. So in pseudo-code, you should do this:

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()

Edit:

Good find in your last edit. I printed an XPS document using MS Word with the A4 sized setting and all the pages come up as 1056 x 816 also. I then created an XPS document using the A3 size and that comes up as 1587.36 x 1122.56. There is clearly something going on internally that we cannot see; my guess is that since the page sizes are similar in dimension, it just uses the 1056 x 816 dimensions? Who really knows... I would suggest filing a bug report to Microsoft.

In the meantime, maybe try to change d.DocumentPaginator.PageSize or even d.PrintTicket objects and try to get the page size to change that way. If there is a way to enumerate the FixedPage 's you could change their settings manually too.

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.

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