vsto-Word相关操作

//添加页码到页脚
Document doc = Globals.ThisAddIn.Application.ActiveDocument;
HeaderFooter hprimary= doc.Sections[1].Footers[WdHeaderFooterIndex.wdHeaderFooterPrimary];
hprimary.Range.Fields.Add(hprimary.Range, WdFieldType.wdFieldPage);
 //添加页脚并显示页码
 Document doc = Globals.ThisAddIn.Application.ActiveDocument;
 Window activeWindow = doc.Application.ActiveWindow;
 object currentPage = WdFieldType.wdFieldPage;
 object totalPages = WdFieldType.wdFieldNumPages;

 // Go to the Footer view
 activeWindow.ActivePane.View.SeekView = WdSeekView.wdSeekCurrentPageFooter;
 // Right-align the current selection
 activeWindow.ActivePane.Selection.Paragraphs.Alignment =
 WdParagraphAlignment.wdAlignParagraphCenter;

 // Type the page number in ‘Page X of Y‘ format
 activeWindow.Selection.TypeText("第 ");
 activeWindow.Selection.Fields.Add(
 activeWindow.Selection.Range, ref currentPage);
 activeWindow.Selection.TypeText("页,共 ");
 activeWindow.Selection.Fields.Add(
 activeWindow.Selection.Range, ref totalPages);
 activeWindow.Selection.TypeText("页 ");

 //清除页眉横线(不知道为什么页眉会多一条横线)
 doc.Sections[1].Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Borders[WdBorderType.wdBorderBottom].LineStyle =WdLineStyle.wdLineStyleNone;
 // Go back to the Main Document view
 activeWindow.ActivePane.View.SeekView = WdSeekView.wdSeekMainDocument;
//页面配置
PageSetup PAGE = doc.PageSetup;
PAGE.PaperSize = WdPaperSize.wdPaperA3;                   //A3纸
PAGE.Orientation = WdOrientation.wdOrientLandscape;  //横向
PAGE.TextColumns.SetCount(3);  //分3栏
//定位选中内容的下一行进行操作Selection sel = Globals.ThisAddIn.Application.Selection;   //获取选中内容
sel.InsertParagraph();               //插入新的段落行
Range range = sel.GoToNext(WdGoToItem.wdGoToLine);   //定位到新的段落行
range.Text="当前行下一行进行编辑";
//获取指定区域中的文本、图片、图形Document doc = Globals.ThisAddIn.Application.ActiveDocument;
Range r = doc.Range();
Range range = doc.Range(r.Bookmarks["bookmark1"].Start, r.Bookmarks["bookmark2"].Start);
string text = range.Text;  //获取指定区域中的文本内容
//int c = Globals.ThisAddIn.Application.ActiveDocument.Shapes.Count;
Shape s = Globals.ThisAddIn.Application.ActiveDocument.Shapes[1];  //获取当前文档中第一个图形对象
InlineShape igs = Globals.ThisAddIn.Application.ActiveDocument.InlineShapes[1];  //获取当前文档中第一个图片对象
InlineShape idfs= range.InlineShapes[1];   //获取指定区域中第一个图片对象
idfs.Range.Select();
Shape sr = range.ShapeRange[1];    //获取指定区域中第一个图形对象
internal void GetImagesInDocument()
{
    // iterate through each shape
    foreach (InlineShape ils in Globals.ThisAddIn.Application.ActiveDocument.InlineShapes)
  {
    // validate
    if (ils != null)
    {
      // check if image
      if (ils.Type == Microsoft.Office.Interop.Word.WdInlineShapeType.wdInlineShapePicture)
      {
        // you can do what you want with the object here
        ils.ScaleHeight = 40;
        ils.ScaleWidth = 40;
      }
    }
  }
}
 
//Table的相关操作
Table t=range.Tables.Add(range,2,2);
t.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleThickThinLargeGap;  ////设置Table的外边框线格式
t.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;
foreach (Row item in t.Rows)
{
     item.Height = 32;     //设置行高
     Range rowRange = item.Cells [1].Range;
     Table rowT = rowRange.Tables.Add(rowRange, 1, 17);   //嵌套Table
     rowT.Rows.Height = 24;
     rowT.Columns.Width = 24;    //设置列宽
     rowT.Borders .OutsideLineStyle = WdLineStyle.wdLineStyleSingle;
     rowT.Borders.InsideLineStyle = WdLineStyle.wdLineStyleSingle;
     //rowT.Borders[WdBorderType.wdBorderTop].LineStyle = WdLineStyle.wdLineStyleSingle;  //设置边框顶线
     //rowT.Borders[WdBorderType.wdBorderBottom].LineStyle = WdLineStyle.wdLineStyleSingle;  //设置边框底线
     //rowT.Borders[WdBorderType.wdBorderLeft].LineStyle = WdLineStyle.wdLineStyleSingle;
     //rowT.Borders[WdBorderType.wdBorderRight].LineStyle = WdLineStyle.wdLineStyleSingle;
     //rowT.Borders[WdBorderType.wdBorderVertical].LineStyle = WdLineStyle.wdLineStyleSingle;    //设置边框内纵线
     //rowT.Borders[WdBorderType.wdBorderHorizontal].LineStyle = WdLineStyle.wdLineStyleSingle;  //设置边框内横线
     item.Cells.VerticalAlignment =WdCellVerticalAlignment.wdCellAlignVerticalCenter;  //设置单元格垂直居中
}
//向单元格添加文本
t.Cell(1,1).Range.Text="Test";   //下标都是从1开始
//单元格中文本格式比较复杂的可以作为段落
 Paragraph pTitle = t.Cell(2, 1).Range.Paragraphs.Add(t.Cell(1, 1).Range);
