PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Sun Jun 30, 2024 4:21 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Sun Mar 13, 2011 8:30 pm 
Offline

Joined: Sun Mar 13, 2011 8:24 pm
Posts: 2
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);
                    }


Top
 Profile  
Reply with quote  
PostPosted: Mon Mar 14, 2011 8:37 am 
Offline
PDFsharp Guru
User avatar

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

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Mon Mar 14, 2011 2:33 pm 
Offline

Joined: Sun Mar 13, 2011 8:24 pm
Posts: 2
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);
                            }

                        }


Top
 Profile  
Reply with quote  
PostPosted: Wed Mar 16, 2011 11:23 pm 
Offline
Supporter
User avatar

Joined: Thu May 27, 2010 7:40 pm
Posts: 59
Location: New Hampshire, USA
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


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 20 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