ในหัวข้อนี้ คุณจะได้รับความเข้าใจเกี่ยวกับวิธีแปลง XLSX เป็น PDF โดยมีสิทธิ์จำกัดโดยใช้ C# บางครั้งคุณอาจต้องการส่งต่อข้อมูลให้ผู้อื่นแต่ต้องการกำหนดข้อจำกัดบางอย่าง เช่น จำกัดไม่ให้คัดลอกหรือพิมพ์ ขั้นตอนต่อไปนี้จะแนะนำให้คุณสร้าง PDF ที่มีสิทธิ์จำกัดจาก XLSX โดยใช้ C#
ขั้นตอนในการแปลง XLSX เป็น PDF ที่มีสิทธิ์จำกัดโดยใช้ C#
- ติดตั้งแพ็คเกจ Aspose.Cells for .NET โดยใช้ NuGet Package Manager
- นำเข้า Aspose.Cells เริ่มต้นด้วยคำสั่ง
- เริ่มต้นใบอนุญาตเพื่อหลีกเลี่ยงข้อความรุ่นทดลอง
- โหลดไฟล์ XLSX ลงในอินสแตนซ์ Workbook ที่จะแปลงเป็น PDF
- สำหรับการตั้งค่าพารามิเตอร์ของเอาต์พุตไฟล์ PDF ให้เริ่มต้นอินสแตนซ์ PDFSaveOptions
- หากต้องการตั้งค่าตัวเลือกความปลอดภัย ให้เริ่มต้นพารามิเตอร์ SecurityOptions ในอินสแตนซ์ PDFSaveOptions
- ตั้งค่าการจำกัดการคัดลอก/แยกและการพิมพ์ในพารามิเตอร์ SecurityOptions
- บันทึกสมุดงานโดยใช้การตั้งค่า PDFSaveOptions
ตัวอย่างต่อไปนี้สาธิตกระบวนการส่งออก Excel เป็น PDF โดยจำกัดสิทธิ์โดยใช้ C# ที่นี่มีการโหลดไฟล์ XLSX ตัวอย่าง จากนั้นบันทึกเป็น PDF โดยมีข้อจำกัดสำหรับผู้ใช้ในการคัดลอก/แยกข้อความจากไฟล์หรือพิมพ์
โค้ดตัวอย่างสำหรับ Excel เพื่อ จำกัด สิทธิ์ PDF โดยใช้ C
using Aspose.Cells; | |
namespace ConvertXlsxToPdfHavingRestrictedPermissionsUsingCSharp | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
// Intialize license for Aspose.Cells before creation of PDF having restricted permission using C# | |
License license = new License(); | |
license.SetLicense("Aspose.Cells.lic"); | |
// Initialize workbook instance | |
Workbook workbookToBeConvertedToPDF = new Workbook(); | |
// Add some sample data | |
workbookToBeConvertedToPDF.Worksheets[0].Cells["A1"].Value = "Test Data"; | |
// Initialize PDFSaveOptions instance to set parameters for saving output PDF file | |
PdfSaveOptions pdfSaveOptions = new PdfSaveOptions(); | |
// Initialize the SecurityOptions parameter to set security options for the output PDF | |
pdfSaveOptions.SecurityOptions = new Aspose.Cells.Rendering.PdfSecurity.PdfSecurityOptions(); | |
// Restrict user from copying/extracting the contents | |
pdfSaveOptions.SecurityOptions.ExtractContentPermission = false; | |
// Restrict user to print output PDF | |
pdfSaveOptions.SecurityOptions.PrintPermission = false; | |
// Save the PDF file with the restrictions | |
workbookToBeConvertedToPDF.Save("XlsxToPdfHavingRestrictedPermissionsUsingCSharp.pdf", pdfSaveOptions); | |
} | |
} | |
} |
รหัสด้านบนสร้าง PDF โดยมีข้อจำกัดเล็กน้อย คุณสามารถเพิ่มข้อจำกัดอื่นๆ เช่น ข้อกำหนดของรหัสผ่านเพื่อเปิดได้โดยการตั้งค่า PdfSaveOptions.SecurityOptions.UserPassword โปรดทราบว่าข้อจำกัดในการคัดลอก/แยกและการพิมพ์จะยังคงมีผลบังคับใช้ และรหัสผ่านผู้ใช้ใช้สำหรับเปิด PDF เท่านั้น หากคุณต้องการอนุญาตให้ผู้ใช้เปิดไฟล์ PDF นี้โดยไม่มีข้อจำกัด ให้ตั้งค่า PdfSaveOptions.SecurityOptions.OwnerPassword โดยป้อนว่าผู้ใช้จะสามารถคัดลอก/แยกหรือพิมพ์ไฟล์ PDF นี้ได้ คุณสามารถทำการแปลงอื่นๆ เช่น แปลง Excel เป็น XPS ใน C#