2017-10-08 1 views
0

ボタンをクリックしてPDFファイルを生成するアプリケーションを作成します。このアプリケーションは中国語ではなく英語でうまく動作します。私はUTF-8を使ってエンコードしているので、問題の原因を突き止めることはできません。PDFを作成する.NETを使用して疑問符として中国語を表示

using PdfSharp.Drawing; 
using PdfSharp.Pdf; 
using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Diagnostics; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 

namespace WindowsFormsApp1 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      PdfDocument pdf = new PdfDocument(); 
      pdf.Info.Title = "My First PDF"; 
      PdfPage pdfPage = pdf.AddPage(); 
      XGraphics graph = XGraphics.FromPdfPage(pdfPage); 
      XFont font = new XFont("Microsoft Himalaya", 35, XFontStyle.Regular); 
      graph.DrawString("香港", font, XBrushes.Black, new XRect(0, 0, pdfPage.Width.Point, 170), XStringFormats.Center); 
      string pdfFilename = "firstpage.pdf"; 
      pdf.Save(pdfFilename); 
      Process.Start(pdfFilename); 
     } 
    } 
} 

これはapp.configをである:ここでは主なスクリプトがある、あなたのプログラムで使用エンコーディングは関係ありません

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <startup> 
     <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" /> 
    </startup> 
</configuration> 

答えて

0

、あなたはPDFのために作成したフォントは、ANSIフォントです。
http://pdfsharp.net/wiki/Unicode-sample.ashx

埋め込むオプションは、IIRCを除去した後、あなたはまだエンコーディングを設定する必要があります。

XPdfFontOptions options = new XPdfFontOptions(PdfFontEncoding.Unicode, 
           PdfFontEmbedding.Always); 

XFont font = new XFont("Times New Roman", 12, XFontStyle.Regular, options); 

も参照してください。

+0

ご返信ありがとうございます。あなたが提供したのとまったく同じコードを使用した後、漢字は疑問符から四角に変わります。私はUnicodeを他のフォントエンコーディング方法に変更しましたが、文字は消えました。問題を解決する他の方法はありますか? –

+0

また、必要なグリフを含むフォントを使用していることを確認してください。私のコンピュータでは、「Microsoft Himalaya」にはチベット語が含まれていますが、CJK文字は含まれていません。多分 "Microsoft JhengHei"を試してみてください。 'charmap.exe'を使って、必要なグリフが利用可能であることを確認してください。 –

関連する問題