บันทึกเอกสาร
งานส่วนใหญ่ที่คุณต้องดำเนินการกับ Aspose.Words คือการบันทึกเอกสาร ในการบันทึกเอกสาร Aspose.Words ให้วิธีการ Save ของคลาส Document มีการโอเวอร์โหลดที่ทำให้สามารถบันทึกเอกสารลงในไฟล์ สตรีม หรือออบเจ็กต์ ASP.NET HttpResponse เพื่อส่งไปยังเบราว์เซอร์ไคลเอ็นต์ สามารถบันทึกเอกสารในรูปแบบบันทึกใดก็ได้ที่ Aspose.Words รองรับ สำหรับรายการรูปแบบการบันทึกที่รองรับทั้งหมด โปรดดูการแจงนับ SaveFormat
บันทึกเป็นไฟล์
เพียงใช้วิธี Save พร้อมชื่อไฟล์ Aspose.Words จะกำหนดรูปแบบการบันทึกจากนามสกุลไฟล์ที่คุณระบุ
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการโหลดและบันทึกเอกสารลงในไฟล์:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
Document doc = new Document(MyDir + "Document.doc"); | |
doc.Save(ArtifactsDir + "BaseConversions.DocToDocx.docx"); |
บันทึกลงในสตรีม
ส่งออบเจ็กต์สตรีมไปยังวิธี Save จำเป็นต้องระบุรูปแบบการบันทึกอย่างชัดเจนเมื่อบันทึกลงในสตรีม
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการโหลดและบันทึกเอกสารลงในสตรีม:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
// Read only access is enough for Aspose.Words to load a document. | |
Stream stream = File.OpenRead(MyDir + "Document.docx"); | |
Document doc = new Document(stream); | |
// You can close the stream now, it is no longer needed because the document is in memory. | |
stream.Close(); | |
// ... do something with the document. | |
// Convert the document to a different format and save to stream. | |
MemoryStream dstStream = new MemoryStream(); | |
doc.Save(dstStream, SaveFormat.Rtf); | |
// Rewind the stream position back to zero so it is ready for the next reader. | |
dstStream.Position = 0; |
ส่งเอกสารไปยังไคลเอ็นต์เบราว์เซอร์
หากต้องการส่งเอกสารไปยังเบราว์เซอร์ไคลเอ็นต์ ให้ใช้โอเวอร์โหลดพิเศษที่รับพารามิเตอร์สี่ตัว ได้แก่ ชื่อไฟล์ รูปแบบการบันทึก ประเภทการบันทึก และออบเจ็กต์ ASP.NET HttpResponse วิธีการนำเสนอเอกสารต่อผู้ใช้นั้นแสดงโดยการแจงนับ ContentDisposition ซึ่งกำหนดว่าเอกสารที่ส่งไปยังเบราว์เซอร์จะให้ตัวเลือกในการเปิดตัวเองโดยตรงในเบราว์เซอร์หรือในแอปพลิเคชันที่เกี่ยวข้องกับนามสกุลไฟล์หรือไม่
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการส่งเอกสารไปยังเบราว์เซอร์ไคลเอ็นต์จากโค้ด ASP.NET:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
Document doc = new Document(MyDir + "Document.docx"); | |
// If this method overload is causing a compiler error then you are using the Client Profile DLL whereas | |
// the Aspose.Words .NET 2.0 DLL must be used instead. | |
doc.Save(ArtifactsDir + "BaseConversions.DocxToDocx.docx"); |
วิธีการ Save
โอเวอร์โหลดนี้ไม่พร้อมใช้งานเมื่อใช้ DLL โปรไฟล์ไคลเอ็นต์ .NET DLL นี้จะอยู่ในโฟลเดอร์ net3.5_ClientProfile โปรไฟล์ไคลเอ็นต์ .NET ไม่รวมแอสเซมบลี เช่น System.Web ดังนั้น HttpResponse จึงไม่พร้อมใช้งาน นี่เป็นการออกแบบทั้งหมด
สิ่งนี้อาจปรากฏเป็นข้อผิดพลาด:
“ไม่มีการโอเวอร์โหลดสำหรับวิธี ‘บันทึก’ รับพารามิเตอร์ ‘4’"
หากคุณต้องการใช้ Aspose.Words ในแอปพลิเคชัน ASP.NET ขอแนะนำให้ใช้ .NET 2.0 DLL ซึ่งมีการโอเวอร์โหลดที่ถูกต้อง ดังที่อธิบายไว้ในบทความนี้
บันทึกลงใน PCL
Aspose.Words รองรับการบันทึกเอกสารลงใน PCL (Printer Command Language) Aspose.Words สามารถบันทึกเอกสารเป็นรูปแบบ PCL 6 (PCL 6 Enhanced หรือ PCL XL) คลาส PclSaveOptions
สามารถใช้เพื่อระบุตัวเลือกเพิ่มเติมเมื่อบันทึกเอกสารเป็นรูปแบบ PCL
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการบันทึกเอกสารไปยัง PCL โดยใช้ตัวเลือกการบันทึก:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
Document doc = new Document(MyDir + "Rendering.docx"); | |
PclSaveOptions saveOptions = new PclSaveOptions { SaveFormat = SaveFormat.Pcl }; | |
doc.Save(ArtifactsDir + "BaseConversions.DocxToPcl.pcl", saveOptions); |
ดูสิ่งนี้ด้วย
- ข้อมูลเกี่ยวกับวัตถุ ASP.NET HttpResponse