Universal Document Converter
製品の概要
ダウンロード
ご購入
チュートリアル
開発者の方へ
サポートサービス
fCoder SIAについて


      サイト検索
   


      Video tutorial
Show tutorial



Main page>Developer Solutions>Examples>Delphi>Word Documents to PDF

Converting Word Documents to PDF


//////////////////////////////////////////////////////////////////////////////////////////////////// // This example was designed for using in Delphi 7 or higher. // // 1. Microsoft Word 97 or above should be installed and activated on your PC. // // 2. Universal Document Converter 5.2 or above should be installed, too. // // 3. Add "Universal Document Converter Type Library" and "Microsoft Word XX.0 Object Library" type libraries to the project. // XX is the Microsoft Office version installed on your computer. // // Delphi 7: // Use the Project | Import Type Library menu. // Delphi 2006 or latter: // Use the Component | Import Component menu. // // Clear the "Generate Component Wrapper" checkbox and click the "Create Unit" button (Delphi 7) or // select the "Create Unit" option (Delphi 2006 or latter). // // 4. Notice that the number of Microsoft Word's method parameters may depend on the Office version. // //////////////////////////////////////////////////////////////////////////////////////////////////// program WordToPDF; {$APPTYPE CONSOLE} {$DEFINE LATE_BINDING} uses SysUtils, Variants, Dialogs, ActiveX, Windows, Word_TLB, UDC_TLB; procedure PrintWordToPDF(WordDocFilePath: string); var objUDC: IUDC; Printer: IUDCPrinter; Profile: IProfile; WordApp: WordApplication; WordDoc: WordDocument; FilePath: OleVariant; ReadOnly: OleVariant; Missing: OleVariant; Background: OleVariant; SaveChanges: OleVariant; begin //Create a UDC object and get its interfaces objUDC := CoAPIWrapper.Create; Printer := objUDC.get_Printers('Universal Document Converter'); Profile := Printer.Profile; //Use Universal Document Converter API to change settings of convertered document Profile.PageSetup.ResolutionX := 600; Profile.PageSetup.ResolutionY := 600; Profile.FileFormat.ActualFormat := FMT_PDF; Profile.FileFormat.PDF.ColorSpace := CS_TRUECOLOR; Profile.FileFormat.PDF.Multipage := MM_MULTI; Profile.OutputLocation.Mode := LM_PREDEFINED; Profile.OutputLocation.FolderPath := 'c:\UDC Output Files'; Profile.OutputLocation.FileName := '&[DocName(0)] -- &[Date(0)] -- &[Time(0)].&[ImageType]'; Profile.OutputLocation.OverwriteExistingFile := False; //Create a Word's Application object WordApp := CoWordApplication.Create; FilePath := WordDocFilePath; ReadOnly := True; Missing := Variants.EmptyParam; //This will be passed when ever we don稚 want to pass value //Open document from file {$IFDEF LATE_BINDING} WordDoc := IDispatch(OleVariant(WordApp).Documents.Open(FileName := FilePath, ReadOnly := ReadOnly)) as WordDocument; {$ELSE} WordDoc := WordApp.Documents.Open(FilePath, Missing, ReadOnly, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing); {$ENDIF} //Print all pages of the document Background := False; WordApp.ActivePrinter := 'Universal Document Converter'; {$IFDEF LATE_BINDING} OleVariant(WordApp).PrintOut(Background := False); {$ELSE} WordApp.PrintOut(Background, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing); {$ENDIF} //Close the document SaveChanges := False; {$IFDEF LATE_BINDING} OleVariant(WordDoc).Close(SaveChanges := SaveChanges); {$ELSE} WordDoc.Close(SaveChanges, Missing, Missing); {$ENDIF} //Quit the word application {$IFDEF LATE_BINDING} OleVariant(WordApp).Quit; {$ELSE} WordApp.Quit(Missing, Missing, Missing); {$ENDIF} end; var TestFilePath: string; begin TestFilePath := ExtractFilePath(ParamStr(0)) + 'TestFile.doc'; try CoInitialize(nil); try PrintWordToPDF(TestFilePath); finally CoUninitialize; end; except on E: Exception do MessageDlg(E.ClassName + ' : ' + E.Message, mtError, [mbOK], 0); end; end.


© fCoder SIA fCoder SIAについて | サイトマップ