关于iTextSharp的问题

wwwzgy 2014-07-13 07:26:55
BaseFont.AddToResourceSearch("iTextAsian-1.0.dll");
BaseFont.AddToResourceSearch("iTextAsianCmaps-1.0.dll");
BaseFont.EMBEDDED);
BaseFont font1 = BaseFont.CreateFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);

照着网上写了一个生成PDF的程序,遇到中文字体时总是提示Font 'STSong-Light' with 'UniGB-UCS2-H' is not recognized.。
如果把字体改成本地C盘下的字体就可以正常使用?iTextAsian.dll和iTextAsianCmaps.dll都已经加载了呀,怎么会无法识别字体呢?
...全文
755 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
trawel 2015-02-28
  • 打赏
  • 举报
回复
测试代码(itextsharp 5.5.4.0,iTextAsian 2.1,iTextAsianCmaps 1.0)
其中iTextAsian ,iTextAsianCmaps可以不用引用,将这两个文件直接复制到运行目录就可以

public bool TxtPDF(out string strNewPdfFileName)
{
strNewPdfFileName = Guid.NewGuid().ToString();
targetPath = System.IO.Path.Combine(targetPath + string.Format(@"\{0}.PDF", strNewPdfFileName));
try
{
var document = new Document(iTextSharp.text.PageSize.A4, 25, 25, 25, 25);
using (var stream = new FileStream(targetPath, FileMode.Create, FileAccess.Write, FileShare.None))
{
PdfWriter.GetInstance(document, stream);
document.Open();
iTextSharp.text.io.StreamUtil.AddToResourceSearch("iTextAsian.dll");
iTextSharp.text.io.StreamUtil.AddToResourceSearch("iTextAsianCmaps.dll");

BaseFont bfont = BaseFont.CreateFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);//字体:黑体
iTextSharp.text.Font f = new iTextSharp.text.Font(bfont);
Chunk ck = new Chunk("中文测试",f);
iTextSharp.text.Paragraph pg = new Paragraph(ck);
document.Add(pg);
document.Close();
return true;
}
}
catch
{
strNewPdfFileName = string.Empty;
return false;
}
}

可以正常显示中文
laowang2 2015-01-04
  • 打赏
  • 举报
回复
没报错,但是还是不显示中文。
michaelsheyong 2014-10-15
  • 打赏
  • 举报
回复
找到解决方法了。 1.net版本用3.5或2.0 2.修改下源码 StreamUtil.cs GetResourceStream函数中的一句话 istr = Assembly.LoadFrom(dir).GetManifestResourceStream(key); 改为下面 istr = Assembly.Load(File.ReadAllBytes(dir)).GetManifestResourceStream(key); 是因为高 版本加载低版本程序集的问题,具体错误提示你可以跟踪源码的时候看到提示。
michaelsheyong 2014-10-15
  • 打赏
  • 举报
回复
补充下,用地址的形式文件确实变大好多
michaelsheyong 2014-10-15
  • 打赏
  • 举报
回复
我也越到相同问题,。net3.5环境不会报错,但是4.0就报错。估计控件内部哪里没有处理好。
wwwzgy 2014-09-08
  • 打赏
  • 举报
回复
引用 11 楼 save4me 的回复:
我把我的代码上传了,需要的话可以下载下来自己试一试 iTextSharp创建中文PDF
下载试了,还是不行。
save4me 2014-07-19
  • 打赏
  • 举报
回复
忘记在项目里面添加iTextAsian.dll和iTextAsianCmaps.dll引用了吧?不只是用AddToResourceSearch。
save4me 2014-07-19
  • 打赏
  • 举报
回复
代码中你的第一次是
BaseFont.AddToResourceSearch("iTextAsian-1.0.dll");
BaseFont.AddToResourceSearch("iTextAsianCmaps-1.0.dll");
第二次是
BaseFont.AddToResourceSearch("iTextAsian.dll");
BaseFont.AddToResourceSearch("iTextAsianCmaps.dll");
哪一个是你的自己项目里的?
可能虽然你添加了,但是AddToResourceSearch里面调用的不对,和没有添加是一样的,同样无法识别字体。
这个是我没有添加时候的错误提示,和你的一样

这个是我添加后生成的PDF,没有错误提示,显示正常

这是我的测试代码,类库是sf上下载的,其中itextsharp 5.5.1,iTextAsian 2.1,iTextAsianCmaps 1.0

using System;
using System.Collections.Generic;
using System.IO;
using System.Drawing;
using System.Windows.Forms;

using iTextSharp;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.io;

