C#操作word文檔和轉換成圖片


1、 word文檔操作

下面功能實現都是用com組件完成。添加引用 MSWord = Microsoft.Office.Interop.Word;

Winfrom操作界面

 

(1)獲取word文檔的總頁數

  private Word.Document _wordDocument;
  int pageNumber  = 0;
  Word.WdStatistic stat = Word.WdStatistic.wdStatisticPages;
  pageNumber   = _wordDocument.ComputeStatistics(stat, ref missing);

(2)定義頁面跳轉方法

/// <summary>
/// 跳轉到指定頁
/// </summary>
/// <param name="_wordDocument"></param>
/// <param name="n"></param>
public void GoToPage(Word.Document _wordDocument,string n)
{
object What = Word.WdGoToItem.wdGoToPage;
object Which = Word.WdGoToDirection.wdGoToNext;
object Name = n;
_wordDocument.ActiveWindow.Selection.GoTo(ref What, ref Which, ref missing, ref Name); 
_wordDocument.ActiveWindow.Selection.Paragraphs[1].Range.Text.ToString();
}

(3)上頁、下頁、首頁、尾頁跳轉

定義全局變量currentPage,記錄當前顯示的頁面

   /// <summary>
        /// 上一頁
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Preview_Click(object sender, EventArgs e)
        {
            if (currentPage-- > 1)
            {
                wordDocemrent.GoToPage(wordDocemrent.WordDocument, currentPage.ToString());
            }            
        }
        /// <summary>
        /// 下一頁
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Nextview_Click(object sender, EventArgs e)
        {
            if (currentPage++ < pageNumber)
            {
                wordDocemrent.GoToPage(wordDocemrent.WordDocument, currentPage.ToString());
            }            
        }
        /// <summary>
        /// 第一頁
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FristPage_Click(object sender, EventArgs e)
        {
            wordDocemrent.GoToPage(wordDocemrent.WordDocument, "1");
            currentPage = 1;
        }
        /// <summary>
        /// 末頁
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void EndPage_Click(object sender, EventArgs e)
        {
            wordDocemrent.GoToPage(wordDocemrent.WordDocument, pageNumber.ToString());
            currentPage = pageNumber;
        }

(4)word滾動

 從textbox空間傳入值,可操作word文檔光標位置,模擬鼠標滾動條

        /// <summary>
        /// 光標上移     
        /// </summary>
        public int MoveUp(object unit, object count, object extend)
        {
            return _wordApplication.Selection.MoveUp(ref unit, ref         count, ref extend);
        }

        /// <summary>
        /// 光標下移 
        /// </summary>
        public int MoveDown(object unit, object count, object extend)
        {
            return _wordApplication.Selection.MoveDown(ref unit, ref count, ref extend);
        }    

 (5)word 轉換成圖片                                                                                                                                                                                                                                                                                                                    

  private Bitmap[] WordtoImage(string filePath)
        {
            string tmpPath = AppDomain.CurrentDomain.BaseDirectory + "\\" + Path.GetFileName(filePath) + ".tmp";
            File.Copy(filePath, tmpFilePath);

            List<Bitmap> imageLst= new List<Bitmap>();
MSWord.Application wordApplicationClass = new MSWord.Application(); wordApplicationClass.Visible = false; object missing = System.Reflection.Missing.Value; try {object filePathObject = tmpPath; MSWord.Document document = wordApplicationClass.Documents.Open(ref filePathObject, ref missing, false, 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); bool finished = false; while (!finished) { document.Content.CopyAsPicture(); System.Windows.Forms.IDataObject data = Clipboard.GetDataObject(); if (data.GetDataPresent(DataFormats.MetafilePict)) { object obj = data.GetData(DataFormats.MetafilePict); Metafile metafile = MetafileHelper.GetEnhMetafileOnClipboard(IntPtr.Zero); Bitmap bm = new Bitmap(metafile.Width, metafile.Height); using (Graphics g = Graphics.FromImage(bm)) { g.Clear(Color.White); g.DrawImage(metafile, 0, 0, bm.Width, bm.Height); } imageLst.Add(bm);
Clipboard.Clear(); }
object Next= MSWord.WdGoToItem.wdGoToPage; object First= MSWord.WdGoToDirection.wdGoToFirst; object startIndex = "1"; document.ActiveWindow.Selection.GoTo(ref Next, ref First, ref missing, ref startIndex); MSWord.Range start = document.ActiveWindow.Selection.Paragraphs[1].Range; MSWord.Range end = start.GoToNext(MSWord.WdGoToItem.wdGoToPage); finished = (start.Start == end.Start); if (finished) { end.Start = document.Content.End; } object oStart = start.Start; object oEnd = end.Start; document.Range(ref oStart, ref oEnd).Delete(ref missing, ref missing); } ((MSWord._Document)document).Close(ref missing, ref missing, ref missing); System.Runtime.InteropServices.Marshal.ReleaseComObject(document); return imageLst.ToArray(); } catch (Exception ex) { throw ex; } finally { wordApplicationClass.Quit(ref missing, ref missing, ref missing); System.Runtime.InteropServices.Marshal.ReleaseComObject(wordApplicationClass); File.Delete(tmpFilePath); } }

 

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  

false


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM