/// <summary> /// Add Page number at top of pdf file. /// </summary> /// <param name="fileName"></param> /// <returns> filename saved as</returns> public static string AddPageNumberToPdf(string fileName) { using PdfReader reader = new PdfReader(fileName); string fName = "cashBook_" + (DateTime.Now.ToFileTimeUtc() + 1001) + ".pdf"; using PdfWriter writer = new PdfWriter(Path.Combine(ReportHeaderDetails.WWWroot, fName)); using PdfDocument pdfDoc2 = new PdfDocument(reader, writer); Document doc2 = new Document(pdfDoc2); int numberOfPages = pdfDoc2.GetNumberOfPages(); for (int i = 1; i <= numberOfPages; i++) { doc2.ShowTextAligned(new Paragraph("Page " + i + " of " + numberOfPages), 559, 806, i, TextAlignment.RIGHT, VerticalAlignment.BOTTOM, 0); } doc2.Close(); return(fName); }
/// <summary> /// /// </summary> public void Work() { // var dest = Settings.Default.OutputLocation + "\\" + "12345678.pdf"; //Initialize PDF writer PdfWriter writer = new PdfWriter(dest); //Initialize PDF document PdfDocument pdf = new PdfDocument(writer); // Initialize document Document document = new Document(pdf); //Add paragraph to the document var para = new Paragraph("Hello World!"); para.SetTextAlignment(TextAlignment.CENTER); document.Add(para); para = new Paragraph("Hello World! For the second time!!"); para.SetTextAlignment(TextAlignment.LEFT); document.Add(para); para = new Paragraph("Hello World! For the third time!!!"); para.SetTextAlignment(TextAlignment.RIGHT); document.Add(para); para = new Paragraph("Hello World! For the fourth time!!!!"); para.SetTextAlignment(TextAlignment.JUSTIFIED_ALL); document.Add(para); // Add an image to the document. var imagePath = Settings.Default.ImageFilesLocation; var fox = new Image(ImageDataFactory.Create(imagePath + "\\" + "desert.jpg")); fox.Scale((float).25, (float).25); var dog = new Image(ImageDataFactory.Create(imagePath + "\\" + "koala.jpg")); dog.Scale((float).25, (float).25); var p = new Paragraph() .Add(fox) .Add(dog); document.Add(p); //Close document document.Close(); }
/// <summary> /// Devuelve un PDF que se guardará en la ruta indicada, a partir de la plantilla necesaria para el tipo de informe indicado. /// </summary> /// <param name="ruta">Ruta completa (nombre de archivo incluido) en la que se guardará el PDF.</param> /// <param name="tipo">Tipo de informe que se va a generar.</param> /// <returns>El PDF listo para editar sus elementos.</returns> public iText.Kernel.Pdf.PdfDocument GetPdfDesdePlantilla(string ruta, TiposInforme tipo = TiposInforme.Ninguno) { // Definimos el pdf que se va a devolver. iText.Kernel.Pdf.PdfDocument docPdf = null; // Si hay un tipo de informe creamos el pdf. if (tipo != TiposInforme.Ninguno) { // Definimos la plantilla string plantilla = Utils.CombinarCarpetas(App.RutaInicial, $"/Plantillas/{tipo}.pdf"); // Se crea el Reader con la plantilla necesaria. iText.Kernel.Pdf.PdfReader reader = new iText.Kernel.Pdf.PdfReader(plantilla); // Se crea el Writer con la ruta pasada. iText.Kernel.Pdf.PdfWriter writer = new iText.Kernel.Pdf.PdfWriter(ruta); // Creamos el documento, usando el reader y el writer anteriores. docPdf = new iText.Kernel.Pdf.PdfDocument(reader, writer); // Añadimos los datos de Metadata docPdf.GetDocumentInfo().SetAuthor("Orion - AnderSoft - A.Herrero"); docPdf.GetDocumentInfo().SetCreator("Orion 1.0"); } // Devolvemos el documento. return(docPdf); }
/// <summary> /// Devuelve un documento PDF nuevo en formato A5 en la ruta que se pasa como argumento. /// </summary> /// <param name="ruta">Ruta completa (nombre de archivo incluido) en la que se guardará el documento PDF.</param> /// <param name="apaisado">Si es true, el documento estará en apaisado. Por defecto es false.</param> /// <returns>El documento PDF listo para trabajar en él.</returns> public iText.Layout.Document GetNuevoPdfA5(string ruta, bool apaisado = false) { // Se crea el Writer con la ruta pasada. iText.Kernel.Pdf.PdfWriter writer = new iText.Kernel.Pdf.PdfWriter(ruta); // Se crea el PDF que se guardará usando el writer. iText.Kernel.Pdf.PdfDocument docPDF = new iText.Kernel.Pdf.PdfDocument(writer); // Añadimos los datos de Metadata docPDF.GetDocumentInfo().SetAuthor("Orion - AnderSoft - A.Herrero"); docPDF.GetDocumentInfo().SetCreator("Orion 1.0"); // Creamos el tamaño de página y le asignamos la rotaciñon si debe ser apaisado. iText.Kernel.Geom.PageSize tamañoPagina; if (apaisado) { tamañoPagina = iText.Kernel.Geom.PageSize.A5.Rotate(); } else { tamañoPagina = iText.Kernel.Geom.PageSize.A5; } // Se crea el documento con el que se trabajará. iText.Layout.Document documento = new iText.Layout.Document(docPDF, tamañoPagina); return(documento); }
public static string PrintCashBook(List <CashBook> cbList) { string fName = "cashBook_" + DateTime.Now.ToFileTimeUtc() + ".pdf"; string fileName = Path.Combine(ReportHeaderDetails.WWWroot, fName); using PdfWriter pdfWriter = new PdfWriter(fileName); using PdfDocument pdfDoc = new PdfDocument(pdfWriter); using Document doc = new Document(pdfDoc, PageSize.A4); Paragraph header = new Paragraph(ReportHeaderDetails.FirstLine + "\n") .SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER) .SetFontColor(ColorConstants.RED); header.Add(ReportHeaderDetails.SecondLine + "\n"); doc.Add(header); float[] columnWidths = { 1, 5, 15, 5, 5, 5 }; Table table = new Table(UnitValue.CreatePercentArray(columnWidths)).SetBorder(new OutsetBorder(2)); PdfFont f = PdfFontFactory.CreateFont(StandardFonts.HELVETICA); Cell cell = new Cell(1, 6) .Add(new Paragraph(ReportHeaderDetails.CashBook)) .SetFont(f) .SetFontSize(13) .SetFontColor(DeviceGray.WHITE) .SetBackgroundColor(DeviceGray.BLACK) .SetTextAlignment(TextAlignment.CENTER); table.AddHeaderCell(cell); Cell[] headerFooter = new Cell[] { new Cell().SetBackgroundColor(new DeviceGray(0.75f)).Add(new Paragraph("#")), new Cell().SetBackgroundColor(new DeviceGray(0.75f)).Add(new Paragraph("Date").SetTextAlignment(TextAlignment.CENTER)), new Cell().SetBackgroundColor(new DeviceGray(0.75f)).Add(new Paragraph("Particulars").SetTextAlignment(TextAlignment.CENTER)), new Cell().SetBackgroundColor(new DeviceGray(0.75f)).Add(new Paragraph("In").SetTextAlignment(TextAlignment.CENTER)), new Cell().SetBackgroundColor(new DeviceGray(0.75f)).Add(new Paragraph("Out").SetTextAlignment(TextAlignment.CENTER)), new Cell().SetBackgroundColor(new DeviceGray(0.75f)).Add(new Paragraph("Balance").SetTextAlignment(TextAlignment.CENTER)) }; Cell[] footer = new[] { new Cell(1, 4).Add(new Paragraph(ReportHeaderDetails.FirstLine + " / " + ReportHeaderDetails.SecondLine).SetFontColor(DeviceGray.GRAY)), new Cell(1, 2).Add(new Paragraph("D:" + DateTime.Now).SetFontColor(DeviceGray.GRAY)), }; foreach (Cell hfCell in headerFooter) { table.AddHeaderCell(hfCell); } foreach (Cell hfCell in footer) { table.AddFooterCell(hfCell); } int count = 0; foreach (var item in cbList) { table.AddCell(new Cell().SetTextAlignment(TextAlignment.CENTER).Add(new Paragraph((++count) + ""))); table.AddCell(new Cell().SetTextAlignment(TextAlignment.CENTER).Add(new Paragraph(item.EDate.ToShortDateString()))); table.AddCell(new Cell().SetTextAlignment(TextAlignment.CENTER).Add(new Paragraph(item.Particulars + ""))); table.AddCell(new Cell().SetTextAlignment(TextAlignment.CENTER).Add(new Paragraph(item.CashIn.ToString("0.##")))); table.AddCell(new Cell().SetTextAlignment(TextAlignment.CENTER).Add(new Paragraph(item.CashOut.ToString("0.##")))); table.AddCell(new Cell().SetTextAlignment(TextAlignment.CENTER).Add(new Paragraph(item.CashBalance.ToString("0.##")))); } doc.Add(table); doc.Close(); using PdfReader reader = new PdfReader(fileName); fName = "cashBook_" + (DateTime.Now.ToFileTimeUtc() + 1001) + ".pdf"; using PdfWriter writer = new PdfWriter(Path.Combine(ReportHeaderDetails.WWWroot, fName)); using PdfDocument pdfDoc2 = new PdfDocument(reader, writer); Document doc2 = new Document(pdfDoc2); int numberOfPages = pdfDoc2.GetNumberOfPages(); for (int i = 1; i <= numberOfPages; i++) { // Write aligned text to the specified by parameters point doc2.ShowTextAligned(new Paragraph("Page " + i + " of " + numberOfPages), 559, 806, i, TextAlignment.RIGHT, VerticalAlignment.BOTTOM, 0); } doc2.Close(); return(fName); }