Created
September 24, 2025 18:08
-
-
Save carloswm85/4a40b97ac8c084e79aba2c7639ecf9a2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.IO; | |
| using System.Text; | |
| using iText.Kernel.Geom; | |
| using iText.Kernel.Pdf; | |
| using iText.Kernel.Pdf.Canvas.Draw; | |
| using iText.Layout; | |
| using iText.Layout.Element; | |
| using iText.Layout.Properties; | |
| namespace Tracker.Service.Helpers | |
| { | |
| /// <summary> | |
| /// Enhanced PDF protection utility with improved security and flexibility | |
| /// </summary> | |
| public class PdfProtection | |
| { | |
| public Stream GenerateTestDocumentWithSecurity() | |
| { | |
| string ownerPassword = "OownerPassword123"; | |
| var config = new PdfProtectionConfig() | |
| { | |
| UserPassword = string.Empty, | |
| OwnerPassword = string.Empty, | |
| Permissions = PdfPermissions.Print, | |
| Encryption = EncryptionLevel.AES256, | |
| GenerateRandomOwnerPassword = true, | |
| Title = "Protected Document", | |
| Content = "Este documento está protegido con contraseña." | |
| }; | |
| MemoryStream memoryStream = new MemoryStream(); | |
| byte[] userPasswordBytes = Encoding.UTF8.GetBytes(config.UserPassword); | |
| byte[] ownerPasswordBytes = Encoding.UTF8.GetBytes(ownerPassword); | |
| var writerProperties = new WriterProperties() | |
| .SetStandardEncryption( | |
| userPasswordBytes, | |
| ownerPasswordBytes, | |
| (int)config.Permissions, | |
| (int)config.Encryption | |
| ); | |
| using (var pdfWriter = new PdfWriter(memoryStream, writerProperties)) | |
| using (var pdfDocument = new PdfDocument(pdfWriter)) | |
| { | |
| pdfWriter.SetCloseStream(false); | |
| var pageSize = PageSize.A4; | |
| pdfDocument.SetDefaultPageSize(pageSize); | |
| Document document = new Document(pdfDocument); | |
| // Set document metadata | |
| var info = pdfDocument.GetDocumentInfo(); | |
| info.SetTitle(config.Title); | |
| info.SetCreator("PDF Protection Utility"); | |
| info.SetProducer("iText 7"); | |
| // Add content | |
| // Title | |
| var title = new Paragraph(config.Title) | |
| .SetTextAlignment(TextAlignment.CENTER) | |
| .SetFontSize(20) | |
| .SimulateBold() | |
| .SetMarginBottom(20); | |
| document.Add(title); | |
| // Separator line | |
| var line = new LineSeparator(new SolidLine()); | |
| document.Add(line); | |
| // Content | |
| var content = new Paragraph(config.Content) | |
| .SetTextAlignment(TextAlignment.JUSTIFIED) | |
| .SetMarginTop(20) | |
| .SetMarginBottom(20); | |
| document.Add(content); | |
| // Security info | |
| var securityInfo = new Paragraph() | |
| .Add("Información de Seguridad:\n") | |
| .SimulateBold() | |
| .SetMarginTop(30); | |
| securityInfo.Add(new Text($"• Nivel de cifrado: {GetEncryptionLevelDescription(config.Encryption)}\n").SimulateBold()); | |
| securityInfo.Add(new Text($"• Permisos: {GetPermissionsDescription(config.Permissions)}\n").SimulateBold()); | |
| securityInfo.Add(new Text($"• Fecha de creación: {DateTime.Now:yyyy-MM-dd HH:mm:ss}\n").SimulateBold()); | |
| document.Add(securityInfo); | |
| document.Close(); | |
| } | |
| memoryStream.Position = 0; | |
| return memoryStream; | |
| } | |
| /// <summary> | |
| /// Configuration for PDF protection | |
| /// </summary> | |
| public class PdfProtectionConfig | |
| { | |
| public string UserPassword { get; set; } = string.Empty; | |
| public string OwnerPassword { get; set; } = string.Empty; | |
| public PdfPermissions Permissions { get; set; } = PdfPermissions.Print; | |
| public EncryptionLevel Encryption { get; set; } = EncryptionLevel.AES256; | |
| public bool GenerateRandomOwnerPassword { get; set; } = true; | |
| public string Title { get; set; } = "Protected Document"; | |
| public string Content { get; set; } = "Este documento está protegido con contraseña."; | |
| } | |
| /// <summary> | |
| /// PDF permission flags that can be combined | |
| /// </summary> | |
| [Flags] | |
| public enum PdfPermissions | |
| { | |
| None = 0, | |
| Print = EncryptionConstants.ALLOW_PRINTING, | |
| PrintHighQuality = EncryptionConstants.ALLOW_PRINTING | EncryptionConstants.ALLOW_DEGRADED_PRINTING, | |
| Copy = EncryptionConstants.ALLOW_COPY, | |
| Modify = EncryptionConstants.ALLOW_MODIFY_CONTENTS, | |
| ModifyAnnotations = EncryptionConstants.ALLOW_MODIFY_ANNOTATIONS, | |
| FillForm = EncryptionConstants.ALLOW_FILL_IN, | |
| ScreenReaders = EncryptionConstants.ALLOW_SCREENREADERS, | |
| Assembly = EncryptionConstants.ALLOW_ASSEMBLY, | |
| All = Print | Copy | Modify | ModifyAnnotations | FillForm | ScreenReaders | Assembly | |
| } | |
| /// <summary> | |
| /// Encryption strength options | |
| /// </summary> | |
| public enum EncryptionLevel | |
| { | |
| Standard40Bit = EncryptionConstants.STANDARD_ENCRYPTION_40, | |
| Standard128Bit = EncryptionConstants.STANDARD_ENCRYPTION_128, | |
| AES128 = EncryptionConstants.ENCRYPTION_AES_128, | |
| AES256 = EncryptionConstants.ENCRYPTION_AES_256 | |
| } | |
| private string GetEncryptionLevelDescription(EncryptionLevel level) | |
| { | |
| switch (level) | |
| { | |
| case EncryptionLevel.Standard40Bit: | |
| return "Estándar 40-bit (Baja)"; | |
| case EncryptionLevel.Standard128Bit: | |
| return "Estándar 128-bit (Media)"; | |
| case EncryptionLevel.AES128: | |
| return "AES 128-bit (Alta)"; | |
| case EncryptionLevel.AES256: | |
| return "AES 256-bit (Muy Alta)"; | |
| default: | |
| return "Desconocido"; | |
| } | |
| } | |
| private string GetPermissionsDescription(PdfPermissions permissions) | |
| { | |
| if (permissions == PdfPermissions.None) | |
| return "Sin permisos"; | |
| if (permissions == PdfPermissions.All) | |
| return "Todos los permisos"; | |
| var permissionList = new List<string>(); | |
| if (permissions.HasFlag(PdfPermissions.Print)) | |
| { | |
| permissionList.Add("Imprimir"); | |
| } | |
| if (permissions.HasFlag(PdfPermissions.Copy)) | |
| { | |
| permissionList.Add("Copiar"); | |
| } | |
| if (permissions.HasFlag(PdfPermissions.Modify)) | |
| { | |
| permissionList.Add("Modificar"); | |
| } | |
| if (permissions.HasFlag(PdfPermissions.ModifyAnnotations)) | |
| { | |
| permissionList.Add("Anotar"); | |
| } | |
| if (permissions.HasFlag(PdfPermissions.FillForm)) | |
| { | |
| permissionList.Add("Rellenar formularios"); | |
| } | |
| return string.Join(", ", permissionList); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment