Good day. I am trying to join two pdf documents that I am generating, since each document must have a different footer. For example I want to create a copy of the other with a different footer on its pages. I have the following code:
public ActionResult genera_report_factura(string empresa, string registros)
{
string Path = Server.MapPath("~/Upload/");
string error = "";
try
{
byte[] pdf = null;
using (MemoryStream stream = new System.IO.MemoryStream())
{
int h_inferior = 200;
using (var pdfDoc = new Document(PageSize.LETTER, 25, 25, 235, h_inferior))
{
var pdfWriter = PdfWriter.GetInstance(pdfDoc, stream);
pdfWriter.PageEvent = new HeaderFooter();
pdfDoc.Open();
PdfPTable table = new PdfPTable(3);
table.DefaultCell.Border = Rectangle.NO_BORDER;
table.WidthPercentage = 100;
table.HorizontalAlignment = 1;
float[] widths = new float[] { 20, 50, 30 };
table.SetWidths(widths);
Paragraph celda = new Paragraph();
PdfPCell cell = new PdfPCell();
celda = new Paragraph();
celda.Add(new Chunk("PRIMER DOCUMENTO", FontFactory.GetFont("Arial", 9, Font.BOLD, BaseColor.BLACK)));
celda.Alignment = Element.ALIGN_LEFT;
cell = new PdfPCell();
cell.Colspan = 3;
cell.Border = 0;
cell.AddElement(celda);
table.AddCell(cell);
pdfDoc.Add(table);
pdfDoc.Close();
}
using (var pdfDoc2 = new Document(PageSize.LETTER, 25, 25, solo_fatura, h_inferior + h_cedible))
{
var pdfWriter2 = PdfWriter.GetInstance(pdfDoc2, stream);
pdfWriter.PageEvent = new EVENTOHEADERDOS();
pdfDoc2.Open();//Referencia a objeto no establecida como instancia de un objeto
PdfPTable table2 = new PdfPTable(3);
table2.DefaultCell.Border = Rectangle.NO_BORDER;
table2.WidthPercentage = 100;
table2.HorizontalAlignment = 1;
float[] widths2 = new float[] { 20, 50, 30 };
table2.SetWidths(widths2);
Paragraph celda2 = new Paragraph();
PdfPCell cell2 = new PdfPCell();
celda2 = new Paragraph();
celda2.Add(new Chunk("SEGUNDO documento", FontFactory.GetFont("Arial", 9, Font.BOLD, BaseColor.BLACK)));
celda2.Alignment = Element.ALIGN_LEFT;
cell2 = new PdfPCell();
cell2.Colspan = 3;
cell2.Border = 0;
cell2.AddElement(celda2);
table2.AddCell(cell2);
pdfDoc2.Add(table2);
pdfDoc2.Close();
}
pdf = stream.ToArray();
}
return File(pdf, "application/pdf");
}
catch (Exception ex)
{
error = ex.Message;
System.IO.File.WriteAllText(Path + "wow_error.txt": " + error);
return null;
}
}
But I get the following error:
Object reference not set as an instance of an object and right on the line where I have defined :pdfDoc2.Open();
I tried this other way, I don't get an error, but the pdfDoc2 doesn't write or generate anything:
public ActionResult genera_report_factura(string empresa, string registros)
{
string Path = Server.MapPath("~/Upload/");
string error = "";
try
{
byte[] pdf = null;
using (MemoryStream stream = new System.IO.MemoryStream())
{
int h_inferior = 200;
using (var pdfDoc = new Document(PageSize.LETTER, 25, 25, 235, h_inferior))
{
var pdfWriter = PdfWriter.GetInstance(pdfDoc, stream);
pdfWriter.PageEvent = new HeaderFooter();
pdfDoc.Open();
PdfPTable table = new PdfPTable(3);
table.DefaultCell.Border = Rectangle.NO_BORDER;
table.WidthPercentage = 100;
table.HorizontalAlignment = 1;
float[] widths = new float[] { 20, 50, 30 };
table.SetWidths(widths);
Paragraph celda = new Paragraph();
PdfPCell cell = new PdfPCell();
celda = new Paragraph();
celda.Add(new Chunk("PRIMER DOCUMENTO", FontFactory.GetFont("Arial", 9, Font.BOLD, BaseColor.BLACK)));
celda.Alignment = Element.ALIGN_LEFT;
cell = new PdfPCell();
cell.Colspan = 3;
cell.Border = 0;
cell.AddElement(celda);
table.AddCell(cell);
pdfDoc.Add(table);
var pdfDoc2 = new Document(PageSize.LETTER, 25, 25, solo_fatura, h_inferior);
var pdfWriter2 = PdfWriter.GetInstance(pdfDoc2, stream);
pdfWriter.PageEvent = new EVENTOHEADERDOS();
pdfDoc2.Open();
PdfPTable table2 = new PdfPTable(3);
table2.DefaultCell.Border = Rectangle.NO_BORDER;
table2.WidthPercentage = 100;
//table.SpacingBefore = 605f;
//0=Left, 1=Centre, 2=Right
table2.HorizontalAlignment = 1;
float[] widths2 = new float[] { 20, 50, 30 };
table2.SetWidths(widths2);
Paragraph celda2 = new Paragraph();
PdfPCell cell2 = new PdfPCell();
celda2 = new Paragraph();
celda2.Add(new Chunk("SEGUNDO documento", FontFactory.GetFont("Arial", 9, Font.BOLD, BaseColor.BLACK)));
celda2.Alignment = Element.ALIGN_LEFT;
cell2 = new PdfPCell();
cell2.Colspan = 3;
cell2.Border = 0;
cell2.AddElement(celda2);
table2.AddCell(cell2);
pdfDoc2.Add(table2);
pdfDoc.Close();
//pdfDoc2.Close();//si lo coloco se cae, porque me dice que no se puede cerrar un bloque que ya está cerrado
}
pdf = stream.ToArray();
}
return File(pdf, "application/pdf");
}
catch (Exception ex)
{
error = ex.Message;
System.IO.File.WriteAllText(Path + "wow_error.txt", linea + ": " + error);
return null;
}
}
If anyone has any ideas, I would greatly appreciate it or an example. Best regards.
I already solved it, for those who need a solution, here is a contribution:
The idea is to create the pdf with itexsharp separately, return a byte[] and in the function that calls them, store it in a list to join them.
Greetings, I hope the example will serve you