The following piece of code is supposed to take an existing pdf and add a
bubble with a text in it. It works correctly when opening in Adode Reader 8
and 9. But in Reader 7, it throws "Error while parsing a Form, Type 3 font,
or Pattern". The code in bold is causing the issue but i dont know how to
fix it.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iTextSharp.text.pdf;
using iTextSharp.text;
using System.IO;

namespace ITextPdfCheck
{
        class Program
        {
                private static Image _GetBubbleImage(PdfContentByte cb, Font 
font, string
label, float bubbleX, float bubbleY)
                {
                        PdfTemplate boxTemplate = cb.CreateTemplate(bubbleX, 
bubbleY);

                        boxTemplate.SetLineWidth(1);
                        boxTemplate.Ellipse(1, 1, bubbleX - 1, bubbleY - 1);

                        if (!String.IsNullOrEmpty(label))
                        {
                                boxTemplate.BeginText();
                                boxTemplate.SetTextRise(bubbleX / 5.0F);
                        
boxTemplate.SetTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_STROKE);
                                boxTemplate.SetFontAndSize(font.BaseFont, 8);
                                float textWidth = 
boxTemplate.GetEffectiveStringWidth(label, false);
                                boxTemplate.MoveText((bubbleX - textWidth) / 2, 
0);
                                boxTemplate.ShowText(label);
                                boxTemplate.EndText();

                                boxTemplate.Stroke();
                        }
                        else
                        {
                                boxTemplate.Fill();
                        }

                        return Image.GetInstance(boxTemplate);
                }

                internal static PdfPCell CreateBubblePdfCell(PdfContentByte 
contentByte,
string cellContent, string tagName, float bubbleX, float bubbleY, Font font,
int border)
                {
                        Image bubbleImage = _GetBubbleImage(contentByte, font, 
cellContent,
bubbleX, bubbleY);

                        Chunk imageChunk = new Chunk(bubbleImage, 0, 0);
                        if (!String.IsNullOrEmpty(tagName))
                        {
                                imageChunk.SetGenericTag(tagName);
                        }

                        PdfPCell bubbleCell = new PdfPCell(new 
Phrase(imageChunk))
                        {
                                Border = border,
                                Padding = 0,
                                BackgroundColor = BaseColor.WHITE,
                                HorizontalAlignment = Element.ALIGN_CENTER,
                                VerticalAlignment = Element.ALIGN_TOP
                        };

                        return bubbleCell;
                }

                private static PdfTemplate _GetBubbleTemplate(Font _Font, 
PdfContentByte
cb, string label, int bubbleX, int bubbleY, string tagName)
                {
                        PdfTemplate pdfTemplate = cb.CreateTemplate(bubbleX + 
2, bubbleY + 2);
                        PdfPTable pdfPTable = new PdfPTable(1);
                        pdfPTable.SetTotalWidth(new float[] { bubbleX });
                        PdfPCell pdfPCell = CreateBubblePdfCell(cb, label, 
tagName, bubbleX,
bubbleY, _Font, 0);

                        pdfPTable.AddCell(pdfPCell);
                        pdfPTable.WriteSelectedRows(0, -1, 0, 
pdfPTable.TotalHeight,
pdfTemplate);
                        return pdfTemplate;
                }

                static void Main(string[] args)
                {
                        Document document = new Document();
                        Font _Font = FontFactory.GetFont(FontFactory.HELVETICA);
                        try
                        {
                                using (MemoryStream localMemoryStream = new 
MemoryStream())
                                {
                                        PdfReader pdfReader = new 
PdfReader(File.ReadAllBytes(@"test.pdf"));
                                        PdfWriter pdfWriter = 
PdfWriter.GetInstance(document,
localMemoryStream);

                                        document.Open();

                                        PdfImportedPage pdfImportedPage = 
pdfWriter.GetImportedPage(pdfReader,
1);

                                        PdfContentByte cb = 
pdfWriter.DirectContent;

                                        cb.AddTemplate(pdfImportedPage, 0, 0);

                                        
cb.AddTemplate(_GetBubbleTemplate(_Font, cb, "A", 20, 20, ""), 101,
101);

                                        pdfReader.Close();
                                        document.Close();
                                        File.WriteAllBytes(@"test1.pdf", 
localMemoryStream.GetBuffer());
                                }
                        }
                        catch (Exception ex)
                        {
                                Console.WriteLine(ex);
                        }
                        document.Close();
                }
        }
}

-- 
View this message in context: 
http://itext-general.2136553.n4.nabble.com/Issue-with-opening-Pdf-in-Acrobat-7-tp3051169p3051169.html
Sent from the iText - General mailing list archive at Nabble.com.
------------------------------------------------------------------------------
Beautiful is writing same markup. Internet Explorer 9 supports
standards for HTML5, CSS3, SVG 1.1,  ECMAScript5, and DOM L2 & L3.
Spend less time writing and  rewriting code and more time creating great
experiences on the web. Be a part of the beta today
http://p.sf.net/sfu/msIE9-sfdev2dev
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions

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