PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Sat Apr 27, 2024 6:48 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 9 posts ] 
Author Message
PostPosted: Tue May 27, 2014 11:45 am 
Offline

Joined: Fri Sep 06, 2013 7:01 am
Posts: 27
Location: Cologne, Germany
Dear community
I try create pdf file with content from rtf-string
But positions of parts of line are not correct
Here is my example
Code:
......
PdfSharp.Pdf.PdfDocument xDoc = new PdfSharp.Pdf.PdfDocument();
            xDoc.Pages.Add(new PdfSharp.Pdf.PdfPage() { Width = 500, Height = 750 });

            xDoc.Info.Author = “Autor";
            xDoc.Info.CreationDate = DateTime.Now;
            xDoc.Info.Creator = "Creator";
            xDoc.Info.Keywords = "KW1, KW2, KW3";
            xDoc.Info.Subject = "Subject";
            xDoc.Info.Title = "Title";

            PdfSharp.Pdf.PdfPage xPage = xDoc.Pages[0];

            PdfSharp.Drawing.XGraphics gfx = PdfSharp.Drawing.XGraphics.FromPdfPage(xPage);
            PdfSharp.Drawing.XPen pen = new PdfSharp.Drawing.XPen(PdfSharp.Drawing.XColor.FromKnownColor(KnownColor.Red), 0.5);
            PdfSharp.Drawing.Layout.XTextFormatter tf = new PdfSharp.Drawing.Layout.XTextFormatter(gfx);
            tf.Alignment = PdfSharp.Drawing.Layout.XParagraphAlignment.Left;
           

            ////myRichTextBox contain text Test1_Test2_Test3

            myRichTextBox.Select(0, 6);
            string str0 = myRichTextBox.SelectedText;
            Font f0 = myRichTextBox.SelectionFont;
            Point p0 = myRichTextBox.GetPositionFromCharIndex(0);
            SizeF size0 = gfx.Graphics.MeasureString(str0, f0);
            PdfSharp.Drawing.XRect xR0 = new PdfSharp.Drawing.XRect();
            xR0.Width = size0.Width;
            xR0.Height = size0.Height;
            xR0.X = p0.X;
            xR0.Y = p0.Y;
           
            d2.RichTextBox.Select(6, 6);
            string str1 = d2.RichTextBox.SelectedText;
            Font f1 = myRichTextBox.SelectionFont;
            Point p1 = myRichTextBox.GetPositionFromCharIndex(6);
            SizeF size1 = gfx.Graphics.MeasureString(str1, f1);
            PdfSharp.Drawing.XRect xR1 = new PdfSharp.Drawing.XRect();
            xR1.Width = size1.Width;
            xR1.Height = size1.Height;
            xR1.X = p1.X;
            xR1.Y = p1.Y;

            myRichTextBox.Select(12, 5);
            string str2 = myRichTextBox.SelectedText;
            Font f2 = myRichTextBox.SelectionFont;
            Point p2 = myRichTextBox.GetPositionFromCharIndex(12);
            SizeF size2 = gfx.Graphics.MeasureString(str2, f2);
            PdfSharp.Drawing.XRect xR2 = new PdfSharp.Drawing.XRect();
            xR2.Width = size2.Width;
            xR2.Height = size2.Height;
            xR2.X = p2.X;
            xR2.Y = p2.Y;

            PdfSharp.Drawing.XFont xf0 = new PdfSharp.Drawing.XFont(f0.FontFamily.Name, f0.Size, (PdfSharp.Drawing.XFontStyle)f0.Style);
            PdfSharp.Drawing.XFont xf1 = new PdfSharp.Drawing.XFont(f1.FontFamily.Name, f1.Size, (PdfSharp.Drawing.XFontStyle)f1.Style);
            PdfSharp.Drawing.XFont xf2 = new PdfSharp.Drawing.XFont(f2.FontFamily.Name, f2.Size, (PdfSharp.Drawing.XFontStyle)f2.Style);
            PdfSharp.Drawing.XBrush xBrush = new System.Drawing.SolidBrush(Color.Red);
            PdfSharp.Drawing.XBrush xBrushRect0 = new System.Drawing.SolidBrush(Color.LightBlue);
            PdfSharp.Drawing.XBrush xBrushRect1 = new System.Drawing.SolidBrush(Color.LightSlateGray);
            PdfSharp.Drawing.XBrush xBrushRect2 = new System.Drawing.SolidBrush(Color.LightSkyBlue);

            gfx.DrawRectangle(xBrushRect0, xR0);
            gfx.DrawRectangle(xBrushRect1, xR1);
            gfx.DrawRectangle(xBrushRect2, xR2);

            tf.DrawString(str0, xf0, xBrush, xR0);
            tf.DrawString(str1, xf1, xBrush, xR1);
            tf.DrawString(str2, xf2, xBrush, xR2);


            xDoc.PageLayout = PdfSharp.Pdf.PdfPageLayout.SinglePage;
            xDoc.Save(fileFN);
            xDoc.Close();

