2010-07-22 2 views
3

PHP를 사용하여 Word Automation을 시작하고 단어 문서를 조작하지만 다른 모든 언어로 수행 할 수 있습니다. 내가해야 할 일은 아주 간단합니다. 첫 페이지를 지우고 머리글과 바닥 글을 추가해야합니다.Word OLE 자동화 - 첫 페이지를 삭제하고 머리글과 바닥 글을 조작합니다.

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

모든 샘플 : 여기

내 코드?

+0

개체 모델에는 실제로 생각하는 것처럼 "페이지"개념이 없습니다. Word 문서는 흐름 문서이므로 첫 번째 페이지를 제거하면 이전의 두 번째 페이지 항목이 모두 포함 된 새로운 첫 페이지가 생성됩니다. 둘째, 머리말/꼬리말 추가라고하면 표준 머리글 만 찾고 있습니까 아니면 홀수/짝수 및 첫 페이지 머리글/바닥 글을 찾으십니까? –

+0

@Otaku, 첫 번째 페이지를 제거하고 두 번째 페이지로 첫 번째 새 페이지를 만들고 싶습니다. 또한 모든 페이지에 바닥 글로 .jpg를 추가하고 헤더로 일부 텍스트를 추가하려고합니다. – aviv

+0

@aviv : 아래 질문에 대한 답변이 있습니까? –

답변

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 

주 - 사진 오른쪽 크기 확인에 무거운 짐이 될 것이다. 이보다 효율적으로 관리를 원하는 경우에, 당신은 대신에하자 당신은 너비/높이, 상단/왼쪽 등

1

{ $ 워드 = 새 COM ("word.application")를 시도 설정으로 .Footers(1).Shapes.AddPicture을 사용// $ word = new COM ("C : \ x.docx"); 또는 die ("단어의 인스턴스를 만들 수 없습니다");

 //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; 
관련 문제