How to print GeckoWebBrowser to default printer?

ぃ、小莉子 提交于 2019-12-23 18:14:40

问题


I'm trying to print the document in a GeckoWebBrowser, but documentation is limited and to me, it's not at all clear.

I found some code on the internet that at least communicates with the printer (it starts beeping) but I think the printer is asking for a Letter size paper, but it requires the settings to be from print.GetGlobalPrintSettingsAttribute(), if I try my own settings, it gives me a NotImplementedException.

I suspect this is exception is raised on my Gecko.PrinterSettings, because when I swap ps in the print.Print(ps, null); with the global settings, this exception isn't raised.

The code below:

        var domWindow = browser.Window.DomWindow;
        var print = Gecko.Xpcom.QueryInterface<Gecko.nsIWebBrowserPrint>(domWindow);

        Gecko.PrintSettings ps = new Gecko.PrintSettings();
        ps.SetPrintSilentAttribute(false);
        ps.SetPrintToFileAttribute(false);
        ps.SetShowPrintProgressAttribute(false);
        ps.SetOutputFormatAttribute(1); //2 == PDF, so I assume 1 is actual printer
        ps.SetPrintBGImagesAttribute(true);
        ps.SetStartPageRangeAttribute(1);
        ps.SetEndPageRangeAttribute(100);
        ps.SetPrintOptions(2, true); // evenPages
        ps.SetPrintOptions(1, true); // oddpages
        ps.SetEffectivePageSize(768 * 20f, 1024 * 20f);
        ps.SetShrinkToFitAttribute(true);
        ps.SetScalingAttribute(1.0);
        ps.SetPrintBGImagesAttribute(true);

        print.Print(ps, null);

回答1:


Managed to come up with a solution.

What was throwing an exception was

public void SetPersistMarginBoxSettingsAttribute(bool aPersistMarginBoxSettings)
{
    throw new NotImplementedException();
}

The above is in PrinterSettings.cs, so it is hard-coded coded to throw a NotImplementedException on a number off attributes (the attribute above isn't the only one hard-coded to throw the exception) as it is not finished(?), so I cannot use it.

However, I can use the GetGlobalSettingsAttribute() as it uses the same interface as PrinterSettings (nsiPrintSettings), so therefore it will have the same attributes all populated for me.

So what can I do is:

I simply copy the GetGlobalPrintSettingsAttribute() into my own printer settings, and adjust them as necessary.

var mySettings = print.GetGlobalPrintSettingsAttribute();
mySettings.SetPrintSilentAttribute(true);
mySettings.SetPrintToFileAttribute(true);
mySettings.SetShowPrintProgressAttribute(false);
mySettings.SetOutputFormatAttribute(2); //2 == PDF
mySettings.SetToFileNameAttribute(@"c:\temp\temp.pdf");
mySettings.SetPrintBGImagesAttribute(true);
mySettings.SetStartPageRangeAttribute(1);
mySettings.SetEndPageRangeAttribute(100);
mySettings.SetPrintOptions(2, true); // evenPages
mySettings.SetPrintOptions(1, true); // oddpages
mySettings.SetShrinkToFitAttribute(true);
mySettings.SetScalingAttribute(1.0);
mySettings.SetPrintBGImagesAttribute(true);

print.Print(mySettings, new Gecko.WebProgressListener());
  • Please notice I reverted back to PDF for now, in the SetOutputFormatAttribute(2); //2 == PDF

  • Also changed the print.Print(ps, null); to print.Print(mySettings, new Gecko.WebProgressListener()); but I think having null or Gecko.WebProgressListener() won't make a difference.

Et voilà! - Now, onto the next step, which is to print to a printer, and not as a PDF file.



来源:https://stackoverflow.com/questions/45433937/how-to-print-geckowebbrowser-to-default-printer

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!