반응형
https://www.c-sharpcorner.com/uploadfile/mahesh/printing-in-wpf/
1. 단순 출력 기초
<MainWindow.xaml>
<Window x:Class="wpf10_767id.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:wpf10_767id"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Button Content="printButton" Height="37" HorizontalAlignment="Left" Margin="22,21,0,0"
Name="PrintSimpleTextButton" VerticalAlignment="Top" Width="134"
Click="printButton_Click" />
</Grid>
</Window>
<MainWindow.xaml.cs>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace wpf10_767id
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
// this.PrintSimpleTextButton.Click += printButton_Click;
}
private void printButton_Click(object sender, RoutedEventArgs e)
{
// Create a PrintDialog
PrintDialog printDialog = new PrintDialog();
if(printDialog.ShowDialog().GetValueOrDefault())
{
FontFamily fontFamily =new FontFamily("나눔고딕코딩");
FlowDocument doc = new FlowDocument(new Paragraph(new Run("출력 텍스트")));
doc.Name="FlowDoc";
IDocumentPaginatorSource idpSource = doc;
printDialog.PrintDocument(idpSource.DocumentPaginator, "(description)WPF Printing");
}
}
}
}
2. 글씨체 편집
<MainWindow.xaml.cs 수정>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace wpf10_767id
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
// this.PrintSimpleTextButton.Click += printButton_Click;
}
private void printButton_Click(object sender, RoutedEventArgs e)
{
// Create a PrintDialog
PrintDialog printDialog = new PrintDialog();
if(printDialog.ShowDialog().GetValueOrDefault())
{
FontFamily fontFamily =new FontFamily("나눔고딕코딩");
//// 함수 추가 ////
// FlowDocument doc = new FlowDocument(new Paragraph(new Run("일부 텍스트가 있습니다.")));
FlowDocument doc = CreateFlowDocument();
doc.Name="FlowDoc";
IDocumentPaginatorSource idpSource = doc;
printDialog.PrintDocument(idpSource.DocumentPaginator, "Hello WPF Printing.");
}
}
//// 함수 추가 ////
private FlowDocument CreateFlowDocument()
{
// Create a FlowDocument
FlowDocument doc = new FlowDocument();
// Create a Section
Section sec = new Section();
// Create first Paragraph
Paragraph p1 = new Paragraph();
// Create and add a new Bold, Italic and Underline
Bold bld = new Bold();
bld.Inlines.Add(new Run("First Paragraph"));
Italic italicBld = new Italic();
italicBld.Inlines.Add(bld);
Underline underlineItalicBld = new Underline();
underlineItalicBld.Inlines.Add(italicBld);
// Add Bold, Italic, Underline to Paragraph
p1.Inlines.Add(underlineItalicBld);
// Add Paragraph to Section
sec.Blocks.Add(p1);
// Add Section to FlowDocument
doc.Blocks.Add(sec);
return doc;
}
}
}
3. 프린트 다이얼로그 안띄우고 출력하기
<MainWindow.xaml.cs 수정>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Printing;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace wpf_test06_print
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void printButton_Click(object sender, RoutedEventArgs e)
{
// Create a PrintDialog
PrintDialog printDialog = new PrintDialog();
// 프린트 다이얼로그 안열고 출력하기
var printers = new LocalPrintServer().GetPrintQueues();
var selectedPrinter = printers.FirstOrDefault(p => p.Name == "Samsung SL-J1560 Series"); // 문자열을 실제 프린트 이름으로 변경
if(selectedPrinter == null)
{
MessageBox.Show("not found!");
return;
}
// 다이얼로그에서 값이 입력되면 폰트를 ...으로 설정
// if(printDialog.ShowDialog().GetValueOrDefault())
// {
// FontFamily fontFamily =new FontFamily("나눔고딕코딩");
// }
FlowDocument doc = new FlowDocument(new Paragraph(new Run(ResultBox.Text)));
doc.Name="FlowDoc";
IDocumentPaginatorSource idpSource = doc;
printDialog.PrintDocument(idpSource.DocumentPaginator, "(description)WPF Printing");
// 그리드 출력이 필요할 땐 추가 작업이 필요함
// PrintVisual 활용법 참조
// Grid grid = new Grid();
// grid.SetValue(FontFamilyProperty, fontFamily);
// grid.SetValue(FontSizeProperty , 32d );
// Canvas canvas = new Canvas();
// canvas.Width = printDialog.PrintableAreaWidth;
// canvas.Height = printDialog.PrintableAreaHeight;
// canvas.Background = null;
// canvas.Children.Add(grid);
// printDialog.PrintVisual(canvas, "Sample");
}
private void previewButton_Click(object sender, RoutedEventArgs e)
{
ResultBox.Text = textBox1.Text + textBox2.Text;
}
}
}
반응형
'Programming > C#' 카테고리의 다른 글
.NET6(dotnet6)에서 entity framework 설치하기 (ef명령어 오류) (2) | 2022.09.14 |
---|---|
C#(WPF) - 실행 프로그램 아이콘 설정하기 (0) | 2022.04.22 |
C#(WPF) - VSCode에서 Resource Directory 추가하기 (0) | 2021.11.02 |
C#(WPF) - GUI 연습하기 2 (0) | 2021.10.30 |
C#(WPF) - GUI 연습하기(with Material Design) (0) | 2021.10.29 |