AE导出图片并插入到word文档中某一固定位置

按矢量图层中各要素范围批量裁剪图片,并将图像粘贴到word中,将已经存在的图像批量粘贴到已存的word文档中。

void ClipPicToWord()

{

      ILayer pLayer=GetCurrentLayer(); 
      IFeatureLayer=pLayer as IFeatureLayer;
      IFeatureClass pFtClass=pFeatureLayer.FeatureClass;
      IFeature pFeature; 
      IQueryFilter pFilter=new QueryFilter(); 
      pFilter.WhereClause=null; 
      IFeatureCursor pCursor=pFtClass.Search(pFilter,false);
      pFeature=pCursor.NextFeature();
      string pWordPath=@"D:\模版文档.doc"; 

      string strFullPath=pWordPath.Substring(0,pWordPath.LastIndexOf(@"\"));

      while(pFeature!=null) 
      {

 IEnvelop pEnvelop=pFeature.Extent; 

 axMapControl1.ActiveView.Extent=pEnvelop; 

 axMapControl1.Refresh(); 

 int index=pFeature.FindField("JCBH");//唯一标识要素的字段,以此命名图像和word文档 

object name=pFeature.get_value(index); 

string strName=name.ToString();

 int lScreenResolution = axMapControl1.ActiveView.ScreenDisplay.DisplayTransformation.Resolution; 

int m_OutPutResolution=300; 

IExport pExport = new ExportJPEG() as IExport;

 pExport.ExportFileName=strFullName+@"\"+name; 

pExport.PixelBounds=pEnvelop; 

tagRECT deviceRECT; 

deviceRECT.left=deviceRECT.rop=0; 

deviceRECT.right= axMapControl1.ActiveView.ExportFrame.right * (m_OutPutResolution / lScreenResolution); 

deviceRECT.bottom = axMapControl1.ActiveView.ExportFrame.bottom * (m_OutPutResolution / lScreenResolution); 

IEnvelope pDriverBounds = new Envelope() as IEnvelope; 

pDriverBounds.PutCoords(deviceRECT.left, deviceRECT.bottom, deviceRECT.right, deviceRECT.top);

 pExporter.PixelBounds = pDriverBounds; 

ITrackCancel pCancel = new CancelTracker(); 

pExporter.TrackCancel = pCancel;

 System.Int32 hDC = pExporter.StartExporting(); 

axMapControl1.ActiveView.Output(hDC, (System.Int16)pExporter.Resolution, ref deviceRECT, pEnvelop, pCancel); 

pExporter.FinishExporting(); 

pExporter.Cleanup(); 

pWordPath=@"D:\模版文档.doc"; wordManager.CreateNewDocument(filePath);

 string picturePath = strFullName + "\\" + strName + ".jpg"; 

wordManager.InsertPicture("picture1", picturePath, this.PicWith, this.PicHeight, 1); //书签位置,图片路径,图片宽度,图片高度 

filePath = strFullName + "\\" + strName + ".doc";

 wordManager.SaveDocument(filePath); //文档路径 

pFeature = pCursor.NextFeature();

    wordManager.killWinWordProcess();//结束word进程 

    axMapControl1.Extent = axMapControl1.FullExtent;

WordManager.cs中部分代码: 

//创建新文档

 public void CreateNewDocument(string filePath)

 {

 killWinWordProcess(); 

 wordApp = new Application(); 

 wordApp.DisplayAlerts = WdAlertLevel.wdAlertsNone; 

 wordApp.Visible = false;

 object missing = System.Reflection.Missing.Value;

 object templateName = filePath; 

 wordDoc = wordApp.Documents.Open(ref templateName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);

 }

 //保存 

public void SaveDocument(string filePath) 

object fileName = filePath; object format = WdSaveFormat.wdFormatDocument;//保存格式 

object miss = System.Reflection.Missing.Value; wordDoc.SaveAs(ref fileName, ref format, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss); 

//关闭wordDoc,wordApp对象 

object SaveChanges = WdSaveOptions.wdSaveChanges;

 object OriginalFormat = WdOriginalFormat.wdOriginalDocumentFormat; 

object RouteDocument = false;

 wordDoc.Close(ref SaveChanges, ref OriginalFormat, ref RouteDocument); 

wordApp.Quit(ref SaveChanges, ref OriginalFormat, ref RouteDocument); 

}

 //插入图片 

public void InsertPicture(string bookmark, string picturePath, float width, float hight,int n) 

object miss = System.Reflection.Missing.Value; 

object oStart = bookmark; 

Object linkToFile = false;

 //图片是否为外部链接 Object saveWithDocument = true; //图片是否随文档一起保存

 object range = wordDoc.Bookmarks.get_Item(ref oStart).Range;//图片插入位置

 wordDoc.InlineShapes.AddPicture(picturePath, ref linkToFile, ref saveWithDocument, ref range); 

wordDoc.Application.ActiveDocument.InlineShapes[n].Width = width; //设置图片宽度 

wordDoc.Application.ActiveDocument.InlineShapes[n].Height = hight; //设置图片高度 

}

 //结束WORD进程

 public void killWinWordProcess()

 { 

System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcessesByName("WINWORD"); 

foreach (System.Diagnostics.Process process in processes) 

     bool b = process.MainWindowTitle == ""; 

    if (process.MainWindowTitle == "") 

    { 

      process.Kill();

    } 

}

void CopyPicToWord(string path1,string path2)

path1+="\\";//图像所在文件夹路径 

path2+="\\";//word所在文件夹路径 

DirectoryInfo dir = new DirectoryInfo(path1); 

DirectoryInfo dir1 = new DirectoryInfo(path2); 

string picName = "", name = "", docName = ""; 

int index = 0; string picPath = "", docPath = ""; 

int num = 1; 

foreach (FileInfo d in dir.GetFiles("*.jpg")) 

     int count = (dir.GetFiles("*.jpg")).Count(); 

     num++; 

     picName = d.Name; 

     index = picName.LastIndexOf("."); 

     if (picName.Substring(index + 1) == "jpg") 

     { 

name = picName.Substring(0, index);

 docName = name + ".doc"; 

FileInfo d2 = new FileInfo(docName); 

if (d2.Exists)

 { 

picPath = path1 + picName; 

docPath = path2 + docName; 

wordManager.CreateNewDocument(docPath); 

wordManager.InsertPicture("picture2", picPath, picWith, picHight, 2); 

wordManager.SaveDocument(docPath); //文档路径 

}

        } 

wordManager.killWinWordProcess();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值