メトロ アプリで TextBox の内容を印刷するにはどうすればよいですか? MSDN のこのクイックスタート ガイドと多くのオンライン チュートリアルを読みましたが、それらは非常に複雑で、TextBox コントロールでは機能せず、RichTextBoxコントロールのみで機能します。
メトロ アプリで TextBox コントロールから印刷するにはどうすればよいですか? それは可能ですか?どのように?
メトロ アプリで TextBox の内容を印刷するにはどうすればよいですか? MSDN のこのクイックスタート ガイドと多くのオンライン チュートリアルを読みましたが、それらは非常に複雑で、TextBox コントロールでは機能せず、RichTextBoxコントロールのみで機能します。
メトロ アプリで TextBox コントロールから印刷するにはどうすればよいですか? それは可能ですか?どのように?
更新 1
テキスト ボックスの内容の印刷を簡素化するヘルパー クラスを作成しました。NuGetを介してヘルパー クラスを追加できます。私の既存のヘルパー クラスを強化したい場合は、GitHubでフォークしてください
ここで、MSDN からの変更された印刷サンプルを提供します。私はあなたが何でも書くことができるテキストボックスを入れました、そしてそれは印刷されます。テキストボックスのテキストを書式設定 (太字、斜体、下線、色) とまったく同じように印刷するサンプルを作成していないことに注意してください。ハードコードされた印刷形式を設定しました。独自のフォーマットを作成できます。
スタック オーバーフローには回答の文字数制限があり、コードが長すぎるため、CodePaste.net リンクを投稿します。
XAML : http://codepaste.net/9nf261
CS: http://codepaste.net/q3hsm3
いくつかの画像を使用していることに注意してください。画像は「Images」フォルダーに入れます
テキストボックス (textBox1) とボタン (button1) を備えた小さな winforms-application を作成しました。コード ビハインドは次のようになります。
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void PrintDocumentOnPrintPage(object sender, PrintPageEventArgs e)
{
e.Graphics.DrawString(this.textBox1.Text, this.textBox1.Font, Brushes.Black, 10, 25);
}
private void button1_Click(object sender, EventArgs e)
{
PrintDocument printDocument = new PrintDocument();
printDocument.PrintPage += PrintDocumentOnPrintPage;
printDocument.Print();
}
}
ボタンをクリックするだけで、印刷が完了します。
ここで私の質問を確認することをお勧めしますここでは、ページから何かを印刷できる最も単純なケースの 1 つを示します (そして、そのコードを現在持っている任意のページに追加できます。使用するサンプル テキスト ボックスのテキストを、あなたの心が望むものに置き換えるだけです)。彼らがリッチ テキスト ボックスを使用する理由は、テキストがページからはみ出すことを検出できるためです。したがって、その情報を使用して、オーバーフローが発生しなくなるまで、別のリッチ テキスト ボックスで新しいページを作成します。いずれにせよ、独自の文字列パーサーを使用して、テキストを別のページに分割できます。基本的に、Windows 8 アプリで印刷すると、必要な UIElement がすべて印刷されるため、XAML でプログラムによってページを調整し、他の Windows アプリと同じようにスタイルを設定できます。真剣に、質問をチェックしてください。それは大きな助けになるでしょう。PrintSample がどのように機能するかを理解するまで、何時間も費やして最も単純なケースまで PrintSample をハッキングしました。車輪を再発明する意味はありません。私の闘争を有利に利用してください。それがスタックのすべてです。乾杯!
編集:皆さんの便宜のために、ここにコードを提示します。
ステップ 1: このコードをテキスト ボックスのあるページに追加します。
protected PrintDocument printDocument = null;
protected IPrintDocumentSource printDocumentSource = null;
internal List<UIElement> printPreviewElements = new List<UIElement>();
protected event EventHandler pagesCreated;
protected void PrintTaskRequested(PrintManager sender, PrintTaskRequestedEventArgs e)
{
PrintTask printTask = null;
printTask = e.Request.CreatePrintTask("C# Printing SDK Sample", sourceRequested =>
{
printTask.Completed += async (s, args) =>
{
if (args.Completion == PrintTaskCompletion.Failed)
{
await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () =>
{
MessageDialog dialog = new MessageDialog("Something went wrong while trying to print. Please try again.");
await dialog.ShowAsync();
});
}
};
sourceRequested.SetSource(printDocumentSource);
});
}
protected void RegisterForPrinting()
{
printDocument = new PrintDocument();
printDocumentSource = printDocument.DocumentSource;
printDocument.Paginate += CreatePrintPreviewPages;
printDocument.GetPreviewPage += GetPrintPreviewPage;
printDocument.AddPages += AddPrintPages;
PrintManager printMan = PrintManager.GetForCurrentView();
printMan.PrintTaskRequested += PrintTaskRequested;
}
protected void UnregisterForPrinting()
{
if (printDocument != null)
{
printDocument.Paginate -= CreatePrintPreviewPages;
printDocument.GetPreviewPage -= GetPrintPreviewPage;
printDocument.AddPages -= AddPrintPages;
PrintManager printMan = PrintManager.GetForCurrentView();
printMan.PrintTaskRequested -= PrintTaskRequested;
}
}
protected void CreatePrintPreviewPages(object sender, PaginateEventArgs e)
{
printPreviewElements.Clear();
PrintTaskOptions printingOptions = ((PrintTaskOptions)e.PrintTaskOptions);
PrintPageDescription pageDescription = printingOptions.GetPageDescription(0);
AddOnePrintPreviewPage(pageDescription);
if (pagesCreated != null)
{
pagesCreated.Invoke(printPreviewElements, null);
}
((PrintDocument)sender).SetPreviewPageCount(printPreviewElements.Count, PreviewPageCountType.Intermediate);
}
protected void GetPrintPreviewPage(object sender, GetPreviewPageEventArgs e)
{
((PrintDocument)sender).SetPreviewPage(e.PageNumber, printPreviewElements[e.PageNumber - 1]);
}
protected void AddPrintPages(object sender, AddPagesEventArgs e)
{
foreach (UIElement element in printPreviewElements)
{
printDocument.AddPage(element);
}
((PrintDocument)sender).AddPagesComplete();
}
protected void AddOnePrintPreviewPage(PrintPageDescription printPageDescription)
{
TextBlock block = new TextBlock();
block.Text = "This is an example.";
block.Width = printPageDescription.PageSize.Width;
block.Height = printPageDescription.PageSize.Height;
printPreviewElements.Add(block);
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
RegisterForPrinting();
}
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
UnregisterForPrinting();
}
ステップ 2: block.Text を目的のテキストに置き換えます。
ステップ 3: 印刷ボタンを使用して印刷 UI を表示します。
private async void PrintDocument(object sender, RoutedEventArgs e)
{
await Windows.Graphics.Printing.PrintManager.ShowPrintUIAsync();
}
ステップ 4: App.xaml に RequestedTheme="Light" を追加すれば完了です。注: 別の方法として、この XAML クラスで必要な方法でテキスト ボックスのスタイルを設定できる場合があり、アプリ全体のテーマを設定する必要はありません。
ステップ 5 (後で): 新しいページを作成するためにそのメソッドを常に呼び出し続ける独自の新しいページ検出ロジックを追加することを検討することをお勧めします。
ステップ 6 (今すぐ): 私たちを苦しめている責任者である M$ の男と戦いましょう。