public void MergePdf(string[] pdfFiles, string outputPath) { int pdfCount = 0; int f = 0; string filename = String.Empty; iTextSharp.text.pdf.PdfReader reader = null; int pageCount = 0; iTextSharp.text.Document pdfDoc = null; iTextSharp.text.pdf.PdfWriter writer = null; iTextSharp.text.pdf.PdfContentByte cb = null; iTextSharp.text.pdf.PdfImportedPage page = null; int rotation = 0; iTextSharp.text.Font bookmarkFont = iTextSharp.text.FontFactory.GetFont(iTextSharp.text.FontFactory.HELVETICA, 4, iTextSharp.text.Font.BOLD, iTextSharp.text.BaseColor.RED); try { pdfCount = pdfFiles.Length; if (pdfCount > 1) { filename = pdfFiles[f]; reader = new iTextSharp.text.pdf.PdfReader(filename); pageCount = reader.NumberOfPages; pdfDoc = new iTextSharp.text.Document(reader.GetPageSizeWithRotation(1), 18, 18, 18, 18); writer = iTextSharp.text.pdf.PdfWriter.GetInstance(pdfDoc, new System.IO.FileStream(outputPath, System.IO.FileMode.Create)); pdfDoc.AddAuthor("OPGK w Lublinie sp. z o. o. Sławomir Aleksak"); pdfDoc.AddCreator("Konwerter"); pdfDoc.Open(); cb = writer.DirectContent; while (f < pdfCount) { var i = 0; while (i < pageCount) { i += 1; pdfDoc.SetPageSize(reader.GetPageSizeWithRotation(i)); pdfDoc.NewPage(); if (i == 1) { iTextSharp.text.Paragraph para = new iTextSharp.text.Paragraph(System.IO.Path.GetFileName(filename).ToUpper(), bookmarkFont); iTextSharp.text.Chapter chpter = new iTextSharp.text.Chapter(para, f + 1); pdfDoc.Add(chpter); } page = writer.GetImportedPage(reader, i); rotation = reader.GetPageRotation(i); if (rotation == 90) { cb.AddTemplate(page, 0, -1.0F, 1.0F, 0, 0, reader.GetPageSizeWithRotation(i).Height); } if (rotation == 270) { cb.AddTemplate(page, 0, 1.0F, -1.0F, 0, reader.GetPageSizeWithRotation(i).Width + 60, -30); } else { cb.AddTemplate(page, 1.0F, 0, 0, 1.0F, 0, 0); } } f += 1; if (f < pdfCount) { filename = pdfFiles[f]; reader = new iTextSharp.text.pdf.PdfReader(filename); pageCount = reader.NumberOfPages; } } pdfDoc.Close(); } } catch (Exception ex) { throw ex; } }
/// <summary> /// a megadott file elérési úttal elkészíti a pdf dokumentumot /// </summary> /// <param name="financedAmount"></param> /// <param name="calcValues"></param> /// <param name="pdfFileWithPath"></param> /// <returns></returns> public void CreateLeasingDocument(string financedAmount, System.Collections.Specialized.StringCollection calcValues, string pdfFileWithPath) { iTextSharp.text.Document document = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 36, 36, 36, 36); //marginTop : 72 try { // writer letrehozas iTextSharp.text.pdf.PdfWriter.GetInstance(document, new System.IO.FileStream(pdfFileWithPath, System.IO.FileMode.Create)); // lablec iTextSharp.text.HeaderFooter footer = new iTextSharp.text.HeaderFooter(new iTextSharp.text.Phrase(), true); footer.Border = iTextSharp.text.Rectangle.NO_BORDER; footer.Alignment = iTextSharp.text.HeaderFooter.ALIGN_CENTER; document.Footer = footer; // dokumentum megnyitas document.Open(); iTextSharp.text.Chapter chapter1 = new iTextSharp.text.Chapter(2); chapter1.NumberDepth = 0; //fejlec kep iTextSharp.text.Image imgHeader = GetHeaderImageFile(); imgHeader.Alignment = iTextSharp.text.Image.ALIGN_LEFT; imgHeader.Alt = "NE VEDD MEG, BÉRELD!"; iTextSharp.text.Table hTable = new iTextSharp.text.Table(1, 1); iTextSharp.text.Cell hCell = new iTextSharp.text.Cell(imgHeader); hTable.AutoFillEmptyCells = true; hTable.TableFitsPage = true; hTable.WidthPercentage = 100; hTable.AddCell(hCell); hTable.Alignment = iTextSharp.text.Table.ALIGN_LEFT; chapter1.Add(hTable); iTextSharp.text.Color defaultTextColor = new iTextSharp.text.Color(0, 0, 128); //uj sor, tavtarto a tabla es a fejleckep kozott chapter1.Add(new iTextSharp.text.Paragraph(" ")); iTextSharp.text.pdf.BaseFont default_ttf = iTextSharp.text.pdf.BaseFont.CreateFont(CompanyGroup.Helpers.ConfigSettingsParser.GetString("FontFile", "c:\\Windows\\Fonts\\calibri.ttf"), iTextSharp.text.pdf.BaseFont.IDENTITY_H, iTextSharp.text.pdf.BaseFont.EMBEDDED); iTextSharp.text.Font titlefont = new iTextSharp.text.Font(default_ttf, 16, iTextSharp.text.Font.BOLDITALIC, defaultTextColor); iTextSharp.text.Font defaultFont = new iTextSharp.text.Font(default_ttf, 15, iTextSharp.text.Font.NORMAL, defaultTextColor); //cimsor iTextSharp.text.Paragraph pgTitle = new iTextSharp.text.Paragraph("Finanszírozási ajánlat", titlefont); chapter1.Add(pgTitle); //tablazat iTextSharp.text.Table table1 = new iTextSharp.text.Table(2, 1); table1.BorderColor = table1.DefaultCellBorderColor = defaultTextColor; table1.Padding = 2; table1.Spacing = 1; table1.AutoFillEmptyCells = true; table1.Alignment = iTextSharp.text.Table.ALIGN_LEFT; table1.WidthPercentage = 80.0f; table1.Widths = new float[] { 60, 20 }; iTextSharp.text.Paragraph tmpParagraph = new iTextSharp.text.Paragraph("A konfiguráció nettó vételára:", defaultFont); iTextSharp.text.Cell cell1 = new iTextSharp.text.Cell(tmpParagraph); cell1.HorizontalAlignment = iTextSharp.text.Element.ALIGN_CENTER; cell1.VerticalAlignment = iTextSharp.text.Element.ALIGN_MIDDLE; table1.AddCell(cell1); tmpParagraph = new iTextSharp.text.Paragraph(financedAmount + " Ft", defaultFont); iTextSharp.text.Cell cell2 = new iTextSharp.text.Cell(tmpParagraph); cell2.HorizontalAlignment = iTextSharp.text.Element.ALIGN_RIGHT; cell2.VerticalAlignment = iTextSharp.text.Element.ALIGN_MIDDLE; table1.AddCell(cell2); chapter1.Add(table1); //5 oszlopos tabla iTextSharp.text.Table table2 = new iTextSharp.text.Table(5); table2.BorderColor = defaultTextColor; table2.Padding = 2; table2.Spacing = 1; table2.AutoFillEmptyCells = true; table2.Alignment = iTextSharp.text.Table.ALIGN_LEFT; table2.WidthPercentage = 100.0f; table2.Widths = new float[] { 20, 20, 20, 20, 20 }; //első sor tmpParagraph = new iTextSharp.text.Paragraph("Önerő", defaultFont); cell1 = new iTextSharp.text.Cell(tmpParagraph); cell1.HorizontalAlignment = iTextSharp.text.Element.ALIGN_CENTER; cell1.VerticalAlignment = iTextSharp.text.Element.ALIGN_MIDDLE; cell1.Header = true; cell1.Colspan = 4; cell1.BorderColor = defaultTextColor; table2.AddCell(cell1); tmpParagraph = new iTextSharp.text.Paragraph("0 Ft", defaultFont); cell2 = new iTextSharp.text.Cell(tmpParagraph); cell2.HorizontalAlignment = iTextSharp.text.Element.ALIGN_RIGHT; cell2.VerticalAlignment = iTextSharp.text.Element.ALIGN_MIDDLE; cell2.BorderColor = defaultTextColor; table2.AddCell(cell2); //második sor //table2.AddCell(""); tmpParagraph = new iTextSharp.text.Paragraph("Deviza: HUF", defaultFont); iTextSharp.text.Cell tmpCell = new iTextSharp.text.Cell(tmpParagraph); tmpCell.HorizontalAlignment = iTextSharp.text.Element.ALIGN_RIGHT; tmpCell.VerticalAlignment = iTextSharp.text.Element.ALIGN_MIDDLE; tmpCell.BorderColor = defaultTextColor; tmpCell.Rowspan = 2; table2.AddCell(tmpCell); tmpParagraph = new iTextSharp.text.Paragraph("Futamidő hónapokban", defaultFont); cell1 = new iTextSharp.text.Cell(tmpParagraph); cell1.HorizontalAlignment = iTextSharp.text.Element.ALIGN_CENTER; cell1.VerticalAlignment = iTextSharp.text.Element.ALIGN_MIDDLE; cell1.BorderColor = defaultTextColor; cell1.Colspan = 4; table2.AddCell(cell1); //harmadik sor tmpParagraph = new iTextSharp.text.Paragraph("24", defaultFont); cell2 = new iTextSharp.text.Cell(tmpParagraph); cell2.HorizontalAlignment = iTextSharp.text.Element.ALIGN_RIGHT; cell2.VerticalAlignment = iTextSharp.text.Element.ALIGN_MIDDLE; cell2.BorderColor = defaultTextColor; table2.AddCell(cell2); tmpParagraph = new iTextSharp.text.Paragraph("36", defaultFont); iTextSharp.text.Cell cell3 = new iTextSharp.text.Cell(tmpParagraph); cell3.HorizontalAlignment = iTextSharp.text.Element.ALIGN_RIGHT; cell3.VerticalAlignment = iTextSharp.text.Element.ALIGN_MIDDLE; cell3.BorderColor = defaultTextColor; table2.AddCell(cell3); tmpParagraph = new iTextSharp.text.Paragraph("48", defaultFont); iTextSharp.text.Cell cell4 = new iTextSharp.text.Cell(tmpParagraph); cell4.HorizontalAlignment = iTextSharp.text.Element.ALIGN_RIGHT; cell4.VerticalAlignment = iTextSharp.text.Element.ALIGN_MIDDLE; cell4.BorderColor = defaultTextColor; table2.AddCell(cell4); tmpParagraph = new iTextSharp.text.Paragraph("60", defaultFont); iTextSharp.text.Cell cell5 = new iTextSharp.text.Cell(tmpParagraph); cell5.HorizontalAlignment = iTextSharp.text.Element.ALIGN_RIGHT; cell5.VerticalAlignment = iTextSharp.text.Element.ALIGN_MIDDLE; cell5.BorderColor = defaultTextColor; table2.AddCell(cell5); //negyedik sor tmpParagraph = new iTextSharp.text.Paragraph("Tartós bérlet", defaultFont); cell1 = new iTextSharp.text.Cell(tmpParagraph); cell1.HorizontalAlignment = iTextSharp.text.Element.ALIGN_RIGHT; cell1.VerticalAlignment = iTextSharp.text.Element.ALIGN_MIDDLE; cell1.BorderColor = defaultTextColor; table2.AddCell(cell1); tmpParagraph = new iTextSharp.text.Paragraph(GetItemByPositionFromStringCollection(0, calcValues), defaultFont); cell2 = new iTextSharp.text.Cell(tmpParagraph); cell2.HorizontalAlignment = iTextSharp.text.Element.ALIGN_RIGHT; cell2.VerticalAlignment = iTextSharp.text.Element.ALIGN_MIDDLE; cell2.BorderColor = defaultTextColor; table2.AddCell(cell2); tmpParagraph = new iTextSharp.text.Paragraph(GetItemByPositionFromStringCollection(1, calcValues), defaultFont); cell3 = new iTextSharp.text.Cell(tmpParagraph); cell3.HorizontalAlignment = iTextSharp.text.Element.ALIGN_RIGHT; cell3.VerticalAlignment = iTextSharp.text.Element.ALIGN_MIDDLE; cell3.BorderColor = defaultTextColor; table2.AddCell(cell3); tmpParagraph = new iTextSharp.text.Paragraph(GetItemByPositionFromStringCollection(2, calcValues), defaultFont); cell4 = new iTextSharp.text.Cell(tmpParagraph); cell4.HorizontalAlignment = iTextSharp.text.Element.ALIGN_RIGHT; cell4.VerticalAlignment = iTextSharp.text.Element.ALIGN_MIDDLE; cell4.BorderColor = defaultTextColor; table2.AddCell(cell4); tmpParagraph = new iTextSharp.text.Paragraph(GetItemByPositionFromStringCollection(3, calcValues), defaultFont); cell5 = new iTextSharp.text.Cell(tmpParagraph); cell5.HorizontalAlignment = iTextSharp.text.Element.ALIGN_RIGHT; cell5.VerticalAlignment = iTextSharp.text.Element.ALIGN_MIDDLE; cell5.BorderColor = defaultTextColor; table2.AddCell(cell5); //ötödik sor table2.AddCell(""); tmpParagraph = new iTextSharp.text.Paragraph("Nettó havidíjak", defaultFont); cell1 = new iTextSharp.text.Cell(tmpParagraph); cell1.HorizontalAlignment = iTextSharp.text.Element.ALIGN_CENTER; cell1.VerticalAlignment = iTextSharp.text.Element.ALIGN_MIDDLE; cell1.BorderColor = defaultTextColor; cell1.Colspan = 4; table2.AddCell(cell1); //hatodik sor //tmpParagraph = new iTextSharp.text.Paragraph( "A kalkulált díjak biztosítási díjat is tartalmaznak.", defaultFont ); //cell1 = new iTextSharp.text.Cell( tmpParagraph ); //cell1.HorizontalAlignment = iTextSharp.text.Element.ALIGN_CENTER; //cell1.VerticalAlignment = iTextSharp.text.Element.ALIGN_MIDDLE; //cell1.BorderColor = defaultTextColor; //cell1.Colspan = 5; //table2.AddCell(cell1); chapter1.Add(table2); chapter1.Add(new iTextSharp.text.Paragraph(" ")); //szoveg iTextSharp.text.Font smallFont = new iTextSharp.text.Font(default_ttf, 9, iTextSharp.text.Font.ITALIC, defaultTextColor); iTextSharp.text.Paragraph sText = new iTextSharp.text.Paragraph("HUF alapú finanszírozás, a havi díj az 1 havi Buborhoz kötött", smallFont); chapter1.Add(sText); //uj sor chapter1.Add(new iTextSharp.text.Paragraph(" ")); //szoveg iTextSharp.text.Font bold_10_Font = new iTextSharp.text.Font(default_ttf, 10, iTextSharp.text.Font.BOLD, defaultTextColor); sText = new iTextSharp.text.Paragraph("Ajánlatunkat ajánlati kötöttség nélkül tettük meg!", bold_10_Font); chapter1.Add(sText); //szoveg sText = new iTextSharp.text.Paragraph("Az ügylet megkötéséhez a refinanszírozó jóváhagyása szükséges.", bold_10_Font); chapter1.Add(sText); //uj sor chapter1.Add(new iTextSharp.text.Paragraph(" ")); ////szoveg //var bold_12_Font = new iTextSharp.text.Font( default_ttf, 12, iTextSharp.text.Font.BOLD, defaultTextColor ); //sText = new iTextSharp.text.Paragraph( "Szerződéskötési díj: 0 Ft", bold_12_Font ); //chapter1.Add(sText); ////uj sor //chapter1.Add(new iTextSharp.text.Paragraph(" ")); //szoveg sText = new iTextSharp.text.Paragraph("A tartós bérlet alapvető jellemzői", bold_10_Font); chapter1.Add(sText); //szoveg iTextSharp.text.Font normal_10_Font = new iTextSharp.text.Font(default_ttf, 10, iTextSharp.text.Font.NORMAL, defaultTextColor); sText = new iTextSharp.text.Paragraph("A bérleti díjakat ÁFA terheli, mely visszaigényelhető", normal_10_Font); sText.IndentationLeft = 50; chapter1.Add(sText); //szoveg sText = new iTextSharp.text.Paragraph("Az eszköz a bérbeadó könyveiben kerül aktiválásra", normal_10_Font); sText.IndentationLeft = 50; chapter1.Add(sText); //szoveg sText = new iTextSharp.text.Paragraph("A havi díj költségként elszámolható, csökkentve ezáltal az adóalapot", normal_10_Font); sText.IndentationLeft = 50; chapter1.Add(sText); //uj sor chapter1.Add(new iTextSharp.text.Paragraph(" ")); //szoveg sText = new iTextSharp.text.Paragraph("Ha bármilyen kérdése merülne fel a konstrukciót illetően, forduljon hozzánk bizalommal!", bold_10_Font); chapter1.Add(sText); //uj sor chapter1.Add(new iTextSharp.text.Paragraph(" ")); //szoveg sText = new iTextSharp.text.Paragraph("Kublik Ádám", bold_10_Font); chapter1.Add(sText); //szoveg sText = new iTextSharp.text.Paragraph("értékesítési vezető", normal_10_Font); chapter1.Add(sText); //szoveg sText = new iTextSharp.text.Paragraph("HRP Finance", bold_10_Font); chapter1.Add(sText); //szoveg sText = new iTextSharp.text.Paragraph("Tel.: +36 1 452 46 16", normal_10_Font); chapter1.Add(sText); //szoveg sText = new iTextSharp.text.Paragraph("mob.: +36 70 452 46 16", normal_10_Font); chapter1.Add(sText); //szoveg sText = new iTextSharp.text.Paragraph("mail: [email protected]", normal_10_Font); chapter1.Add(sText); document.Add(chapter1); } catch (iTextSharp.text.DocumentException documentException) { throw documentException; } catch (System.IO.IOException ioeException) { throw ioeException; } finally { // dokumentum bezarasa document.Close(); } }
public iTextSharp.text.IElement ToPDF() { iTextSharp.text.Chapter pg = new iTextSharp.text.Chapter((iTextSharp.text.Paragraph)Paragraph.ToPDF(), Number); return(pg); }
/// <summary> /// 引用iTextSharp.dll导出pdf数据库字典文档 /// </summary> /// <param name="databaseName"></param> /// <param name="tables"></param> public static void ExportPdfByITextSharp(string fileName, string fontPath, string databaseName, List <TableDto> tables) { // TODO 创建并添加文档信息 iTextSharp.text.Document pdfDocument = new iTextSharp.text.Document(); pdfDocument.AddTitle(fileName); iTextSharp.text.pdf.PdfWriter pdfWriter = iTextSharp.text.pdf.PdfWriter.GetInstance(pdfDocument, new System.IO.FileStream(fileName, System.IO.FileMode.Create)); pdfDocument.Open(); // 打开文档 // TODO 标题 iTextSharp.text.Paragraph title = new iTextSharp.text.Paragraph("数据库字典文档\n\n", BaseFont(fontPath, 30, iTextSharp.text.Font.BOLD)); title.Alignment = iTextSharp.text.Element.ALIGN_CENTER; pdfDocument.Add(title); iTextSharp.text.Paragraph subTitle = new iTextSharp.text.Paragraph(" —— " + databaseName, BaseFont(fontPath, 20, iTextSharp.text.Font.NORMAL)); subTitle.Alignment = iTextSharp.text.Element.ALIGN_CENTER; pdfDocument.Add(subTitle); // TODO PDF换页 pdfDocument.NewPage(); // TODO 创建添加书签章节 int chapterNum = 1; // TODO 全局字体设置,处理iTextSharp中文不识别显示问题 iTextSharp.text.Font pdfFont = BaseFont(fontPath, 12, iTextSharp.text.Font.NORMAL); // TODO log table iTextSharp.text.Chapter chapter1 = new iTextSharp.text.Chapter(new iTextSharp.text.Paragraph(AppConst.LOG_CHAPTER_NAME, pdfFont), chapterNum); pdfDocument.Add(chapter1); pdfDocument.Add(new iTextSharp.text.Paragraph("\n", pdfFont)); // 换行 CreateLogTable(pdfDocument, pdfFont, tables); // TODO PDF换页 pdfDocument.NewPage(); // TODO overview table iTextSharp.text.Chapter chapter2 = new iTextSharp.text.Chapter(new iTextSharp.text.Paragraph(AppConst.TABLE_CHAPTER_NAME, pdfFont), (++chapterNum)); pdfDocument.Add(chapter2); pdfDocument.Add(new iTextSharp.text.Paragraph("\n", pdfFont)); // 换行 CreateOverviewTable(pdfDocument, pdfFont, tables); // TODO PDF换页 pdfDocument.NewPage(); // TODO table structure // TODO 添加书签章节 iTextSharp.text.Chapter chapter3 = new iTextSharp.text.Chapter(new iTextSharp.text.Paragraph(AppConst.TABLE_STRUCTURE_CHAPTER_NAME, pdfFont), (++chapterNum)); chapter3.BookmarkOpen = true; pdfDocument.Add(chapter3); pdfDocument.Add(new iTextSharp.text.Paragraph("\n", pdfFont)); // 换行 foreach (var table in tables) { string docTableName = table.TableName + " " + (!string.IsNullOrWhiteSpace(table.Comment) ? table.Comment : ""); // TODO 添加书签章节 iTextSharp.text.Section selection = chapter3.AddSection(20f, new iTextSharp.text.Paragraph(docTableName, pdfFont), chapterNum); pdfDocument.Add(selection); pdfDocument.Add(new iTextSharp.text.Paragraph("\n", pdfFont)); // 换行 // TODO 遍历数据库表 // TODO 创建表格 iTextSharp.text.pdf.PdfPTable pdfTable = new iTextSharp.text.pdf.PdfPTable(10); // TODO 添加列标题 pdfTable.AddCell(CreatePdfPCell("序号", pdfFont)); pdfTable.AddCell(CreatePdfPCell("列名", pdfFont)); pdfTable.AddCell(CreatePdfPCell("数据类型", pdfFont)); pdfTable.AddCell(CreatePdfPCell("长度", pdfFont)); pdfTable.AddCell(CreatePdfPCell("小数位", pdfFont)); pdfTable.AddCell(CreatePdfPCell("主键", pdfFont)); pdfTable.AddCell(CreatePdfPCell("自增", pdfFont)); pdfTable.AddCell(CreatePdfPCell("允许空", pdfFont)); pdfTable.AddCell(CreatePdfPCell("默认值", pdfFont)); pdfTable.AddCell(CreatePdfPCell("列说明", pdfFont)); // TODO 添加数据行,循环数据库表字段 foreach (var column in table.Columns) { pdfTable.AddCell(CreatePdfPCell(column.ColumnOrder, pdfFont)); pdfTable.AddCell(CreatePdfPCell(column.ColumnName, pdfFont)); pdfTable.AddCell(CreatePdfPCell(column.ColumnTypeName, pdfFont)); pdfTable.AddCell(CreatePdfPCell(column.Length, pdfFont)); pdfTable.AddCell(CreatePdfPCell(column.Scale, pdfFont)); pdfTable.AddCell(CreatePdfPCell(column.IsPK, pdfFont)); pdfTable.AddCell(CreatePdfPCell(column.IsIdentity, pdfFont)); pdfTable.AddCell(CreatePdfPCell(column.CanNull, pdfFont)); pdfTable.AddCell(CreatePdfPCell(column.DefaultVal, pdfFont)); pdfTable.AddCell(CreatePdfPCell(column.Comment, pdfFont)); } // TODO 设置表格居中 pdfTable.HorizontalAlignment = iTextSharp.text.Element.ALIGN_CENTER; pdfTable.TotalWidth = 520F; pdfTable.LockedWidth = true; pdfTable.SetWidths(new float[] { 50F, 60F, 60F, 50F, 50F, 50F, 50F, 50F, 50F, 50F }); // TODO 添加表格 pdfDocument.Add(pdfTable); // TODO PDF换页 pdfDocument.NewPage(); } // TODO 关闭释放PDF文档资源 pdfDocument.Close(); }