Print Page | Close Window

How to draw text fit to page width without wrap?

Printed From: Debenu Quick PDF Library - PDF SDK Community Forum
Category: For Users of the Library
Forum Name: I need help - I can help
Forum Description: Problems and solutions while programming with the Debenu Quick PDF Library and Debenu PDF Viewer SDK
URL: http://www.quickpdf.org/forum/forum_posts.asp?TID=2186
Printed Date: 26 Jun 25 at 12:43PM
Software Version: Web Wiz Forums 11.01 - http://www.webwizforums.com


Topic: How to draw text fit to page width without wrap?
Posted By: ExchangeViews
Subject: How to draw text fit to page width without wrap?
Date Posted: 13 Mar 12 at 7:24AM
Hi,
I want to draw a text string on pdf to fit into page width but unable to
calculate the textsize to be set to cover page width without wrapping into
next line. Please help.
EV



Replies:
Posted By: edvoigt
Date Posted: 13 Mar 12 at 9:36AM
Hi,

look at here:
 
  pw := 150;                         // as variable to play with
  QP.SetMeasurementUnits(1);         // unit mm
  QP.SetPageDimensions(pw, 300);
  QP.SetOrigin(0); // Bottomleft
  txt := 'This is to be fitted in a line';
  QP.DrawText(0, 250, txt);          // unfitted only to show the difference
  tsize := QP.GetTextSize;           // old textsize
  twidth := QP.GetTextWidth(txt);    // old textwidth
  QP.SetTextSize(tsize*pw
/twidth);
  QP.DrawText(0, 200,txt);           // now it fits in

The goal is to have the textwidth as same as pagewidth. So we we correct textsize by pagewidth/textwidth to get textwidth equal to pagewidth.

To find this look in the reference guide under text and get inspiration by names of functions. Then play with them. It helps ever.


Cheers,
Werner


Posted By: ExchangeViews
Date Posted: 13 Mar 12 at 12:36PM
Thanks a lot. Trick is working great.
 
when I use DrawRotatedText function with 0 degree, it is working fine but when I change degree to 45 or 60... it is not drawing in full width even i tried to calculate diagonal length using pw/cos(DegToRad(45)) to find out font size. Can you guide in this point?
 
Thanks in advance.


Posted By: edvoigt
Date Posted: 13 Mar 12 at 1:23PM
Hi,

no real problem. Only more complicated, because you have to think the text being in a box. But not the lower left corner ist the point, where rotation has origin. The origin is on baseline. So I have to build a new explanation near to this:

http://www.quickpdf.org/forum/tquickpdf0813drawtext_topic2074.html - http://www.quickpdf.org/forum/tquickpdf0813drawtext_topic2074.html

I'll do it in the evening.

Werner


Posted By: edvoigt
Date Posted: 13 Mar 12 at 5:57PM
Hi,

here the solution with some decorations for better understanding. The kernel is much more short.

  QP.SetMeasurementUnits(1);                // unit mm
  angle := 30; // degrees for DrawRotated, betwenn 0 and 360°
  QP.SetPageDimensions(210, 300);
  QP.SetOrigin(0); // Bottomleft
  txt := 'This is to be fitted for x-direction, not y';

  x0 := 20;     // borders in which we want to place the text
  x1 := 190;
  fitwidth := x1-x0;
  x := x0; y := 180; // startpoint
// 1. fitting for 0 degrees
  factor := (fitwidth)/QP.GetTextWidth(txt);
  QP.SetTextSize(QP.GetTextSize*factor);

  QP.SetLineWidth(0.1);
  QP.SetFillColor(1, 0, 0);
  QP.DrawCircle(x, y, 0.7, 1); // mark referenzpoint
  QP.SetTextColor(0.75, 0.75, 0.75);
  QP.DrawText(x, y,txt);
// to show the relation between box and baseline
  tw := QP.GetTextWidth(txt);
  th := QP.GetTextHeight;
  td := -QP.GetTextDescent;
  tt := th-td;
  QP.DrawBox(x, y+tt, tw, tt+td, 0); // box from bottom to top
  QP.SetLineColor(1, 0, 0);          // baseline
  QP.DrawLine(x, y, x+tw, y);

// now fitting for rotation
  QP.SetLineColor(0, 0, 1);
  salpha := sin(angle*pi/180);
  calpha := cos(angle*pi/180);

// show x-borders
  QP.SetLineColor(1, 0, 0);
  QP.DrawLine(x0, y-50, x0, y+100);
  QP.DrawLine(x1, y-50, x1, y+100);

// x-distance from the most left to most right point of box is to be calced as:
  factor := fitwidth/(calpha*tw + salpha*th); // there are 2 boxsides!

  QP.SetTextSize(QP.GetTextSize*factor);
// new values for sizes
  td := -QP.GetTextDescent;
  tt := QP.GetTextBound(2);
  dx := salpha*(tt+td);
  tw := QP.GetTextWidth(txt);
  th := QP.GetTextHeight;
  QP.SetTextColor(0, 0, 0);
  QP.DrawRotatedText(x+dx-salpha*td, y, angle, txt);
  QP.SetLineColor(0, 0, 1);
  QP.DrawRotatedBox(x+dx, (y-td), tw, th, angle, 0);

We have to notice, that we handle a Box, which is a container for the text. If we rotate it, the edges must not be parallel to pageborders. To get the needed size in x we add the projektion of long (textwidth) and short (textheight) edges of the box onto x-axis. This value we have to compare with our goal the with from border to border. So we get a factor to choose the right Textsize.

And so it looks:


It is to see, there is no fully exact solution possible, without a lot of more work. It depends on the first and last character. A last "q" would be more near to right border and a "p" as first more far from left border.

Be careful using the code. It is only correct for 0 to <90°. On 90° cos(alpha) is zero. So have a look on this, if dividing. The next issue is if you are in another quarter of coordinatesystem, you have to change some signs. For testing and understanding rotations it is a good idea, to set the refererencepoint in the center of page. So you can test four cases at one time and see the differences.


Have fun,

Werner



Print Page | Close Window

Forum Software by Web Wiz Forums® version 11.01 - http://www.webwizforums.com
Copyright ©2001-2014 Web Wiz Ltd. - http://www.webwiz.co.uk