namespace iTextSharpDemo
{
/// <summary>
/// Description of MainForm.
/// </summary>
public class MainForm : Form
{
/// <summary>
/// Designer variable used to keep track of non-visual components.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Disposes resources used by the form.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing) {
if (components != null) {
components.Dispose();
}
}
base.Dispose(disposing);
}

/// <summary>
/// This method is required for Windows Forms designer support.
/// Do not change the method contents inside the source code editor. The Forms designer might
/// not be able to load this method if it was changed manually.
/// </summary>
private void InitializeComponent()
{
this.btnOK = new System.Windows.Forms.Button();
this.lblPath = new System.Windows.Forms.Label();
this.txtPath = new System.Windows.Forms.TextBox();
this.lblFile = new System.Windows.Forms.Label();
this.txtFile = new System.Windows.Forms.TextBox();
this.txtContent = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// btnOK
//
this.btnOK.Location = new System.Drawing.Point(13, 157);
this.btnOK.Name = "btnOK";
this.btnOK.Size = new System.Drawing.Size(75, 23);
this.btnOK.TabIndex = 0;
this.btnOK.Text = "创建";
this.btnOK.UseVisualStyleBackColor = true;
this.btnOK.Click += new System.EventHandler(this.BtnOKClick);
//
// lblPath
//
this.lblPath.Location = new System.Drawing.Point(13, 13);
this.lblPath.Name = "lblPath";
this.lblPath.Size = new System.Drawing.Size(100, 23);
this.lblPath.TabIndex = 1;
this.lblPath.Text = "保存路径";
//
// txtPath
//
this.txtPath.Location = new System.Drawing.Point(119, 12);
this.txtPath.Name = "txtPath";
this.txtPath.Size = new System.Drawing.Size(363, 21);
this.txtPath.TabIndex = 2;
//
// lblFile
//
this.lblFile.Location = new System.Drawing.Point(12, 40);
this.lblFile.Name = "lblFile";
this.lblFile.Size = new System.Drawing.Size(100, 23);
this.lblFile.TabIndex = 3;
this.lblFile.Text = "文件名";
//
// txtFile
//
this.txtFile.Location = new System.Drawing.Point(119, 40);
this.txtFile.Name = "txtFile";
this.txtFile.Size = new System.Drawing.Size(363, 21);
this.txtFile.TabIndex = 4;
this.txtFile.Text = "Demo.pdf";
//
// txtContent
//
this.txtContent.Location = new System.Drawing.Point(13, 67);
this.txtContent.Multiline = true;
this.txtContent.Name = "txtContent";
this.txtContent.Size = new System.Drawing.Size(469, 84);
this.txtContent.TabIndex = 5;
//
// MainForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(494, 192);
this.Controls.Add(this.txtContent);
this.Controls.Add(this.txtFile);
this.Controls.Add(this.lblFile);
this.Controls.Add(this.txtPath);
this.Controls.Add(this.lblPath);
this.Controls.Add(this.btnOK);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.Name = "MainForm";
this.Text = "iTextSharpDemo";
this.ResumeLayout(false);
this.PerformLayout();
}
private System.Windows.Forms.TextBox txtContent;
private System.Windows.Forms.TextBox txtFile;
private System.Windows.Forms.Label lblFile;
private System.Windows.Forms.TextBox txtPath;
private System.Windows.Forms.Label lblPath;
private System.Windows.Forms.Button btnOK;

public MainForm()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
txtPath.Text = path;
//
// TODO: Add constructor code after the InitializeComponent() call.
//
}

string path = AppDomain.CurrentDomain.BaseDirectory;
string file = "Deom.pdf";
string content = "";
public void CreatePDF()
{
//第一步,创建一个 iTextSharp.text.Document对象的实例:
Document document = new Document();

//第二步,为该Document创建一个Writer实例:
PdfWriter.GetInstance(document, new FileStream(path + file, FileMode.Create));

//第三步,打开当前Document
document.Open();

//第四步,为当前Document添加内容:
iTextSharp.text.Font font = CreateChineseFont();
document.Add(new Paragraph(content, font));

//第五步,关闭Document
document.Close();
}

public iTextSharp.text.Font CreateChineseFont()
{
//如果不使用CID字体,下面三行不需要
//BaseFont.AddToResourceSearch("iTextAsian.dll");
//BaseFont.AddToResourceSearch("iTextAsianCmaps.dll");
//新版的AddToResourceSearch移到了StreamUtil
StreamUtil.AddToResourceSearch("iTextAsian.dll");
StreamUtil.AddToResourceSearch("iTextAsianCmaps.dll");
BaseFont baseFT = BaseFont.CreateFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);

