在c#中使用microsoft.office.tools.word如何将单页保存为rtf文件。

问题描述 投票:0回答:1

在c#中使用microsoft.office.tools.word,我们如何将一个文档的单页保存为rtf文件。

比方说,只有第5页要保存为rtf文件,我看不到任何像Document.Pages[5]这样的东西来访问某页。

我看不到任何像Document.Pages[5]这样的东西来访问某一页。

c# api word
1个回答
1
投票

这将第2页保存为 "page2.pdf"

    Document d = wordApp.ActiveDocument;

    object what = Microsoft.Office.Interop.Word.WdGoToItem.wdGoToPage;
    object which = Microsoft.Office.Interop.Word.WdGoToDirection.wdGoToFirst;
    object count = 2;

    Range r =wordApp.Selection.GoTo(ref what, ref which, ref count);
    count = (int)count+1;
    Range r2 = wordApp.Selection.GoTo(ref what, ref which, ref count);
    r.End = r2.End;
    r.Select();

    r.ExportAsFixedFormat(@"d:\temp\page2.pdf", WdExportFormat.wdExportFormatPDF);

它应该给出了如何选择另一个页面的想法,并将其保存为RTF... ...

页面就另当别论了。

Pages pages = wordApp.ActiveDocument.ActiveWindow.ActivePane.Pages;
MessageBox.Show("This document has "+pages.Count+" pages");

但我的知识有限,只能在这两行之后停止... 😁。

EDIT:我找不到把选中的文本写成RTF的方法,但是,你可以随时把它复制到一个新的文档中,然后SaveAs......。

有了选择。

        r.Copy();
        Document dtmp = wordApp.Documents.Add();
        dtmp.Activate();
        Selection sel = wordApp.ActiveWindow.ActivePane.Selection;
        sel.Paste();
        dtmp.SaveAs2(@"d:\temp\page2.rtf", WdSaveFormat.wdFormatRTF);
        dtmp.Close();
© www.soinside.com 2019 - 2024. All rights reserved.