private void AddImageToDocument(Microsoft.Office.Interop.Word.Document doc) { object oEndOfDoc = "SIGNATURE"; Range imageRange = doc.Bookmarks.get_Item(ref oEndOfDoc).Range; string imagePath = @"C:\AdarshData\Project Documents\OrthoInitialImplementation\JsonToDocumentPOC\JsonToDocumentPOC\Data\Signature.jpg"; // Create an InlineShape in the InlineShapes collection where the picture should be added later // It is used to get automatically scaled sizes. InlineShape autoScaledInlineShape = imageRange.InlineShapes.AddPicture(imagePath); float scaledWidth = autoScaledInlineShape.Width; float scaledHeight = autoScaledInlineShape.Height; autoScaledInlineShape.Delete(); // Create a new Shape and fill it with the picture Shape newShape = doc.Shapes.AddShape(1, 0, 0, scaledWidth, scaledHeight); newShape.Fill.UserPicture(imagePath); // Convert the Shape to an InlineShape and optional disable Border InlineShape finalInlineShape = newShape.ConvertToInlineShape(); finalInlineShape.Line.Visible = Microsoft.Office.Core.MsoTriState.msoFalse; // Cut the range of the InlineShape to clipboard finalInlineShape.Range.Cut(); // And paste it to the target Range imageRange.Paste(); }
// Function to insert a photo at a specific location (range) within a word document public static void insertImage(Document doc, Range rng, String path) { // Create an InlineShape in the InlineShapes collection where the picture should be added later // It is used to get automatically scaled sizes. InlineShape autoScaledInlineShape = rng.InlineShapes.AddPicture(path); float scaledWidth = autoScaledInlineShape.Width; float scaledHeight = autoScaledInlineShape.Height; autoScaledInlineShape.Delete(); // Create a new Shape and fill it with the picture Shape newShape = doc.Shapes.AddShape(1, 0, 0, scaledWidth, scaledHeight); newShape.Fill.UserPicture(path); // Convert the Shape to an InlineShape and optional disable Border InlineShape finalInlineShape = newShape.ConvertToInlineShape(); finalInlineShape.Line.Visible = Microsoft.Office.Core.MsoTriState.msoFalse; // Cut the range of the InlineShape to clipboard finalInlineShape.Range.Cut(); // And paste it to the target Range rng.Paste(); }
/// <summary> /// Fetch an evidence from the temp folder referenced by the evidence ID (step number) and write it to the target document. /// </summary> /// <param name="strEvidenceID"></param> private static void writeEvidence(string strEvidenceID) { string strScreenshotImageURI; Range rngScreenshotRange = null; InlineShape shpScreenshot = null; int iEvidenceOrdinal = tpPlan.lstStrPropEvidenceID.IndexOf(strEvidenceID); int iTableID = iEvidenceOrdinal + 2; strScreenshotImageURI = evEvidence.dictPropActualEvidences[strEvidenceID].strPropEvidenceImageURI; foreach (InlineShape shpImage in docTargetDocument.InlineShapes) { if (strEvidenceID.Equals(shpImage.AlternativeText)) { shpScreenshot = shpImage; rngScreenshotRange = shpImage.Range; break; } } if (shpScreenshot == null || rngScreenshotRange == null) { Log("Corrupted target document. Could not save evidence for " + strEvidenceID); return; } try { shpScreenshot.Delete(); dictShpScreenshots[strEvidenceID] = rngScreenshotRange.InlineShapes.AddPicture(strScreenshotImageURI); dictShpScreenshots[strEvidenceID].AlternativeText = strEvidenceID; dictShpScreenshots[strEvidenceID].Title = strEvidenceID; Log(" \nAdd actual " + dictShpScreenshots[strEvidenceID].AlternativeText); if (!lstStrEvidencesWritten.Contains(strEvidenceID)) { lstStrEvidencesWritten.Add(strEvidenceID); } } catch (System.Runtime.InteropServices.COMException ex) { throw new Exception(ex.Message); } catch (Exception) { MessageBox.Show(new Form { TopMost = true }, "Could not save evidence for " + strEvidenceID); } deleteEvidenceImage(strScreenshotImageURI); docTargetDocument.Tables[iTableID].Rows[1].Cells[4].Range.Text = evEvidence.dictPropActualEvidences[strEvidenceID].strStatus; docTargetDocument.Tables[iTableID].Rows[2].Cells[2].Range.Text = evEvidence.dictPropActualEvidences[strEvidenceID].strComments; saveTargetDocument(); }
private void AddImageToDocument( Microsoft.Office.Interop.Word.Document doc, string signatureKeyName, string signatureFilePath) { object oEndOfDoc = signatureKeyName; if (doc.Bookmarks.Exists(signatureKeyName)) { Range imageRange = doc.Bookmarks.get_Item(ref oEndOfDoc).Range; // Create an InlineShape in the InlineShapes collection where the picture should be added later // It is used to get automatically scaled sizes. InlineShape autoScaledInlineShape = imageRange.InlineShapes.AddPicture(signatureFilePath); float scaledWidth = autoScaledInlineShape.Width; float scaledHeight = autoScaledInlineShape.Height; autoScaledInlineShape.Delete(); // Create a new Shape and fill it with the picture Shape newShape = doc.Shapes.AddShape(1, 0, 0, scaledWidth, scaledHeight); newShape.Fill.UserPicture(signatureFilePath); // Convert the Shape to an InlineShape and optional disable Border InlineShape finalInlineShape = newShape.ConvertToInlineShape(); finalInlineShape.Line.Visible = Microsoft.Office.Core.MsoTriState.msoFalse; // Cut the range of the InlineShape to clipboard finalInlineShape.Range.Cut(); // And paste it to the target Range imageRange.Paste(); } else { FindAndReplaceText(doc, signatureKeyName, "Signature Image is missig");; } }
/// <summary> /// 规范表格,而且删除表格中的嵌入式图片 /// </summary> /// <param name="TableStyle">要应用的表格样式</param> /// <param name="ParagraphFormat">表格中的段落样式</param> /// <param name="blnDeleteShapes">是否要删除表格中的图片,包括嵌入式或非嵌入式图片。</param> /// <remarks></remarks> public void TableFormat(string TableStyle = "zengfy表格-上下总分型1", string ParagraphFormat = "表格内容置顶", bool blnDeleteShapes = false) { var Selection = _app.Selection; if (Selection.Tables.Count > 0) { //定位表格 Table Tb = default(Table); Range rg = default(Range); foreach (Table tempLoopVar_Tb in Selection.Range.Tables) { Tb = tempLoopVar_Tb; rg = Tb.Range; _app = Tb.Application; // _app.ScreenUpdating = false; //调整表格尺寸 dynamic with_1 = Tb; with_1.AutoFitBehavior(WdAutoFitBehavior.wdAutoFitContent); with_1.AutoFitBehavior(WdAutoFitBehavior.wdAutoFitWindow); //清除表格中的超链接 Hyperlinks hps = default(Hyperlinks); hps = rg.Hyperlinks; int hpCount = 0; hpCount = hps.Count; for (var i = 1; i <= hpCount; i++) { hps[1].Delete(); } //将手动换行符删除 { Tb.Range.Find.ClearFormatting(); Tb.Range.Find.Replacement.ClearFormatting(); Tb.Range.Find.Text = "^l"; Tb.Range.Find.Replacement.Text = ""; Tb.Range.Find.Execute(WdReplace.wdReplaceAll); } //删除表格中的乱码空格 { Tb.Range.Find.ClearFormatting(); Tb.Range.Find.Replacement.ClearFormatting(); Tb.Range.Find.Text = " "; Tb.Range.Find.Replacement.Text = " "; Tb.Range.Find.Execute(WdReplace.wdReplaceAll); } //删除表格中的嵌入式图片 if (blnDeleteShapes) { InlineShapes inlineshps = default(InlineShapes); int Count = 0; InlineShape inlineShp = default(InlineShape); inlineshps = Tb.Range.InlineShapes; Count = inlineshps.Count; for (var i = Count; i >= 1; i--) { inlineShp = inlineshps[Convert.ToInt32(i)]; inlineShp.Delete(); } //删除表格中的图片 ShapeRange shps = default(ShapeRange); Shape shp = default(Shape); shps = Tb.Range.ShapeRange; Count = shps.Count; for (var i = Count; i >= 1; i--) { shp = shps[i]; shp.Delete(); } } //清除表格中的格式设置 rg.Select(); Selection.ClearFormatting(); // ----- 设置表格样式与表格中的段落样式 try //设置表格样式 { Tb.set_Style(TableStyle); } catch (Exception) { } try //设置表格中的段落样式 { rg.ParagraphFormat.set_Style(ParagraphFormat); } catch (Exception) { } } //取消选择并刷新界面 Selection.Collapse(); _app.ScreenRefresh(); _app.ScreenUpdating = true; } else { MessageBox.Show("请至少选择一个表格。", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } }