在ShowDialog()之后更改打印属性时,无法使用PrintDialog进行打印

时间:2017-02-12 12:42:02

标签: c# wpf printing printdocument printdialog

当我在设置WPF控件的打印页面属性(高度,宽度,页数)然后使用所需文档调用PrintDocument()之前使用ShowDialog()时,会出现问题,这些步骤的结果是打印空页。

但是当我正在执行这些步骤VICE VERSA(在调用ShowDialog之前设置打印页面属性)它可以正常工作。 但这不是理想的流程,因为我希望用户能够在设置打印页面属性之前更改所选的打印机。

我该如何解决这个问题?

   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;

    }

0 个答案:

没有答案