How to read pages in a Word document (C#)

偶尔善良 提交于 2019-12-11 04:36:22

问题


We use PDF-Focus. And we want to export PDF-documents in landscape-format to a Word-document. We want to export this document to Word. We get the document in Word. And everything looks fine. If we want to print this new word-document the margins of this document are portrait-orientated.

I tried to solve this by creating an empty Word document and then inserting a temporary exported Word-document. Then I changed the orientation to Landscape. I saw that this solution works. The document is now landscape orientated.

But now the pages with graphics and other images are overlapping the pages with tables.

So I thought I have to inserting the temporary document to the new created document by reading separate pages and then inserting with a loop.

Which functionality should we use to solve this programmatically? Or maybe there is a better solution? Can you help me?

Wesley


回答1:


Following code works for me and opens a word document changing its page orientation. were you looking for something like this?

using System;
using Microsoft.Office.Interop.Word;

namespace PageSetup
{
    class TestPageOrientation
    {
        static void Main(string[] args)
        {
            var app = new Microsoft.Office.Interop.Word.Application();
            app.Visible = true;

            //Load Document
            Document document = app.Documents.Open(@"C:\Temp\myDocument.docx");

            document.PageSetup.Orientation = WdOrientation.wdOrientLandscape;
        }
    }
}



回答2:


using System;
using Microsoft.Office.Interop.Word;

namespace PageSetup
{
    class TestPageOrientation
    {
        static void Main(string[] args)
        {
            var app = new Microsoft.Office.Interop.Word.Application();
            app.Visible = true;

            //Load Document
            Document document = app.Documents.Open(@"C:\Temp\myDocument.docx");

            // I've added this rows below. ...And that works
            document.Sections.First.PageSetup.Orientation = WdOrientation.wdOrientLandscape;
            document.Sections.Last.PageSetup.Orientation = WdOrientation.wdOrientLandscape;
            document.PageSetup.Orientation = WdOrientation.wdOrientLandscape;

            // ... and this. But the LeftMargin I can leave it.
            document.PageSetup.LeftMargin = 1.00F;
            document.Save();
        }
    }
}

I don't know how it works in the Word-library source. But I've tried WdOrientation.wdOrientPortrait and once I was surprised. I saw this page in Landscape-format.

I think there is something wrong with my document sections, because the documents (with a lot of tables, graphics and a image) is much too big. And that's only after using this method.

So my next question is: How can I shrink the size of this Word document?

And what do I have to do to limit the amount of format-settings in this word-document?




回答3:


The problem was that PDFFocus converts PDF to RTF-format. And if you change the settings, the size of the document will grow fast.

So I solved this to save it first as a RTF-document with a RTF-extension. And then I save this document as Word97-document with a DOC-extension.

I had for both conversions the same document name. Both with a DOC-extension. The Save as a Word97-document didn't work. I think it's a bug of Word 2007.



来源:https://stackoverflow.com/questions/11686761/how-to-read-pages-in-a-word-document-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!