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);
}