You have two content streams in the PDF - one that renders an XObject that is a 
green(ish) rect and then the actual page content.  Perhaps you don't want that 
first one?!?!

From: Suneet Shah [mailto:[email protected]]
Sent: Wednesday, September 05, 2012 8:49 PM
To: [email protected]
Subject: [iText-questions] Annotation Appearance Stream Created in iText Not 
Set properly?

Hey there,

I'm using iText to create highlight annotations, and I'm defining the 
appearance stream manually for the highlights.

I've found that when opening such a document in Adobe Reader, the highlight 
displays as expected-- but if you select the highlight and delete it, the 
"appearance" of the highlight is still visible, but there is no annotation 
left.  So in a sense, Adobe Reader is rendering both the annotation, and the 
annotation appearance stream, (not 1 or the other, as is the expected behavior).

I've attached an example file created like this, as well as some example code 
for creating such an annotation.

Any idea what I'm doing wrong? I've used iText RUPS and compared a highlight 
annotation generated by Adobe Reader to one generated with my code, and I can't 
really seem to find what I'm doing wrong.

Thanks

------

 string outputFile = "test.pdf";


            using (FileStream fs = new FileStream(outputFile, FileMode.Create, 
FileAccess.Write, FileShare.None))
            {
                using (Document doc = new Document(PageSize.LETTER))
                {
                    using (PdfWriter w = PdfWriter.GetInstance(doc, fs))
                    {

                        doc.Open();

                        w.CompressionLevel = 0;

                        Paragraph p = new Paragraph("This is a test with 
compression level is 0");

                        doc.Add(p);

                        doc.Add(new Paragraph("This is a test with compression 
level is 0"));
                        doc.Close();
                    }
                }
            }

            //Create a new file from our test file with highlighting
            string highLightFile = "Highlighted.pdf";

            //Bind a reader and stamper to our test PDF
            PdfReader reader = new PdfReader(outputFile);

            using (FileStream fs = new FileStream(highLightFile, 
FileMode.Create, FileAccess.Write, FileShare.None))
            {
                using (PdfStamper stamper = new PdfStamper(reader, fs))
                {
                    //Create a rectangle for the highlight. NOTE: Technically 
this isn't used but it helps with the quadpoint calculation

                    iTextSharp.text.Rectangle rect = new 
iTextSharp.text.Rectangle(60.6755f, 735.3f, 94.0195f, 749.172f);
                    //Create an array of quad points based on that rectangle. 
NOTE: The order below doesn't appear to match the actual spec but is what 
Acrobat produces
                    float[] quad = { rect.Left, rect.Bottom, rect.Right, 
rect.Bottom, rect.Left, rect.Top, rect.Right, rect.Top };

                    //Create our hightlight
                    PdfAnnotation highlight = 
PdfAnnotation.CreateMarkup(stamper.Writer, rect, null, 
PdfAnnotation.MARKUP_HIGHLIGHT, quad);



                    //Set the color
                    highlight.Color = BaseColor.YELLOW;

                    //PdfContentByte canvas = stamper.Writer.DirectContent;
                    PdfContentByte canvas = stamper.GetUnderContent(1);

                    PdfAppearance highlight_ap = 
canvas.CreateAppearance(rect.Width, rect.Height);

                    /*
                    highlight_ap.MoveTo(0, 0);
                    highlight_ap.LineTo(highlight_ap.Width, 0);
                    highlight_ap.LineTo(highlight_ap.Width, 
highlight_ap.Height);
                    highlight_ap.LineTo(0, highlight_ap.Height);
                    highlight_ap.ClosePath();
                    */

                    PdfGState transparent = new PdfGState();
                    transparent.FillOpacity = 0.4f;
                    highlight_ap.SetGState(transparent);

                    highlight_ap.Rectangle(0, 0, highlight_ap.Width, 
highlight_ap.Height);
                    highlight_ap.SetRGBColorFill(127, 255, 0);
                    highlight_ap.Fill();

                    stamper.Writer.ReleaseTemplate(highlight_ap);
                    canvas.AddTemplate(highlight_ap, rect.Left, rect.Bottom);

                    PdfName highlight_name = new PdfName("N");
                    highlight.SetAppearance(highlight_name, highlight_ap);


                    //Add the annotation
                    stamper.AddAnnotation(highlight, 1);
                }
            }
------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions

iText(R) is a registered trademark of 1T3XT BVBA.
Many questions posted to this list can (and will) be answered with a reference 
to the iText book: http://www.itextpdf.com/book/
Please check the keywords list before you ask for examples: 
http://itextpdf.com/themes/keywords.php

Reply via email to