是否有人已经使用Word 2007或Word 2010 pdf导出功能编写了将doc或docx转换为pdf的代码?
答案 0 :(得分:8)
我还没有到目前为止,但这应该不难:
CreateOLEObject
('Word.Application')
Documents.Open
ExportAsFixedFormat
这是一个基本的骨架:
uses
ComObj;
const
wdExportFormatPDF = 17;
var
Word, Doc: OleVariant;
begin
Word := CreateOLEObject('Word.Application');
Doc := Word.Documents.Open('C:\Document.docx');
Doc.ExportAsFixedFormat('C:\Document.pdf', wdExportFormatPDF);
end;
请注意,我已将Word
和Doc
变量声明为OleVariants,以便独立于版本(即无论您使用的是Word 2007还是2010,此代码都将有效)。如果需要,您还可以使用VCL Office组件库。如果您要在文档中进行大量处理,那肯定会更快。
答案 1 :(得分:3)
我使用以下.vbs脚本执行此操作。如果你需要Delphi代码,那么转换就很容易了:
Const wdDoNotSaveChanges = 0
Const wdRevisionsViewFinal = 0
Const wdFormatPDF = 17
Dim arguments
Set arguments = WScript.Arguments
Function DOC2PDF(sDocFile)
Dim fso ' As FileSystemObject
Dim wdo ' As Word.Application
Dim wdoc ' As Word.Document
Dim wdocs ' As Word.Documents
Set fso = CreateObject("Scripting.FileSystemObject")
sDocFile = fso.GetAbsolutePathName(sDocFile)
sPdfFile = fso.GetParentFolderName(sDocFile) + "\" + fso.GetBaseName(sDocFile) + ".pdf"
Set wdo = CreateObject("Word.Application")
Set wdocs = wdo.Documents
WScript.Echo "Opening: " + sDocFile
Set wdoc = wdocs.Open(sDocFile)
if fso.FileExists(sPdfFile) Then
fso.DeleteFile sPdfFile, True
End If
WScript.Echo "Converting to PDF: " + sPdfFile
Set wview = wdoc.ActiveWindow.View
wview.ShowRevisionsAndComments = False
wview.RevisionsView = wdRevisionsViewFinal
wdoc.SaveAs sPdfFile, wdFormatPDF
WScript.Echo "Conversion completed"
wdo.Quit wdDoNotSaveChanges
Set fso = Nothing
Set wdo = Nothing
End Function
If arguments.Count=1 Then
Call DOC2PDF(arguments.Unnamed.Item(0))
Else
WScript.Echo "Generates a PDF file from a Word document using Word PDF export."
WScript.Echo ""
WScript.Echo "Usage: doc2pdf.vbs <doc-file>"
WScript.Echo ""
End If