PDF管理控件Aspose.PDF for .Net使用教程(二十五):將PDF轉換為DOC和DOCX
Aspose.PDF for .NET是一種高PDF處理和解析API,用于在跨平臺應用程序中執行文檔管理和操作任務。API可以輕松用于生成、修改、轉換、渲染、保護和打印PDF文檔,而無需使用Adobe Acrobat。此外,API還提供PDF壓縮選項,表格創建和操作,圖形和圖像功能,廣泛的超鏈接功能,印章和水印任務,擴展的安全控制和自定義字體處理。
在接下來的系列教程中,將為開發者帶來Aspose.PDF for .NET的一系列使用教程,例如進行文檔間的轉換,如何標記PDF文件,如何使用表單和圖表等等。本文將介紹如何將PDF轉換為DOC和DOCX。
>>Aspose.PDF for .NET更新至最新版v20.1,歡迎下載體驗。
▲將PDF轉換為DOC
許多用戶都要求將PDF轉換為DOC:將PDF文件轉換為Microsoft Word文檔。之所以需要這樣做,是因為PDF文件不容易編輯,而Word文檔卻可以。一些公司希望其用戶能夠處理以PDF開頭的文件中的文本,表格和圖像。
Aspose.PDF for .NET允許使用兩行代碼將源PDF文件轉換為DOC文件。為了實現此功能,引入了一個名為的枚舉SaveFormat ,從而可以將源文件保存為Microsoft Word格式。以下代碼段顯示了將PDF文件轉換為DOC的過程。
// For complete examples and data files, please go to //github.com/aspose-pdf/Aspose.PDF-for-.NET // The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion(); // Open the source PDF document Document pdfDocument = new Document(dataDir + "PDFToDOC.pdf"); // Save the file into MS document format pdfDocument.Save(dataDir + "PDFToDOC_out.doc", SaveFormat.Doc);
DocSaveOptions類提供了完善的PDF文件轉換成DOC格式的過程中眾多特性。在這些屬性中,Mode可以指定PDF內容的識別模式。從而可以從RecognitionMode枚舉中為此屬性指定任何值。這些值均具有特定的優點和局限性:
- Textbox 模式可以快速且很好地保留PDF文件的原始外觀,但是結果文檔的編輯能力可能會受到限制。原始PDF中每個按視覺分組的文本塊都將轉換為輸出文檔中的文本框。這樣可以達到與原始文件的最大相似度,因此輸出文檔看起來不錯,但是它完全由文本框組成,并且可能使在Microsoft Word中進行編輯非常困難。
- Flow 是完全識別模式,其中引擎執行分組和多級分析以根據作者的意圖還原原始文檔,同時生成易于編輯的文檔。限制是輸出文檔可能看起來與原始文檔不同。
- 該 RelativeHorizontalProximity 屬性可用于控制文本元素之間的相對接近度,并且意味著距離由字體大小確定。較大的字體在音節之間的距離可能更大,但仍視為一個整體。它指定為字體大小的百分比,例如1 = 100%。這意味著相距12點的兩個12pt字符在近端。
- RecognitionBullets 用于在轉換期間打開項目符號的識別。
// For complete examples and data files, please go to //github.com/aspose-pdf/Aspose.PDF-for-.NET // The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion(); // Open the source PDF document Document pdfDocument = new Document(dataDir + "PDFToDOC.pdf"); // Save using save options // Create DocSaveOptions object DocSaveOptions saveOptions = new DocSaveOptions(); // Set the recognition mode as Flow saveOptions.Mode = DocSaveOptions.RecognitionMode.Flow; // Set the Horizontal proximity as 2.5 saveOptions.RelativeHorizontalProximity = 2.5f; // Enable the value to recognize bullets during conversion process saveOptions.RecognizeBullets = true; // Save the resultant DOC file pdfDocument.Save(dataDir + "saveOptionsOutput_out.doc", saveOptions);
▲將PDF轉換為Word DOCX
Docx是Microsoft Word文檔的一種眾所周知的格式,其結構從純二進制更改為XML和二進制文件的組合。 可以使用Word 2007和橫向版本打開Docx文件,但不能使用支持DOC文件擴展名的早期版本的MS Word打開。
DocSaveOptions類具有一個名為Format的屬性,該屬性提供了指定結果文檔格式(即DOC或DOCX)的功能。 為了將PDF文件轉換為DOCX格式,請傳遞DocSaveOptions.DocFormat枚舉中的Docx值。以下代碼片段提供了將PDF文件轉換為DOCX格式的功能。
// For complete examples and data files, please go to //github.com/aspose-pdf/Aspose.PDF-for-.NET // The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion(); // Open the source PDF document Document pdfDocument = new Document(dataDir + "PDFToDOC.pdf"); // Instantiate DocSaveOptions object DocSaveOptions saveOptions = new DocSaveOptions(); // Specify the output format as DOCX saveOptions.Format = DocSaveOptions.DocFormat.DocX; // Save document in docx format pdfDocument.Save("ConvertToDOCX_out.docx", saveOptions);
還想要更多嗎?您可以點擊閱讀【2019 · Aspose最新資源整合】,查找需要的教程資源。如果您有任何疑問或需求,請隨時,我們很高興為您提供查詢和咨詢。