Join several PDF files into one

Asked

Viewed 1,189 times

1

I am developing an application to join several pdfs in one, I managed to do through two uploads, however, I would like in one upload to be able to select several and unify the pdfs. Could someone help me? Below is the code already made:

<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";

    }

}
  • I don’t get it, you can do it with two, but you can’t do it with N?

  • exactly, I can join two pdfs files through 2 uploads. The idea would be in just 1 upload file, I can select several pdfs and unify them in one.

2 answers

0

What is missing from your code is to get the multiple files selected in FileUpload. The estate AllowMultiple="true" is well awarded but is not taking advantage of it.

The goal is to use only one FileUpload and select all PDF’s 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
}

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 is to leave only one field with multiple selection or N with 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";

    }
}
  • It worked!! I gave an adapted here and it was right! Thanks!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.