private void CreateObject ( byte[] Metadata ) { // test for first time if (Document.CatalogObject.Dictionary.Find("/Metadata") >= 0) { throw new ApplicationException("Metadata is already defined"); } // add metadata object to catalog object Document.CatalogObject.Dictionary.AddIndirectReference("/Metadata", this); // add subtype Dictionary.Add("/Subtype", "/XML"); // metadata to object value array ObjectValueArray = Metadata; // no compression NoCompression = true; // no encryption PdfEncryption SaveEncryption = Document.Encryption; Document.Encryption = null; // write stream WriteToPdfFile(); // restore encryption Document.Encryption = SaveEncryption; return; }
//////////////////////////////////////////////////////////////////// // Clear document object //////////////////////////////////////////////////////////////////// internal void Clear() { // dispose all FontApi resources foreach (PdfObject Obj in ObjectArray) { if (Obj.GetType() == typeof(PdfFont)) { ((PdfFont)Obj).FontInfo.Dispose(); } } // dispose encryption resources if (Encryption != null) { Encryption.Dispose(); Encryption = null; } // clear object array ObjectArray.Clear(); // reset resource code number Array.Clear(ResCodeNo, 0, ResCodeNo.Length); // reset string objects StrObjects.Clear(); // invoke garbage collector GC.Collect(); // exit return; }
//////////////////////////////////////////////////////////////////// /// <summary> /// Set encryption /// </summary> /// <param name="UserPassword">User password</param> /// <param name="OwnerPassword">Owner password</param> /// <param name="Permissions">Permission flags</param> /// <param name="EncryptionType">Encryption type</param> /// <remarks> /// The PDF File Writer library will encrypt the PDF document /// using either AES-128 encryption or standard 128 (RC4) encryption. /// Encryption type is specified by the last argument. Note: the /// standard 128 (RC4) is considered unsafe and should not be used. /// User and owner passwords are as per /// the two arguments. A PDF reader such as Acrobat will request the /// user to enter a password. The user can supply either the user /// or the owner password. Permissions flags are set as per argument. /// </remarks> //////////////////////////////////////////////////////////////////// public void SetEncryption ( string UserPassword, string OwnerPassword, Permission Permissions, EncryptionType EncryptionType = EncryptionType.Aes128 ) { // encryption can be set only once if (Encryption != null) { throw new ApplicationException("Encryption is already set"); } // create encryption dictionary object Encryption = new PdfEncryption(this, UserPassword, OwnerPassword, Permissions, EncryptionType); // exit }
//////////////////////////////////////////////////////////////////// /// <summary> /// Set encryption /// </summary> /// <param name="UserPassword">User password</param> /// <param name="OwnerPassword">Owner password</param> /// <param name="Permissions">Permission flags</param> /// <remarks> /// The PDF File Writer library will encrypt the PDF document /// using AES-128 encryption. /// The PDF reader will accept either user or owner passwords. /// If owner password is used to open document, the PDF reader /// will open it with all permissions set to allow operation. /// </remarks> //////////////////////////////////////////////////////////////////// public void SetEncryption ( String UserPassword, String OwnerPassword, Permission Permissions ) { // encryption can be set only once if (Encryption != null) { throw new ApplicationException("Encryption is already set"); } // create encryption dictionary object Encryption = new PdfEncryption(this, UserPassword, OwnerPassword, Permissions); // exit return; }
//////////////////////////////////////////////////////////////////// /// <summary> /// Set encryption /// </summary> /// <param name="UserPassword">User password</param> /// <param name="OwnerPassword">Owner password</param> /// <param name="Permissions">Permission flags</param> /// <param name="EncryptionType">Encryption type</param> /// <remarks> /// The PDF File Writer library will encrypt the PDF document /// using either AES-128 encryption or standard 128 (RC4) encryption. /// Encryption type is specified by the last argument. Note: the /// standard 128 (RC4) is considered unsafe and should not be used. /// User and owner passwords are as per /// the two arguments. A PDF reader such as Acrobat will request the /// user to enter a password. The user can supply either the user /// or the owner password. Permissions flags are set as per argument. /// </remarks> //////////////////////////////////////////////////////////////////// public void SetEncryption( String UserPassword, String OwnerPassword, Permission Permissions, EncryptionType EncryptionType = EncryptionType.Aes128 ) { // encryption can be set only once if(Encryption != null) throw new ApplicationException("Encryption is already set"); // create encryption dictionary object Encryption = new PdfEncryption(this, UserPassword, OwnerPassword, Permissions, EncryptionType); // exit return; }
//////////////////////////////////////////////////////////////////// /// <summary> /// Create PDF document file /// </summary> /// <remarks> /// <para>The last step of document creation after all pages were constructed.</para> /// <para>If PdfDocument was constructed with a file name, /// the CreateFile method will close the file after the file is /// written to. If the PdfDocument was constructed with a stream, /// the CreateFile does not close the stream. It is the user application /// that should close the stream after the stream was used.</para> /// </remarks> //////////////////////////////////////////////////////////////////// public void CreateFile() { // create page array StringBuilder Kids = new StringBuilder("["); for(Int32 Index = 0; Index < PageArray.Count; Index++) Kids.AppendFormat("{0} 0 R ", PageArray[Index].ObjectNumber); if(Kids.Length > 1) Kids.Length--; Kids.Append("]"); PagesObject.Dictionary.Add("/Kids", Kids.ToString()); // page count PagesObject.Dictionary.AddInteger("/Count", PageArray.Count); // objects for(Int32 Index = 0; Index < ObjectArray.Count; Index++) ObjectArray[Index].WriteObjectHeaderToPdfFile(); // save cross reference table position Int32 XRefPos = (Int32) PdfFile.BaseStream.Position; // cross reference PdfFile.WriteFormat("xref\n0 {0}\n0000000000 65535 f \n", ObjectArray.Count + 1); foreach(PdfObject PO in ObjectArray) PdfFile.WriteFormat("{0:0000000000} 00000 n \n", PO.FilePosition); // trailer PdfFile.WriteFormat("trailer\n<</Size {0}/Root 1 0 R/ID [{1}{1}]{2}>>\nstartxref\n{3}\n", ObjectArray.Count + 1, ByteArrayToPdfHexString(DocumentID), Encryption == null ? String.Empty : String.Format("/Encrypt {0} 0 R", Encryption.ObjectNumber), XRefPos); // write PDF end of file marker PdfFile.WriteString("%%EOF\n"); // close file Dispose(); // dispose all FontApi resources foreach(PdfObject Obj in ObjectArray) { if(Obj.GetType() == typeof(PdfFont)) ((PdfFont) Obj).FontInfo.Dispose(); } // dispose encryption resources if(Encryption != null) { Encryption.Dispose(); Encryption = null; } // successful exit return; }
//////////////////////////////////////////////////////////////////// /// <summary> /// Create PDF document file /// </summary> /// <remarks> /// The last step of document creation after all pages were constructed. /// FileName is the path and name of the output file. /// </remarks> //////////////////////////////////////////////////////////////////// public void CreateFile() { // create page array StringBuilder Kids = new StringBuilder("["); for (Int32 Index = 0; Index < PageArray.Count; Index++) { Kids.AppendFormat("{0} 0 R ", PageArray[Index].ObjectNumber); } if (Kids.Length > 1) { Kids.Length--; } Kids.Append("]"); PagesObject.Dictionary.Add("/Kids", Kids.ToString()); // page count PagesObject.Dictionary.AddInteger("/Count", PageArray.Count); // objects for (Int32 Index = 0; Index < ObjectArray.Count; Index++) { ObjectArray[Index].WriteObjectHeaderToPdfFile(); } // save cross reference table position Int32 XRefPos = (Int32)PdfFile.BaseStream.Position; // cross reference PdfFile.WriteFormat("xref\n0 {0}\n0000000000 65535 f \n", ObjectArray.Count + 1); foreach (PdfObject PO in ObjectArray) { PdfFile.WriteFormat("{0:0000000000} 00000 n \n", PO.FilePosition); } // trailer PdfFile.WriteFormat("trailer\n<</Size {0}/Root 1 0 R/ID [{1}{1}]{2}>>\nstartxref\n{3}\n", ObjectArray.Count + 1, ByteArrayToPdfHexString(DocumentID), Encryption == null ? String.Empty : String.Format("/Encrypt {0} 0 R", Encryption.ObjectNumber), XRefPos); // write PDF end of file marker PdfFile.WriteString("%%EOF\n"); // close file PdfFile.Close(); // dispose all FontApi resources foreach (PdfObject Obj in ObjectArray) { if (Obj.GetType() == typeof(PdfFont)) { ((PdfFont)Obj).FontInfo.Dispose(); } } // dispose encryption resources if (Encryption != null) { Encryption.Dispose(); Encryption = null; } // successful exit return; }