123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332 |
- using Microsoft.Win32;
- using Spire.Doc;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- using System.Threading.Tasks;
- using MSWord = Microsoft.Office.Interop.Word;
- namespace ConsoleWordInsertPicStudy
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- object path;
- string strContent;
- MSWord.Application wordApp;
- MSWord.Document wordDoc;
-
- path = @"C:\Users\ASUS-PC\Desktop\MyWord_Print.doc";
- wordApp = new MSWord.Application();
- wordApp.Visible = true;
-
- if (File.Exists((string)path))
- {
- File.Delete((string)path);
- }
-
- Object Nothing = Missing.Value;
- wordDoc = wordApp.Documents.Add(ref Nothing,ref Nothing,ref Nothing,ref Nothing);
- #region 页面设置、页眉图片和文字设置,最后跳出页眉设置
-
- wordDoc.PageSetup.PaperSize = MSWord.WdPaperSize.wdPaperA4;
- wordDoc.PageSetup.Orientation = MSWord.WdOrientation.wdOrientPortrait;
- wordDoc.PageSetup.TopMargin = 57.0f;
- wordDoc.PageSetup.BottomMargin = 57.0f;
- wordDoc.PageSetup.LeftMargin = 57.0f;
- wordDoc.PageSetup.RightMargin = 57.0f;
- wordDoc.PageSetup.HeaderDistance = 30.0f;
-
- wordApp.ActiveWindow.View.Type = MSWord.WdViewType.wdNormalView;
- wordApp.ActiveWindow.View.SeekView = MSWord.WdSeekView.wdSeekPrimaryHeader;
- wordApp.Selection.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphRight;
-
- wordApp.Selection.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphCenter;
- string headerfile = @"C:\Users\ASUS-PC\Desktop\sjy.png";
- MSWord.InlineShape shape1 = wordApp.ActiveWindow.ActivePane.Selection.InlineShapes.AddPicture(headerfile, ref Nothing, ref Nothing, ref Nothing);
- shape1.Height = 15;
- shape1.Width = 15;
- wordApp.ActiveWindow.ActivePane.Selection.InsertAfter(" 文档页眉");
-
- wordApp.ActiveWindow.ActivePane.Selection.ParagraphFormat.Borders[MSWord.WdBorderType.wdBorderBottom].LineStyle = MSWord.WdLineStyle.wdLineStyleNone;
- wordApp.ActiveWindow.ActivePane.Selection.Borders[MSWord.WdBorderType.wdBorderBottom].Visible = false;
- wordApp.ActiveWindow.ActivePane.View.SeekView = MSWord.WdSeekView.wdSeekMainDocument;
- #endregion
- #region 页码设置并添加页码
-
- MSWord.PageNumbers pns = wordApp.Selection.Sections[1].Headers[MSWord.WdHeaderFooterIndex.wdHeaderFooterEvenPages].PageNumbers;
- pns.NumberStyle = MSWord.WdPageNumberStyle.wdPageNumberStyleNumberInDash;
- pns.HeadingLevelForChapter = 0;
- pns.IncludeChapterNumber = false;
- pns.RestartNumberingAtSection = false;
- pns.StartingNumber = 0;
- object pagenmbetal = MSWord.WdPageNumberAlignment.wdAlignPageNumberCenter;
- object first = true;
- wordApp.Selection.Sections[1].Footers[MSWord.WdHeaderFooterIndex.wdHeaderFooterEvenPages].PageNumbers.Add(ref pagenmbetal,ref first);
- #endregion
- #region 行间距与缩进、文本字体、字号、加粗、斜体、颜色、下划线、下划线颜色设置
-
- wordApp.Selection.ParagraphFormat.LineSpacing = 16f;
- wordApp.Selection.ParagraphFormat.FirstLineIndent = 30;
-
- strContent = "这是普通文本\n";
- wordDoc.Paragraphs.Last.Range.Text = strContent;
- wordDoc.Paragraphs.Last.Range.Text = "再加一行试试,这里不加\\n";
-
- wordDoc.Paragraphs.Last.Range.Text += "不会覆盖的,";
-
- wordDoc.Paragraphs.Last.Range.InsertAfter("这是后面的内容\n");
-
- object start = 0;
- object end = 4;
- MSWord.Range rang = wordDoc.Range(ref start, ref end);
- rang.Font.Color = MSWord.WdColor.wdColorRed;
- rang.Text = "这是替换文字";
- wordDoc.Range(ref start,ref end);
-
- object unite = MSWord.WdUnits.wdStory;
- wordApp.Selection.EndKey(ref unite, ref Nothing);
- wordApp.Selection.ParagraphFormat.FirstLineIndent = 0;
- strContent = "这是黑体文本\n";
- wordDoc.Paragraphs.Last.Range.Font.Name = "黑体";
- wordDoc.Paragraphs.Last.Range.Text = strContent;
-
- strContent = "这是粗体文本\n";
- wordApp.Selection.EndKey(ref unite, ref Nothing);
- wordDoc.Paragraphs.Last.Range.Font.Bold = 1;
- wordDoc.Paragraphs.Last.Range.Text = strContent;
-
- strContent = "我这个文本的字号是15号,而且是宋体\n";
- wordApp.Selection.EndKey(ref unite, ref Nothing);
- wordDoc.Paragraphs.Last.Range.Font.Size = 15;
- wordDoc.Paragraphs.Last.Range.Font.Name = "宋体";
- wordDoc.Paragraphs.Last.Range.Text = strContent;
-
- strContent = "我是斜体字文本\n";
- wordApp.Selection.EndKey(ref unite, ref Nothing);
- wordDoc.Paragraphs.Last.Range.Font.Italic = 1;
- wordDoc.Paragraphs.Last.Range.Text = strContent;
-
- strContent = "我是蓝色的文本\n";
- wordApp.Selection.EndKey(ref unite, ref Nothing);
- wordDoc.Paragraphs.Last.Range.Font.Color = MSWord.WdColor.wdColorBlue;
- wordDoc.Paragraphs.Last.Range.Text = strContent;
-
- strContent = "我是下划线文本\n";
- wordApp.Selection.EndKey(ref unite, ref Nothing);
- wordDoc.Paragraphs.Last.Range.Font.Underline = MSWord.WdUnderline.wdUnderlineThick;
- wordDoc.Paragraphs.Last.Range.Text = strContent;
-
- strContent = "我是点线下划线,并且下划线是红色的\n";
- wordApp.Selection.EndKey(ref unite, ref Nothing);
- wordDoc.Paragraphs.Last.Range.Font.Underline = MSWord.WdUnderline.wdUnderlineDottedHeavy;
- wordDoc.Paragraphs.Last.Range.Font.UnderlineColor = MSWord.WdColor.wdColorRed;
- wordDoc.Paragraphs.Last.Range.Text = strContent;
-
- strContent = "我他妈不要下划线了,并且设置字号为12号,黑色不要斜体\n";
- wordApp.Selection.EndKey(ref unite, ref Nothing);
- wordDoc.Paragraphs.Last.Range.Font.Size = 12;
- wordDoc.Paragraphs.Last.Range.Font.Underline = MSWord.WdUnderline.wdUnderlineNone;
- wordDoc.Paragraphs.Last.Range.Font.Color = MSWord.WdColor.wdColorBlack;
- wordDoc.Paragraphs.Last.Range.Font.Italic = 0;
- wordDoc.Paragraphs.Last.Range.Text = strContent;
- #endregion
- #region 插入图片、居中显示,设置图片的绝对尺寸和缩放尺寸,并给图片添加标题
- wordApp.Selection.EndKey(ref unite, ref Nothing);
-
-
- string filename = @"C:\Users\ASUS-PC\Desktop\sjy.png";
-
- Object range = wordDoc.Paragraphs.Last.Range;
-
- Object linkToFile = false;
-
- Object saveWithDocument = true;
-
- wordDoc.InlineShapes.AddPicture(filename, ref linkToFile, ref saveWithDocument, ref range);
- wordApp.Selection.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphCenter;
-
-
-
-
- wordDoc.InlineShapes[1].ScaleWidth = 20;
- wordDoc.InlineShapes[1].ScaleHeight = 20;
-
- wordDoc.Content.InsertAfter("\n");
- wordApp.Selection.EndKey(ref unite, ref Nothing);
- wordApp.Selection.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphCenter;
- wordApp.Selection.Font.Size = 10;
- wordApp.Selection.TypeText("图1 测试图片\n");
- #endregion
- #region 添加表格、填充数据、设置表格行列宽高、合并单元格、添加表头斜线、给单元格添加图片
- wordDoc.Content.InsertAfter("\n");
- wordApp.Selection.EndKey(ref unite, ref Nothing);
- wordApp.Selection.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphLeft;
-
-
-
- int tableRow = 6;
- int tableColumn = 6;
-
- MSWord.Table table = wordDoc.Tables.Add(wordApp.Selection.Range,
- tableRow, tableColumn, ref Nothing, ref Nothing);
-
- table.Borders.Enable = 1;
-
- wordDoc.Tables[1].Cell(1, 1).Range.Text = "列\n行";
- for (int i = 1; i < tableRow; i++)
- {
- for (int j = 1; j < tableColumn; j++)
- {
- if (i == 1)
- {
- table.Cell(i, j + 1).Range.Text = "Column " + j;
- }
- if (j == 1)
- {
- table.Cell(i + 1, j).Range.Text = "Row " + i;
- }
- table.Cell(i + 1, j + 1).Range.Text = i + "行 " + j + "列";
- }
- }
-
- table.Rows.Add(ref Nothing);
- table.Rows[tableRow + 1].Height = 35;
-
- string FileName = Environment.CurrentDirectory + "\\sjy.png";
- object LinkToFile = false;
- object SaveWithDocument = true;
- object Anchor = table.Cell(tableRow + 1, tableColumn).Range;
- wordDoc.Application.ActiveDocument.InlineShapes.AddPicture(filename, ref linkToFile, ref saveWithDocument, ref Anchor);
-
- wordDoc.Application.ActiveDocument.InlineShapes[2].Width = 50;
- wordDoc.Application.ActiveDocument.InlineShapes[2].Height = 35;
-
- MSWord.Shape s = wordDoc.Application.ActiveDocument.InlineShapes[2].ConvertToShape();
- s.WrapFormat.Type = MSWord.WdWrapType.wdWrapSquare;
-
- table.Rows.HeightRule = MSWord.WdRowHeightRule.wdRowHeightAtLeast;
- table.Rows.Height = wordApp.CentimetersToPoints(float.Parse("0.8"));
- table.Range.Font.Size = 10.5F;
- table.Range.Font.Bold = 0;
- table.Range.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphCenter;
- table.Range.Cells.VerticalAlignment = MSWord.WdCellVerticalAlignment.wdCellAlignVerticalBottom;
-
- table.Borders.OutsideLineStyle = MSWord.WdLineStyle.wdLineStyleDouble;
- table.Borders.InsideLineStyle = MSWord.WdLineStyle.wdLineStyleSingle;
- table.Rows[1].Range.Font.Bold = 1;
- table.Rows[1].Range.Font.Size = 12F;
- table.Cell(1, 1).Range.Font.Size = 10.5F;
- wordApp.Selection.Cells.Height = 30;
-
- for (int i = 2; i <= tableRow; i++)
- {
- table.Rows[i].Height = 20;
- }
-
- table.Cell(1, 1).Range.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphRight;
-
- table.Cell(1, 1).Range.Paragraphs[2].Format.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphLeft;
- table.Columns[1].Width = 50;
-
- for (int i = 2; i <= tableColumn; i++)
- {
- table.Columns[i].Width = 75;
- }
-
- table.Cell(1, 1).Borders[MSWord.WdBorderType.wdBorderDiagonalDown].Visible = true;
- table.Cell(1, 1).Borders[MSWord.WdBorderType.wdBorderDiagonalDown].Color = MSWord.WdColor.wdColorRed;
- table.Cell(1, 1).Borders[MSWord.WdBorderType.wdBorderDiagonalDown].LineWidth = MSWord.WdLineWidth.wdLineWidth150pt;
-
- table.Cell(4, 4).Merge(table.Cell(4, 5));
- table.Cell(2, 3).Merge(table.Cell(4, 3));
- #endregion
- wordApp.Selection.EndKey(ref unite, ref Nothing);
- wordDoc.Content.InsertAfter("\n");
-
-
- object format = MSWord.WdSaveFormat.wdFormatDocument;
-
- wordDoc.SaveAs(ref path, ref format, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
-
-
-
- wordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
-
- wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
- Console.WriteLine(path + " 创建完毕!");
- Console.ReadKey();
- }
- }
- }
|