2

The issue occurs when i am using ShowDialog() before setting the Print page properties of my WPF Control (Height ,width ,Number of pages) and then calling PrintDocument() with the desired document, the result of those steps is an empty page printed.

BUT when I'm doing those steps VICE VERSA (setting the Print page properties BEFORE calling ShowDialog) it works. But that's not the desired flow, because i want the user to be able to change the selected printer before I'm setting the print page properties.

How can i solve this issue?

   void OnIMSendToPrint(object objId, object data)
    {
        PrintDialog printDialog = new PrintDialog();
        if (printDialog.ShowDialog().Value != true) return;

        CheckPrinterPaperType(printDialog);  // Determine the type of the choosen printer , and the paper type.
        PreparePrintVM(); // Prepare the wpf control to print accord to the paper and print type

        if (mainView != null
            && mainView.PrintScreenView != null
            && mainView.PrintScreenView.flowDocument != null
            )
        {
            int extraWidth = -10;
            if (CurrentPrinterPaperType == PrinterPaperSize.Small) extraWidth = 690;
            mainVM.PrintScreen.SetPageSize(printDialog.PrintableAreaWidth + extraWidth,
                printDialog.PrintableAreaHeight);
            printDialog.PrintDocument(((IDocumentPaginatorSource)(mainView.PrintScreenView.flowDocument))
                .DocumentPaginator, "This is a Flow Document");
        }
    }


   private void CheckPrinterPaperType(PrintDialog printDialog)
    {
        CurrentPrinterPaperType = printDialog.PrintableAreaWidth >= 595 && printDialog.PrintableAreaHeight >=792
            ? PrinterPaperSize.Large
            : PrinterPaperSize.Small;
    }

 private void PreparePrintVM()
    {
        PrintScreenVM newPrintScreen = new PrintScreenVM();
        int NumOfColumsInPrintPage = ScResMgr.GetValue("ImageManager", "NumOfColumsInPrintPage", 2);
        int NumOfRowsInPrintPage = ScResMgr.GetValue("ImageManager", "NumOfRowsInPrintPage", 4);
        int imagesNumPerPage = NumOfColumsInPrintPage * NumOfRowsInPrintPage;
        if (CurrentPrinterPaperType == PrinterPaperSize.Small) imagesNumPerPage = 1;

        foreach (SeriesVM seriesVM in mainVM.Patient.Series)
        {
            List<ImageVM> selectedImageList = seriesVM.GetSelectedImages();
            foreach (ImageVM image in selectedImageList)
                image.LoadImage(true);

            for (int i = 0; i < selectedImageList.Count; i += imagesNumPerPage)
            {
                int numOfImages = (i + imagesNumPerPage > selectedImageList.Count) ? selectedImageList.Count - i : imagesNumPerPage;
                newPrintScreen.AddPage(
                    mainVM.Patient.FirstName,
                    mainVM.Patient.LastName,
                    mainVM.Patient.PatientId,
                    seriesVM.DateTime,
                    selectedImageList.GetRange(i, numOfImages),
                    NumOfColumsInPrintPage,
                    NumOfRowsInPrintPage);
            }
        }
        mainVM.PrintScreen = newPrintScreen;

    }
  • Not so sure I see the problem, the snippet doesn't help much. The user uses PrintDialog to select a paper size so that sets the "print page properties". Maybe you have additional settings you want to use that determines how the document is laid-out on paper. But that only works intuitively when those settings don't depend on the paper size. Compare to Notepad's File > Page Setup dialog. If they do depend on the paper size then you can't do anything but display your settings dialog after the PrintDialog. – Hans Passant Feb 12 '17 at 13:52

0 Answers0