public void WorkerProcess() { SetPhase("Starting"); Document activeDoc = Globals.BookPublishingStartup.Application.ActiveDocument; SetFields(activeDoc.ContentControls.Count); SetShapes(activeDoc.Shapes.Count); SetPhase("Getting Inline Word fields"); WordFieldsInfo.Get(activeDoc, wordFields, ref keepGoing); SetPhase("Checking Shapes for fields"); WordFieldsInfo.GetFromShapes(activeDoc, wordFields, ref keepGoing, SetCurrentShape); string tempFileName = System.IO.Path.GetTempFileName(); if (keepGoing) { SetPhase("Writing PDF"); PDFForm.SaveDocToPDF(activeDoc, tempFileName); } if (keepGoing) { SetPhase("Adding Fields to PDF"); PDFForm.AddFieldsToPDF(tempFileName, txtFileName.Text, wordFields, ref keepGoing); } if (keepGoing) { SetPhase("Done"); } else { SetPhase("Stopped"); } SetGoEnabled(); }
public static bool GetFromShapes(Document doc, WordFieldsInfo wfi, ref bool keepGoing, Action <int> updateStatus) { Microsoft.Office.Interop.Word.Application app = doc.Application; // Process fields that may be embedded in shapes int currentShape = 0; foreach (Shape shp in doc.Shapes) { currentShape++; if (updateStatus != null) { updateStatus(currentShape); } if (shp.Type == Microsoft.Office.Core.MsoShapeType.msoTextBox) { shp.Select(); app.Selection.ShapeRange.TextFrame.TextRange.Select(); if (app.Selection.ContentControls.Count == 1) { WordFieldInfo field = new WordFieldInfo(); wfi.Fields.Add(field); field.GetFromDocumentShape(doc, app.Selection.ContentControls[1], shp); } } } return(true); }
public static WordFieldsInfo Get(Document doc, ISynchronizeInvoke sync) { bool keepGoing = true; WordFieldsInfo wfi = new WordFieldsInfo(sync); bool finished = Get(doc, wfi, ref keepGoing); return(wfi); }
public static bool CreatePDFForm(Document wordDoc, string targetFileName, ISynchronizeInvoke sync, ref bool keepGoing) { string tempName = Path.GetTempFileName(); WordFieldsInfo wfi = WordFieldsInfo.Get(wordDoc, sync); if (SaveDocToPDF(wordDoc, tempName)) { AddFieldsToPDF(tempName, targetFileName, wfi, ref keepGoing); } return(false); }
public static bool Get(Document doc, WordFieldsInfo wfi, ref bool keepGoing) { Microsoft.Office.Interop.Word.Application app = doc.Application; doc.Repaginate(); foreach (ContentControl cc in doc.ContentControls) { if (!keepGoing) { return(false); } try { WordFieldInfo field = new WordFieldInfo(); wfi.Fields.Add(field); field.GetFromDocument(doc, cc); } catch (Exception ex) { System.Diagnostics.Trace.WriteLine($"{ex}"); } } return(true); }
public static bool AddFieldsToPDF(string fileName, string targetFileName, WordFieldsInfo wfi, ref bool keepGoing) { try { using (Stream stream = File.Open(fileName, FileMode.Open, FileAccess.ReadWrite, FileShare.None)) using (PdfLoadedDocument pdfDoc = new PdfLoadedDocument(stream)) { if (pdfDoc.Form == null) { pdfDoc.CreateForm(); } else { // Clear the existing Form while (pdfDoc.Form.Fields.Count > 0) { pdfDoc.Form.Fields.RemoveAt(0); } } foreach (WordFieldInfo fi in wfi.Fields) { if (!keepGoing) { return(false); } fi.Status = "Adding to PDF"; if (fi.Page <= pdfDoc.Pages.Count) { PdfPageBase page = pdfDoc.Pages[fi.Page - 1]; switch (fi.FieldType) { case FieldType.DropDownList: PdfComboBoxField cboField = new PdfComboBoxField(page, GetFieldName(fi)); cboField.Bounds = new System.Drawing.RectangleF((float)fi.X, (float)fi.Y, (float)fi.Width, (float)fi.Height); foreach (KeyValuePair <string, string> kvp in fi.Options) { cboField.Items.Add(new PdfListFieldItem(kvp.Key, kvp.Value)); } cboField.SelectedIndex = 0; pdfDoc.Form.Fields.Add(cboField); break; case FieldType.SingleLine: case FieldType.MultiLine: PdfTextBoxField txtField = new PdfTextBoxField(page, GetFieldName(fi)); txtField.BackColor = new Syncfusion.Pdf.Graphics.PdfColor((float)0xdd); txtField.Bounds = new System.Drawing.RectangleF((float)fi.X, (float)fi.Y, (float)fi.Width, (float)fi.Height); txtField.Multiline = true; txtField.Scrollable = true; txtField.SpellCheck = true; pdfDoc.Form.Fields.Add(txtField); break; case FieldType.Checkbox: PdfCheckBoxField chkField = new PdfCheckBoxField(page, GetFieldName(fi)); chkField.Bounds = new System.Drawing.RectangleF((float)fi.X, (float)fi.Y, (float)fi.Width, (float)fi.Height); pdfDoc.Form.Fields.Add(chkField); break; default: System.Diagnostics.Trace.WriteLine($"{fi.FieldType}"); break; } fi.Status = "Added to PDF"; } else { throw new IndexOutOfRangeException($"Field {fi.FieldType} at {fi.Page} ({fi.X}, {fi.Y}) is not on a valid page in the PDF"); } } using (Stream outStream = File.Open(targetFileName, FileMode.Create, FileAccess.ReadWrite, FileShare.None)) { pdfDoc.Save(outStream); } pdfDoc.Close(true); } return(true); } catch (Exception ex) { throw; } }
public static bool AddFieldsToPDF(string fileName, string targetFileName, WordFieldsInfo wfi) { bool keepGoing = true; return(AddFieldsToPDF(fileName, targetFileName, wfi, ref keepGoing)); }
public FieldStatus() { InitializeComponent(); wordFields = new WordFieldsInfo(this); SetDataBinding(wordFields.Fields); }