pTitle.Range.Font.Size = 24;
pTitle.Range.Text = "这里是标题";
pTitle.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;   //设置水平居中

原文地址:https://www.cnblogs.com/Betty-IT/p/9072725.html

时间: 2024-08-03 17:13:09

vsto-Word相关操作的相关文章

python字符串格式化输出及相关操作代码举例

字符串的格式化 Python 支持格式化字符串的输出 .尽管这样可能会用到非常复杂的表达式,但最基本的用法是将一个值插入到一个有字符串格式符 %s 的字符串中.在   Python 中,字符串格式化使用与 C 中 sprintf 函数一样的语法. 在python中格式化输出字符串通用的形式为: 格式标记字符串 % 要输出的值组 其中,左边部分的"格式标记字符串"可以完全和c中的一致.右边的"值组"如果有两个及以上的值则需要用小括号括起来,中间用逗号隔开. 重点来看左

(1)spark核心RDD的概念解析、创建、以及相关操作

spark核心之RDD 什么是RDD RDD指的是弹性分布式数据集(Resilient Distributed Dataset),它是spark计算的核心.尽管后面我们会使用DataFrame.Dataset进行编程,但是它们的底层依旧是依赖于RDD的.我们来解释一下RDD(Resilient Distributed Dataset)的这几个单词含义. 弹性:在计算上具有容错性,spark是一个计算框架,如果某一个节点挂了,可以自动进行计算之间血缘关系的跟踪 分布式:很好理解,hdfs上数据是跨

二叉树的相关操作

#include<stdio.h> #include<malloc.h> #define MAXSIZE 20 typedef char TEelemtype; typedef struct BiTNode{ TEelemtype data; struct BiTNode *lchild,*rchild; }BiTNode,*BiTree; //队列的方式 typedef struct queueelem { BiTNode* b[MAXSIZE]; int front,rear;

在C#中使用Spire.doc对word的操作总结

在C#中使用Spire.doc对word的操作总结 在最近的工程中我们要处理一些word文档.通过在网上的大量搜索,我发现大多数软件功能不是不完整就是有重复.极少数可以完全实现的word组件又要收费.功夫不负有心人,终于找到了可以满足我们需要的免费的C# word程序库.为了和其他的作比较,我在这里先做以下汇总.希望对大家有帮助. 如何得到? 这个免费版的word 组件可以在Codeplex下载到,你也可以从本文里直接下载msi文件.它还提供了一些源代码. Word操作汇总 1.        

(二十四)linux新定时器:timefd及相关操作函数

timerfd是Linux为用户程序提供的一个定时器接口.这个接口基于文件描述符,通过文件描述符的可读事件进行超时通知,所以能够被用于select/poll的应用场景. 一,相关操作函数 #include <sys/timerfd.h> int timerfd_create(int clockid, int flags); int timerfd_settime(int fd, int flags, const struct itimerspec *new_value, struct itim

二叉树各种相关操作(建立二叉树、前序、中序、后序、求二叉树的深度、查找二叉树节点,层次遍历二叉树等)(C语言版)

将二叉树相关的操作集中在一个实例里,有助于理解有关二叉树的相关操作: 1.定义树的结构体: 1 typedef struct TreeNode{ 2 int data; 3 struct TreeNode *left; 4 struct TreeNode *right; 5 }TreeNode; 2.创建根节点: 1 TreeNode *creatRoot(){ 2 TreeNode * root =(TreeNode *)malloc(sizeof(TreeNode)); 3 if(NULL=

linux下进程相关操作

一.定义和理解 狭义定义:进程是正在运行的程序的实例. 广义定义:进程是一个具有一定独立功能的程序关于某个数据集合的一次运行活动. 进程的概念主要有两点: 第一,进程是一个实体.每一个进程都有它自己的地址空间,一般情况下,包括文本区域.数据区域和堆栈区域.文本区域存储处理器执行的代码:数据区域存储变量和进程执行期间使用的动态分配的内存:堆栈区域存储着活动过程调用的指令和本地变量. 第二,进程是一个“执行中的程序”.程序是一个没有生命的实体,只有处理器赋予程序生命时,它才能成为一个活动的实体,我们

android DataBase的相关操作(建立表结构和创建表)

先建立一个table的基类: public abstract class DbBaseTable { private static final String TAG = "DbBaseTable"; /** * @return the DB table name */ abstract String getName(); /** * Creates the DB table according to the DB scheme * * @param db */ abstract voi

WebView中的视频全屏的相关操作

最近工作中,基本一直在用WebView,今天就把它整理下: WebView 顾名思义,就是放一个网页,一个看起来十分简单,但是用起来不是那么简单的控件. 首先你肯定要定义,初始化一个webview,其实网上的例子很多,我这里就简单的把一些WebView 中可能会用到的的很重要的属性以及支持全屏播放视频该怎么实现的代码粘出来,直接放到项目中去就行了 <span style="white-space:pre"></span><pre name="co