PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Thu Oct 31, 2024 10:57 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Tue Mar 18, 2014 5:01 pm 
Offline

Joined: Mon Mar 10, 2014 11:38 pm
Posts: 4
Hello all! I'm working on a multiline function to determine the xPosition where a character is going to be drawn and then return the last Y position where the last character was drawn. This is because I'm working with a lots of dynamic strings with variable lengths.

Code:
 
public double fnEscribirMultilinea(string sLinea, int nCaracteres, double nSeparacion, double posX, double posY,  XFont xFuente, ref XGraphics gfx)
        {
           // StringBuilder sb1 = new StringBuilder();
           // StringBuilder sb2 = new StringBuilder();
            sLinea = sLinea.Replace("\n", "");
            sLinea = sLinea.Replace("\r", "");

            double separacionLetra = xFuente.Metrics.Ascent;

            int nCaracteresCadena = sLinea.Length;
            double posXTemp = posX;

            double xColumna = posX;
            double yRenglon = posY;

            if (nCaracteresCadena > nCaracteres)
            {

                for (int i = 0; i < nCaracteresCadena; i++)
                {
                    char caracter = sLinea[i];

                    if (((i == nCaracteres) || i % nCaracteres == 0) & i != 0)
                    {
                        yRenglon += nSeparacion;
                        gfx.DrawString(caracter.ToString(), xFuente, XBrushes.Black, posX, yRenglon, XStringFormats.TopLeft);
                        xColumna = posX + separacionLetra;

                    }
                    else if (i == 0)
                    {
                        gfx.DrawString(caracter.ToString(), xFuente, XBrushes.Black, xColumna, yRenglon, XStringFormats.TopLeft);
                        xColumna += separacionLetra;
                    }
                    else if (i < nCaracteres)
                    {
                        gfx.DrawString(caracter.ToString(), xFuente, XBrushes.Black, xColumna, yRenglon, XStringFormats.TopLeft);
                        xColumna += separacionLetra;
                    }
                    else
                    {
                        gfx.DrawString(caracter.ToString(), xFuente, XBrushes.Black, xColumna, yRenglon, XStringFormats.TopLeft);
                        xColumna += separacionLetra;
                    }
                }

               
            }
            else { gfx.DrawString(sLinea, xFuente, XBrushes.Black, xColumna, yRenglon, XStringFormats.TopLeft); }


            return yRenglon;
        }



So far, the Y position separation works properly, but the X separation between characters needs to be adjusted according to the font that is being used.

Is there a way to determine the separation between characters according to the font without having to manually adjust this value?


Top
 Profile  
Reply with quote  
PostPosted: Tue Mar 18, 2014 5:05 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3111
Location: Cologne, Germany
Hi!

The trick used by the XTextFormatter class can be seen in the source code of this class and in this post:
viewtopic.php?p=5801#p5801

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Tue Mar 18, 2014 5:46 pm 
Offline

Joined: Mon Mar 10, 2014 11:38 pm
Posts: 4
Thank you so much! It works perfectly now :D


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 17 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group