PDFsharp & MigraDoc Foundation
http://forum.pdfsharp.de/

Text's location in PDF is not exact it is off a little...
http://forum.pdfsharp.de/viewtopic.php?f=2&t=1579
Page 1 of 1

Author:  cpphacker69 [ Sun Mar 13, 2011 8:30 pm ]
Post subject:  Text's location in PDF is not exact it is off a little...

I need to draw strings in my PDF document, by iterating through all the text box controls in the panel control and then use the textbox's text and location for the parameters in the DrawString subroutine. But the location of the text in the PDF is off a little. I have read other forum posts, but it still does not solve my problem. Here is the code below:
Code:
try
            {
               
                if(numofpages > 1)
                newPageToolStripMenuItem.PerformClick();
                SaveFileDialog save = new SaveFileDialog();
                save.Title = "Save File As A Portable Document Format";
                save.Filter = "Portable Document Format | *.pdf";
                if (save.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    changed = false;
                    // This section shows the currently save PDF on the title of the window.
                    this.Text += " - " + save.FileName;
                    this.GetText = this.Text;
                    if (numofpages == 1)
                    {
                        LineProperties prop;
                        ImgProperties iprop;
                        foreach (Control c in panel1.Controls)
                        {
                            if (c.GetType() == typeof(RichTextBox))
                            {

                                prop.point = c.Location;
                                prop.size = c.Size;
                                prop.text = c.Text;
                                prop.font = c.Font;
                                prop.color = c.ForeColor;
                                lineprop.Add(prop);

                            }
                            else if (c.GetType() == typeof(PictureBox))
                            {
                                PictureBox px = (PictureBox)c;
                                iprop.img = px.Image;
                                iprop.location = px.ImageLocation;
                                iprop.point = px.Location;
                                iprop.size = px.Size;
                                imgprop.Add(iprop);
                            }

                        }
                 
                       
                        Graphics g = panel1.CreateGraphics();
                        page = doc.AddPage(); // Adds another page to the pdf document;
                       
                        page.Width = XUnit.FromInch(Convert.ToDouble(p.width.Value)); /*/ g.DpiX*/; // Gets the width by convert pixels to inches using DPI of the computer screen.
                        page.Height = XUnit.FromInch(Convert.ToDouble( p.height.Value)); /*/ g.DpiY*/; // Same above, but for the height.
                        XGraphics gfx = XGraphics.FromPdfPage(page);
                        XStringFormat form = new XStringFormat();
                        form.Alignment = XStringAlignment.Near;
                        form.LineAlignment = XLineAlignment.Near;
                        form.FormatFlags = XStringFormatFlags.MeasureTrailingSpaces;
                        foreach (LineProperties pr in lineprop.ToArray())
                        {

                            if (pr.font.Style == FontStyle.Regular)
                            {

                                float dpi = gfx.Graphics.DpiX;
                                XFont font = new XFont(pr.font.Name, (double)pr.font.Size, XFontStyle.Regular);
                                gfx.DrawString(pr.text, font, new XSolidBrush(pr.color), pr.point.X, pr.point.Y, form);

                            }
                            else if (pr.font.Style == FontStyle.Italic)
                            {
                                XFont font = new XFont(pr.font.Name, (double)pr.font.Size, XFontStyle.Italic);
                                gfx.DrawString(pr.text, font, new XSolidBrush(pr.color), pr.point, form);
                            }
                            else if (pr.font.Style == FontStyle.Bold)
                            {
                                XFont font = new XFont(pr.font.Name, (double)pr.font.Size, XFontStyle.Bold);
                                gfx.DrawString(pr.text, font, new XSolidBrush(pr.color), pr.point, form);
                            }
                            else if (pr.font.Style == FontStyle.Strikeout)
                            {
                                XFont font = new XFont(pr.font.Name, (double)pr.font.Size, XFontStyle.Strikeout);
                                gfx.DrawString(pr.text, font, new XSolidBrush(pr.color), pr.point, form);
                            }
                            else if (pr.font.Style == FontStyle.Underline)
                            {
                                XFont font = new XFont(pr.font.Name, (double)pr.font.Size, XFontStyle.Underline);
                                gfx.DrawString(pr.text, font, new XSolidBrush(pr.color), pr.point, form);
                            }
                        }
                        foreach (ImgProperties ip in imgprop.ToArray())
                        {
                            XImage image = XImage.FromFile(ip.location);
                           
                            gfx.DrawImage(image, ip.point.X, ip.point.Y, XUnit.FromPoint( image.PointWidth), XUnit.FromPoint( image.PointHeight));

                        }
                        if (s.password.Text != "")
                        {
                            PdfSharp.Pdf.Security.PdfSecuritySettings settings = doc.SecuritySettings;
                            settings.UserPassword = s.password.Text;
                            settings.PermitAnnotations = Convert.ToBoolean(s.anno.Text);
                            settings.PermitPrint = Convert.ToBoolean(s.print.Text);
                            settings.PermitModifyDocument = Convert.ToBoolean(s.modifi.Text);
                            settings.PermitExtractContent = Convert.ToBoolean(s.extract.Text);
                            settings.PermitAccessibilityExtractContent = Convert.ToBoolean(s.access.Text);
                            settings.PermitFullQualityPrint = Convert.ToBoolean(s.full.Text);
                        }
                        doc.Save(save.FileName);
                        doc.Close();
                        page.Close();
                        gfx.Dispose();
                        System.Diagnostics.Process.Start(save.FileName);
                    }

Author:  Thomas Hoevel [ Mon Mar 14, 2011 8:37 am ]
Post subject:  Re: Text's location in PDF is not exact it is off a little...

cpphacker69 wrote:
Here is the code below:

The code doesn't help a bit.
Where do you want to have the text? Where is the text?
Is it a problem in the PDF (you can check positions with Adobe Acrobat) or on the printout (make sure you print at 100 % without scaling)?

Author:  cpphacker69 [ Mon Mar 14, 2011 2:33 pm ]
Post subject:  Re: Text's location in PDF is not exact it is off a little...

Okay, the text are located on different textboxes that are child controls of a panel control. So I iterate through all the controls in the panel. If the control is a textbox control it obtains the text, position, and size in a structure called LineProperities. The text is displaying correctly, even the font, but the position of the text is not like it is relative to the panel. Both the panel and PDF document are both in standard letter size in inches. Here is the code snippet the shows what I described:

Code:
if (numofpages == 1)
                    {
                        LineProperties prop;
                        ImgProperties iprop;
                        foreach (Control c in panel1.Controls)
                        {
                            if (c.GetType() == typeof(RichTextBox))
                            {

                                prop.point = c.Location;
                                prop.size = c.Size;
                                prop.text = c.Text;
                                prop.font = c.Font;
                                prop.color = c.ForeColor;
                                lineprop.Add(prop);

                            }
                            else if (c.GetType() == typeof(PictureBox))
                            {
                                PictureBox px = (PictureBox)c;
                                iprop.img = px.Image;
                                iprop.location = px.ImageLocation;
                                iprop.point = px.Location;
                                iprop.size = px.Size;
                                imgprop.Add(iprop);
                            }

                        }

Author:  jeffhare [ Wed Mar 16, 2011 11:23 pm ]
Post subject:  Re: Text's location in PDF is not exact it is off a little...

It's helpful to include a snapshot picture or pdf of what you're getting out and explain what you expect to see.

Since the code example isn't complete enough to build, it's really hard to help out.

Just my .02
-Jeff

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