Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Fri Mar 29, 2019 2:00 am

[code]
PdfTable table = new PdfTable();
table.Style.CellPadding = 2;
table.Style.HeaderSource = PdfHeaderSource.Rows;
table.Style.HeaderRowCount = 1;
table.Style.ShowHeader = true;
table.DataSource = dataSource;
PdfLayoutResult result = table.Draw(page, new PointF(0, y));
y = y + result.Bounds.Height + 5;
PdfBrush brush2 = PdfBrushes.Gray;
PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Arial", 9f));

doc.SaveToFile(@"d:\FirstPDF.pdf", FileFormat.PDF);
doc.Close();

输出的pdf中,中文变成乱码了

Saunterer
 
Posts: 16
Joined: Wed Oct 11, 2017 6:37 am

Fri Mar 29, 2019 3:08 am

您好,

在画中文到PDF的时候需要选用支持显示中文的字体,比如宋体,微软雅黑等。您这里没有给添加的表格设置字体,内部默认为西文字体Helvetica,它不支持显示中文,所以您得到乱码。请参考如下代码设置表格样式字体为中文字体。
Code: Select all
....
//创建PdfTrueTypeFont支持中文字体
PdfTrueTypeFont trueTypeFont = new PdfTrueTypeFont(new Font("微软雅黑", 18f), true);
//应用字体到PDF table
table.Style.AlternateStyle.Font = trueTypeFont;
table.Style.DefaultStyle.Font = trueTypeFont;
table.Style.HeaderStyle.Font = trueTypeFont;
PdfLayoutResult result = table.Draw(page, new PointF(0, y));

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1385
Joined: Tue Sep 27, 2016 1:06 am

Fri Mar 29, 2019 3:57 am

[code] PdfCjkStandardFont font = new PdfCjkStandardFont(PdfCjkFontFamily.SinoTypeSongLight, 12f);
PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Center);
page.Canvas.DrawString(condition.title, font, brush1, page.Canvas.ClientSize.Width / 2, y, format1);
y = y + font.MeasureString(condition.title, format1).Height;
y = y + 5;

这一块的标题也是 无法转换成中文

Saunterer
 
Posts: 16
Joined: Wed Oct 11, 2017 6:37 am

Fri Mar 29, 2019 6:05 am

您好,

感谢反馈!
我这边用最新版(Spire.PDF Pack(Hot Fix) Version:5.3.8)测试PdfCjkStandardFont字体画中文没有任何问题。您使用的哪个版本的Spire.PDF呢?如果使用的是旧版本,我建议您下载最新版试试。另外请确保您的电脑中是安装了"宋体"字体的。下面是我的完整测试代码,以及附上我的生成文档供参考。有问题请随时反馈。
Code: Select all
 static void Main(string[] args)
 {
     PdfDocument doc = new PdfDocument();
     PdfPageBase page=doc.Pages.Add();

     //画标题到页面
     float y = 50.0f;
     PdfCjkStandardFont font = new PdfCjkStandardFont(PdfCjkFontFamily.SinoTypeSongLight, 20f);
     PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Center);
     string title = "添加表格";
     PdfBrush brush1 = PdfBrushes.Green;
     page.Canvas.DrawString(title, font, brush1, page.Canvas.ClientSize.Width / 2, y, format1);
     y = y + font.MeasureString(title, format1).Height;
     y = y + 5;

     PdfTable table = new PdfTable();
     table.Style.CellPadding = 2;
     table.Style.HeaderSource = PdfHeaderSource.Rows;
     table.Style.HeaderRowCount = 1;
     table.Style.ShowHeader = true;
     table.DataSource = GetData();
     //创建PdfTrueTypeFont支持中文字体
     PdfTrueTypeFont trueTypeFont = new PdfTrueTypeFont(new Font("微软雅黑", 18f), true);
     //应用字体到PDF table
     table.Style.AlternateStyle.Font = trueTypeFont;
     table.Style.DefaultStyle.Font = trueTypeFont;
     table.Style.HeaderStyle.Font = trueTypeFont;
     PdfLayoutResult result = table.Draw(page, new PointF(0, y));
     doc.SaveToFile("Table.pdf", FileFormat.PDF);
     doc.Close();
 }
 private static DataTable GetData()
 {
     DataTable table = new DataTable();
     table.Columns.Add("one",Type.GetType("System.String"));
     table.Columns.Add("two",Type.GetType("System.String"));
     table.Columns.Add("three", Type.GetType("System.String"));
     table.Rows.Add("中文","test","test");
     table.Rows.Add("文本", "test", "test");
     table.Rows.Add("乱码", "test", "test");
     return table;
}

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1385
Joined: Tue Sep 27, 2016 1:06 am

Mon Apr 01, 2019 5:59 am

表格中的 字体 全部居中怎么调整

Saunterer
 
Posts: 16
Joined: Wed Oct 11, 2017 6:37 am

Mon Apr 01, 2019 6:36 am

您好,

请使用下面的代码居中表格文本。
Code: Select all
//...
table.Style.HeaderStyle.StringFormat = new PdfStringFormat(PdfTextAlignment.Center);
foreach (PdfColumn column in table.Columns)
{
    column.StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
}
PdfLayoutResult result = table.Draw(page, new PointF(0, y));
//...

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1385
Joined: Tue Sep 27, 2016 1:06 am

Return to Spire.PDF

cron