Merge multiple PDF files into one

1

I am developing an application to join several pdfs into one, I managed to do through two uploads, however, I would like in an upload to select multiple and unify the pdfs. can anybody help me? Here is the code already done:

<asp:FileUpload ID="file1" runat="server" AllowMultiple="true"></asp:FileUpload>
<asp:FileUpload ID="file2" runat="server" AllowMultiple="true"></asp:FileUpload>


  protected void Merge(object sender, EventArgs e)
   {
    if (file1.HasFile && file2.HasFile)
    {
        PdfReader pdfReader1 = new PdfReader(file1.PostedFile.InputStream);
        PdfReader pdfReader2 = new PdfReader(file2.PostedFile.InputStream);

        List<PdfReader> readerList = new List<PdfReader>();
        readerList.Add(pdfReader1);
        readerList.Add(pdfReader2);


        //Define a new output document and its size, type
        Document document = new Document(PageSize.A4, 0, 0, 0, 0);
        //Get instance response output stream to write output file.
        PdfWriter writer = PdfWriter.GetInstance(document, Response.OutputStream);
        document.Open();

        foreach (PdfReader reader in readerList)
        {
            for (int i = 1; i <= reader.NumberOfPages; i++)
            {
                PdfImportedPage page = writer.GetImportedPage(reader, i);
                document.Add(iTextSharp.text.Image.GetInstance(page));
            }
        }
        document.Close();

        Response.AppendHeader("content-disposition", "inline; filename=OutPut.pdf");
        Response.ContentType = "application/pdf";

    }

}
    
asked by anonymous 02.10.2018 / 16:02

2 answers

0

Iterate between the posted files and add them to your list, although you allow each input to receive multiple files, I think what would make more sense would be to leave only a field with multiple selection or N with the simple selection. But in the example below, it will work in both cases.

protected void Merge(object sender, EventArgs e)
{
    var postFiles = Request.Files as HttpFileCollection;

    List<PdfReader> readerList = new List<PdfReader>();

    for (int key = 0; key < postFiles.Count; key++)
    {
        var arquivo = postFiles[key] as HttpPostedFile;
        var pdf = new PdfReader(arquivo.InputStream);
        readerList.Add(pdf);
    }

    //Define a new output document and its size, type
    Document document = new Document(PageSize.A4, 0, 0, 0, 0);
    //Get instance response output stream to write output file.
    PdfWriter writer = PdfWriter.GetInstance(document, Response.OutputStream);
    document.Open();

    foreach (PdfReader reader in readerList)
    {
        for (int i = 1; i <= reader.NumberOfPages; i++)
        {
            PdfImportedPage page = writer.GetImportedPage(reader, i);
            document.Add(iTextSharp.text.Image.GetInstance(page));
        }
    }
    document.Close();

    Response.AppendHeader("content-disposition", "inline; filename=OutPut.pdf");
    Response.ContentType = "application/pdf";

    }
}
    
02.10.2018 / 17:04
0

What's missing in your code is getting the multiple files selected in FileUpload . AllowMultiple="true" property is well-assigned but is not taking advantage of it.

The goal is to use only FileUpload and select all PDFs there, then treat it in code:

HTML

<asp:FileUpload ID="files" runat="server" AllowMultiple="true"></asp:FileUpload>

C #

protected void Merge(object sender, EventArgs e)
{
    HttpFileCollection postedFiles = Request.Files;
    List<PdfReader> readerList = new List<PdfReader>();

    foreach(HttpPostedFile postedFile in postedFiles)
        readerList.Add(new PdfReader(postedFile.InputStream));

    // ... resto do código
}
    
02.10.2018 / 17:03