2015-10-13 93 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(); 
}