Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8884815
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:09:17+00:00 2026-06-14T21:09:17+00:00

Hi My applion is MVC3 c#, I am using itextsharp to produce PDF files

  • 0

Hi My applion is MVC3 c#, I am using itextsharp to produce PDF files for pre disgned forms. In this application I have to different forms. To generate a form I use:

public ActionResult TestPDF(long learnerID = 211, long courseID = 11)
{
    var caseList = _studyCaseSvc.ListStudyCases().Where(x => x.Course_ID == courseID);
    try
    {
        MemoryStream memoryStream = new MemoryStream();
        PdfConcatenate whole = new PdfConcatenate(memoryStream);
        foreach (var ca in caseList)
        {
            byte[] part = null;
            if (ca.CaseType == "CTA")
            {
                part = GenerateEvaluationCAT_PDF(learnerID, ca.ID);
            }
            else if (ca.CaseType == "CTAH")
            {
                part = GenerateEvaluationCATH_PDF(learnerID, ca.ID);
            }
            else
            {
                part = null;
            }
            if (part != null)
            {
                PdfReader partReader = new PdfReader(part);
                whole.AddPages(partReader);
                partReader.Close();
            }
        }

        whole.Close();
        byte[] byteInfo = memoryStream.ToArray();
        SendPdfToBrowser(byteInfo);
    }
    catch (Exception ex)
    {
    }
    return null;
}

I get this error: An item with the same key has already been added. The error happens at AddPages. So I developed this simpler test:

private void merge()
    {
        try
        {
            FileStream output = new FileStream("p3.pdf", FileMode.Create);
            PdfConcatenate pdfConcatenate = new PdfConcatenate(output, true);

            PdfReader r1 = new PdfReader("p2.pdf");

            MemoryStream memoryStream = new MemoryStream();
            PdfStamper pdfStamper = new PdfStamper(r1, memoryStream);

            pdfStamper.FormFlattening = true;
            pdfStamper.Close();
            PdfReader r2 = new PdfReader(memoryStream.ToArray());

            //pdfConcatenate.AddPages(tempReader);

            pdfConcatenate.Open();
            int n = r1.NumberOfPages;
            for (int i = 1; i <= n; i++)
            {
                PdfImportedPage imp = pdfConcatenate.Writer.GetImportedPage(r1, i);
                pdfConcatenate.Writer.AddPage(imp);
            }
            pdfConcatenate.Writer.FreeReader(r1);

            pdfStamper.Close();
            r1.Close();
            pdfConcatenate.Close();
        }
        catch (Exception ex)
        {
        }
    }

Same error.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T21:09:18+00:00Added an answer on June 14, 2026 at 9:09 pm

    Well, the problem is the misconception that you can combine multiple PDF files into one by simply concatenating them. That is wrong for PDFs (just like it is wrong for most binary file formats).

    Thus, you should update your GenerateAllEvaluation_PDF method to have a PdfConcatenate instance instead of your byte Array whole, cf. http://api.itextpdf.com/itext/com/itextpdf/text/pdf/PdfConcatenate.html, open each byte array returned by your GenerateEvaluationCATH_PDF method in a PdfReader, add all pages of these readers to the PdfConcatenate and eventually return the bytes generated by that class.

    EDIT (I’m more into Java than C#, thus forgive minor errors)

    PdfConcatenate whole = new PdfConcatenate(...);
    foreach (var ca in caseList)
    {
        byte[] part = null;
        if (ca.CaseType == "CTA")
        {
            part = GenerateEvaluationCAT_PDF(learnerID, ca.ID);
        }
        else if (ca.CaseType == "CTAH")
        {
            part = GenerateEvaluationCATH_PDF(learnerID, ca.ID);
        }
        else
        {
            part = ???;
        }
        PdfReader partReader = new PdfReader(part);
        whole.AddPages(partReader);
        partReader.Close();
    }
    

    The PdfConcatenate can be constructed with a MemoryStream from which you can retrieve the final byte[].

    PS: PdfConcatenate may not be a part of iTextSharp version 4.x yet but it is merely a convenience wrapper of PdfCopy and PdfSmartCopy. Thus, you may simply have a look at the sources of iTextSharp (OSS after all) and be inspired: PdfConcatenate.cs.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is probably the most annoying error I have ever encountered. When I go
It seems like Jquery dialog is in love with my mouse pointer. I have
On lion after each crash when I start the app Lion asks if I
I think the issue is explained best with an example. public class MyService {
Here is my issue, if I want the OS to handle the screen rotation,
I'm working on refining a database that I wrote for my app while ago.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.