Boy Pasmo
Boy Pasmo

Reputation: 8511

How to merge a physical file (pdf) and a generated one (pdf) and output them to the browser?

Been struggling with this.

I have a physical file (pdf) and a generated one that is generated by iTextSharp (pdf) my goal is to merge both of them and output it to the browser.

By the way, I am using ASP.NET MVC 4

So, in my controller, I have something like this:

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();
}

And is it possible to set the generated pdf as the first (or how many pages it will generate) page then append the physical file?

Any help would be much appreciated. Thanks

Upvotes: 1

Views: 1980

Answers (2)

Roman
Roman

Reputation: 19

I can provide a sample for c#

First you need to instal a third party library: pdfium.net sdk for example

You can do it through nuget Install-package 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();
}

Upvotes: 0

Ralt
Ralt

Reputation: 2206

I've done something similar (merging physical and code generated PDF's) using PDFSharp if it's of any help.

PdfDocument document = new PdfDocument();

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

document.AddPage(coverPage);

And then adding you own generated pages can be done as:

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)

Upvotes: 1

Related Questions