1

I am trying to download and merges multiple pdf files by using ITextSharp.

It used to working before but I being got an "Content can not be added to a PdfImportedPage." error message on the line:

importedPage = writer.GetImportedPage(reader, currentPageIndex);

The full code is below, any help will be very appreciated.

private string MergeDocuments(IList<string> fileUrls, string fileName)
{
  var reportFolder = this.ReportFolder + "\\";

  using (MemoryStream output = new MemoryStream())
  {
      Document document = new Document();

      try
      {
          // Initialize pdf writer
          PdfWriter writer = PdfWriter.GetInstance(document, output);

          // Open document to write
          document.Open();
          PdfContentByte content = writer.DirectContent;
          PdfImportedPage importedPage;

          // Iterate through all pdf documents
          foreach (var url in fileUrls)
          {
              // Create pdf reader
              using (PdfReader reader = new PdfReader(new Uri(url)))
              {
                  int numberOfPages = reader.NumberOfPages;

                  // Iterate through all pages
                  for (int currentPageIndex = 1; currentPageIndex <= numberOfPages; currentPageIndex++)
                  {
                      // Determine page size for the current page
                      document.SetPageSize( reader.GetPageSizeWithRotation(currentPageIndex) );

                      // Create page
                      document.NewPage();
                      importedPage = writer.GetImportedPage(reader, currentPageIndex);
                      content.AddTemplate(importedPage, 1f, 0, 0, 1f, 0, 0);

                  }
              }
          }
      }
      catch (Exception exception)
      {
          throw new Exception("Error occured", exception);
      }

      File.WriteAllBytes(reportFolder + fileName + ".pdf", output.GetBuffer());

  }

  return "Reports/" + fileName + ".pdf";
}

When I try the following code, I get a null pointer exception in the addDocument() method:

using (MemoryStream output = new MemoryStream()) {
    Document document = new Document();
    document.Open();
    PdfCopy copy = new PdfSmartCopy(document, output);
    foreach (var url in fileUrls) {
        using (WebClient client = new WebClient()) {
            var byteArray = client.DownloadData(url);
            PdfReader reader = new PdfReader(byteArray);
            copy.AddDocument(reader);
            reader.Close();
        }
    }
}
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
londondev
  • 231
  • 2
  • 13
  • 2
    Read http://stackoverflow.com/questions/21731439/pdf-page-cutting-through-itext-api/21733554#21733554 and find out why your code is wrong. See also: http://stackoverflow.com/questions/16471673/merge-2-pdfs-in-java/16476188#16476188 – Bruno Lowagie Feb 24 '14 at 16:18
  • Thanks your reply, I tried to change my code on the first link you put, but I am receiving null pointer exception on the line "copy.AddDocument(reader);" although, I initiated copy object . I replaced BufferedReader with WebClient as there is no BufferedReader in C# and i need to read file from internet as opposed to local. The full code as below: – londondev Feb 24 '14 at 17:46
  • using (MemoryStream output = new MemoryStream()) { Document document = new Document(); document.Open(); PdfCopy copy = new PdfSmartCopy(document, output); foreach (var url in fileUrls) { using (WebClient client = new WebClient()) { var byteArray = client.DownloadData(url); PdfReader reader = new PdfReader(byteArray); copy.AddDocument(reader); reader.Close(); } } } – londondev Feb 24 '14 at 17:49
  • Sorry for the format of the code, the comment box is too limited. – londondev Feb 24 '14 at 17:49
  • 1
    Something seems to be wrong with the PDF you're trying to add. Can you share it? Also: you can update your question if you want to add a new code snippet. I've done this for you. I think that you're closing the `reader` too early and you never close the document. – Bruno Lowagie Feb 26 '14 at 07:55
  • Thanks Bruno. I fixed the problem by closing Document as below. But, I used the PDFWriter that you don't suggest. But I will replace it later with PDFCopy as you recommend. – londondev Feb 26 '14 at 15:56

1 Answers1

0

I found the problem, the document object should be closed before writing memory stream to file.

Just added document.Close() as below.

 document.Close();
 File.WriteAllBytes(reportFolder + fileName + ".pdf", output.GetBuffer());
londondev
  • 231
  • 2
  • 13