再会。我正在尝试加入我正在生成的两个 pdf 文档,因为每个文档必须有不同的页脚。例如,我想在其页面上创建另一个具有不同页脚的副本。我有以下代码:
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;
}
}
但我收到以下错误:
对象引用未设置为对象的实例, 并且就在我定义的行上:pdfDoc2.Open();
我尝试了这种其他方式,我没有收到错误,但 pdfDoc2 没有写入或生成任何内容:
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;
}
}
如果有人有任何想法,我将不胜感激或举个例子。此致。
我已经解决了,对于那些需要解决方案的人,这里是一个贡献:
这个想法是分别用itexsharp创建pdf,返回一个字节[]并在调用它们的函数中,将它存储在一个列表中以加入它们。
问候,我希望这个例子能为你服务