using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
using System.Diagnostics;
namespace ConsoleApplication1
{
class pdfCreater
{
/// create pdf
/// </summary>
public static void CreatePdf()
{
iTextSharp.text.Document doc1 = new Document(PageSize.A4);
iTextSharp.text.Document doc2 = new Document(PageSize.A4); try
{
PdfWriter.GetInstance(doc1, new FileStream(@"D:\test1.pdf", FileMode.Create));
PdfWriter.GetInstance(doc2, new FileStream(@"D:\test2.pdf", FileMode.Create)); #region 设置PDF的头信息,一些属性设置,在Document.Open 之前完成
doc1.AddAuthor("cxa");
doc1.AddCreationDate();
doc1.AddCreator("cxa");
doc1.AddSubject("使用Do Net 创建了一个pdf1");
doc1.AddTitle("这是个标题");
doc1.AddKeywords("什么是keyword?不清楚反正这是个keyword"); doc2.AddAuthor("cxa");
doc2.AddCreationDate();
doc2.AddCreator("cxa");
doc2.AddSubject("使用Do Net 创建了一个pdf2");
doc2.AddTitle("这是个标题");
doc2.AddKeywords("什么是keyword?不清楚反正这是个keyword");
//自定义头
doc1.AddHeader("Expires", "0"); //0表示立即过期。我猜是立即生效
doc2.AddHeader("Expires", "0"); //0表示立即过期。我猜是立即生效
#endregion //打开document,此时打开啥呀没有
doc1.Open();
doc2.Open();
//载入字体
BaseFont bf = BaseFont.CreateFont("C:/Windows/Fonts/SIMHEI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
iTextSharp.text.Font font = new iTextSharp.text.Font(bf);
doc1.Add(new Paragraph("这是一个由C#创建的pdf文件,6661", font));
doc2.Add(new Paragraph("这是一个由C#创建的pdf文件,6662", font));
doc1.Close();
doc2.Close();
Process.Start(@"D:\test1.pdf");
Process.Start(@"D:\test2.pdf");
}
catch (DocumentException de)
{
Console.WriteLine(de.Message); Console.ReadKey();
}
catch (IOException io)
{
Console.WriteLine(io.Message); Console.ReadKey();
}
}
public static void CreatePdf2()
{ iTextSharp.text.Document doc = new Document(PageSize.A4, 36, 72, 108, 180);
try
{
PdfWriter.GetInstance(doc, new FileStream(@"d:\test3.pdf", FileMode.Create));
doc.Open();
//创建表格
PdfPTable table = new PdfPTable(3); PdfPCell cell = new PdfPCell(new Phrase("Header spanning 3 columns")); cell.Colspan = 3; cell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
cell.BorderColor = BaseColor.RED;
cell.BorderWidth = 2F; table.AddCell(cell);
table.AddCell("Col 1 Row 1"); table.AddCell("Col 2 Row 1"); table.AddCell("Col 3 Row 1"); table.AddCell("Col 1 Row 2"); table.AddCell("Col 2 Row 2"); table.AddCell("Col 3 Row 2"); doc.Add(table);
Image jpeg = Image.GetInstance(@"D:\re.jpg");
doc.Add(jpeg);
doc.Add(new Paragraph("this is rege"));
doc.Close();
Process.Start(@"d:\test3.pdf");
}
catch (DocumentException de)
{
Console.WriteLine(de.Message);
}
catch (IOException io)
{
Console.WriteLine(io.Message);
} }
}
}