Thanks Paulo.

I downloaded and built the SVN tip and was able to reproduce the problem.
The gist of it is that if you use PdfWriter to import a page and re-create
link annotations, and those annotations contain indirect references (such as
a Parent, i.e. "/P 8 0 R"), you get the exception. Enclosed is a PDF file
that I clobbered together with such a link, along with a few line demo
program that will crash when you copy the page out to a new file and
re-create links. It will succeed when the patch I suggested is applied.

Note that I stripped everything down to the bare bones - this code is not
useful for any particular purpose other than to demonstrate the bug. My
guess is that most folks use PdfCopy or PdfSmartCopy for operations like
this, so perhaps that's why nobody has run into this before. It is not clear
to me whether the problem is purely Annotation related - I suspect that this
could happen during other operations as well.

Upon further testing, I noticed is that the patch I suggested creates a
reference to a non-existent object, but Acrobat doesn't care as the parent
reference is redundant, but it may cause problems for other tools/parsers.
This makes me believe that there is a better way to fix the problem although
we are getting by at the moment.

Any feedback would be greatly appreciated.

Thanks - Gylfi

-----Original Message-----
From: Paulo Soares [mailto:psoa...@glintt.com] 
Sent: Wednesday, August 04, 2010 12:36 PM
To: Post all your questions about iText here
Subject: Re: [iText-questions] Null pointer exception in PdfWriter

This area was extensively changed in 5.0.3 (when it's out this weekend but
you can get it from the SVN), it would be worth to see if the problem is
already fixed.

Paulo 

-----Original Message-----
From: Gylfi Ingvason [mailto:gylfi.ingva...@solimarsystems.com]
Sent: Wednesday, August 04, 2010 4:52 PM
To: itext-questions@lists.sourceforge.net
Subject: [iText-questions] Null pointer exception in PdfWriter

Gents,

I'm using an older version of iTextSharp (4.1.6 equivalent), and recently
ran into a null pointer exception in PdfWriter while splitting a PDF file.
Never seen this before despite running all kinds of data for years. The
error happens on the PdfDocument Close() call, that leads to a
RotateAnnotations call, that calls writer.AddToBody, and eventually
GetNewObjectNumber in the PdfWriter is called and that method looks like
this:

protected internal virtual int GetNewObjectNumber(PdfReader reader, int
number, int generation) {
    return currentPdfReaderInstance.GetNewObjectNumber(number, generation);
}

At the time the call is made, the currentPdfReaderInstance is null causing
the exception. I looked at the code in the repository for the latest, and
this method has not changed. I patched the code to check for null as such:

protected internal virtual int GetNewObjectNumber(PdfReader reader, int
number, int generation) {

    if (currentPdfReaderInstance == null) {
        currentPdfReaderInstance = reader.GetPdfReaderInstance(this);
    }

    return currentPdfReaderInstance.GetNewObjectNumber(number, generation);
}

This creates proper output. My question is, is this an acceptable patch or
should this be handled differently? Furthermore, it worries me that this has
not come up before - is it normal that the currentPdfReaderInstance is null
under these circumstances? I have not tried this under 5.0.2 but I suspect
that the same thing will happen - would it make sense to patch the latest?

Any feedback would be appreciated.

Thanks - Gylfi


Aviso Legal:

Esta mensagem i destinada exclusivamente ao destinatario. Pode conter
informagco confidencial ou legalmente protegida. A incorrecta transmissco
desta mensagem nco significa a perca de confidencialidade. Se esta mensagem
for recebida por engano, por favor envie-a de volta para o remetente e
apague-a do seu sistema de imediato. I proibido a qualquer pessoa que nco o
destinatario de usar, revelar ou distribuir qualquer parte desta mensagem. 



Disclaimer:

This message is destined exclusively to the intended receiver. It may
contain confidential or legally protected information. The incorrect
transmission of this message does not mean the loss of its confidentiality.
If this message is received by mistake, please send it back to the sender
and delete it from your system immediately. It is forbidden to any person
who is not the intended receiver to use, distribute or copy any part of this
message.



Attachment: LinkTest.pdf
Description: Adobe PDF document

using System;
using System.IO;
using System.Collections.Generic;
using System.Text;

using iTextSharp.text;
using iTextSharp.text.pdf;

namespace SplitTest
{
    class Program
    {
        static void Main(string[] args)
        {
            string inputFileName = @"..\..\..\LinkTest.pdf";
            PdfReader reader = new PdfReader(inputFileName);

            int pageNumber = 1;

            while (pageNumber <= reader.NumberOfPages)
            {
                string outputFileName = 
Path.Combine(Path.GetDirectoryName(inputFileName), ("Page_" + pageNumber + 
".pdf"));
                Document document = new Document();
                PdfWriter writer = PdfWriter.GetInstance(document, new 
FileStream(outputFileName, FileMode.Create, FileAccess.Write));
                document.Open();
                
document.SetPageSize(reader.GetPageSizeWithRotation(pageNumber));
                document.NewPage();

                PdfImportedPage page = writer.GetImportedPage(reader, 
pageNumber);
                writer.DirectContent.AddTemplate(page, 1, 0, 0, 1, 0, 0);

                // re-create links...

                List<PdfAnnotation.PdfImportedLink> links = 
reader.GetLinks(pageNumber);

                foreach (PdfAnnotation.PdfImportedLink link in links)
                {
                    PdfAnnotation annot = link.CreateAnnotation(writer);
                    PdfDictionary actionDictionary = 
(PdfDictionary)PdfReader.GetPdfObject(annot.Get(PdfName.A));
                    PdfString uri = 
(PdfString)actionDictionary.Get(PdfName.URI);
                    PdfAction action = new PdfAction(uri.ToString());
                    annot.Action = action;
                    writer.AddAnnotation(annot);
                }

                document.Close();
                ++pageNumber;
            }
        }
    }
}
------------------------------------------------------------------------------
This SF.net email is sponsored by 

Make an app they can't live without
Enter the BlackBerry Developer Challenge
http://p.sf.net/sfu/RIM-dev2dev 
_______________________________________________
iText-questions mailing list
iText-questions@lists.sourceforge.net
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