简体   繁体   中英

Add and display FixedDocument to WPF form

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)

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

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;

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