public void PopulateControl(string tag, string value) { var contentControls = _document.SelectContentControlsByTag(tag); if (contentControls == null) { return; } foreach (WordOM.ContentControl contentControl in contentControls) { if (contentControl.Type == WordOM.WdContentControlType.wdContentControlCheckBox) { if (!String.IsNullOrEmpty(value)) { contentControl.Checked = Boolean.Parse(value); } } else { value = value.Replace("\r\n", ((char)11).ToString()); contentControl.Range.Text = value; } if (value != null) { contentControl.SetPlaceholderText(null, null, value); } } }
private void InsertOrRemoveImage(Word.Document wordDoc, string title, Guid entityRefId, List <ImageHouse> imageHouses) { var img = imageHouses.Where(w => w.EntityRefGuid == entityRefId).FirstOrDefault(); if (img != null) { var ext = img.Entity == "signature" ? "png" : "jpeg"; string imagePath = $"{AppSettings.ImageUploadPath}{img.ImageFileGuid}.{ext}"; try { if (!string.IsNullOrWhiteSpace(imagePath)) { Word.Range rngPic = wordDoc.SelectContentControlsByTag(title)[1].Range; rngPic.InlineShapes.AddPicture(imagePath); } else { RemoveImage(wordDoc, title); } } catch (Exception ex) { _logger.Error("Error in Insert Image", ex); } } else { RemoveImage(wordDoc, title); } }
private void RemoveImage(Word.Document wordDoc, string title) { try { var rngPic = wordDoc.SelectContentControlsByTag(title)[1]; rngPic.Delete(true); } catch (Exception ex) { _logger.Error("Error in Remove Image", ex); } }
public void CreateTableByTag(List <string[]> tableData, string tag) { int cols = 5; var contentTablaMateriales = Document.SelectContentControlsByTag(tag); Word.Range firstRange = contentTablaMateriales[1].Range; Word.Table table = Document.Tables.Add(firstRange, tableData.Count, cols); float padding = 3; for (int iRow = 1; iRow <= tableData.Count; iRow++) { for (int iCol = 1; iCol <= cols; iCol++) { Word.Cell cell = table.Cell(iRow, iCol); if (iRow == 1) { cell.Range.Shading.BackgroundPatternColor = Word.WdColor.wdColorGray125; cell.Range.Bold = 3; } if (iCol != cols) { cell.Width = 50f; cell.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter; } else { cell.Width = 300f; cell.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphJustify; } cell.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleOutset; cell.TopPadding = padding; cell.LeftPadding = padding; cell.RightPadding = padding; cell.BottomPadding = padding; string cellValue = tableData[iRow - 1][iCol - 1]; cell.Range.Text = cellValue; } } }
private void Button1_Click(object sender, EventArgs e) { string constring = @"Data Source=DESKTOP-JO50TII\SQLEXPRESS;Initial Catalog=SignatureBox;Integrated Security=True"; using (SqlConnection con = new SqlConnection(constring)) { con.Open(); string query = "select * from MySignatureTable where SignatureHolder = @SignatureHolder"; using (SqlCommand cmd = new SqlCommand(query, con)) { cmd.Parameters.AddWithValue("@SignatureHolder", textBox1.Text); using (SqlDataReader rd = cmd.ExecuteReader()) { if (rd.Read()) { string fileName = @"C:\Users\emi\Desktop\test.jpg"; byte[] imageBytes = Convert.FromBase64String(rd["SignatureBase64"].ToString()); MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length); ms.Write(imageBytes, 0, imageBytes.Length); Image image = Image.FromStream(ms, true, true); image.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg); Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application(); Microsoft.Office.Interop.Word.Document doc = null; try { doc = app.Documents.Open(@"C:\Users\emi\Desktop\sign1.docx", Type.Missing); var Signature2Ctrl = doc.SelectContentControlsByTag("Signature2"); var testingCtrl = Signature2Ctrl[1]; testingCtrl.Range.InlineShapes.AddPicture(fileName, Type.Missing, Type.Missing, Type.Missing); doc.Save(); MessageBox.Show("Complete!"); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } } } } }