PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Mon Jul 15, 2024 10:35 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 16 posts ] 
Author Message
PostPosted: Mon May 14, 2012 2:08 pm 
Offline

Joined: Mon Sep 12, 2011 1:09 pm
Posts: 23
Is it possible to adjust the Letter spacing between the words using the pdfSharp.dll ?


Top
 Profile  
Reply with quote  
PostPosted: Mon May 14, 2012 3:33 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3101
Location: Cologne, Germany
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.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Tue May 15, 2012 12:13 pm 
Offline

Joined: Mon Sep 12, 2011 1:09 pm
Posts: 23
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.


Top
 Profile  
Reply with quote  
PostPosted: Tue May 15, 2012 12:36 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3101
Location: Cologne, Germany
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.)

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Tue May 15, 2012 2:55 pm 
Offline

Joined: Mon Sep 12, 2011 1:09 pm
Posts: 23
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));
}
}


Top
 Profile  
Reply with quote  
PostPosted: Wed May 16, 2012 3:50 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3101
Location: Cologne, Germany
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.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Tue May 22, 2012 3:20 pm 
Offline

Joined: Mon Sep 12, 2011 1:09 pm
Posts: 23
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();


Top
 Profile  
Reply with quote  
PostPosted: Tue May 22, 2012 3:41 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3101
Location: Cologne, Germany
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.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Thu May 24, 2012 8:38 am 
Offline

Joined: Mon Sep 12, 2011 1:09 pm
Posts: 23
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;
}
}


Top
 Profile  
Reply with quote  
PostPosted: Thu May 24, 2012 8:58 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3101
Location: Cologne, Germany
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.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Thu May 24, 2012 12:33 pm 
Offline

Joined: Mon Sep 12, 2011 1:09 pm
Posts: 23
Where to find MyTextFormatter ?

Can you place your code using Textformatter and rectangle ?


Top
 Profile  
Reply with quote  
PostPosted: Thu May 24, 2012 12:37 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3101
Location: Cologne, Germany
Vadivelan wrote:
Where to find MyTextFormatter?
Make a copy of the XTextFormatter class (e.g. as MyTextFormatter).

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Thu May 24, 2012 12:52 pm 
Offline

Joined: Mon Sep 12, 2011 1:09 pm
Posts: 23
Sorry Thomas.

I did not get your point again.

Can you explain me little bit ?


Top
 Profile  
Reply with quote  
PostPosted: Thu May 24, 2012 1:01 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3101
Location: Cologne, Germany
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.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Thu May 24, 2012 2:04 pm 
Offline

Joined: Mon Sep 12, 2011 1:09 pm
Posts: 23
Can i chat with you to close it quickly ?
Please let me know.


Top
 Profile  
Reply with quote  
PostPosted: Tue May 29, 2012 8:24 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3101
Location: Cologne, Germany
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.

_________________
Regards
Thomas Hoevel
PDFsharp Team


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

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 41 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