PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

Adjusting the Letter spacing between the words
https://forum.pdfsharp.net/viewtopic.php?f=2&t=2019
Page 1 of 1

Author:  Vadivelan [ Mon May 14, 2012 2:08 pm ]
Post subject:  Adjusting the Letter spacing between the words

Is it possible to adjust the Letter spacing between the words using the pdfSharp.dll ?

Author:  Thomas Hoevel [ Mon May 14, 2012 3:33 pm ]
Post subject:  Re: Adjusting the Letter spacing between the words

When you create new files with PDFsharp, you can easily set the spacing to any amount you want.
If you want to modify an existing PDF file: that'll be difficult.

Author:  Vadivelan [ Tue May 15, 2012 12:13 pm ]
Post subject:  Re: Adjusting the Letter spacing between the words

Hi Thomas,

Thanks for your reply.

Actually the application is generating the pdf document on the fly using this pdfsharp.dll.

Can you please tell me the property / method to achieve this feature - "adjusting the letter spacing between the words" in the below code snippet ?

Please find below the code(c#) we are using in our application.

PdfDocument document = new PdfDocument();

// Page object have all the data to write on pdf.
Document page = (Document)GetObject(xml, typeof(Document));
...
...
...

//Draw the PDF document
page.Draw(document);

using (MemoryStream stream = new MemoryStream()) {
try {
document.Save(stream, false);
Array pdfBpData = stream.ToArray();

document.Close();
return pdfBpData;
}
catch (Exception exception) {

}
}


Thanks
Vadivelan.

Author:  Thomas Hoevel [ Tue May 15, 2012 12:36 pm ]
Post subject:  Re: Adjusting the Letter spacing between the words

Hi!
Vadivelan wrote:
Can you please tell me the property / method to achieve this feature - "adjusting the letter spacing between the words" in the below code snippet?
I don't see any code that draws text.

If you use the XTextFormatter class, look for the code that calculates the width of a space and change it to your needs:
Code:
this.spaceWidth = gfx.MeasureString("x x", value).width;
this.spaceWidth -= gfx.MeasureString("xx", value).width;

(Better include a copy of XTextFormatter.cs in your project under a new name and modify the copy.)

Author:  Vadivelan [ Tue May 15, 2012 2:55 pm ]
Post subject:  Re: Adjusting the Letter spacing between the words

Sorry to miss the draw code.Please find it below.XTextFormatter is also used.But i can't implement your code.
Can you look at my code and help me how to implement yours ?

public override void Draw(XGraphics graph)
{
XBrush brush = GetBrush(Color);

string[] pos = Point.Split(new char[] { ',', ';' });
if (pos.Length != 2) throw new FormatException("Invalid Parameters in " + this.GetType());
XPoint point = new XPoint(Double.Parse(pos[0]), Double.Parse(pos[1]));

pos = Rect.Split(new char[] { ',', ';' });
if (pos.Length != 4) throw new FormatException("Invalid Parameters in " + this.GetType());
XRect rect = new XRect(Double.Parse(pos[0]), Double.Parse(pos[1]), Double.Parse(pos[2]), Double.Parse(pos[3]));

if (rect.Width < 1 && rect.Height < 1)
graph.DrawString(Text, Font.Create(), brush, point);
else {
XTextFormatter tf = new XTextFormatter(graph);
tf.Alignment = GetAlignment(Alignment);
tf.DrawString(Text, Font.Create(), brush, rect, XStringFormats.TopLeft); // GetStringFormat(Format));
}
}

Author:  Thomas Hoevel [ Wed May 16, 2012 3:50 pm ]
Post subject:  Re: Adjusting the Letter spacing between the words

How much space do you need? More? Less?

Replace "graph.DrawString" with an own routine that calls gfx.MeasureString and gfx.DrawString to draw the string word by word, adding the space you need.

Or modify XTextFormatter and use "tf.DrawString" for all text.
I tried the modification and it worked with the sample program.

Author:  Vadivelan [ Tue May 22, 2012 3:20 pm ]
Post subject:  Re: Adjusting the Letter spacing between the words

Hi,

Thanks for your reply.Space between the words to be reduced.

I am not able to use the method -MeasureString("",font) with my code.Can you help me how to use measurestring() in my below code.

I copied below my sample code which has a lengthy string that should be displayed in single line but not adjusting into single line.
The lengthty value is "For liability issues and other transport conditions, please refer to the Swiss General Conditions of Carriage available at any Swiss International Air Lines‘ sales office, or on SWISS.com".

// Create a new PDF document
PdfDocument document = new PdfDocument();
document.Info.Title = "Created with PDFsharp";

// Create an empty page
PdfPage page = document.AddPage();

// Get an XGraphics object for drawing
XGraphics gfx = XGraphics.FromPdfPage(page);

// Create a font
XFont font = new XFont("Verdana", 7);

string value = "For liability issues and other transport conditions, please refer to the Swiss General Conditions of Carriage available at any Swiss International Air Lines‘ sales office, or on SWISS.com";
gfx.DrawString(value, font, XBrushes.Black, new XPoint(35, 372));


// Save the document...
const string filename = "C:\\Temp\\HelloWorld.pdf";
document.Save(filename);

//Set the appropriate ContentType.
Response.ContentType = "Application/pdf";
Response.WriteFile(filename);
Response.End();

Author:  Thomas Hoevel [ Tue May 22, 2012 3:41 pm ]
Post subject:  Re: Adjusting the Letter spacing between the words

Here's the code:
Code:
// Create a font
XFont font = new XFont("Verdana", 7);

string value = "For liability issues and other transport conditions, please refer to the Swiss General Conditions of Carriage available at any Swiss International Air Lines‘ sales office, or on SWISS.com";
gfx.DrawString(value, font, XBrushes.Black, new XPoint(35, 372));

// Now draw text with reduced spacing between words:
double spaceWidth = gfx.MeasureString("x x", font).Width;
spaceWidth -= gfx.MeasureString("xx", font).Width;
spaceWidth /= 2; // Use 50% space between words

string[] words = value.Split(' ');
double x = 35;
for (int i = 0; i < words.Length; ++i)
{
  double wordWidth = gfx.MeasureString(words[i], font).Width;
  gfx.DrawString(words[i], font, XBrushes.Black, new XPoint(x, 360));
  x += wordWidth + spaceWidth;
}
It should be moved to a method that receives gfx, font, and text as parameters.

Author:  Vadivelan [ Thu May 24, 2012 8:38 am ]
Post subject:  Re: Adjusting the Letter spacing between the words

Thanks.it is working fine now where ever i am using graph.Drawstring() method.

But i need to implement the same in the below code which used XTextformatter.Drawstring().Usually this method will be called when the data is to be mulitlines.
I tried to implement Measurestring here that reducing the space width but the sentence is not wrapping into the next line.So please help me here how to use measurestring here with XTextformatter.Only this part is pending now.

public override void Draw(XGraphics graph)
{
XBrush brush = GetBrush(Color);


XRect rect = new XRect(125,400,450,40);


XTextFormatter tf = new XTextFormatter(graph);
tf.Alignment = GetAlignment(Alignment);
tf.DrawString(Text, Font.Create(), brush, rect, XStringFormats.TopLeft);
}


//////////////////////////

i tried this above method with Measurestring which is below.

public override void Draw(XGraphics graph)
{
XBrush brush = GetBrush(Color);


XRect rect = new XRect(125,400,450,40);


XTextFormatter tf = new XTextFormatter(graph);
tf.Alignment = GetAlignment(Alignment);

double spaceWidth = graph.MeasureString("x x", Font.Create()).Width;
spaceWidth -= graph.MeasureString("xx", Font.Create()).Width;
spaceWidth /= 2; // Use 50% space between words

string[] words = Text.Split(' ');
double x =125 ;
for (int i = 0; i < words.Length; ++i)
{
double wordWidth = graph.MeasureString(words[i], Font.Create()).Width;

tf.DrawString(words[i], Font.Create(), brush, new XRect(x, 400,450,40), XStringFormats.TopLeft);
x += wordWidth + spaceWidth;
}
}

Author:  Thomas Hoevel [ Thu May 24, 2012 8:58 am ]
Post subject:  Re: Adjusting the Letter spacing between the words

Make a copy of the XTextFormatter class (e.g. MyTextFormatter), look for the code that calculates the width of a space and change it to your needs:
Code:
this.spaceWidth = gfx.MeasureString("x x", value).width;
this.spaceWidth -= gfx.MeasureString("xx", value).width;

So you would add
Code:
this.spaceWidth /= 2;


Then use MyTextFormatter instead of XTextFormatter when you need reduced spacing.

Author:  Vadivelan [ Thu May 24, 2012 12:33 pm ]
Post subject:  Re: Adjusting the Letter spacing between the words

Where to find MyTextFormatter ?

Can you place your code using Textformatter and rectangle ?

Author:  Thomas Hoevel [ Thu May 24, 2012 12:37 pm ]
Post subject:  Re: Adjusting the Letter spacing between the words

Vadivelan wrote:
Where to find MyTextFormatter?
Make a copy of the XTextFormatter class (e.g. as MyTextFormatter).

Author:  Vadivelan [ Thu May 24, 2012 12:52 pm ]
Post subject:  Re: Adjusting the Letter spacing between the words

Sorry Thomas.

I did not get your point again.

Can you explain me little bit ?

Author:  Thomas Hoevel [ Thu May 24, 2012 1:01 pm ]
Post subject:  Re: Adjusting the Letter spacing between the words

Make a copy of the XTextFormatter class (e.g. as MyTextFormatter), look for the code that calculates the width of a space and change it to your needs:
Code:
this.spaceWidth = gfx.MeasureString("x x", value).width;
this.spaceWidth -= gfx.MeasureString("xx", value).width;

So you would add here:
Code:
this.spaceWidth /= 2;


Then use MyTextFormatter instead of XTextFormatter when you need reduced spacing for multi-line text.

Author:  Vadivelan [ Thu May 24, 2012 2:04 pm ]
Post subject:  Re: Adjusting the Letter spacing between the words

Can i chat with you to close it quickly ?
Please let me know.

Author:  Thomas Hoevel [ Tue May 29, 2012 8:24 am ]
Post subject:  Re: Adjusting the Letter spacing between the words

Vadivelan wrote:
Can i chat with you to close it quickly?
Chat is an option if you purchase Premium Support for PDFsharp/MigraDoc.
I'm sorry for the late response, but I wasn't at the office on Friday, and Monday was a bank holiday here.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/