iTextSharp.text.Font font = new iTextSharp.text.Font(baseFT);
return font;
}

void BtnOKClick(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(txtPath.Text.Trim())) {
path = txtPath.Text;
}
if (!string.IsNullOrEmpty(txtFile.Text.Trim())) {
path = txtFile.Text;
}
content = txtContent.Text;
CreatePDF();
MessageBox.Show("创建完成");
}
}
}

引用 8 楼 wwwzgy 的回复:
[quote=引用 7 楼 save4me 的回复:]
忘记在项目里面添加iTextAsian.dll和iTextAsianCmaps.dll引用了吧?不只是用AddToResourceSearch。

没添加会报错的,虽然是刚入门,但不会犯这样的错误。[/quote]
save4me 2014-07-19
  • 打赏
  • 举报
回复
我把我的代码上传了,需要的话可以下载下来自己试一试 iTextSharp创建中文PDF
wwwzgy 2014-07-19
  • 打赏
  • 举报
回复
引用 9 楼 save4me 的回复:
代码中你的第一次是 BaseFont.AddToResourceSearch("iTextAsian-1.0.dll"); BaseFont.AddToResourceSearch("iTextAsianCmaps-1.0.dll"); 第二次是 BaseFont.AddToResourceSearch("iTextAsian.dll"); BaseFont.AddToResourceSearch("iTextAsianCmaps.dll"); 哪一个是你的自己项目里的? 可能虽然你添加了,但是AddToResourceSearch里面调用的不对,和没有添加是一样的,同样无法识别字体。 这个是我没有添加时候的错误提示,和你的一样
里面的引用iTextAsian.dll和iTextAsian-1.0.dll,都试了,我原来也以为是需要加版本,后来发现不管加不加-1.0都不正确,添加项目中的名称是iTextAsian.dll。
wwwzgy 2014-07-19
  • 打赏
  • 举报
回复
引用 7 楼 save4me 的回复:
忘记在项目里面添加iTextAsian.dll和iTextAsianCmaps.dll引用了吧?不只是用AddToResourceSearch。
没添加会报错的,虽然是刚入门,但不会犯这样的错误。
wwwzgy 2014-07-18
  • 打赏
  • 举报
回复
引用 4 楼 zuo_hy 的回复:
  BaseFont font1 = BaseFont.CreateFont("STSong-light", "BaseFont.IDENTITY_H", BaseFont.EMBEDDED);
这样可以吗?
这样也不可以。
孙大诚_SunRobin 2014-07-15
  • 打赏
  • 举报
回复
也在使用ItextShrap. 正在鼓捣格式问题。
弘毅致远 2014-07-14
  • 打赏
  • 举报
回复
  BaseFont font1 = BaseFont.CreateFont("STSong-light", "BaseFont.IDENTITY_H", BaseFont.EMBEDDED);
这样可以吗?
wwwzgy 2014-07-14
  • 打赏
  • 举报
回复
直接把字体文件从C盘复制到服务器目录下面(只能用鼠标拖的方法复制过去,不能右键选复制),算是间接解决了。
wwwzgy 2014-07-14
  • 打赏
  • 举报
回复
 BaseFont.AddToResourceSearch("iTextAsian.dll");
       BaseFont.AddToResourceSearch("iTextAsianCmaps.dll");
       // BaseFont font1 = BaseFont.CreateFont(@"C:\WINDOWS\Fonts\SimHei.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
       BaseFont font1 = BaseFont.CreateFont("STSong-light", "UniGB-UCS2-H", BaseFont.EMBEDDED);
       //BaseFont font1 = BaseFont.CreateFont("Courier", BaseFont.IDENTITY_H, false);
上面这段代码中三个BaseFont font1,只选其中一个,把其它两个变成注释行,那么就第一个BaseFont font1是可用的,因为它是调用了本机C盘里的字库,而其它两个都是错误的,反复检查了多次,跟网上写的没有啥区别。 之前发贴多了一个)的是复制错误,原程序没有这行。
wangnaisheng 2014-07-13
  • 打赏
  • 举报
回复
http://www.cnblogs.com/xiaoshuai1992/p/itextsharp.html http://www.jb51.net/article/34796.htm 参考 另外, BaseFont.EMBEDDED);你这句是不是多一个 ) 。 如你说的:如果把字体改成本地C盘下的字体就可以正常使用? 改为别的地址就不好用,那么应该是你的引用路径不对。

62,039

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