你可能想看看http://social.msdn.microsoft.com/Forums/office/en-US/e48b3126-941d-490a-85ee-e327bbe7e81b/convert-specific-word-pages-to-pdf-in-c?forum=worddev
它展示了如何从aw获取特定范围的页面ord文件(保存格式)。
相关部分(如果链接的页面消失):
打开Word的一个实例。
Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
并加载您的文档。打开文件后,您必须准备好您的选择范围。 Count和count2是您在特例中提供的页码。
object what = WdGoToItem.wdGoToPage;
object which = WdGoToDirection.wdGoToFirst;
object count = 1;
Range startRange = word.Selection.GoTo(ref what, ref which, ref count, ref oMissing);
object count2 = (int)count + 3;
Range endRange = word.Selection.GoTo(ref what, ref which, ref count2, ref oMissing);
endRange.SetRange(startRange.Start, endRange.End - 1);
endRange.Select();
Selection.Copy()然后将所选页面复制到剪贴板,同时保留格式。
word.Selection.Copy();
来源的其余部分创建一个新的文档,在其中粘贴您的选择。
word.Documents.Add();
word.Selection.Paste();
object outputFileName = "d:\\test1.doc";
object fileFormat = WdSaveFormat.wdFormatDocument97;
word.ActiveDocument.SaveAs(ref outputFileName,
ref fileFormat, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing);
我希望这会有所帮助。
嗨@Thorias ..首先,非常感谢这个解决方案..这似乎工作,除了我有两个说明。 1.我不知道为什么以及如何使用这个 对象what = WdGoToItem.wdGoToPage; object which = WdGoToDirection.wdGoToFirst; 2.格式将保留在目标文档中,但每个页面都会添加更多空格,并且页眉/页脚会丢失。 在我提到的解决方案论坛中,整个文档被复制,包括格式和页眉/页脚 任何解决方法在此实现相同? 同样,非常感谢您... – user3816807
@ user3816807 Hi.WdGoToItem.wdGoToPage和WdGoToDirection.wdGoToFirst是Selection.Goto使用的枚举,以便它知道您希望选择的起点。在这种情况下,Selection.Goto将在由'count'指定的页面顶部设置开始范围。我昨天跑了一个简短的测试,并没有问题的页眉/页脚缺少或额外的空间添加到页面。对不起,帮不了你。 – Thorias