コード例 #1
0
        private void StartMakingPdf()
        {
            _print = Xpcom.QueryInterface <nsIWebBrowserPrint>(_browser.Window.DomWindow);

            var service = Xpcom.GetService <nsIPrintSettingsService>("@mozilla.org/gfx/printsettings-service;1");

            _printSettings = service.GetNewPrintSettingsAttribute();

            _printSettings.SetToFileNameAttribute(_pathToTempPdf);
            _printSettings.SetPrintToFileAttribute(true);
            _printSettings.SetPrintSilentAttribute(true);             //don't show a printer settings dialog
            _printSettings.SetShowPrintProgressAttribute(false);

            if (_conversionOrder.PageHeightInMillimeters > 0)
            {
                _printSettings.SetPaperHeightAttribute(_conversionOrder.PageHeightInMillimeters);
                _printSettings.SetPaperWidthAttribute(_conversionOrder.PageWidthInMillimeters);
                _printSettings.SetPaperSizeUnitAttribute(1);                 //0=in, >0 = mm
            }
            else
            {
                //doesn't actually work.  Probably a problem in the geckofx wrapper. Meanwhile we just look it up from our small list
                //printSettings.SetPaperNameAttribute(_conversionOrder.PageSizeName);

                var          size = GetPaperSize(_conversionOrder.PageSizeName);
                const double inchesPerMillimeter = 0.0393701;                   // (or more precisely, 0.0393700787402)
                _printSettings.SetPaperHeightAttribute(size.HeightInMillimeters * inchesPerMillimeter);
                _printSettings.SetPaperWidthAttribute(size.WidthInMillimeters * inchesPerMillimeter);
            }

            // BL-2346: On Linux the margins were not being set correctly due to the "unwritable margins"
            //          which were defaulting to 0.25 inches.
            _printSettings.SetUnwriteableMarginTopAttribute(0d);
            _printSettings.SetUnwriteableMarginBottomAttribute(0d);
            _printSettings.SetUnwriteableMarginLeftAttribute(0d);
            _printSettings.SetUnwriteableMarginRightAttribute(0d);

            //this seems to be in inches, and doesn't have a unit-setter (unlike the paper size ones)
            const double kMillimetersPerInch = 25.4;             // (or more precisely, 25.3999999999726)

            _printSettings.SetMarginTopAttribute(_conversionOrder.TopMarginInMillimeters / kMillimetersPerInch);
            _printSettings.SetMarginBottomAttribute(_conversionOrder.BottomMarginInMillimeters / kMillimetersPerInch);
            _printSettings.SetMarginLeftAttribute(_conversionOrder.LeftMarginInMillimeters / kMillimetersPerInch);
            _printSettings.SetMarginRightAttribute(_conversionOrder.RightMarginInMillimeters / kMillimetersPerInch);

            _printSettings.SetOrientationAttribute(_conversionOrder.Landscape ? 1 : 0);
            _printSettings.SetHeaderStrCenterAttribute("");
            _printSettings.SetHeaderStrLeftAttribute("");
            _printSettings.SetHeaderStrRightAttribute("");
            _printSettings.SetFooterStrRightAttribute("");
            _printSettings.SetFooterStrLeftAttribute("");
            _printSettings.SetFooterStrCenterAttribute("");

            _printSettings.SetPrintBGColorsAttribute(true);
            _printSettings.SetPrintBGImagesAttribute(true);

            _printSettings.SetShrinkToFitAttribute(false);


            //TODO: doesn't seem to do anything. Probably a problem in the geckofx wrapper
            //printSettings.SetScalingAttribute(_conversionOrder.Zoom);

            _printSettings.SetOutputFormatAttribute(2);             // 2 == kOutputFormatPDF

            Status = "Making PDF..";
            if (_conversionOrder.FirstPageToPrint > 0)
            {
                _printSettings.SetPrintRangeAttribute(1);                       // print only a range of pages
                if (_conversionOrder.LastPageToPrint < _conversionOrder.FirstPageToPrint)
                {
                    _conversionOrder.LastPageToPrint = _conversionOrder.FirstPageToPrint;
                }
                _printSettings.SetStartPageRangeAttribute(_conversionOrder.FirstPageToPrint);
                _printSettings.SetEndPageRangeAttribute(_conversionOrder.LastPageToPrint);
            }
            else if (_conversionOrder.ReduceMemoryUse)
            {
                _printSettings.SetPrintRangeAttribute(1);                       // print a range of pages
                _printSettings.SetStartPageRangeAttribute(1);
                _printSettings.SetEndPageRangeAttribute(1);
                _currentPage = 1;
                _currentFile = String.Format("{0}-page{1:000}", _pathToTempPdf, _currentPage);
                _printSettings.SetToFileNameAttribute(_currentFile);
                _beginPages = DateTime.Now;
                Status      = "Making Page 1 of PDF...";
            }

            _startMakingPdf = DateTime.Now;
            _print.Print(_printSettings, this);
            _checkForPdfFinishedTimer.Enabled = true;
        }