Word OLE 自动化 - 删除首页并操作页眉和页脚

发布于 2024-09-11 03:24:28 字数 289 浏览 3 评论 0原文

我正在使用 PHP 来启动 Word Automation 并操作 Word 文档,但我想它可以用所有其他语言来完成。我需要做的很简单,我需要删除第一页并添加页眉和页脚。

这是我的代码:

 $word = new COM('word.applicantion');
 $word->Documents->Open('xxx.docx');
 $word->Documents[1]->SaveAs($result_file_name, 12);

有示例吗?

I am using PHP to start Word Automation and manipulate word documents, but i guess it can be done in all any other language. What i need to do is quite simple, i need to remove the first page and add header and footer.

Here is my code:

 $word = new COM('word.applicantion');
 $word->Documents->Open('xxx.docx');
 $word->Documents[1]->SaveAs($result_file_name, 12);

Any samples?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

栖迟 2024-09-18 03:24:28

这就是您在 VBA 中可以实现的方法。这可能可以相当简单地移植到 PHP。

Sub RemoveFirstPageAndAddHeaderFooter()
    Dim d As Document
    Set d = ActiveDocument
    Dim pageOne As Range
    Set pageOne = d.Bookmarks("\page").Range
    pageOne.Select
    Selection.Delete
    d.Sections(1).Headers(1).Range.Text = "Some text"
    d.Sections(1).Footers(1).Range.InlineShapes.AddPicture "C:\beigeplum.jpg", False, True
End Sub

请注意 ...InlineShapes.AddPicture - 您有责任确保图片尺寸正确。如果您想对此进行更多控制,您可以使用 .Footers(1).Shapes.AddPicture 来代替,因为这样您就可以设置宽度/高度、顶部/左侧等。

This is the way you could do it in VBA. This can likely be ported to PHP fairly simply.

Sub RemoveFirstPageAndAddHeaderFooter()
    Dim d As Document
    Set d = ActiveDocument
    Dim pageOne As Range
    Set pageOne = d.Bookmarks("\page").Range
    pageOne.Select
    Selection.Delete
    d.Sections(1).Headers(1).Range.Text = "Some text"
    d.Sections(1).Footers(1).Range.InlineShapes.AddPicture "C:\beigeplum.jpg", False, True
End Sub

Note on the ...InlineShapes.AddPicture - the onus would be on you to ensure the picture is the right size. If you want more control over this, you would use .Footers(1).Shapes.AddPicture instead as that let's you set the width/height, top/left, etc.

愛上了 2024-09-18 03:24:28

尝试
{
$word = new COM("word.application") //$word = new COM("C:\x.docx");
或死(“无法创建单词的实例”);

        //bring word to the front
        $word->Visible = 1;

        //open a word document
        $word->Documents->Open("file.docx");

        // remove first page
        $range = $word->ActiveDocument->Bookmarks("\page");
        $range->Select();
        $word->Selection->Delete();

        //save the document as docx
        $word->Documents[1]->SaveAs("modified_file.docx", 12); // SaveAs('filename', format) // format: 0 - same?, 1 - doc?, 2 - text,  4 - text other encoding
    }
    catch(Exception $e)
    {
        echo "error class.document.php - convert_to_docx: $e 20100816.01714";
    }

    //close word
    if($word)
        $word->Quit();

    //free object resources
    //$word->Release();
    $word = null;

try
{
$word = new COM("word.application") //$word = new COM("C:\x.docx");
or die("couldnt create an instance of word");

        //bring word to the front
        $word->Visible = 1;

        //open a word document
        $word->Documents->Open("file.docx");

        // remove first page
        $range = $word->ActiveDocument->Bookmarks("\page");
        $range->Select();
        $word->Selection->Delete();

        //save the document as docx
        $word->Documents[1]->SaveAs("modified_file.docx", 12); // SaveAs('filename', format) // format: 0 - same?, 1 - doc?, 2 - text,  4 - text other encoding
    }
    catch(Exception $e)
    {
        echo "error class.document.php - convert_to_docx: $e 20100816.01714";
    }

    //close word
    if($word)
        $word->Quit();

    //free object resources
    //$word->Release();
    $word = null;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文