2014-07-17 5 views
3

XPS 문서에서 PageHeight를 선언하려고 할 때 문제가 있습니다.XpsDocument가 높이 설정을 무시합니다.

내 코드는 지금까지 다음과 같습니다

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이 높이는 NorthAmericanLetter Page 형식 높이의 높이가됩니다. 명시 적 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; 
} 

는 내가 처음 사용하는 블록으로 만든있어 파일에 보면서 난 아직도 볼 수 있습니다

<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가 : 페이지의 높이가 1056의 1.fpage 파일의 출력이다

내 문제의 해결책을 찾았습니다. 내 DocumentPaginator에서 GetPage 메서드를 재정의해야하고 새 DocumentPage (visual)를 반환해야합니다.이 생성자에는 PageSize를 올바르게 설정 한 경우에만 PageSize를 설정할 수있는 오버로드가 있습니다. http://msdn.microsoft.com/en-us/library/ms597306(v=vs.110).aspx

http://msdn.microsoft.com/en-us/library/system.windows.documents.documentpage(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()); 
} 
+0

Win7에서 32 비트 응용 프로그램으로 실행하는 동안 응용 프로그램에서이 작업을 수행하지 않아도되었습니다. 저는 지금 Win8.1에 있는데, 제 인쇄 작업은 조경 작업을하기 시작했습니다. 그러나 결과는 초상화로 인쇄되었습니다. 나는이 포스트에서 끝내고, 오버로드 된 생성자를 사용했고, 이제 나의 결과는 예상대로이다. 감사! – Kcvin

+0

@Netscape 문제가 없습니다. - 당신이 질문에 대답 한 사람이라는 것을 알았을 때 축하해주었습니다. 제 경험으로 이익을 얻을 수 있다는 것을 알았 기 때문에 기뻤습니다. –

+0

감사. 니가 내 목숨을 구했어! – Brcinho

답변

1

문제는 동일한 크기를 가지도록 xpsDocument를 기대하고 있다는 것입니다 당신이 만든 파일의.

XpsDocument을 만들면 기본값은 FixedDocumentSequence입니다. XpsDocument를 만들고 열기를하지 않으므로 기본 고정 문서 시퀀스가 ​​표시됩니다. 따라서 1056이 높이로 표시됩니다. 기본값은 기본값입니다.

FixedDocumentSequence의 코드 예제를 보면 을 새로운 것으로 바꾼 LoadDocumentViewer이 표시됩니다.

writer.Write(paginator, printTicket);으로 전화하면 제공 한 PrintTicket을 사용하여 지정한 파일에 XpsDocument가 기록됩니다.이제 해당 파일 경로를 사용하여 XpsDocument을 작성해야합니다.이 파일 경로를 열면 해당 문서의 FixedDocumentSequencePrintTicket 특성과 일치하는 크기를 얻을 수 있습니다. 의사 코드에 그래서, 당신은이 작업을 수행해야합니다

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입니다. 우리가 볼 수없는 내부적 인 일이 분명히 있습니다. 내 생각 엔 페이지 크기가 차원면에서 비슷하기 때문에 1056 x 816 크기 만 사용합니다. 누가 정말로 알고 있는지 ... Microsoft에 버그 보고서를 제출할 것을 제안합니다.

그동안 d.DocumentPaginator.PageSize 또는 심지어 d.PrintTicket 개 개체를 변경하고 페이지 크기를 변경하려고 시도해보세요. FixedPage을 열거 할 방법이 있다면 수동으로 설정을 변경할 수도 있습니다.

+0

제 편집을 좀보세요. –

+0

@RandRandom 수정 된 것을 추가했습니다 ... 당신이 뭔가 흥미로운 것을 발견했다고 생각합니다. – Kcvin

+0

도움을 주신 덕분에 편집을 참조하십시오. –