1

I'm programmatically generating a FixedDocument to help me print. FixedDocument.ActualWidth is coming out as 0. I suspect this is because I am not actually displaying the FixedDocument. How can I add and display a FixedDocument object?

This is a beginner question. I'm not skilled with WPF. I looked on MSDN/Goog. Sites make the assumption that I've already added the FixedDocument and just need to manipulate it.

I have:

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

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

        return fixedDocument;
    }

Pseudocode of what I want: myWpfFormObject.AddChild(fixedDocument)

P.Brian.Mackey
  • 43,228
  • 68
  • 238
  • 348
  • Why do you need the ActualWidth value? – dovid Oct 07 '13 at 16:36
  • Please try `content.UpdateLayout();`. – LPL Oct 07 '13 at 16:38
  • @lomed - It's used in calculating the print document dimensions. The best example I am aware of for printing in this way: http://www.a2zdotnet.com/View.aspx?id=66#.UlLSyGYo5aQ – P.Brian.Mackey Oct 07 '13 at 16:39
  • @lomed If you can show me an alternative method to print accurately that does not use the variables in the link I'm happy to switch. Right now, it's the only thing that works. – P.Brian.Mackey Oct 07 '13 at 19:20

2 Answers2

3

for show FixedDocument:

in your Wpf window, add the DocumentViewer Controle, then set the Document property.

for ActualWidth pb:

I think you should call the methods Measure & Arrange for each FixedPage.

See the code below from the exapmle in msdn:

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

see also https://stackoverflow.com/a/1695518/1271037

Community
  • 1
  • 1
dovid
  • 6,354
  • 3
  • 33
  • 73
0

So I had a slightly different situation, but this answer got me close. I'm using the fixed document to display Tiffs from a scanner. Some of those Tiffs can be in legal letter format (so longer than the standard A4 8.5 by 11 size). The code below fixed my issue and this answer helped.

So ended up I'm taking a fixed document, creating a page content, creating a fixed page, creating an image.

Then taking the image and adding it to the fixed page, then taking the fixed page and adding it to the page content, then taking the page content and adding it to the fixed document.

            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;
Cyrus Downey
  • 213
  • 3
  • 6