2015-10-13 29 views
1

一直在努力。如何合并物理文件(pdf)和生成的文件(pdf)并将其输出到浏览器?

我有一个物理文件(pdf)和一个由iTextSharp(pdf)生成的生成文件,我的目标是合并它们并将其输出到浏览器。

顺便说一句,我使用ASP.NET MVC 4

所以,在我的控制,我有这样的事情:

public ActionResult Index() 
{ 
    MemoryStream memoryStream = new MemoryStream(); 
    var path = Server.MapPath("~/Doc/../myfile.pdf"); // This is my physical file 
    var fileStream = new FileStream(path, FileMode.Open, FileAccess.Read); 

    GenerateFile(); // This is my generated file thru iTextSharp 

    Response.AddHeader("Content-Disposition", "inline"); 
    memoryStream.Position = 0; 

    return new FileStreamResult(// Maybe merged file goes here? not sure. 
          ,"application/pdf"); 
} 

private void GenerateFile() 
{ 
    MemoryStream stream = new MemoryStream(); 
    var document = new Document(/*some settings here*/); 
    PdfWriter.GetInstance(document, stream).CloseStream = false; 

    document.Open(); 
    // generate pdf here 
    document.Close(); 
} 

,并可以设置生成的PDF作为首先(或它将生成多少页)页面然后附加物理文件?

任何帮助将不胜感激。谢谢

+1

请访问此链接http://stackoverflow.com/questions/6029142/merging-multiple-pdfs-using-itextsharp-in-c-net –

+0

'VAR generatedFile = GenerateFile();''但GenerateFile '返回'无效' –

+0

@codroipo哦,是的,这是一个错误。我会编辑它。 –

回答

0

我使用PDFSharp做了类似的事情(合并物理和代码生成的PDF),如果它有任何帮助。

PdfDocument document = new PdfDocument(); 

PdfDocument physicalDoc = PdfSharp.Pdf.IO.PdfReader.Open(filepath); 
PdfPage coverPage = physicalDoc.Pages[0]; 

document.AddPage(coverPage); 

,然后添加你自己生成的页面是可以做到的:例如pdfium.net sdk

PdfPage generatedPage = new PdfPage(); 
XGraphics g = XGraphics.FromPdfPage(generatedPage); 

g.DrawRectangle(color, x, y, width, height); 
g.DrawString("This release document describes the contents of..." 
      ,font, textColor, x, y); 

document.AddPage(generatedPage) 
+0

你怎么知道什么页面会先来? –

+0

@BoyPasmo“Pages”数组的排列方式与物理pdf相同。因此,PDF中的第一页将成为数组中的第一页 – Ralt

+0

生成的PDF如何?我将如何添加它们?对不起,这种问题 –

0

我可以提供C#

首先,你需要安装装配一个第三方库的样本

您可以通过nuget完成 安装包pdfium.net.sdk

public void MergeDocument() 
{ 
    //Initialize the SDK library 
    //You have to call this function before you can call any PDF processing functions. 
    PdfCommon.Initialize(); 

    //Open and load a PDF document in which will be merged other files 
    using (var mainDoc = PdfDocument.Load(@"c:\test001.pdf")) 
    { 
     //Open one PDF document. 
     using (var doc = PdfDocument.Load(@"c:\doc1.pdf")) 
     { 
      //Import all pages from document 
      mainDoc.Pages.ImportPages(
       doc, 
       string.Format("1-{0}", doc.Pages.Count), 
       mainDoc.Pages.Count 
       ); 
     } 

     //Open another PDF document. 
     using (var doc = PdfDocument.Load(@"c:\doc2.pdf")) 
     { 
      //Import all pages from document 
      mainDoc.Pages.ImportPages(
       doc, 
       string.Format("1-{0}", doc.Pages.Count), 
       mainDoc.Pages.Count 
       ); 
     } 

     mainDoc.Save(@"c:\ResultDocument.pdf", SaveFlags.NoIncremental); 


    } 
    //Release all resources allocated by the SDK library 
    PdfCommon.Release(); 
}