62,046
社区成员
发帖
与我相关
我的任务
分享
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);
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;
}
}
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("创建完成");
}
}
}
BaseFont font1 = BaseFont.CreateFont("STSong-light", "BaseFont.IDENTITY_H", BaseFont.EMBEDDED);
这样可以吗? 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盘里的字库,而其它两个都是错误的,反复检查了多次,跟网上写的没有啥区别。
之前发贴多了一个)的是复制错误,原程序没有这行。