public IActionResult BarCode() { Document doc = new Document(PageSize.A4); doc.SetMargins(85f, 28.34f, 85f, 28.34f); // Igual a un cm MemoryStream ms = new MemoryStream(); PdfWriter writer = PdfWriter.GetInstance(doc, ms); doc.AddAuthor("Carlos D Agostino"); doc.AddTitle("Pdf BarCode"); doc.Open(); BarcodeInter25 code25 = new BarcodeInter25(); Rectangle r = new iTextSharp.text.Rectangle(38, 152); code25.ChecksumText = false; code25.Code = "1462029681051408202019603171969320"; code25.BarHeight = 40; PdfContentByte cb = writer.DirectContent; iTextSharp.text.Image img = code25.CreateImageWithBarcode(cb, null, null); doc.Add(img); doc.Add(new Paragraph()); writer.Close(); doc.Close(); ms.Seek(0, SeekOrigin.Begin); //file.Dispose(); //return File(new FileStream("hola mundo.pdf",FileMode.Open,FileAccess.Read),"application/pdf"); return(File(ms.ToArray(), "application/pdf")); }
private void CreatePDF(List <ObjetoFactura> lista) { // Create a Document object Document document = new Document(PageSize.A4, 70, 70, 70, 70); //MemoryStream PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(@"C:\Users\johan\Documents\Bajo Rojo\Factura.pdf", FileMode.Create)); // First, create our fonts var titleFont = FontFactory.GetFont("Arial", "14", Font.Bold); var boldTableFont = FontFactory.GetFont("Arial", "10", Font.Bold); var bodyFont = FontFactory.GetFont("Arial", "10", Font.Bold); iTextSharp.text.Rectangle pageSize = writer.PageSize; // Open the Document for writing document.Open(); //Add elements to the document here //Add elements to the document here #region Top table // Create the header table PdfPTable headertable = new PdfPTable(3); headertable.HorizontalAlignment = 0; headertable.WidthPercentage = 100; headertable.SetWidths(new float[] { 4, 2, 4 }); // then set the column's __relative__ widths headertable.DefaultCell.Border = Rectangle.NO_BORDER; //headertable.DefaultCell.Border = Rectangle.BOX; //for testing headertable.SpacingAfter = 30; PdfPTable nested = new PdfPTable(1); nested.DefaultCell.Border = Rectangle.BOX; PdfPCell nextPostCell1 = new PdfPCell(new Phrase("ABC Co.,Ltd", bodyFont)); nextPostCell1.Border = Rectangle.LEFT_BORDER | Rectangle.RIGHT_BORDER; nested.AddCell(nextPostCell1); PdfPCell nextPostCell2 = new PdfPCell(new Phrase("111/206 Moo 9, Ramkhamheang Road,", bodyFont)); nextPostCell2.Border = Rectangle.LEFT_BORDER | Rectangle.RIGHT_BORDER; nested.AddCell(nextPostCell2); PdfPCell nextPostCell3 = new PdfPCell(new Phrase("Nonthaburi 11120", bodyFont)); nextPostCell3.Border = Rectangle.LEFT_BORDER | Rectangle.RIGHT_BORDER; nested.AddCell(nextPostCell3); PdfPCell nesthousing = new PdfPCell(nested); nesthousing.Rowspan = 4; nesthousing.Padding = 0f; headertable.AddCell(nesthousing); headertable.AddCell(""); PdfPCell invoiceCell = new PdfPCell(new Phrase("Factura de ventas.", titleFont)); invoiceCell.HorizontalAlignment = 2; invoiceCell.Border = Rectangle.NO_BORDER; headertable.AddCell(invoiceCell); PdfPCell noCell = new PdfPCell(new Phrase("No :", bodyFont)); noCell.HorizontalAlignment = 2; noCell.Border = Rectangle.NO_BORDER; headertable.AddCell(noCell); var xxh = new BaseColor(255, 30, 30); var Calibri8 = FontFactory.GetFont("Calibri", 12, xxh); headertable.AddCell(new Phrase(ObtenerConsecutivo(""), Calibri8)); PdfPCell dateCell = new PdfPCell(new Phrase("Fecha :", bodyFont)); dateCell.HorizontalAlignment = 2; dateCell.Border = Rectangle.NO_BORDER; headertable.AddCell(dateCell); headertable.AddCell(new Phrase("2 de febrero 2018", bodyFont)); PdfPCell billCell = new PdfPCell(new Phrase("Direccion :", bodyFont)); billCell.HorizontalAlignment = 2; billCell.Border = Rectangle.NO_BORDER; headertable.AddCell(billCell); headertable.AddCell(new Phrase("Johan" + "\n" + "La Cruz", bodyFont)); document.Add(headertable); #endregion #region Items Table //Create body table PdfPTable itemTable = new PdfPTable(4); itemTable.HorizontalAlignment = 0; itemTable.WidthPercentage = 100; itemTable.SetWidths(new float[] { 40, 15, 20, 30 }); // then set the column's __relative__ widths itemTable.SpacingAfter = 40; itemTable.DefaultCell.Border = Rectangle.BOX; PdfPCell cell1 = new PdfPCell(new Phrase("Tipo Pescado", boldTableFont)); cell1.BackgroundColor = new iTextSharp.text.BaseColor(145, 205, 255); cell1.HorizontalAlignment = 1; itemTable.AddCell(cell1); PdfPCell cell2 = new PdfPCell(new Phrase("Cantidad Kg", boldTableFont)); cell2.BackgroundColor = new iTextSharp.text.BaseColor(145, 205, 255); cell2.HorizontalAlignment = 1; itemTable.AddCell(cell2); PdfPCell cell3 = new PdfPCell(new Phrase("Precio/Kg", boldTableFont)); cell3.BackgroundColor = new iTextSharp.text.BaseColor(145, 205, 255); cell3.HorizontalAlignment = 1; itemTable.AddCell(cell3); PdfPCell cell4 = new PdfPCell(new Phrase("Monto", boldTableFont)); cell4.BackgroundColor = new iTextSharp.text.BaseColor(145, 205, 255); cell4.HorizontalAlignment = 1; itemTable.AddCell(cell4); foreach (var x in lista) { PdfPCell numberCell = new PdfPCell(new Phrase(x.pez, bodyFont)); numberCell.HorizontalAlignment = 0; numberCell.PaddingLeft = 10f; numberCell.Border = Rectangle.LEFT_BORDER | Rectangle.RIGHT_BORDER; itemTable.AddCell(numberCell); PdfPCell descCell = new PdfPCell(new Phrase(x.pesa, bodyFont)); descCell.HorizontalAlignment = 0; descCell.PaddingLeft = 10f; descCell.Border = Rectangle.LEFT_BORDER | Rectangle.RIGHT_BORDER; itemTable.AddCell(descCell); PdfPCell qtyCell = new PdfPCell(new Phrase(x.precio, bodyFont)); qtyCell.HorizontalAlignment = 0; qtyCell.PaddingLeft = 10f; qtyCell.Border = Rectangle.LEFT_BORDER | Rectangle.RIGHT_BORDER; itemTable.AddCell(qtyCell); PdfPCell amtCell = new PdfPCell(new Phrase(x.totalLinea, bodyFont)); amtCell.HorizontalAlignment = 1; amtCell.Border = Rectangle.LEFT_BORDER | Rectangle.RIGHT_BORDER; itemTable.AddCell(amtCell); } // Table footer PdfPCell totalAmtCell1 = new PdfPCell(new Phrase("")); totalAmtCell1.Border = Rectangle.LEFT_BORDER | Rectangle.TOP_BORDER; itemTable.AddCell(totalAmtCell1); PdfPCell totalAmtCell2 = new PdfPCell(new Phrase("")); totalAmtCell2.Border = Rectangle.TOP_BORDER; //Rectangle.NO_BORDER; //Rectangle.TOP_BORDER; itemTable.AddCell(totalAmtCell2); PdfPCell totalAmtStrCell = new PdfPCell(new Phrase("Monto Total ", boldTableFont)); totalAmtStrCell.Border = Rectangle.TOP_BORDER; //Rectangle.NO_BORDER; //Rectangle.TOP_BORDER; totalAmtStrCell.HorizontalAlignment = 1; itemTable.AddCell(totalAmtStrCell); PdfPCell totalAmtCell = new PdfPCell(new Phrase(MontoTotalDinero.Text, boldTableFont)); totalAmtCell.HorizontalAlignment = 1; itemTable.AddCell(totalAmtCell); PdfPCell cell = new PdfPCell(new Phrase("***Factura emitida por software Bajo Rojo del Pacifico S.A***", bodyFont)); cell.Colspan = 4; cell.HorizontalAlignment = 1; itemTable.AddCell(cell); document.Add(itemTable); #endregion //Approved by PdfContentByte cb = new PdfContentByte(writer); BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1250, true); cb = writer.DirectContent; cb.BeginText(); cb.SetFontAndSize(bf, 10); cb.SetTextMatrix(pageSize.GetLeft(300), 200); cb.ShowText("Aprobado por: "); cb.EndText(); cb = new PdfContentByte(writer); bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1250, true); cb = writer.DirectContent; cb.BeginText(); cb.SetFontAndSize(bf, 10); cb.SetTextMatrix(pageSize.GetLeft(70), 100); cb.ShowText("Factura impresa por Bajo Rojo del Pacifico Sociedad Anonima."); cb.EndText(); writer.CloseStream = false; //set the closestream property // Close the Document without closing the underlying stream document.Close(); }