2010-07-22 63 views
3

我正在使用PHP啓動Word自動化並操縱Word文檔,但我想它可以在所有其他語言中完成。我需要做的事很簡單,我需要刪除第一頁並添加頁眉和頁腳。字OLE自動化 - 刪除第一頁並操作頁眉和頁腳

這裏是我的代碼:

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

任何樣品?

+0

對象模型並沒有像您認爲的那樣真正擁有「頁面」的概念。 Word文檔是一個流程文檔,所以通過刪除第一頁,您將擁有一個新的第一頁,其中包含所有先前的第二頁內容。其次,當你說添加頁眉/頁腳,你只是尋找一個標準的或你是否尋找奇/偶和首頁頁眉/頁腳? – 2010-07-22 22:11:51

+0

@Otaku,我想刪除第一頁,並有一個新的第一頁作爲第二頁。而且我還想將一個.jpg作爲頁腳添加到所有頁面,並將一些文本添加到頁眉中。 – aviv 2010-07-25 08:21:18

+0

@aviv:下面的答案是否回答你的問題? – 2010-07-31 01:40:21

回答

2

這是你可以在VBA中完成的方式。這可能相當簡單地移植到PHP。在...InlineShapes.AddPicture

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 

注 - 責任將是對你保證畫面是正確的大小。如果你想在這個更多的控制,你可以使用.Footers(1).Shapes.AddPicture作爲,而不是你設置寬度/高度,頂部/左等

1

嘗試 {$ 字=新的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;