public static void ExportTestSettings(Chapter chapter, Font font, int flowCount, long recordCount, float randomness) { Section settings = chapter.AddSection(new Paragraph("Test settings.", font)); string version = FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location).FileVersion; settings.Add(new Paragraph(String.Format("\t \t Benchmark - {0}", version))); settings.Add(new Paragraph(String.Format("\t \t Export date - {0}", DateTime.Now))); settings.Add(new Chunk("\n")); Section testSettings = chapter.AddSection(new Paragraph("Settings.", font)); testSettings.Add(new Paragraph(String.Format("\t \t Flow count - {0}", flowCount))); testSettings.Add(new Paragraph(String.Format("\t \t Record count - {0}", recordCount))); testSettings.Add(new Paragraph(String.Format("\t \t Randomness - {0}%", randomness * 100))); testSettings.Add(new Paragraph(String.Format("\t \t Key type - {0}", randomness == 0f ? KeysType.Sequential : KeysType.Random))); testSettings.Add(new Chunk("\n")); }
public void CreateTaggedPdf11() { InitializeDocument("11"); Chapter c = new Chapter( new Paragraph("First chapter", new Font(Font.FontFamily.HELVETICA, 16, Font.BOLD, BaseColor.BLUE)), 1); c.TriggerNewPage = false; c.Indentation = 40; Section s1 = c.AddSection(new Paragraph("First section of a first chapter", new Font(Font.FontFamily.HELVETICA, 13, Font.BOLD, BaseColor.BLUE))); s1.Indentation = 20; Section s2 = s1.AddSection(new Paragraph("First subsection of a first section of a first chapter", new Font(Font.FontFamily.HELVETICA, 10, Font.BOLD, BaseColor.BLUE))); s2.Indentation = 10; s2.Add(new Paragraph("Some text...")); s2 = s1.AddSection(new Paragraph("Second subsection of a first section of a first chapter", new Font(Font.FontFamily.HELVETICA, 10, Font.BOLD, BaseColor.BLUE))); s2.Indentation = 10; s2.Add(new Paragraph("Some text...")); s2 = s1.AddSection(new Paragraph("Third subsection of a first section of a first chapter", new Font(Font.FontFamily.HELVETICA, 10, Font.BOLD, BaseColor.BLUE))); s2.Indentation = 10; s2.Add(new Paragraph("Some text...")); s1 = c.AddSection(new Paragraph("Second section of a first chapter", new Font(Font.FontFamily.HELVETICA, 13, Font.BOLD, BaseColor.BLUE))); s1.Indentation = 20; s2 = s1.AddSection(new Paragraph("First subsection of a second section of a first chapter", new Font(Font.FontFamily.HELVETICA, 10, Font.BOLD, BaseColor.BLUE))); s2.Indentation = 10; s2.Add(new Paragraph("Some text...")); s2 = s1.AddSection(new Paragraph("Second subsection of a second section of a first chapter", new Font(Font.FontFamily.HELVETICA, 10, Font.BOLD, BaseColor.BLUE))); s2.Indentation = 10; s2.Add(new Paragraph("Some text...")); s2 = s1.AddSection(new Paragraph("Third subsection of a second section of a first chapter", new Font(Font.FontFamily.HELVETICA, 10, Font.BOLD, BaseColor.BLUE))); s2.Indentation = 10; s2.Add(new Paragraph("Some text...")); s1 = c.AddSection(new Paragraph("Third section of a first chapter", new Font(Font.FontFamily.HELVETICA, 13, Font.BOLD, BaseColor.BLUE))); s1.Indentation = 20; s2 = s1.AddSection(new Paragraph("First subsection of a third section of a first chapter", new Font(Font.FontFamily.HELVETICA, 10, Font.BOLD, BaseColor.BLUE))); s2.Indentation = 10; s2.Add(new Paragraph("Some text...")); s2 = s1.AddSection(new Paragraph("Second subsection of a third section of a first chapter", new Font(Font.FontFamily.HELVETICA, 10, Font.BOLD, BaseColor.BLUE))); s2.Indentation = 10; s2.Add(new Paragraph("Some text...")); s2 = s1.AddSection(new Paragraph("Third subsection of a third section of a first chapter", new Font(Font.FontFamily.HELVETICA, 10, Font.BOLD, BaseColor.BLUE))); s2.Indentation = 10; s2.Add(new Paragraph("Some text...")); document.Add(c); c = new Chapter( new Paragraph("Second chapter", new Font(Font.FontFamily.HELVETICA, 16, Font.BOLD, BaseColor.BLUE)), 2); c.TriggerNewPage = false; c.Indentation = 40; s1 = c.AddSection(new Paragraph("First section of a second chapter", new Font(Font.FontFamily.HELVETICA, 13, Font.BOLD, BaseColor.BLUE))); s1.Indentation = 20; s2 = s1.AddSection(new Paragraph("First subsection of a first section of a second chapter", new Font(Font.FontFamily.HELVETICA, 10, Font.BOLD, BaseColor.BLUE))); s2.Indentation = 10; s2.Add(new Paragraph("Some text...")); s2 = s1.AddSection(new Paragraph("Second subsection of a first section of a second chapter", new Font(Font.FontFamily.HELVETICA, 10, Font.BOLD, BaseColor.BLUE))); s2.Indentation = 10; s2.Add(new Paragraph("Some text...")); s2 = s1.AddSection(new Paragraph("Third subsection of a first section of a second chapter", new Font(Font.FontFamily.HELVETICA, 10, Font.BOLD, BaseColor.BLUE))); s2.Indentation = 10; s2.Add(new Paragraph("Some text...")); s1 = c.AddSection(new Paragraph("Second section of a second chapter", new Font(Font.FontFamily.HELVETICA, 13, Font.BOLD, BaseColor.BLUE))); s1.Indentation = 20; s2 = s1.AddSection(new Paragraph("First subsection of a second section of a second chapter", new Font(Font.FontFamily.HELVETICA, 10, Font.BOLD, BaseColor.BLUE))); s2.Indentation = 10; s2.Add(new Paragraph("Some text...")); s2 = s1.AddSection(new Paragraph("Second subsection of a second section of a second chapter", new Font(Font.FontFamily.HELVETICA, 10, Font.BOLD, BaseColor.BLUE))); s2.Indentation = 10; s2.Add(new Paragraph("Some text...")); s2 = s1.AddSection(new Paragraph("Third subsection of a second section of a second chapter", new Font(Font.FontFamily.HELVETICA, 10, Font.BOLD, BaseColor.BLUE))); s2.Indentation = 10; s2.Add(new Paragraph("Some text...")); s1 = c.AddSection(new Paragraph("Third section of a second chapter", new Font(Font.FontFamily.HELVETICA, 13, Font.BOLD, BaseColor.BLUE))); s1.Indentation = 20; s2 = s1.AddSection(new Paragraph("First subsection of a third section of a second chapter", new Font(Font.FontFamily.HELVETICA, 10, Font.BOLD, BaseColor.BLUE))); s2.Indentation = 10; s2.Add(new Paragraph("Some text...")); s2 = s1.AddSection(new Paragraph("Second subsection of a third section of a second chapter", new Font(Font.FontFamily.HELVETICA, 10, Font.BOLD, BaseColor.BLUE))); s2.Indentation = 10; s2.Add(new Paragraph("Some text...")); s2 = s1.AddSection(new Paragraph("Third subsection of a third section of a second chapter", new Font(Font.FontFamily.HELVETICA, 10, Font.BOLD, BaseColor.BLUE))); s2.Indentation = 10; s2.Add(new Paragraph("Some text...")); document.Add(c); c = new Chapter( new Paragraph("Third chapter", new Font(Font.FontFamily.HELVETICA, 16, Font.BOLD, BaseColor.BLUE)), 3); c.TriggerNewPage = false; c.Indentation = 40; s1 = c.AddSection(new Paragraph("First section of a third chapter", new Font(Font.FontFamily.HELVETICA, 13, Font.BOLD, BaseColor.BLUE))); s1.Indentation = 20; s2 = s1.AddSection(new Paragraph("First subsection of a first section of a third chapter", new Font(Font.FontFamily.HELVETICA, 10, Font.BOLD, BaseColor.BLUE))); s2.Indentation = 10; s2.Add(new Paragraph("Some text...")); s2 = s1.AddSection(new Paragraph("Second subsection of a first section of a third chapter", new Font(Font.FontFamily.HELVETICA, 10, Font.BOLD, BaseColor.BLUE))); s2.Indentation = 10; s2.Add(new Paragraph("Some text...")); s2 = s1.AddSection(new Paragraph("Third subsection of a first section of a third chapter", new Font(Font.FontFamily.HELVETICA, 10, Font.BOLD, BaseColor.BLUE))); s2.Indentation = 10; s2.Add(new Paragraph("Some text...")); s1 = c.AddSection(new Paragraph("Second section of a third chapter", new Font(Font.FontFamily.HELVETICA, 13, Font.BOLD, BaseColor.BLUE))); s1.Indentation = 20; s2 = s1.AddSection(new Paragraph("First subsection of a second section of a third chapter", new Font(Font.FontFamily.HELVETICA, 10, Font.BOLD, BaseColor.BLUE))); s2.Indentation = 10; s2.Add(new Paragraph("Some text...")); s2 = s1.AddSection(new Paragraph("Second subsection of a second section of a third chapter", new Font(Font.FontFamily.HELVETICA, 10, Font.BOLD, BaseColor.BLUE))); s2.Indentation = 10; s2.Add(new Paragraph("Some text...")); s2 = s1.AddSection(new Paragraph("Third subsection of a second section of a third chapter", new Font(Font.FontFamily.HELVETICA, 10, Font.BOLD, BaseColor.BLUE))); s2.Indentation = 10; s2.Add(new Paragraph("Some text...")); s1 = c.AddSection(new Paragraph("Third section of a third chapter", new Font(Font.FontFamily.HELVETICA, 13, Font.BOLD, BaseColor.BLUE))); s1.Indentation = 20; s2 = s1.AddSection(new Paragraph("First subsection of a third section of a third chapter", new Font(Font.FontFamily.HELVETICA, 10, Font.BOLD, BaseColor.BLUE))); s2.Indentation = 10; s2.Add(new Paragraph("Some text...")); s2 = s1.AddSection(new Paragraph("Second subsection of a third section of a third chapter", new Font(Font.FontFamily.HELVETICA, 10, Font.BOLD, BaseColor.BLUE))); s2.Indentation = 10; s2.Add(new Paragraph("Some text...")); s2 = s1.AddSection(new Paragraph("Third subsection of a third section of a third chapter", new Font(Font.FontFamily.HELVETICA, 10, Font.BOLD, BaseColor.BLUE))); s2.Indentation = 10; s2.Add(new Paragraph("Some text...")); document.Add(c); document.Close(); int[] nums = new int[] {114, 60}; CheckNums(nums); CompareResults("11"); }
static void Main(string[] args) { string folder = @"C:\Users\Administrator\Desktop\TESTIMGS"; string pdf = @"C:\Users\Administrator\Desktop\generatedpdf.pdf"; Document pdfDoc = new Document(PageSize.A4,5f,5f,5f,5f); float docw = pdfDoc.PageSize.Width; float doch = pdfDoc.PageSize.Height; PdfDate st = new PdfDate(DateTime.Today); Chapter chapter = new Chapter(new Paragraph("Dragon ball chapter 1"),1); try { var stream = File.Create(pdf); var writer = PdfWriter.GetInstance(pdfDoc, stream); pdfDoc.Open(); DirectoryInfo di = new DirectoryInfo(folder); var files = di.GetFiles(); if (files != null) { foreach (var fi in files) { // img.ScaleToFit(750, 550); pdfDoc.NewPage(); Image img = Image.GetInstance(fi.FullName); float h = img.Height; float w = img.Width; float hp = doch / h; float wp = docw / w; //if (img.Width > docw*1.15f) { // img.ScaleToFit(docw*1.35f, doch*1.35f); //img.SetAbsolutePosition(5, 5); // img.ScaleToFit(pdfDoc.PageSize.Width - 10, pdfDoc.PageSize.Width - 10); } //Section section = chapter.AddSection(0f, "Pages", 1); //pdfDoc.Add(chapter); PdfPTable nestedTable = new PdfPTable(1); PdfPCell cell = new PdfPCell(img); cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER; nestedTable.AddCell(cell); //pdfDoc.Add(nestedTable); Section section = chapter.AddSection(0f,new Paragraph("", new Font() { Color = BaseColor.WHITE })); section.Add(nestedTable); pdfDoc.Add(section); //pdfDoc.Add(img); //pdfDoc.Add(new iTextSharp.text.Jpeg(new Uri(fi.FullName))); } } } catch (Exception ex) { } finally { //pdfDoc.Add(chapter); pdfDoc.Close(); Process.Start(pdf); } }
public static void ExportComputerSpecification(Chapter chapter, Font font, ComputerConfiguration computerInfo) { Section sectionPC = chapter.AddSection(new Paragraph("Computer specification.", font)); sectionPC.Add(new Chunk("\n")); Section osSection = sectionPC.AddSection(new Paragraph("Operating System.", font)); string bits = computerInfo.OperatingSystem.Is64bit ? " 64bit" : "32bit"; osSection.Add(new Paragraph(String.Format("\t \t {0} {1}", computerInfo.OperatingSystem.Name, bits))); sectionPC.Add(new Chunk("\n")); Section processor = sectionPC.AddSection(new Paragraph("Processors.", font)); foreach (var pr in computerInfo.Processors) { processor.Add(new Paragraph(String.Format("\t \t Name: {0}", pr.Name))); processor.Add(new Paragraph(String.Format("\t \t Threads: {0}", pr.Threads))); processor.Add(new Paragraph(String.Format("\t \t Max clock speed: {0} MHz", pr.MaxClockSpeed))); } sectionPC.Add(new Chunk("\n")); Section memory = sectionPC.AddSection(new Paragraph("Memory modules.", font)); PdfPTable table = new PdfPTable(3); table.AddCell(CreateHeaderPdfPCell("Type")); table.AddCell(CreateHeaderPdfPCell("Capacity (GB)")); table.AddCell(CreateHeaderPdfPCell("Speed (MHz)")); foreach (var mem in computerInfo.MemoryModules) { table.AddCell(new PdfPCell(new Phrase(mem.MemoryType.ToString()))); table.AddCell(new PdfPCell(new Phrase(mem.Capacity.ToString())) { HorizontalAlignment = Element.ALIGN_RIGHT }); table.AddCell(new PdfPCell(new Phrase(mem.Speed.ToString())) { HorizontalAlignment = Element.ALIGN_RIGHT }); } memory.Add(new Chunk("\n")); memory.Add(table); sectionPC.Add(new Chunk("\n")); Section storage = sectionPC.AddSection(new Paragraph("Storages.", font)); table = new PdfPTable(3); table.AddCell(CreateHeaderPdfPCell("Model")); table.AddCell(CreateHeaderPdfPCell("Size (GB)")); table.AddCell(CreateHeaderPdfPCell("Partitions")); foreach (var stor in computerInfo.StorageDevices) { table.AddCell(new PdfPCell(new Phrase(stor.Model))); table.AddCell(new PdfPCell(new Phrase(stor.Size.ToString())) { HorizontalAlignment = Element.ALIGN_RIGHT }); table.AddCell(new PdfPCell(new Phrase(string.Join(",", stor.DriveLetters.Select(x => x.Replace(":", "")))))); } storage.Add(new Chunk("\n")); storage.Add(table); }
/// <summary> /// 保存为pdf /// </summary> /// <param name="filename"></param> public void SavePdf(string filename) { FileInfo fileinfo = new FileInfo(filename); if (fileinfo.Directory.Exists == false) Directory.CreateDirectory(fileinfo.DirectoryName); Document doc = new Document(PageSize.A5, 10, 10, 10, 10); PdfWriter.GetInstance(doc, new FileStream(filename, FileMode.Create)); doc.Open(); //指定字体库,并创建字体 BaseFont baseFont = BaseFont.CreateFont( "C:\\WINDOWS\\FONTS\\SIMYOU.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); iTextSharp.text.Font font1 = new iTextSharp.text.Font(baseFont, 18); iTextSharp.text.Font font2 = new iTextSharp.text.Font(baseFont, 20); Chapter chapter1 = new Chapter(title, 1); chapter1.Add(new Paragraph(title, font2)); chapter1.Add(new Paragraph(auther, font1)); chapter1.Add(new Paragraph(introduction, font1)); for (int i = 0; i < catalogs.Count; i++) { Section section1 = chapter1.AddSection(catalogs[i].text); section1.Add(new Paragraph(catalogs[i].page.text, font1)); section1.TriggerNewPage = true; section1.BookmarkOpen = false; } chapter1.BookmarkOpen = false; doc.Add(chapter1); doc.Close(); }
public static void CreatePDF(string directoryPath , string pdfFile, string name, ComicDownloaderSettings settings){ try { Directory.CreateDirectory(Path.GetDirectoryName(pdfFile)); } finally{} Document pdfDoc = new Document(PageSize.A4); AssemblyInfoHelper info = new AssemblyInfoHelper(typeof(PDFHelper)); pdfDoc.AddAuthor(info.Company); pdfDoc.AddCreationDate(); pdfDoc.AddTitle(name); float docw = pdfDoc.PageSize.Width; float doch = pdfDoc.PageSize.Width; PdfDate st = new PdfDate(DateTime.Today); Chapter chapter = new Chapter(new Paragraph(name), 1); try { var stream = File.Create(pdfFile); var writer = PdfWriter.GetInstance(pdfDoc, stream); pdfDoc.Open(); if (settings.IncludePDFIntroPage && settings.PdfIntroPagePosition == PagePosition.FirstPage) EmbedeIntroPage(pdfDoc, writer); DirectoryInfo di = new DirectoryInfo(directoryPath); var files = di.GetFiles(); if (files != null) { foreach (var fi in files) { Image img = Image.GetInstance(fi.FullName); float h = img.Height; float w = img.Width; float hp = doch / h; float wp = docw / w; ///img.ScaleToFit(docw * 1.35f, doch * 1.35f); // img.ScaleToFit(750, 550); pdfDoc.NewPage(); //pdfDoc.Add(img); PdfPTable nestedTable = new PdfPTable(1); PdfPCell cell = new PdfPCell(img); cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER; nestedTable.AddCell(cell); //pdfDoc.Add(nestedTable); Section section = chapter.AddSection(0f, new Paragraph("", new Font() { Color = BaseColor.WHITE })); //section.Add(nestedTable); //pdfDoc.Add(section); pdfDoc.Add(nestedTable); } if (settings.IncludePDFIntroPage && settings.PdfIntroPagePosition == PagePosition.LastPage) EmbedeIntroPage(pdfDoc, writer); } } catch (Exception ex) { MyLogger.Log(ex); } finally { pdfDoc.Close(); } }
//public void PDModel2Html(PDModel m) //{ // Export(m, ExportTyep.HTML); //} private void Export(IList<PDTable> tableList,string title, ExportTyep exportType) { Document doc = new Document(PageSize.A4.Rotate(), 20, 20, 20, 20); DocWriter w; switch (exportType) { case ExportTyep.PDF: w = PdfWriter.GetInstance(doc, new FileStream(fileName, FileMode.Create, FileAccess.Write)); break; case ExportTyep.RTF: w = RtfWriter2.GetInstance(doc, new FileStream(fileName, FileMode.Create, FileAccess.Write)); break; //case ExportTyep.HTML: // w = HtmlWriter.GetInstance(doc, new FileStream(fileName, FileMode.Create, FileAccess.Write)); //break; default: break; } doc.Open(); doc.NewPage(); //IList<PDTable> tableList = m.AllTableList; //Chapter cpt = new Chapter(m.Name, 1); Chapter cpt = new Chapter(title, 1); Section sec; //doc.AddTitle(m.Name); doc.AddTitle(title); doc.AddAuthor("Kalman"); doc.AddCreationDate(); doc.AddCreator("Kalman"); doc.AddSubject("PDM数据库文档"); foreach (PDTable table in tableList) { sec = cpt.AddSection(new Paragraph(string.Format("{0}[{1}]", table.Name, table.Code), font)); if (string.IsNullOrEmpty(table.Comment) == false) { Chunk chunk = new Chunk(table.Comment, font); sec.Add(chunk); } t = new Table(9, table.ColumnList.Count); //t.Border = 15; //t.BorderColor = Color.BLACK; //t.BorderWidth = 1.0f; t.AutoFillEmptyCells = true; t.CellsFitPage = true; t.TableFitsPage = true; t.Cellpadding = 3; //if (exportType == ExportTyep.PDF) t.Cellspacing = 2; t.DefaultVerticalAlignment = Element.ALIGN_MIDDLE; t.SetWidths(new int[] { 200, 200, 150, 50, 50, 50, 50, 50, 300 }); t.AddCell(BuildHeaderCell("名称")); t.AddCell(BuildHeaderCell("代码")); t.AddCell(BuildHeaderCell("数据类型")); t.AddCell(BuildHeaderCell("长度")); t.AddCell(BuildHeaderCell("精度")); t.AddCell(BuildHeaderCell("主键")); t.AddCell(BuildHeaderCell("外键")); t.AddCell(BuildHeaderCell("可空")); t.AddCell(BuildHeaderCell("注释")); foreach (PDColumn column in table.ColumnList) { t.AddCell(BuildCell(column.Name)); t.AddCell(BuildCell(column.Code)); t.AddCell(BuildCell(column.DataType)); t.AddCell(BuildCell(column.Length == 0 ? "" : column.Length.ToString())); t.AddCell(BuildCell(column.Precision == 0 ? "" : column.Precision.ToString())); t.AddCell(BuildCell(column.IsPK ? " √" : "")); t.AddCell(BuildCell(column.IsFK ? " √" : "")); t.AddCell(BuildCell(column.Mandatory ? "" : " √")); t.AddCell(BuildCell(column.Comment)); } sec.Add(t); } doc.Add(cpt); doc.Close(); }
private List<SOTable> Export(DbSchema schema, SODatabase db, List<SOTable> tableList, ExportTyep exportType) { if (schema == null) throw new ArgumentException("参数schema不能为空", "schema"); if (db == null) throw new ArgumentException("参数dbName不能为空", "dbName"); Document doc = new Document(PageSize.A4.Rotate(), 20, 20, 20, 20); DocWriter w; switch (exportType) { case ExportTyep.PDF: w = PdfWriter.GetInstance(doc, new FileStream(fileName, FileMode.Create, FileAccess.Write)); break; case ExportTyep.RTF: w = RtfWriter2.GetInstance(doc, new FileStream(fileName, FileMode.Create, FileAccess.Write)); break; //case ExportTyep.HTML: // w = HtmlWriter.GetInstance(doc, new FileStream(fileName, FileMode.Create, FileAccess.Write)); //break; default: break; } doc.Open(); doc.NewPage(); if (tableList == null) tableList = schema.GetTableList(db); Chapter cpt = new Chapter(db.Name, 1); Section sec; doc.AddTitle(db.Name); doc.AddAuthor("Kalman"); doc.AddCreationDate(); doc.AddCreator("Kalman"); doc.AddSubject("数据库文档"); foreach (SOTable table in tableList) { sec = cpt.AddSection(new Paragraph(table.Name, font)); if (string.IsNullOrEmpty(table.Comment) == false) { Chunk chunk = new Chunk(table.Comment, font); sec.Add(chunk); } List<SOColumn> columnList = schema.GetTableColumnList(table); t = new Table(7, columnList.Count); t.AutoFillEmptyCells = true; t.CellsFitPage = true; t.TableFitsPage = true; t.Cellpadding = 3; //if (exportType == ExportTyep.PDF) t.Cellspacing = 2; t.DefaultVerticalAlignment = Element.ALIGN_MIDDLE; t.SetWidths(new int[] { 200, 150, 50, 50, 50, 100, 300 }); t.AddCell(BuildHeaderCell("名称")); t.AddCell(BuildHeaderCell("数据类型")); t.AddCell(BuildHeaderCell("主键")); t.AddCell(BuildHeaderCell("标志")); t.AddCell(BuildHeaderCell("可空")); t.AddCell(BuildHeaderCell("默认值")); t.AddCell(BuildHeaderCell("注释")); foreach (SOColumn column in columnList) { t.AddCell(BuildCell(column.Name)); t.AddCell(BuildCell(GetDbColumnType(column))); t.AddCell(BuildCell(column.PrimaryKey ? " √" : "")); t.AddCell(BuildCell(column.Identify ? " √" : "")); t.AddCell(BuildCell(column.Nullable ? " √" : "")); t.AddCell(BuildCell(column.DefaultValue == null ? "" : column.DefaultValue.ToString())); t.AddCell(BuildCell(column.Comment)); } sec.Add(t); } doc.Add(cpt); doc.Close(); return tableList; }
public static void Genera(string sourcePath,string content) { sourcePath = HttpContext.Current.Server.MapPath(sourcePath); //定义一个Document,并设置页面大小为A4,竖向 iTextSharp.text.Document doc = new Document(PageSize.A4); try { //写实例 PdfWriter.GetInstance(doc, new FileStream(sourcePath, FileMode.Create)); //打开document doc.Open(); //载入字体 BaseFont baseFont = BaseFont.CreateFont( "C:\\WINDOWS\\FONTS\\SIMHEI.TTF", //黑体 BaseFont.IDENTITY_H, //横向字体 BaseFont.NOT_EMBEDDED); iTextSharp.text.Font font = new iTextSharp.text.Font(baseFont, 9); //写入一个段落, Paragraph doc.Add(new Paragraph("第一段:" + content, font)); doc.Add(new Paragraph("这是第二段 !", font)); #region 图片 //以下代码用来添加图片,此时图片是做为背景加入到pdf文件当中的: Stream inputImageStream = new FileStream(HttpContext.Current.Server.MapPath("~/images/logo.jpg"), FileMode.Open, FileAccess.Read, FileShare.Read); iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(inputImageStream); image.SetAbsolutePosition(0, 0); image.Alignment = iTextSharp.text.Image.UNDERLYING; //这里可以设定图片是做为背景还是做为元素添加到文件中 doc.Add(image); #endregion #region 其他元素 doc.Add(new Paragraph("Hello World")); //另起一行。有几种办法建立一个段落,如: Paragraph p1 = new Paragraph(new Chunk("This is my first paragraph.\n", FontFactory.GetFont(FontFactory.HELVETICA, 12))); Paragraph p2 = new Paragraph(new Phrase("This is my second paragraph.", FontFactory.GetFont(FontFactory.HELVETICA, 12))); Paragraph p3 = new Paragraph("This is my third paragraph.", FontFactory.GetFont(FontFactory.HELVETICA, 12)); //所有有些对象将被添加到段落中: p1.Add("you can add string here\n\t"); p1.Add(new Chunk("you can add chunks \n")); p1.Add(new Phrase("or you can add phrases.\n")); doc.Add(p1); doc.Add(p2); doc.Add(p3); //创建了一个内容为“hello World”、红色、斜体、COURIER字体、尺寸20的一个块: Chunk chunk = new Chunk("创建了一个内容为“hello World”、红色、斜体、COURIER字体、尺寸20的一个块", FontFactory.GetFont(FontFactory.COURIER, 20, iTextSharp.text.Font.COURIER, new iTextSharp.text.Color(255, 0, 0))); doc.Add(chunk); //如果你希望一些块有下划线或删除线,你可以通过改变字体风格简单做到: Chunk chunk1 = new Chunk("This text is underlined", FontFactory.GetFont(FontFactory.HELVETICA, 12, iTextSharp.text.Font.UNDEFINED)); Chunk chunk2 = new Chunk("This font is of type ITALIC | STRIKETHRU", FontFactory.GetFont(FontFactory.HELVETICA, 12, iTextSharp.text.Font.ITALIC | iTextSharp.text.Font.STRIKETHRU)); //改变块的背景 chunk2.SetBackground(new iTextSharp.text.Color(0xFF, 0xFF, 0x00)); //上标/下标 chunk1.SetTextRise(5); doc.Add(chunk1); doc.Add(chunk2); //外部链接示例: Anchor anchor = new Anchor("website", FontFactory.GetFont(FontFactory.HELVETICA, 12, iTextSharp.text.Font.UNDEFINED, new iTextSharp.text.Color(0, 0, 255))); anchor.Reference = "http://itextsharp.sourceforge.net"; anchor.Name = "website"; //内部链接示例: Anchor anchor1 = new Anchor("This is an internal link\n\n"); anchor1.Name = "link1"; Anchor anchor2 = new Anchor("Click here to jump to the internal link\n\f"); anchor2.Reference = "#link1"; doc.Add(anchor); doc.Add(anchor1); doc.Add(anchor2); //排序列表示例: List list = new List(true, 20); list.Add(new ListItem("First line")); list.Add(new ListItem("The second line is longer to see what happens once the end of the line is reached. Will it start on a new line?")); list.Add(new ListItem("Third line")); doc.Add(list); //文本注释: Annotation a = new Annotation("authors", "Maybe its because I wanted to be an author myself that I wrote iText."); doc.Add(a); //包含页码没有任何边框的页脚。 iTextSharp.text.HeaderFooter footer = new iTextSharp.text.HeaderFooter(new Phrase("This is page: "), true); footer.Border = iTextSharp.text.Rectangle.NO_BORDER; doc.Footer = footer; //Chapter对象和Section对象自动构建一个树: iTextSharp.text.Font f1 = new iTextSharp.text.Font(); f1.SetStyle(iTextSharp.text.Font.BOLD); Paragraph cTitle = new Paragraph("This is chapter 1", f1); Chapter chapter = new Chapter(cTitle, 1); Paragraph sTitle = new Paragraph("This is section 1 in chapter 1", f1); Section section = chapter.AddSection(sTitle, 1); doc.Add(chapter); //构建了一个简单的表: Table aTable = new Table(4, 4); aTable.AutoFillEmptyCells = true; aTable.AddCell("2.2", new System.Drawing.Point(2, 2)); aTable.AddCell("3.3", new System.Drawing.Point(3, 3)); aTable.AddCell("2.1", new System.Drawing.Point(2, 1)); aTable.AddCell("1.3", new System.Drawing.Point(1, 3)); doc.Add(aTable); //构建了一个不简单的表: Table table = new Table(3); table.BorderWidth = 1; table.BorderColor = new iTextSharp.text.Color(0, 0, 255); table.Cellpadding = 5; table.Cellspacing = 5; Cell cell = new Cell("header"); cell.Header = true; cell.Colspan = 3; table.AddCell(cell); cell = new Cell("example cell with colspan 1 and rowspan 2"); cell.Rowspan = 2; cell.BorderColor = new iTextSharp.text.Color(255, 0, 0); table.AddCell(cell); table.AddCell("1.1"); table.AddCell("2.1"); table.AddCell("1.2"); table.AddCell("2.2"); table.AddCell("cell test1"); cell = new Cell("big cell"); cell.Rowspan = 2; cell.Colspan = 2; cell.BackgroundColor = new iTextSharp.text.Color(0xC0, 0xC0, 0xC0); table.AddCell(cell); table.AddCell("cell test2"); // 改变了单元格“big cell”的对齐方式: cell.HorizontalAlignment = Element.ALIGN_CENTER; cell.VerticalAlignment = Element.ALIGN_MIDDLE; doc.Add(table); #endregion //关闭document doc.Close(); //打开PDF,看效果 //Process.Start(sourcePath); } catch (DocumentException de) { Console.WriteLine(de.Message); Console.ReadKey(); } catch (IOException io) { Console.WriteLine(io.Message); Console.ReadKey(); } }