static void Main(string[] args) { PDFNet.Initialize(); // Relative path to the folder containing test files. string input_path = "../../TestFiles/"; string output_path = "../../TestFiles/Output/"; // Example 1: Securing a document with password protection and adjusting permissions // on the document. try { // Open the test file Console.WriteLine("-------------------------------------------------"); Console.WriteLine("Securing an existing document..."); using (PDFDoc doc = new PDFDoc(input_path + "fish.pdf")) { if (!doc.InitSecurityHandler()) { Console.WriteLine("Document authentication error..."); return; } // Perform some operation on the document. In this case we use low level SDF API // to replace the content stream of the first page with contents of file 'my_stream.txt' if (true) // Optional { Console.WriteLine("Replacing the content stream, use flate compression..."); // Get the first page dictionary using the following path: trailer/Root/Pages/Kids/0 Obj page_dict = doc.GetTrailer().Get("Root").Value(). Get("Pages").Value().Get("Kids").Value().GetAt(0); // Embed a custom stream (file mystream.txt) using Flate compression. MappedFile embed_file = new MappedFile(input_path + "my_stream.txt"); FilterReader mystm = new FilterReader(embed_file); page_dict.Put("Contents", doc.CreateIndirectStream(mystm)); embed_file.Close(); } // Apply a new security handler with given security settings. // In order to open saved PDF you will need a user password 'test'. SecurityHandler new_handler = new SecurityHandler(); // Set a new password required to open a document string my_password = "******"; new_handler.ChangeUserPassword(my_password); // Set Permissions new_handler.SetPermission(SecurityHandler.Permission.e_print, true); new_handler.SetPermission(SecurityHandler.Permission.e_extract_content, false); // Note: document takes the ownership of new_handler. doc.SetSecurityHandler(new_handler); // Save the changes. Console.WriteLine("Saving modified file..."); doc.Save(output_path + "secured.pdf", 0); } Console.WriteLine("Done. Result saved in secured.pdf"); } catch (PDFNetException e) { Console.WriteLine(e.Message); } // Example 2: Reading password protected document without user feedback. try { // In this sample case we will open an encrypted document that // requires a user password in order to access the content. Console.WriteLine("-------------------------------------------------"); Console.WriteLine("Open the password protected document from the first example..."); using (PDFDoc doc = new PDFDoc(output_path + "secured.pdf")) // Open the encrypted document that we saved in the first example. { Console.WriteLine("Initializing security handler without any user interaction..."); // At this point MySecurityHandler callbacks will be invoked. // MySecurityHandler.GetAuthorizationData() should collect the password and // AuthorizeFailed() is called if user repeatedly enters a wrong password. if (!doc.InitStdSecurityHandler("test")) { Console.WriteLine("Document authentication error..."); Console.WriteLine("The password is not valid."); return; } else { Console.WriteLine("The password is correct! Document can now be used for reading and editing"); // Remove the password security and save the changes to a new file. doc.RemoveSecurity(); doc.Save(output_path + "secured_nomore1.pdf", 0); Console.WriteLine("Done. Result saved in secured_nomore1.pdf"); } } } catch (PDFNetException e) { Console.WriteLine(e.Message); } Console.WriteLine("Tests completed."); }
static void Create3DAnnotation(PDFDoc doc, Obj annots) { // --------------------------------------------------------------------------------- // Create a 3D annotation based on U3D content. PDF 1.6 introduces the capability // for collections of three-dimensional objects, such as those used by CAD software, // to be embedded in PDF files. Obj link_3D = doc.CreateIndirectDict(); link_3D.PutName("Subtype", "3D"); // Annotation location on the page Rect bbox = new Rect(25, 180, 585, 643); link_3D.PutRect("Rect", bbox.x1, bbox.y1, bbox.x2, bbox.y2); annots.PushBack(link_3D); // The 3DA entry is an activation dictionary (see Table 9.34 in the PDF Reference Manual) // that determines how the state of the annotation and its associated artwork can change. Obj activation_dict_3D = link_3D.PutDict("3DA"); // Set the annotation so that it is activated as soon as the page containing the // annotation is opened. Other options are: PV (page view) and XA (explicit) activation. activation_dict_3D.PutName("A", "PO"); // Embed U3D Streams (3D Model/Artwork). MappedFile u3d_file = new MappedFile(input_path + "dice.u3d"); FilterReader u3d_reader = new FilterReader(u3d_file); Obj u3d_data_dict = doc.CreateIndirectStream(u3d_reader); u3d_data_dict.PutName("Subtype", "U3D"); link_3D.Put("3DD", u3d_data_dict); // Set the initial view of the 3D artwork that should be used when the annotation is activated. Obj view3D_dict = link_3D.PutDict("3DV"); view3D_dict.PutString("IN", "Unnamed"); view3D_dict.PutString("XN", "Default"); view3D_dict.PutName("MS", "M"); view3D_dict.PutNumber("CO", 27.5); // A 12-element 3D transformation matrix that specifies a position and orientation // of the camera in world coordinates. Obj tr3d = view3D_dict.PutArray("C2W"); tr3d.PushBackNumber(1); tr3d.PushBackNumber(0); tr3d.PushBackNumber(0); tr3d.PushBackNumber(0); tr3d.PushBackNumber(0); tr3d.PushBackNumber(-1); tr3d.PushBackNumber(0); tr3d.PushBackNumber(1); tr3d.PushBackNumber(0); tr3d.PushBackNumber(0); tr3d.PushBackNumber(-27.5); tr3d.PushBackNumber(0); // Create annotation appearance stream, a thumbnail which is used during printing or // in PDF processors that do not understand 3D data. Obj ap_dict = link_3D.PutDict("AP"); ElementBuilder builder = new ElementBuilder(); ElementWriter writer = new ElementWriter(); writer.Begin(doc); writer.WritePlacedElement(builder.CreateImage( Image.Create(doc, input_path + "dice.jpg"), 0, 0, bbox.Width(), bbox.Height())); Obj normal_ap_stream = writer.End(); normal_ap_stream.PutName("Subtype", "Form"); normal_ap_stream.PutRect("BBox", 0, 0, bbox.Width(), bbox.Height()); ap_dict.Put("N", normal_ap_stream); }