Converting the chapter 6 example from the new book here:

http://itextpdf.com/examples/index.php?page=example&id=109

to C#. C# code and PDF used by PdfReader attached. An Exception is thrown:

============================================================================
Unhandled Exception: System.NullReferenceException: Object reference not set to
an instance of an object.
   at iTextSharp.text.pdf.PdfReader.GetNormalizedRectangle(PdfArray box)
   at iTextSharp.text.pdf.PdfImportedPage..ctor(PdfReaderInstance readerInstance
, PdfWriter writer, Int32 pageNumber)
   at iTextSharp.text.pdf.PdfReaderInstance.GetImportedPage(Int32 pageNumber)
   at iTextSharp.text.pdf.PdfCopy.GetImportedPage(PdfReader reader, Int32 
pageNumber)
   at SelectPages.manipulateWithCopy(PdfReader reader)
   at SelectPages.Main(String[] args)
============================================================================

Also tried using the latest code from SVN, with the same result. The
PdfArray passed to GetNormalizedRectangle() is null if the PdfReader is used
more than once - if either manipulateWithStamper() or manipulateWithCopy()
is commented out the example works. If two separate instances of PdfReader
are used the example works. The second (and first?) book mentioned that a
single PdfReader object must only be used with one PdfStamper object, but
am guessing this doesn't apply here, since PdfStamper/PdfCopy are used and
the Java example works?

Thanks, keith
                                          
_________________________________________________________________
The New Busy is not the old busy. Search, chat and e-mail from your inbox.
http://www.windowslive.com/campaign/thenewbusy?ocid=PID28326::T:WLMTAGL:ON:WL:en-US:WM_HMP:042010_3

Attachment: Chapter03.MovieTemplates.pdf
Description: Adobe PDF document

using System;
using System.IO;
using System.Reflection;
using iTextSharp.text;
using iTextSharp.text.pdf;

public class SelectPages {
  public static void Main (string[] args) {
    string file = Path.Combine(
      SelectPages.CurrentDirectory,
      "Chapter03.MovieTemplates.pdf"
    );
    PdfReader reader = new PdfReader(file);
    reader.SelectPages("4-8");
    manipulateWithStamper(reader);
    manipulateWithCopy(reader);
  }

  public static string CurrentDirectory;
  static SelectPages() {
    CurrentDirectory = Path.GetDirectoryName(
      Assembly.GetExecutingAssembly().Location
    );
  }

  /**
    * Creates a new PDF based on the one in the reader
    * @param reader a reader with a PDF file
    * @throws IOException
    * @throws DocumentException
    */
  private static void manipulateWithStamper(PdfReader reader) {
    string file = Path.Combine(
      SelectPages.CurrentDirectory, "timetable_stamper.pdf"
    );
/*    
    using (FileStream fs = new FileStream(file, FileMode.OpenOrCreate))
    {
*/
      FileStream fs = new FileStream(file, FileMode.OpenOrCreate);
      PdfStamper stamper = new PdfStamper(reader, fs);
      stamper.Close();
/*    
    }
*/
  }

  /**
    * Creates a new PDF based on the one in the reader
    * @param reader a reader with a PDF file
    * @throws IOException
    * @throws DocumentException
    */
  private static void manipulateWithCopy(PdfReader reader) {
    string file = Path.Combine(
      SelectPages.CurrentDirectory, "timetable_copy.pdf"
    );    
/*    
    using (FileStream fs = new FileStream(file, FileMode.OpenOrCreate))
    {
*/
    FileStream fs = new FileStream(file, FileMode.OpenOrCreate);
      int n = reader.NumberOfPages;
      Document document = new Document();
      PdfCopy copy = new PdfCopy(document, fs);
      document.Open();
      for (int i = 0; i < n;) {
        copy.AddPage(copy.GetImportedPage(reader, ++i));
      }
      document.Close();
/*    
    }
*/
  }
}
------------------------------------------------------------------------------
This SF.net email is sponsored by Sprint
What will you do first with EVO, the first 4G phone?
Visit sprint.com/first -- http://p.sf.net/sfu/sprint-com-first
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions

Buy the iText book: http://www.itextpdf.com/book/
Check the site with examples before you ask questions: 
http://www.1t3xt.info/examples/
You can also search the keywords list: http://1t3xt.info/tutorials/keywords/

Reply via email to