2013-10-07 30 views
1

我正在編程生成一個FixedDocument以幫助我打印。 FixedDocument.ActualWidth即爲0。我懷疑這是因爲我實際上並沒有顯示FixedDocument。如何添加並顯示FixedDocument對象?添加固定文檔並將其顯示爲WPF格式

這是一個初學者的問題。我對WPF不熟練。我看着MSDN/Goog。網站做出這樣的假設,我已經添加了FixedDocument,只需要操作它。

我:我想要什麼

private FixedDocument CreateFixedDocumentWithPages() 
    {    
     FixedDocument fixedDocument = CreateFixedDocument(); 
     fixedDocument.DocumentPaginator.PageSize = size;    

     PageContent content = AddContentFromImage(); 
     fixedDocument.Pages.Add(content); 

     return fixedDocument; 
    } 

僞代碼:myWpfFormObject.AddChild(fixedDocument)

+0

爲什麼你需要ActualWidth值? – dovid

+0

請嘗試'content.UpdateLayout();'。 – LPL

+0

@lomed - 它用於計算打印文檔尺寸。我知道以這種方式打印的最佳示例:http://www.a2zdotnet.com/View.aspx?id=66#.UlLSyGYo5aQ –

回答

4

作秀固定文檔:

在WPF窗口

,添加的DocumentViewer CONTROLE,然後設置文件屬性。

爲ActualWidth的PB:

我想你應該調用的方法測量&安排每個固定頁。

請參閱從exapmle下面的代碼在msdn

Size sz = new Size(8.5 * 96, 11 * 96); 
fixedPage.Measure(sz); 
fixedPage.Arrange(new Rect(new Point(), sz)); 
fixedPage.UpdateLayout(); 

又見https://stackoverflow.com/a/1695518/1271037

+0

有關DocumentViewer.Document屬性的第一個答案是我所需要的。 –

+1

謝謝,但是如果要得到ActualWidth的實際值,我認爲這是沒有必要的和低效的。 – dovid

0

所以我有一個稍微不同的情況,但這個答案讓我接近。我正在使用固定文檔從掃描儀顯示Tiff。其中一些Tiff可以採用合法的字母格式(比標準A4 8.5乘11尺寸更長)。下面的代碼解決了我的問題,這個答案有所幫助。

所以最終我正在採取固定文檔,創建一個頁面內容,創建一個固定頁面,創建一個圖像。

然後拍攝圖像並將其添加到固定頁面,然後將固定頁面添加到頁面內容中,然後獲取頁面內容並將其添加到固定文檔中。

  System.Windows.Documents.FixedPage fixedPage = new System.Windows.Documents.FixedPage(); 

      System.Windows.Documents.PageContent pageContent = new System.Windows.Documents.PageContent(); 
      pageContent.Child = fixedPage; 
      if (fixedDocument == null) 
      { 
       fixedDocument = new System.Windows.Documents.FixedDocument(); 
      } 
      fixedDocument.Pages.Add(pageContent); 


      System.Windows.Controls.Image image = new System.Windows.Controls.Image(); 

      TiffBitmapDecoder decoder = new TiffBitmapDecoder(new Uri(tiffImage, UriKind.Relative), BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnDemand); 
      image.Source = decoder.Frames[0]; 

      fixedPage.Children.Add(image); 

      //Code to make the legal letter size work. 
      Size sz = new Size(decoder.Frames[0].Width, decoder.Frames[0].Height); 
      fixedPage.Width = sz.Width; 
      fixedPage.Height = sz.Height; 

      pageContent.Width = sz.Width; 
      pageContent.Height = sz.Height; 
相關問題