......


If I draw same parts with same positions on WinForm all is correct. But in pdf I see that distances between parts are more as needed. Is one bug or is my mistake?
Thanks in advance


Top
 Profile  
Reply with quote  
PostPosted: Tue May 27, 2014 12:55 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3096
Location: Cologne, Germany
nav3000 wrote:
But positions of parts of line are not correct
They are not the same - but maybe they are correct.
The screen is a raster device with low resolution and maybe some rounding. PDF is a vector format without rounding.
Or maybe kerning is treated differently.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Tue May 27, 2014 1:16 pm 
Offline

Joined: Fri Sep 06, 2013 7:01 am
Posts: 27
Location: Cologne, Germany
I say that distances between parts MORE AS NEEDED
I have split text to 3 parts and draw every part alone.
You can that self see from my example. You can self draw this parts on WinForms and equal with pdf. In reality I have no distances - that also during drawing on WinForm. On WinForms I see Test1_Test2_Test3. On Pdf page I see GREAT DISTANCES between Test1_ and Test2_ and Test3
Should I send them screenshot?


Top
 Profile  
Reply with quote  
PostPosted: Tue May 27, 2014 1:33 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3096
Location: Cologne, Germany
The same words have different widths on different devices.

Get the position for every character from RichTextBox and the final result will look better.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Tue May 27, 2014 5:46 pm 
Offline

Joined: Fri Sep 06, 2013 7:01 am
Posts: 27
Location: Cologne, Germany
Maybe you do not understand that problem which I tall. Of cource I know that same words have different widths on different devices. Did you try to execute that code which is given in my post? If you do not have such an opportunity that I can send you the pdf-file and screenshot of WinForm (but not early as on Friday) - now only sketch by Paint. On left side of my sketch can you see as is on winForm an on right side - on pdf


Attachments:
Sketch.png
Sketch.png [ 7.02 KiB | Viewed 8226 times ]
Top
 Profile  
Reply with quote  
PostPosted: Wed May 28, 2014 7:45 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3096
Location: Cologne, Germany
I understand the problem.
I didn't try to run the code snippet because it is just a code snippet and takes time to turn it into a running program. And the snippet doesn't even show which OS and which font you are using - or which build of PDFsharp.

Are you sure that RichTextBox is correct and PDFsharp is wrong?
Maybe they are both right - just in different contexts.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Wed May 28, 2014 12:22 pm 
Offline

Joined: Fri Sep 06, 2013 7:01 am
Posts: 27
Location: Cologne, Germany
I am sure that, I make all correct with RichTextBox, but I am not sure, that I make all correct with PDFSharp. My question at the beginning of this post
Quote:
Is one bug or is my mistake?

I make test application, but on Friday


Top
 Profile  
Reply with quote  
PostPosted: Fri May 30, 2014 8:57 am 
Offline

Joined: Fri Sep 06, 2013 7:01 am
Posts: 27
Location: Cologne, Germany
Here is my test example


Attachments:
PDFSharpAndRtf__Test1.zip [23.54 KiB]
Downloaded 375 times
Top
 Profile  
Reply with quote  
PostPosted: Thu Jul 17, 2014 6:37 am 
Offline

Joined: Fri Sep 06, 2013 7:01 am
Posts: 27
Location: Cologne, Germany
Problem is resolved.
Cause - resolution in pdf 72 dpi, in gdi+ 96 dpi


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

All times are UTC


Who is online

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