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 8800859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:42:01+00:00 2026-06-14T00:42:01+00:00

I looked at a few examples online and came up with this code to

  • 0

I looked at a few examples online and came up with this code to merge pdfs using iTextSharp. But I am getting an error :.

{“The document has no pages.”}

It fails at Page = writer.GetImportedPage(reader, X);

Here is stack trace :

 at iTextSharp.text.pdf.PdfPages.WritePageTree()
 at iTextSharp.text.pdf.PdfWriter.Close()
 at iTextSharp.text.pdf.PdfCopy.Close()
 at iTextSharp.text.Document.Close()

It has 3 pages when I debugged it. What is wrong here??

Here is my code

public static MemoryStream MergePdfs(List<MemoryStream> pdfStreams)
       {
        //Create output stream
           MemoryStream OutStream = new MemoryStream();
           Document Document = null;

           try
           {
                //Create Main reader
                PdfReader Reader = new PdfReader(pdfStreams.ElementAt(0));
                //Create Main Doc
                Document = new Document(Reader.GetPageSizeWithRotation(1));
                //Create main writer
                PdfCopy Writer = new PdfCopy(Document, OutStream);
                //Open document for writing
                Document.Open();
                //Add pages
                AddPages(Reader.NumberOfPages, Reader, ref Writer);

                //For each additional pdf after first combine them into main document
                foreach (MemoryStream PdfStream in pdfStreams.Skip(1))
                {
                    PdfReader Reader2 = new PdfReader(PdfStream);
                    // Add content
                    AddPages(Reader2.NumberOfPages, Reader2, ref Writer);
                }
             }
             finally
             {
                 // Step 5: Close the document
                 if (Document != null)
                     Document.Close();

                foreach (var Strm in pdfStreams)
                {
                    try { if (null != Strm) Strm.Dispose(); } catch { }
                }
             }

             return OutStream;
         }

        private static void AddPages(int Pages, PdfReader reader, ref PdfCopy writer)
        {
            PdfImportedPage Page = null;

            for (int X = 0; X < Pages; X++)
            {
                 Page = writer.GetImportedPage(reader, X);
                 writer.AddPage(Page);
            }

            if (reader.AcroForm != null)
                writer.CopyAcroForm(reader);
        }
  • 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-14T00:42:02+00:00Added an answer on June 14, 2026 at 12:42 am

    Page numbers for GetImportedPage are 1-based. Thus:

    for (int X = 1; X < Pages + 1; X++)
    {
        Page = writer.GetImportedPage(reader, X);
        writer.AddPage(Page);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have looked at a few examples on using arrays in a query, but
I've looked at a few examples, but they were all written when the project
I have looked on here and found a few examples but nothing works so
I looked at a few examples here on OS and also followed this one
I've read and looked a quite a few examples for Threadpooling but I just
I've looked at a few posts here like this but this doesn't seem to
I have looked at few examples here Calling a Method from an Expression and
I looked at a few examples and as far as I can tell I'm
So i've looked around at a few things involving writting an HTTP Proxy using
This is a beginner question, but it's been frustrating me... I am using C#,

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.