我有一个ActionResult可以正常返回PDF。
public ActionResult TrainingHours(int id)
{
EmployeeModel model = new EmployeeModel(id);
if (!model.CheckEmployeeAccess())
throw new HttpException(403, "Access Denied");
byte[] report = model.GetTrainingHoursReport(id);
return File(report, Constants.PdfMimeType, model.EmployeeHoursReportFileName);
}
byte []是从LocalReport的Render(" PDF",DeviceInfoString)方法返回的,并且在下载时看起来很完美。
客户希望能够点击链接并为一组人运行相同的报告,并合并为一个pdf以供下载。
public ActionResult MassTrainingHours(int[] employeeIds)
{
if (employeeIds == null)
return new EmptyResult();
List<byte[]> reportsList = new List<byte[]>();
foreach (int employeeId in employeeIds)
{
EmployeeModel model = new EmployeeModel(employeeId);
reportsList.Add(model.GetTrainingHoursReport(employeeId));
}
Post.Domain.Services.PdfMerger merger = new Post.Domain.Services.PdfMerger();
byte[] report = merger.MergeFiles(reportsList);
return File(report, Constants.PdfMimeType, "TrainingHours.pdf");
这是我的班级,使用iTextSharp合并pdf。
public class PdfMerger
{
public byte[] MergeFiles(List<byte[]> inputFiles)
{
MemoryStream outputMS = new MemoryStream();
Document document = new Document();
PdfCopy writer = new PdfCopy(document, outputMS);
writer.CloseStream = false;
PdfImportedPage page = null;
document.Open();
foreach (byte[] fileData in inputFiles)
{
PdfReader reader = new PdfReader(fileData);
int n = reader.NumberOfPages;
for (int i = 1; i <= n; i++)
{
page = writer.GetImportedPage(reader, i);
writer.AddPage(page);
}
PRAcroForm form = reader.AcroForm;
if (form != null)
writer.CopyAcroForm(reader);
}
document.Close();
return outputMS.ToArray();
}
}
这一切似乎都运行没有错误,但我从ActionResult中得到的绝对没有。
如果我在winforms应用程序上使用略微修改为FileStream的MergeFiles方法,则该过程可以顺利运行,并且我得到了我正在寻找的输出文件。