2013-10-07 34 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; 
相关问题