WPF DocumentViewer Print Landscape Content Cut Off

415 Views Asked by At

I'm using DocumentViewer in a WPF XAML project. I create a fixed document with the following code. The page looks fine in the viewer. When I print it, it prints in landscape view, but the top 1-2 cm is cut off! I tried it without the scale transform on the dockpanel but that had no effect.

    private void PrintReport()
    {
        PrintDialog pd = new PrintDialog();
        pd.PrintQueue = LocalPrintServer.GetDefaultPrintQueue();
        pd.PrintTicket = pd.PrintQueue.DefaultPrintTicket;            
        pd.PrintTicket.PageOrientation = PageOrientation.Landscape;
        pd.PrintDocument(docViewer.Document.DocumentPaginator, "DaySheet");
    }

    public void BuildReport(DockPanel dpPrint)
    {
        try
        {
            // init
            ScaleTransform st = new ScaleTransform(0.5, 0.5, 0, 0);
            dpPrint.RenderTransform = st;

            Size pgSize = new Size(96 * 11.69, 96 * 8.27);
            FixedDocument fd = new FixedDocument();
            
            // add page content
            FixedPage fp = new FixedPage();
            fp.Width = pgSize.Width;
            fp.Height = pgSize.Height;
            fp.Children.Add(dpPrint);

            // set page content
            PageContent pc = new PageContent();
            pc.Child = fp;
            fd.Pages.Add(pc);

            // set up fresh XpsDocument
            var uri = new Uri("pack://daysheet_report.xps");
            PackageStore.RemovePackage(uri);
            var stream = new MemoryStream();
            var package = Package.Open(stream, FileMode.Create, FileAccess.ReadWrite);
            PackageStore.AddPackage(uri, package);
            var xpsDoc = new XpsDocument(package, CompressionOption.NotCompressed, uri.AbsoluteUri);

            // write FixedDocument to the XpsDocument
            var docWriter = XpsDocument.CreateXpsDocumentWriter(xpsDoc);
            docWriter.Write(fd);

            // display XpsDocument in DocumentViewer
            docViewer.Document = xpsDoc.GetFixedDocumentSequence();
            docViewer.Document.DocumentPaginator.PageSize = pgSize;                
        }
        catch (Exception ex)
        {
            gFunc.ProcessError(ex);
        }
    }
1

There are 1 best solutions below

0
Ross Kelly On

I tried playing around with printer drivers but it seemed to make no difference. The printer driver I have installed is the correct one for my laser printer and the right version for Windows 10 64-bit. When I print from Word, Excel, PDF its all fine, but from my WPF app it crops the top part of the page content.

As is so often the case, I got around the problem with a hack. I just changed the page content to use a StackPanel and I placed a blank TextBlock as the first child to push the other content down.