我最终创建了一个错误的 PDF 内存流。
MemoryStream ms = new MemoryStream();
try
{
PdfReader reader = new PdfReader(readerURL);
StampWatermark(WO, ms, reader);
}
catch (Exception ex)
{
RenderErrorPDF(WO, ms, ex);
}
byte[] byteinfo = ms.ToArray();
ms.Write(byteinfo, 0, byteinfo.Length);
ms.Position = 0;
ms.Seek(0, SeekOrigin.Begin);
return new FileStreamResult(ms, "application/pdf");
RenderErrorPDF 方法
private static void RenderErrorPDF(WorkOrder WO, MemoryStream ms, Exception ex)
{
BaseFont bfTimes = BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1250, false);
var doc = new Document(new Rectangle(792f, 540f));
var wr = PdfWriter.GetInstance(doc, ms);
doc.Open();
doc.Add(new Paragraph("There was an error rendering the file that you requested...", new Font(bfTimes, 24f)));
doc.Add(new Paragraph(string.Format("\r\rFile: {0}", WO.DRAWING_FILE)));
doc.Add(new Paragraph(string.Format("\r\rError: {0}", ex.Message)));
wr.CloseStream = false;
doc.Close();
}