0
我將創建一個用於在單擊按鈕後生成PDF文件的應用程序。該應用程序適用於英語,但不適用於中文。我使用utf-8進行編碼,所以我無法確定導致問題的原因。下面是主要的腳本:使用.NET創建PDF作爲問號標記顯示中文
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>
感謝您的回覆。使用完全相同的代碼後,中文字符會從問號變爲方塊。我將Unicode更改爲其他字體編碼方法,但字符消失。有沒有其他解決問題的方法? –
還要確保您使用的字體包含您需要的字形。在我的電腦上,「微軟喜馬拉雅」包含藏文,但沒有CJK字符。也許試試「微軟JhengHei」。使用'charmap.exe'來檢查所需的字形是否可用。 –