Wed Jun 10, 2015 8:23 pm
Hi Sweety,
I needed to be able to view and edit PDF forms in my application.
The approach that I used is as follows:
1. Open the PDF form and save its pages as images.
2. Load the images onto a picture boxes onto tabpages in a tabcontrol.
3. Overlay windows form controls on each image over top of where the PDF form controls are located.
4. Allow the user to make edits on the windows form controls.
5. Save the windows form data to the PDF form.
6. Save the PDF.
I created the following 2 methods to achieve this:
1. LoadControls
2. savePage
private void LoadControls()
{
if (File.Exists(_FileName))
{
doc.LoadFromFile(_FileName);
Image formImage = null;
for (int i = 0; i < doc.Pages.Count && i < 6; i++)
{
while(doc.Pages.Count > tcBody.TabPages.Count)
{
tcBody.TabPages.Add(string.Format("Page {0}", tcBody.TabPages.Count + 1));
}
tcBody.TabPages[i].Width = Convert.ToInt32(doc.Pages[i].ActualSize.Width);
tcBody.TabPages[i].Height = Convert.ToInt32(doc.Pages[i].ActualSize.Height);
tcBody.TabPages[i].AutoScrollMinSize = new Size(Convert.ToInt32(doc.Pages[i].ActualSize.Width),
Convert.ToInt32(doc.Pages[i].ActualSize.Height));
doc.PageScaling = PdfPrintPageScaling.ActualSize;
float scaleFactorX, scaleFactorY;
formImage = doc.SaveAsImage(i, PdfImageType.Bitmap);
PictureBox pb = GetPB(i);
pb.Dock = DockStyle.Fill;
pb.Image = formImage;
pb.SendToBack();
pb.Visible = true;
pb.Height = formImage.Height;
pb.Width = formImage.Width;
tcBody.TabPages[i].Controls.Add(pb);
//Get form
formWidget = doc.Form as PdfFormWidget;
if (formWidget == null)
{
return;
}
using (Graphics g = GetPB(i).CreateGraphics())
{
scaleFactorX = g.DpiX / 72;
scaleFactorY = g.DpiY / 72;
}
for (int j = 0; j < formWidget.FieldsWidget.List.Count; j++)
{
PdfField field = formWidget.FieldsWidget.List[j] as PdfField;
int pageNum = doc.Pages.IndexOf(field.Page);
//Fill the data for textBoxField
if (field is PdfTextBoxFieldWidget)
{
PdfTextBoxFieldWidget textBoxField = field as PdfTextBoxFieldWidget;
TextBox tb = new TextBox();
tb.Font = new System.Drawing.Font(textBoxField.Font.Name, Convert.ToInt32(Convert.ToDouble(textBoxField.Size.Height) * 0.65)); // / scaleFactorX);
tb.Name = textBoxField.Name;
tb.Width = Convert.ToInt32(textBoxField.Size.Width * scaleFactorX) - 2;
tb.Multiline = true;
//tb.BorderStyle = BorderStyle.None;
tb.Height = Convert.ToInt32(textBoxField.Size.Height * scaleFactorY);
tb.Text = textBoxField.Text;
tb.Location = new Point(Convert.ToInt32(textBoxField.Location.X * scaleFactorX) + 1,
Convert.ToInt32(textBoxField.Location.Y * scaleFactorY));
AddToPB(tb, pageNum);
}
else if (field is PdfListBoxWidgetFieldWidget)
{
PdfListBoxWidgetFieldWidget listBoxField = field as PdfListBoxWidgetFieldWidget;
TextBox tb = new TextBox();
//tb.Font = new System.Drawing.Font(listBoxField.Font.Name, Convert.ToInt32(Convert.ToDouble(listBoxField.Size.Height) * 0.7));
tb.Name = listBoxField.Name;
tb.Width = Convert.ToInt32(listBoxField.Size.Width * scaleFactorX) - 2;
tb.Multiline = true;
tb.BorderStyle = BorderStyle.None;
tb.Height = Convert.ToInt32(listBoxField.Size.Height * scaleFactorY);
tb.Location = new Point(Convert.ToInt32(listBoxField.Location.X * scaleFactorX) + 2,
Convert.ToInt32(listBoxField.Location.Y * scaleFactorY));
foreach (PdfListWidgetItem s in listBoxField.Values)
{
tb.Text += string.Format("{0}\r\n", s.Value);
}
AddToPB(tb, pageNum);
}
else if (field is PdfComboBoxWidgetFieldWidget)
{
PdfComboBoxWidgetFieldWidget comBoxField = field as PdfComboBoxWidgetFieldWidget;
ComboBox cmb = new ComboBox();
cmb.Font = new System.Drawing.Font(comBoxField.Font.Name, Convert.ToInt32(Convert.ToDouble(comBoxField.Size.Height) * 0.7));
cmb.Name = comBoxField.Name;
cmb.Width = Convert.ToInt32(comBoxField.Size.Width * scaleFactorX);
cmb.Height = Convert.ToInt32(comBoxField.Size.Height * scaleFactorY);
cmb.Location = new Point(Convert.ToInt32(comBoxField.Location.X * scaleFactorX),
Convert.ToInt32(comBoxField.Location.Y * scaleFactorY));
foreach (PdfListWidgetItem s in comBoxField.Values)
{
cmb.Items.Add(s.Value);
}
AddToPB(cmb, pageNum);
}
else if (field is PdfCheckBoxWidgetFieldWidget)
{
PdfCheckBoxWidgetFieldWidget checkBoxField = field as PdfCheckBoxWidgetFieldWidget;
CheckBox tb = new CheckBox();
tb.Name = checkBoxField.Name;
tb.Width = Convert.ToInt32(checkBoxField.Size.Width * scaleFactorX);
tb.Height = Convert.ToInt32(checkBoxField.Size.Height * scaleFactorY);
tb.Location = new Point(Convert.ToInt32(checkBoxField.Location.X * scaleFactorX),
Convert.ToInt32(checkBoxField.Location.Y * scaleFactorY));
AddToPB(tb, pageNum);
}
else if (field is PdfRadioButtonListFieldWidget)
{
PdfRadioButtonListFieldWidget radioButtonField = field as PdfRadioButtonListFieldWidget;
foreach (PdfRadioButtonWidgetItem item in radioButtonField.WidgetItems)
{
RadioButton rb = new RadioButton();
rb.Name = item.Value;
rb.Tag = field.Name;
rb.Checked = item.Checked;
rb.Location = new Point(Convert.ToInt32(item.Location.X * scaleFactorX),
Convert.ToInt32(item.Location.Y * scaleFactorY));
rb.BackColor = Color.Transparent;
AddToPB(rb, pageNum);
}
}
}
}
}
else
{
l.LogError(string.Format("Couldn't Find {0}", _FileName), null);
}
}
private void savePage(PictureBox pb, PdfFormWidget formWidget)
{
foreach (Control c in pb.Controls)
{
if (c.GetType().Name == "TextBox")
{
TextBox tb = (TextBox)c;
PdfField field = formWidget.FieldsWidget[tb.Name] as PdfField;
if (field.GetType().Name == "PdfTextBoxFieldWidget")
{
PdfTextBoxFieldWidget textBoxField = field as PdfTextBoxFieldWidget;
textBoxField.Text = tb.Text;
}
}
else if (c.GetType().Name == "CheckBox")
{
CheckBox cb = (CheckBox)c;
PdfField field = formWidget.FieldsWidget[cb.Name] as PdfField;
if (field.GetType().Name == "PdfCheckBoxWidgetFieldWidget")
{
PdfCheckBoxWidgetFieldWidget checkBoxField = field as PdfCheckBoxWidgetFieldWidget;
checkBoxField.Checked = cb.Checked;
//checkBoxField.BorderColor = Color.Black;
}
}
else if (c.GetType().Name == "ComboBox")
{
ComboBox cb = (ComboBox)c;
PdfField field = formWidget.FieldsWidget[cb.Name] as PdfField;
if (field.GetType().Name == "PdfComboBoxWidgetFieldWidget")
{
PdfComboBoxWidgetFieldWidget checkBoxField = field as PdfComboBoxWidgetFieldWidget;
if (cb.SelectedIndex != -1)
{
int[] items = { cb.SelectedIndex };
checkBoxField.SelectedIndex = items;
}
}
}
else if (c.GetType().Name == "RadioButton")
{
RadioButton rb = (RadioButton)c;
PdfRadioButtonListFieldWidget field = (PdfRadioButtonListFieldWidget)formWidget.FieldsWidget[string.Format("{0}", rb.Tag)];
foreach (PdfRadioButtonWidgetItem item in field.WidgetWidgetItems)
{
if (item.Value == rb.Name)
{
item.Checked = rb.Checked;
}
}
}
else
{
MessageBox.Show(c.GetType().Name);
}
}
}
This approach works great on single page PDFs but fails to save the data back to the PDF form on multiple page forms.
Please let me know if there is a better way of doing this.
Thanks for your help,
Grant