private void Form1_Closed(object sender, System.EventArgs e) { // Create a PDF document object PDFDocument doc = new PDFDocument(); // Creates a PDF page PDFPage page = new PDFPage(this.Width, this.Heigh,PDFMeasurementUnit.Points); page.MeasurementUnit = PDFMeasurementUnit.Points; // Parses controls on the form for (int i = 0; i < this.Controls.Count; i++) {
Label label = this.Controls[i] as Label; if (label != null) { // Renders labels as formatted text page.WriteText(label.Text, new PDFFont(new Font(label.Font.Name, label.Font.Size), false), label.Left, label.Top); continue; }
TextBox text = this.Controls[i] as TextBox; if (text != null) { // Renders text boxes as text form fields TextBox text = this.Controls[i] as TextBox;
PDFFormTextField field = new PDFFormTextField(); field.BackgroundColor = text.BackColor; field.BorderColor = Color.Black; field.Font = new PDFFont(new Font(text.Font.Name, text.Font.Size), false); field.FieldName = text.Name; field.BorderWidth = 1; field.FieldValue = text.Text; field.Rectangle = new RectangleF(text.Left, text.Top, text.Width, text.Height); field.Alignment = PDFTextAlignment.Left; field.Multiline = text.Multiline; field.Visible = text.Visible; page.AddFormField(field); continue; }
Button button = this.Controls[i] as Button; if (button != null) { // Renders buttons as push button form fields Button button = this.Controls[i] as Button;
PDFFormPushButton field = new PDFFormPushButton(); field.BackgroundColor = button.BackColor; field.BorderColor = Color.Black; field.Font = new PDFFont(new Font(button.Font.Name, button.Font.Size), false); field.FieldName = button.Name; field.BorderWidth = 1; field.Rectangle = new RectangleF(button.Left, button.Top, button.Width, button.Height); field.NormalCaption = button.Text; field.Visible = button.Visible; page.AddFormField(field); continue; } }
// Adds page to PDF document doc.AddPage(page); // Writes PDF document to file and displays it doc.OpenAfterCreate = true; doc.ApplyFormAppearances = true; doc.Save("Output.pdf"); doc.Close(); }
Abdullahi_1 0 Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.