效果图
界面设计
添加图片时,将图片复制到粘贴板后,右键picturebox,将图片添加到picturebox
1:使用之前需要先进行引用 Microsoft.Office.Interop.Word.dll
using MSWord = Microsoft.Office.Interop.Word;
//属性
MSWord.Application wordApp;//Word应用程序变量
MSWord.Document wordDoc;
object unite1 = MSWord.WdUnits.wdStory;
object Nothing = Missing.Value;//Word文档变量
string strContent;
//方法
/// <summary>
/// Word初始化
/// </summary>
private void OneWord()
{
wordApp = new MSWord.Application(); //初始化 //如果已存在,则删除
wordApp.Visible = true;//使文档可见
#region 页面设置、页眉图片和文字设置,最后跳出页眉设置
wordDoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
//页面设置
wordDoc.PageSetup.PaperSize = MSWord.WdPaperSize.wdPaperA4;//设置纸张样式为A4纸
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;//页眉位置
#endregion
#region 页码设置并添加页码
//为当前页添加页码
MSWord.PageNumbers pns = wordApp.Selection.Sections[1].Headers[MSWord.WdHeaderFooterIndex.wdHeaderFooterEvenPages].PageNumbers;//获取当前页的号码
pns.NumberStyle = MSWord.WdPageNumberStyle.wdPageNumberStyleNumberInDash;//设置页码的风格,是Dash形还是圆形的
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
wordApp.Selection.ParagraphFormat.LineSpacing = 16f;//设置文档的行间距
wordApp.Selection.ParagraphFormat.FirstLineIndent = 30;//首行缩进的长度
}
/// <summary>
/// 添加标题文字
/// </summary>
/// <param name="title"></param>
private void AddTitle(string title)
{
try
{
//标题的
wordDoc.Paragraphs.Last.Range.Font.Bold = 1;
wordDoc.Paragraphs.Last.Range.Font.Name = "宋体";//设置字体
wordDoc.Paragraphs.Last.Range.Font.Size = 15;//设置字体大小
wordApp.Selection.EndKey(ref unite1, ref Nothing);//将光标移到文本末尾
wordApp.Selection.ParagraphFormat.FirstLineIndent = 0;//取消首行缩进的长度
wordApp.Selection.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphLeft;
strContent = title + "\n";
wordDoc.Paragraphs.Last.Range.Text = strContent;
}
catch (Exception err)
{
MessageBox.Show("添加失败:" + err.Message);
}
}
/// <summary>
///添加正文到Word文档里面
/// </summary>
/// <param name="text"></param>
private void addText(string text)
{
try
{
wordDoc.Paragraphs.Last.Range.Font.Bold = 0;
wordDoc.Paragraphs.Last.Range.Font.Name = "宋体";//设置字体
wordDoc.Paragraphs.Last.Range.Font.Size = 12;//设置字体大小
wordDoc.Paragraphs.LineSpacing = 7;
wordApp.Selection.EndKey(ref unite1, ref Nothing);//将光标移到文本末尾
wordApp.Selection.ParagraphFormat.FirstLineIndent = 0;//取消首行缩进的长度
wordApp.Selection.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphLeft;
strContent = text + "\n";
wordDoc.Paragraphs.Last.Range.Text = strContent;
}
catch (Exception err)
{
MessageBox.Show("添加失败:" + err.Message);
}
}
/// <summary>
/// 插入图片
/// </summary>
/// <param name="TitlePicturePath"></param>
private void AddPicture()
{
try
{
//要想插入的图片大小一样的话,保存图片时要指定好指定的大小
System.Drawing.Bitmap objNewPic = new System.Drawing.Bitmap(pictureBox1.Image, 300, 300);//图片保存的大小尺寸
objNewPic.Save("1.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
string TitlePicturePath = Path.Combine(Application.StartupPath, "1.jpg");
wordApp.Selection.EndKey(ref unite1, ref Nothing); //将光标移动到文档末尾
object range = wordDoc.Paragraphs.Last.Range;
//定义该插入的图片是否为外部链接
Object linkToFile = false;//默认,这里貌似设置为bool类型更清晰一些 //定义要插入的图片是否随Word文档一起保存
Object saveWithDocument = true;//默认 //使用InlineShapes.AddPicture方法(【即“嵌入型”】)插入图片
wordDoc.InlineShapes.AddPicture(TitlePicturePath, ref linkToFile, ref saveWithDocument, ref range);
wordApp.Selection.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphCenter;//居中显示图片 //在图下方居中添加图片标题
wordDoc.Content.InsertAfter("\n");//这一句与下一句的顺序不能颠倒,原因还没搞透
wordApp.Selection.EndKey(ref unite1, ref Nothing);
wordApp.Selection.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphCenter;
wordApp.Selection.Font.Size = 10;//字体大小
}
catch (Exception err)
{
MessageBox.Show("添加失败:" + err.Message);
}
}
/// <summary>
/// 复制图片到粘贴板上后,右键picturebox,图片粘贴过去
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
if (Clipboard.ContainsImage())
{
Image im = Clipboard.GetImage();
if (im != null)
{
pictureBox1.Image = im;
}
}
}
}