Hi,

 

I have a project to write PDF files from content that has some RTF generated
from a .Net C# rich textbox.  I've parsed the rtf into a model that has
Paragraphs and Text elements.  In the following code I'm adapting these to
iTextSharp objects.  Whatever I try I cannot seems to repeat is the
paragraph line spacing that I can see in MS word (rtf file) or wordpad.
Using Paragraph..SetLeading(0, 1.2F) seems too small for large fonts sizes
(above 12pt) and too large for small fonts sizes (below 12pt) also depending
on the font.  I do not see that setting the leading in the Phase object has
any effect at all and I'd be insterested to know how this is processed later
on.  Any tips on how I ge the same formatting as MS would be most
appreciated.  I'm happy to share any code snippets that I'm using elsewhere
if this helps.

 

My testing text:

This is the original text in the first paragraph, first text in Times New
Roman 22 point blue regular.  Some more text to see if the word wrapping is
working as it should, and the left indent is now set at 1 in. or 2.54 cm and
the line spacing is simple.

NOW A second Paragraph starting in red text in Algerian 12 bold and then
veranda 14 blue bold italic strikethrough in the same para.

 

My adapater classes:

public class PdfParaAdapter : Paragraph

    {

        public PdfParaAdapter(RtfParaElement paraElement)

        {

            this.InitRtfPara(paraElement);

        }

 

        private void InitRtfPara(RtfParaElement paraElement)

        {

            //Alignment

            this.Alignment = (int)paraElement.Align;

 

            //Indents

            if (paraElement.LeftIndent != -1)

                this.IndentationLeft = paraElement.LeftIndent / 20; //From
TWIPS

            if (paraElement.RightIndent != -1)

                this.IndentationRight = paraElement.RightIndent / 20;

            if (paraElement.FirstLineIndent != -1)

                this.FirstLineIndent = paraElement.FirstLineIndent / 20;

 

            //Spacing

            if (paraElement.SpaceBefore != -1)

                this.SpacingBefore = paraElement.SpaceBefore / 20;

 

            if (paraElement.SpaceAfter != -1)

                this.SpacingAfter = paraElement.SpaceAfter / 20;

 

            //Other props

            this.KeepTogether = !paraElement.NoWidows;

            

            this.AddChunks(paraElement);

 

            this.SetLeading(0, 1.2F);

           

            if (paraElement.SpaceBetween != -1)

            {

                this.SetLeading(0, paraElement.SpaceBetween / 250);

                Chunk lastChunk = (Chunk)this.Chunks[this.Chunks.Count - 1];

                this.SpacingAfter += (lastChunk.Font.Size*1.5F);

            }

        }

 

        private void AddChunks(RtfParaElement paraElement)

        {

            foreach (RtfElement element in paraElement.Elements)

            {

                if (element is RtfImageElement)

                    this.Add(new PdfChunkAdapter((RtfImageElement)element));

                else

                    this.Add(new PdfPhraseAdapter((RtfTextElement)element));

            }

        }

    }

 

    public class PdfPhraseAdapter : Phrase

    {

        public PdfPhraseAdapter(RtfTextElement textElement)

            : base(textElement.Font.Height, new
PdfChunkAdapter(textElement))

 

        {

            this.InitRtfTextElement(textElement);

        }

 

        private void InitRtfTextElement(RtfTextElement textElement)

        {

        }

    }

 

 

    public class PdfChunkAdapter: Chunk

    {

        public PdfChunkAdapter(RtfTextElement textElement)

            :base(textElement.Text, new PdfFontAdapter(textElement))

        {

            this.InitRtfTextElement(textElement);

        }

 

        public PdfChunkAdapter(RtfImageElement imageElement)

            :base(
(Image.GetInstance(imageElement.Image,ImageUtil.FormatFromString(imageElemen
t.ImageFormat))),0,0)

        {

        }

 

        private void InitRtfTextElement(RtfTextElement textElement)

        {

            if(textElement.TextBackColorIndex != -1)

                this.SetBackground(new
BaseColor(textElement.TextBackColor));

 

        }

    }

    public class PdfFontAdapter : Font

    {

        public PdfFontAdapter(RtfTextElement textElement)

            :base(

            FontFactory.GetFont(

            textElement.FontName,

            textElement.FontSize/2,

            (int)textElement.FontStyle,

            new BaseColor(textElement.TextForeColor)))

        {  }

 

    }

 

Thanks in advance for any help

Brian Akehurst

------------------------------------------------------------------------------
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