C#/WPF

[WPF/Syncufsion] PDF 파일 다루기

kjun.kr 2022. 6. 22. 23:19
728x90
728x170

1. WPF .NET 6 프로젝트 생성

2. Nuget 설치
Syncfusion.PdfViewer.WPF 
System.Drawing.Common

3. PdfViewer 를 이용한 pdf 파일 로드
MainWindow.xaml

<Window x:Class="PdfTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:syncfusion="clr-namespace:Syncfusion.Windows.PdfViewer;assembly=Syncfusion.PdfViewer.WPF"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800"
        WindowState="Maximized">
    <Grid>
        <syncfusion:PdfViewerControl x:Name="pdfViewer"></syncfusion:PdfViewerControl>
    </Grid>
</Window>

MainWindow.xaml.cs

PdfLoadedDocument pdfLoadedDocument = new PdfLoadedDocument(@"test.pdf");
pdfViewer.ItemSource = pdfLoadedDocument;

 

4. Text Insert (폰트 유의 - 한글지원 폰트)
5페이지의 해당 좌표위치에 '테스트 테스트' 문자열을 추가합니다.

PdfLoadedPage page = pdfLoadedDocument.Pages[5] as PdfLoadedPage;

var graphics = page.Graphics;

// 1. Text Insert
PdfFont font = new PdfCjkStandardFont(PdfCjkFontFamily.HanyangSystemsShinMyeongJoMedium, 13);
graphics.DrawString("테스트도 테스트시", font, PdfBrushes.Black, new PointF(170, 107));

5. Image Insert
5페이지의 해당 좌표위치에 'kei.jpg' 이미지를 추가합니다.

PdfLoadedPage page = pdfLoadedDocument.Pages[5] as PdfLoadedPage;

var graphics = page.Graphics;

// 2. Image Insert
PdfBitmap image = new PdfBitmap("kei.jpg");
graphics.DrawImage(image, 110, 190);

6. Table Insert
Sample Data

DataTable table = new DataTable();
table.Columns.Add("순위/구분");
table.Columns.Add("시간/수량(mm)");
table.Columns.Add("시간/일시");
table.Columns.Add("일/수량(mm)");
table.Columns.Add("일/일시");

table.Rows.Add(new string[] { "1", "95", "1998.08.12", "407", "1998.08.12" });
table.Rows.Add(new string[] { "2", "67", "2020.08.28", "202", "2003.07.09" });
table.Rows.Add(new string[] { "3", "66", "1997.08.03", "197", "2016.07.04" });


PdfLightTable 사용

PdfLightTable pdfLightTable = new PdfLightTable();
pdfLightTable.DataSource = table;
pdfLightTable.Draw(graphics, new PointF(100, 200));


PdfGrid 사용

PdfGrid pdfGrid = new PdfGrid();
pdfGrid.DataSource = table;

pdfGrid.Columns[0].Width = 80;
pdfGrid.Columns[1].Width = 100;
pdfGrid.Columns[2].Width = 80;
pdfGrid.Columns[3].Width = 100;
pdfGrid.Columns[4].Width = 80;

PdfStringFormat format = new PdfStringFormat();
format.Alignment = PdfTextAlignment.Center;
format.LineAlignment = PdfVerticalAlignment.Bottom;

pdfGrid.Columns[0].Format = format;
pdfGrid.Columns[1].Format = format;
pdfGrid.Columns[2].Format = format;
pdfGrid.Columns[3].Format = format;
pdfGrid.Columns[4].Format = format;

PdfGridRowStyle pdfGridRowStyle = new PdfGridRowStyle();
pdfGridRowStyle.Font = new PdfCjkStandardFont(PdfCjkFontFamily.HanyangSystemsShinMyeongJoMedium, 13);
pdfGrid.Rows[0].Style = pdfGridRowStyle;
pdfGrid.Rows[1].Style = pdfGridRowStyle;
pdfGrid.Rows[2].Style = pdfGridRowStyle;
pdfGrid.Headers[0].Style = pdfGridRowStyle;

pdfGrid.Draw(graphics, new PointF(80, 400));

728x90
그리드형