是否可以合并pdf文件而不将其保存到磁盘?
我有一个生成的pdf(通过itextsharp)和一个物理pdf文件。这两个应该显示为浏览器合并。
我目前拥有的是(伪代码)
public ActionResult Index()
{
// Generate dyanamic pdf first
var pdf = GeneratePdf();
// Then save it to disk for retrieval later
SaveToDisc(pdf);
// Retrieve the static pdf
var staticPdf = GetStaticPdf();
// Retrieve the generated pdf that was made earlier
var generatedPdf = GetGeneratedPdf("someGeneratedFile.pdf");
// This creates the merged pdf
MergePdf(new List<string> { generatedPdf, staticPdf }, "mergedPdf.pdf");
// Now retrieve the merged pdf and show it to the browser
var mergedPdf = GetMergedPdf("mergedPdf.pdf");
return new FileStreamResult(mergedFile, "application/pdf");
}
这样可行,但我只是想知道是否可以合并pdf并将其显示到浏览器而不在光盘上保存任何内容?
非常感谢任何帮助。感谢
答案 0 :(得分:3)
您可以尝试使用PdfWriter
类和MemoryStream
,如下所示:
using (MemoryStream ms = new MemoryStream())
{
Document doc = new Document(PageSize.A4, 60, 60, 10, 10);
PdfWriter pw = PdfWriter.GetInstance(doc, ms);
//your code to write something to the pdf
return ms.ToArray();
}
您还可以参考:Creating a PDF from a RDLC Report in the Background
此外:如果data
是内存中的PDF,则需要data.CopyTo(base.Response.OutputStream);
答案 1 :(得分:0)
试试这个
string path = Server.MapPath("Yourpdf.pdf");
WebClient client = new WebClient();
Byte[] buffer = client.DownloadData(path);
if (buffer != null)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-length", buffer.Length.ToString());
Response.BinaryWrite(buffer);
}