に印刷HTML私はHTMLファイルを作成し、C#WPF .NET 4.6のプログラムを持っていると私は知られている、デフォルト以外のプリンタで(ダイアログなし)に自動的にそれらを印刷したいと思います。もちろん、HTMLのレンダリングも含まれます。プログラムによってこれらのファイルが作成されるため、HTMLデータはMemoryStream
,FileStream
、または文字列から直接取得できます。レンダリングと、デフォルト以外のプリンタ
プログラムは、ユーザが、各ファイルが別のプリンタを必要とするかもしれないように、System.Drawing.Printing.PrinterSettings.InstalledPrinters
を用いて、予めに印刷するためにどのプリンタを指定することを可能にする設定を有しています。印刷時に、プリンタの名前が知られているが、おそらくWindowsのデフォルトのプリンタとは異なります。
私は他のプロジェクトの多くを研究してきたが、彼らは、プリンタがデフォルトと異なることを考慮していないようです。デフォルトのプリンタを変更することは反社会的であり、スレッド関連の苦痛の原因となるでしょう。これは#1の受け入れられた解決策であるようですが、最善の解決策ではありませんか?研究ソリューションを見た
:COM ExecWB機能は、デフォルトのプリンタに印刷するように
Printing the contents of a WPF WebBrowserとSilently print HTML from WPF WebBrowserと corresponding MSDNforum discussions は不十分である
MSDN exampleのみ使用(?)再びデフォルトのプリンタを使用していますWebBrowser
、上の印刷()コマンド。
だから私は、プリンタオプションを変更しようとしているのルートを下って行きました。 Programmatically changing the destination printer for a WinForms WebBrowser controlが尋ねられましたが、リンクが壊れていて、不満足な答えがあります。実行中のコンピュータの外部プログラムがわからないので、Adobe、OpenOfficeなどを保証することはできません。OPは、COM細部までは行かない。音がトリッキー。
おそらく、this projectのようなRichTextBoxに書き込んでから何かを拾い読みして、ボックスを隠すことができますか?
私はSilent print HTML file in C# using WPFが良いパスにあると思っていましたが、元の投稿には画面サイズのハードコード番号があり、OPはプリンタがドキュメントを切断したことを示しました。受け入れられた(および取り込まれた)応答は、ExecWBのデフォルトのプリンタ設定方法を再度使用します。その答えは、MSDN answerを更新しましたよう
execCommand("Print", false, IDon'tUnderstandThisArgument)も、約束を示したが、プリンタに送信されたファイルストリームは、HTMLを許可していません、またWebBrowser
からDocumentStream
は(プリンタが1つの空白のページを印刷します)動作しているように見えません。
How do I programatically change printer settings with the WebBrowser control?は、解決策としてのレジストリの変更を除いて、私に非常に似た要件を持っています。他の人がそれを行っている方法を研究以外
は、私もそれがVisual
コントロールで直接としてWPF WebBrowser
を印刷してみました:(WebBrowser
が表示されていないため?)
public static bool Print(string printer, Visual objToPrint)
{
if (string.IsNullOrEmpty(printer))
{
return false;
}
var dlg = new PrintDialog
{
PrintQueue = new PrintServer().GetPrintQueue(printer)
};
dlg.PrintTicket.CopyCount = 1;
dlg.PrintTicket.PageOrientation = PageOrientation.Portrait;
dlg.PrintTicket.PagesPerSheet = 1;
dlg.PrintVisual(objToPrint, "Print description");
return true;
}
しかし、これは何も印刷されません。 。
public static async Task<bool> PrintHTMLAsync(string printer, string html)
{
bool result;
using (var webBrowser = new System.Windows.Forms.WebBrowser())
{
webBrowser.DocumentCompleted += ((sender, e) => browserReadySemaphore.Release());
byte[] buffer = Encoding.UTF8.GetBytes(html);
webBrowser.DocumentStream = new MemoryStream(buffer);
// Wait until the page loads.
await browserReadySemaphore.WaitAsync();
try
{
using (PrintDocument pd = new PrintDocument())
{
pd.PrinterSettings.PrinterName = printer;
pd.PrinterSettings.Collate = false;
pd.PrinterSettings.Copies = 1;
pd.PrinterSettings.FromPage = 1;
pd.PrinterSettings.ToPage = 1;
pd.Print();
result = true;
}
}
catch (Exception ex)
{
result = false;
Debug.WriteLine(ex);
}
return result;
}
}
をなし喜びで: が更新MSDN articleとしてPrintDocument
を提案しようとしました。
私はまた、PRINT DOSコマンドを使用しました:
public static string PerformSilentPrinting(string fileName, string printerName)
{
try
{
ProcessStartInfo startInfo = new ProcessStartInfo(fileName)
{
Arguments = string.Format("/C PRINT /D:\"{0}\" \"{1}\"", printerName, fileName),
FileName = "cmd.exe",
RedirectStandardOutput = true,
UseShellExecute = false,
WindowStyle = ProcessWindowStyle.Hidden,
};
// Will execute the batch file with the provided arguments
Process process = Process.Start(startInfo);
// Reads the output
return process.StandardOutput.ReadToEnd();
}
catch (Exception ex)
{
return ex.ToString();
}
}
が、printコマンドは、テキストファイルだけを受け入れているようです。