using iTextSharp.text;
using iTextSharp.text.pdf;
public void CreatePDF(string msg)
{
Document document = new Document(page, 56, 56, 50, 50);//控制距离左右上下的距离
string path = "D:\\Queue.pdf";//要生成的pdf
PdfWriter.GetInstance(document, new FileStream(path, FileMode.Create));
document.Open();
iTextSharp.text.Font normalfont = new iTextSharp.text.Font(bfHei, 10.6f, Element.TITLE);
BaseFont bfHei = BaseFont.CreateFont(@"C:\Windows\Fonts\STSONG.TTF.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);//需要设置为中文字体,不然显示不出来中文
iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(System.Drawing.Image.FromFile(@"C:\Users\Public\Pictures\Sample Pictures\111.jpg"), ImageFormat.Png);
img.ScaleAbsolute(54, 54);//设置图片的大小,也可以用ScalePercent属性设置百分比大小
img.Alignment = Element.ALIGN_CENTER;//把图片设置为居中
document.Add(img);//插入图片
content = new Paragraph(new Chunk(“测试段落”, normalfont ));//具体的内容,及字体样式
content.FirstLineIndent = -12f;//这个是设置这一个段落第一行的缩进
content.IndentationLeft = 20f;//这个是设置这一个段落左边的缩进
content.IndentationRight = 0;//这个是设置这一个段落右边的缩进
content.Leading = 22;//设置行间距
document.Add(content);
document.Close();
}
以上内容都是自己测试过的,先总结一下,以后可能会用到
补充:代码中含有路径的最好都放到web.config中,可以通过配置进行修改,不然如果出现环境不一致时,文件不存在时会报错
对于进程最后通过using包起来,比如using(FileStream fs=new FileStream(path, FileMode.Create)){ xxx代码内容},这样就算报错也能释放掉本次新建的进程