C# write in pdf file

 

//using iTextSharp.text;
//using iTextSharp.text.pdf;

//需要调用到这个iTextSharp应用

//string fileName = string.Empty;
//Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
//dlg.FileName = "我的第一个PDF";
//dlg.DefaultExt = ".pdf";
//dlg.Filter = "Text documents (.pdf)|*.pdf";
//Nullable<bool> result = dlg.ShowDialog();
// if (result == true)
// {
//fileName = dlg.FileName;
//设置页面大小
//string path = string.Empty;
//path = "c:";

//<add key="JOBDOC" value="c:\app\k35\dev_app\app\doc\" />
var path = System.Configuration.ConfigurationManager.AppSettings["JOBDOC"] as string;

//如果没有该文件就新建
if (!Directory.Exists(path + "temp\\"))
{
Directory.CreateDirectory(path + "temp\\");
}
path = path + "temp\\" + "我的第一个PDF2"+".pdf";
iTextSharp.text.Rectangle pageSize = new iTextSharp.text.Rectangle(716f, 816f);
//var sd=new iTextSharp.text.BaseColor();
pageSize.BackgroundColor = iTextSharp.text.BaseColor.WHITE;
//pageSize.BackgroundColor = new iTextSharp.text.BaseColor(0xFF, 0xFF, 0xDE);
//设置边界
Document document = new Document(pageSize, 36f, 72f, 8f, 10f);
PdfWriter.GetInstance(document, new FileStream(path, FileMode.Create));
//PdfWriter padf= PdfWriter.GetInstance(document, new FileStream(System.Web.HttpContext.Current.Server.MapPath(path), FileMode.Create));
// // 添加文档信息
document.AddTitle("PDFInfo");
document.AddSubject("Demo of PDFInfo");
document.AddKeywords("Info, PDF, Demo");
document.AddCreator("SetPdfInfoDemo");
document.AddAuthor("焦涛");


document.Open();
// 添加文档内容
for (int i = 0; i < 5; i++)
{
document.Add(new iTextSharp.text.Paragraph("Hello World! Hello People! " +
"Hello Sky! Hello Sun! Hello Moon! Hello Stars!"));
}
document.Close();

posted on 2016-03-04 13:53  kyle.hdk  阅读(303)  评论(0编辑  收藏  举报

导航