using System;
// 필요한 라이브러리 추가
using PdfSharp;
using PdfSharp.Drawing;
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;
using PdfSharp.Drawing.Layout;
using PdfSharp.Fonts;
namespace FourTypesApp
{
public partial class typeinfo_uc_form : UserControl
{
// 필요없는 코드는 생략
// 일본어 폰트를 사용하기 위한 Resolver를 Implement 함.
public class JapaneseFontResolver : IFontResolver
{
// 공개무료폰트를 다운로드 해서 프로젝트에 추가한 후, 경로를 지정함.
private static readonly string GEN_SHIN_GOTHIC_MEDIUM_TTF =
"FourTypesApp.fonts.GenShinGothic-Monospace-Medium.ttf";
// 폰트를 로드함.
public byte[] GetFont(string faceName)
{
switch (faceName)
{
case "GenShinGothic#Medium":
return LoadFontData(GEN_SHIN_GOTHIC_MEDIUM_TTF);
}
return null;
}
public FontResolverInfo ResolveTypeface(
string familyName, bool isBold, bool isItalic)
{
var fontName = familyName.ToLower();
switch (fontName)
{
case "gen shin gothic":
return new FontResolverInfo("GenShinGothic#Medium");
}
// 디폴트 폰트. 혹시 로딩이 안되거나 폰트파일이 없는 경우 사용됨.
return PlatformFontResolver.ResolveTypeface("Arial", isBold, isItalic);
}
// 프로젝트 리스소로부터 폰트정보를 읽어옴.
private byte[] LoadFontData(string resourceName)
{
var assembly = Assembly.GetExecutingAssembly();
using (Stream stream = assembly.GetManifestResourceStream(resourceName))
{
if (stream == null)
throw new ArgumentException("No resource with name " + resourceName);
int count = (int)stream.Length;
byte[] data = new byte[count];
stream.Read(data, 0, count);
return data;
}
}
}
private void typeinfo_uc_form_Load(object sender, EventArgs e)
{
switch (member.TypeId)
{
case 1:
lb_type.Text = "~「" + this.member.UserName + "」さんの体質診断結果~ \n <Dry(乾)> タイプです。";
pb_type.Image = FourTypesApp.Properties.Resources.dry_img;
pb_bedge.Image = FourTypesApp.Properties.Resources.img_dry;
richTextBox.Text = FourTypesApp.Properties.Resources.memo_dry;
break;
// 이하 생략, 목표는 richTextBox에 리소스에 저장된 문자열을 표시한 후, 이 내용을 PDF로 출력하는 것임.
}
}
private void btn_pdf_Click(object sender, EventArgs e)
{
// PDF 출력버튼을 눌렀을 때 실행되는 코드
try
{
// 일본어 폰트를 사용하기 위해서 글로벌폰트설정 개체를 생성함
PdfSharp.Fonts.GlobalFontSettings.FontResolver = new JapaneseFontResolver();
// PDF문서를 생성
PdfDocument document = new PdfDocument();
document.Info.Title = "四象体質診断結果";
document.Info.Author = this.member.UserName;
// 문서에 공백인 페이지를 추가한다.
PdfPage page = document.AddPage();
// 페이지에 출력을 하기 위한 Graphics개체를 설정함.
XGraphics gfx = XGraphics.FromPdfPage(page);
// 폰트를 설정함.
var pdf_ja_font_options = new XPdfFontOptions(PdfFontEncoding.Unicode, PdfFontEmbedding.Always);
var pdf_ja_font_title = new XFont("Gen Shin Gothic", 14, XFontStyle.Bold, pdf_ja_font_options);
var pdf_ja_font = new XFont("Gen Shin Gothic", 12, XFontStyle.Regular, pdf_ja_font_options);
// 타이틀 문자열을 출력함.
gfx.DrawString(lb_type.Text, pdf_ja_font_title, XBrushes.Black,
new XRect(30, 30, page.Width, page.Height), XStringFormats.TopCenter);
// 내용 문자열의 포멧을 설정함. (이걸 안 하면 개행이 안 됨.)
XTextFormatter tf = new XTextFormatter(gfx);
tf.Alignment = XParagraphAlignment.Left;
// 내용 텍스트를 출력함.
tf.DrawString(richTextBox.Text, pdf_ja_font, XBrushes.Black,
new XRect(30, 80, page.Width, page.Height), XStringFormats.TopLeft);
// 문서파일명을 설정함.
string FileName = "FourTypeResult_" + this.member.UserName + ".pdf";
// 문서파일을 저장함.
document.Save(FileName);
// PDF 뷰어로 작성한 문서를 표시함.
Process.Start(FileName);
}
catch (Exception)
{
throw new Exception();
}
}
}
}
댓글
댓글 쓰기