Problem adding annotation for a rotated PDF

View: New views
3 Messages — Rating Filter:   Alert me  

Problem adding annotation for a rotated PDF

by Hashim Saleem :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

I am facing a problem adding annotation on a PDF which has non zero rotation. The following code works fine for a PDF which has zero rotation. I have added stp.RotateContents = false; and also setting text rotation to the PDF rotation while adding text in the appearance of annotation but the output is not correct. Please guide me about what I am missing here. I am using itextsharp v4.0.8. Thanks.

RotationWithAnnotations.rar

//            String inFile = @"C:\Portrait-Letter.pdf";
            String inFile = @"C:\Landscape-Letter.pdf";

//            String outFile = @"C:\Portrait-Letter-annotated.pdf";
            String outFile = @"C:\Landscape-Letter-annotated.pdf";
           
            PdfReader reader = new PdfReader(new RandomAccessFileOrArray(inFile), null);

            using (Stream s = new MemoryStream())
            {
                PdfStamper stp = new PdfStamper(reader, s, '\0', true);

                iTextSharp.text.Rectangle pageSize = reader.GetBoxSize(1, "crop");
                if (pageSize == null)
                    pageSize = reader.GetPageSize(1);

                // Create annotation object
                iTextSharp.text.Rectangle rect = new iTextSharp.text.Rectangle(100, 100, 357, 181);
                iTextSharp.text.pdf.PdfAnnotation pdfAnnotation = new iTextSharp.text.pdf.PdfAnnotation(stp.Writer, rect);

                // Set basic fields
                pdfAnnotation.SubType = PdfName.STAMP;
                pdfAnnotation.Name = System.Guid.NewGuid().ToString();
                pdfAnnotation.Title = "Author";
                pdfAnnotation.Subject = "Approved";
                pdfAnnotation.Contents = "Approved";
                pdfAnnotation.Date = DateTime.Now;
                pdfAnnotation.Flags = iTextSharp.text.pdf.PdfAnnotation.FLAGS_PRINT;

                // Create appearance object
                PdfAppearance app = stp.GetOverContent(1).CreateAppearance(rect.Width, rect.Height);

                // Prepare appearance
                app.SetColorStroke(new iTextSharp.text.Color(64, 105, 27));
                app.SetColorFill(new iTextSharp.text.Color(218, 231, 211));
                app.RoundRectangle(2, 2, rect.Width - 4, rect.Height - 4, 10);
                app.FillStroke();

                app.SetColorFill(new iTextSharp.text.Color(64, 105, 27));
                app.BeginText();
                iTextSharp.text.Font font = iTextSharp.text.FontFactory.GetFont("Halvetica", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
                font.SetStyle(iTextSharp.text.Font.BOLDITALIC);
                BaseFont bf = font.GetCalculatedBaseFont(false);
                rect = new iTextSharp.text.Rectangle(rect.Width * (float)0.1, rect.Height * (float)0.15, rect.Width * (float)0.8, rect.Height * (float)0.7);
                float fontSize = PdfSignatureAppearance.FitText(font, pdfAnnotation.Subject, rect, 1024, 0);
                app.SetFontAndSize(bf, fontSize);
                app.ShowTextAligned(PdfContentByte.ALIGN_CENTER, pdfAnnotation.Subject, app.Width / 2, app.Height / (float)2.5, reader.GetPageRotation(1));
                app.EndText();

                // Set annotation appearance
                pdfAnnotation.SetAppearance(iTextSharp.text.pdf.PdfAnnotation.APPEARANCE_NORMAL, app);
                pdfAnnotation.SetAppearance(iTextSharp.text.pdf.PdfAnnotation.APPEARANCE_DOWN, app);
                pdfAnnotation.SetAppearance(iTextSharp.text.pdf.PdfAnnotation.APPEARANCE_ROLLOVER, app);

                // Add annotation
                stp.AddAnnotation(pdfAnnotation, 1);
                stp.RotateContents = false;
                stp.Close();
                File.WriteAllBytes(outFile, ((MemoryStream)s).ToArray());
            }

Re: Problem adding annotation for a rotated PDF

by Wahaj :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

This might help: http://itext.ugent.be/library/question.php?id=588

Regards,
Wahaj

"Hashim Saleem" <hashim.saleem@...>
wrote in message news:22286877.post@......

>
> Hi,
>
> I am facing a problem adding annotation on a PDF which has non zero
> rotation. The following code works fine for a PDF which has zero rotation.
> I
> have added stp.RotateContents = false; and also setting text rotation to
> the
> PDF rotation while adding text in the appearance of annotation but the
> output is not correct. Please guide me about what I am missing here. I am
> using itextsharp v4.0.8. Thanks.
>
> http://www.nabble.com/file/p22286877/RotationWithAnnotations.rar
> RotationWithAnnotations.rar
>
> //            String inFile = @"C:\Portrait-Letter.pdf";
>            String inFile = @"C:\Landscape-Letter.pdf";
>
> //            String outFile = @"C:\Portrait-Letter-annotated.pdf";
>            String outFile = @"C:\Landscape-Letter-annotated.pdf";
>
>            PdfReader reader = new PdfReader(new
> RandomAccessFileOrArray(inFile), null);
>
>            using (Stream s = new MemoryStream())
>            {
>                PdfStamper stp = new PdfStamper(reader, s, '\0', true);
>
>                iTextSharp.text.Rectangle pageSize = reader.GetBoxSize(1,
> "crop");
>                if (pageSize == null)
>                    pageSize = reader.GetPageSize(1);
>
>                // Create annotation object
>                iTextSharp.text.Rectangle rect = new
> iTextSharp.text.Rectangle(100, 100, 357, 181);
>                iTextSharp.text.pdf.PdfAnnotation pdfAnnotation = new
> iTextSharp.text.pdf.PdfAnnotation(stp.Writer, rect);
>
>                // Set basic fields
>                pdfAnnotation.SubType = PdfName.STAMP;
>                pdfAnnotation.Name = System.Guid.NewGuid().ToString();
>                pdfAnnotation.Title = "Author";
>                pdfAnnotation.Subject = "Approved";
>                pdfAnnotation.Contents = "Approved";
>                pdfAnnotation.Date = DateTime.Now;
>                pdfAnnotation.Flags =
> iTextSharp.text.pdf.PdfAnnotation.FLAGS_PRINT;
>
>                // Create appearance object
>                PdfAppearance app =
> stp.GetOverContent(1).CreateAppearance(rect.Width, rect.Height);
>
>                // Prepare appearance
>                app.SetColorStroke(new iTextSharp.text.Color(64, 105, 27));
>                app.SetColorFill(new iTextSharp.text.Color(218, 231, 211));
>                app.RoundRectangle(2, 2, rect.Width - 4, rect.Height - 4,
> 10);
>                app.FillStroke();
>
>                app.SetColorFill(new iTextSharp.text.Color(64, 105, 27));
>                app.BeginText();
>                iTextSharp.text.Font font =
> iTextSharp.text.FontFactory.GetFont("Halvetica", BaseFont.IDENTITY_H,
> BaseFont.EMBEDDED);
>                font.SetStyle(iTextSharp.text.Font.BOLDITALIC);
>                BaseFont bf = font.GetCalculatedBaseFont(false);
>                rect = new iTextSharp.text.Rectangle(rect.Width *
> (float)0.1, rect.Height * (float)0.15, rect.Width * (float)0.8,
> rect.Height
> * (float)0.7);
>                float fontSize = PdfSignatureAppearance.FitText(font,
> pdfAnnotation.Subject, rect, 1024, 0);
>                app.SetFontAndSize(bf, fontSize);
>                app.ShowTextAligned(PdfContentByte.ALIGN_CENTER,
> pdfAnnotation.Subject, app.Width / 2, app.Height / (float)2.5,
> reader.GetPageRotation(1));
>                app.EndText();
>
>                // Set annotation appearance
>
> pdfAnnotation.SetAppearance(iTextSharp.text.pdf.PdfAnnotation.APPEARANCE_NORMAL,
> app);
>
> pdfAnnotation.SetAppearance(iTextSharp.text.pdf.PdfAnnotation.APPEARANCE_DOWN,
> app);
>
> pdfAnnotation.SetAppearance(iTextSharp.text.pdf.PdfAnnotation.APPEARANCE_ROLLOVER,
> app);
>
>                // Add annotation
>                stp.AddAnnotation(pdfAnnotation, 1);
>                stp.RotateContents = false;
>                stp.Close();
>                File.WriteAllBytes(outFile, ((MemoryStream)s).ToArray());
>            }
> --
> View this message in context:
> http://www.nabble.com/Problem-adding-annotation-for-a-rotated-PDF-tp22286877p22286877.html
> Sent from the itextsharp-questions mailing list archive at Nabble.com.
>
>
> ------------------------------------------------------------------------------
> Open Source Business Conference (OSBC), March 24-25, 2009, San Francisco,
> CA
> -OSBC tackles the biggest issue in open source: Open Sourcing the
> Enterprise
> -Strategies to boost innovation and cut costs with open source
> participation
> -Receive a $600 discount off the registration fee with the source code:
> SFAD
> http://p.sf.net/sfu/XcvMzF8H 




------------------------------------------------------------------------------
Apps built with the Adobe(R) Flex(R) framework and Flex Builder(TM) are
powering Web 2.0 with engaging, cross-platform capabilities. Quickly and
easily build your RIAs with Flex Builder, the Eclipse(TM)based development
software that enables intelligent coding and step-through debugging.
Download the free 60 day trial. http://p.sf.net/sfu/www-adobe-com
_______________________________________________
itextsharp-questions mailing list
itextsharp-questions@...
https://lists.sourceforge.net/lists/listinfo/itextsharp-questions

Re: Problem adding annotation for a rotated PDF

by manojbamola :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

 hi
 i have used the iTextSharp dll for adding pdf document but now i am not able to change the
font size of my document because may be its taking default fonsize from iTextSharp so kindly help me out