我正在使用PHP启动Word自动化和操作word文档,但我猜它可以在所有其他语言中完成。我需要做的是非常简单,我需要删除第一页,并添加页眉和页脚。
下面是我的代码:
$word = new COM('word.applicantion');
$word->Documents->Open('xxx.docx');
$word->Documents[1]->SaveAs($result_file_name, 12);
有样品吗?
发布于 2010-07-25 17:30:44
这就是你可以在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
来代替,因为让我们来设置宽度/高度、顶部/左侧等等。
发布于 2010-08-16 14:13:56
尝试{ $word = new COM("word.application") //$word = new COM("C:\x.docx");或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;
https://stackoverflow.com/questions/3309423
复制