Esempio n. 1
4
        /// <summary>
        /// uses itextsharp 4.1.6 to convert any pdf to 1.4 compatible pdf, called instead of PdfReader.open
        /// </summary>
        public static PdfDocument Open(MemoryStream sourceStream, PdfDocumentOpenMode openmode)
        {
            PdfDocument outDoc = null;
            sourceStream.Position = 0;

            try
            {
                outDoc = PdfReader.Open(sourceStream, openmode);
            }
            catch (PdfSharp.Pdf.IO.PdfReaderException)
            {
                //workaround if pdfsharp doesn't support this pdf
                sourceStream.Position = 0;
                MemoryStream outputStream = new MemoryStream();
                iTextSharp.text.pdf.PdfReader reader = new iTextSharp.text.pdf.PdfReader(sourceStream);
                iTextSharp.text.pdf.PdfStamper pdfStamper = new iTextSharp.text.pdf.PdfStamper(reader, outputStream);
                pdfStamper.FormFlattening = true;
                pdfStamper.Writer.SetPdfVersion(iTextSharp.text.pdf.PdfWriter.PDF_VERSION_1_4);
                pdfStamper.Writer.CloseStream = false;
                pdfStamper.Close();

                outDoc = PdfReader.Open(outputStream, openmode);
            }

            return outDoc;
        }
Esempio n. 2
0
 public PDFDoc(string filename, PdfDocumentOpenMode openmode)
 {
     if (filename == null)
         throw new ArgumentNullException("filename is null");
     this.filename = filename;
     this.openMode = openmode;
     this.document = PDFDoc.LoadPDF(FileName, openMode);
 }
Esempio n. 3
0
        /// <summary>
        /// uses itextsharp 4.1.6 to convert any pdf to 1.4 compatible pdf, called instead of PdfReader.open
        /// </summary>
        public static PdfDocument Open(string PdfPath, PdfDocumentOpenMode openmode)
        {
            using (FileStream fileStream = new FileStream(PdfPath, FileMode.Open, FileAccess.Read))
            {
                int len = (int)fileStream.Length;
                Byte[] fileArray = new Byte[len];
                fileStream.Read(fileArray, 0, len);
                fileStream.Close();

                return Open(fileArray, openmode);
            }
        }
Esempio n. 4
0
 //opens PdfDocument
 public static PdfDocument LoadPDF(string filename, PdfDocumentOpenMode openmode)
 {
     try
     {
         return PdfReader.Open(filename, openmode);
     }
     catch (PdfReaderException ex)
     {
         //if password protected - returns null
         if (ex.Message == "To modify the document the owner password is required")
         {
             MessageBox.Show("При открытии файла произошла ошибка (возможно, он защищён)", "Ошибка!");
             return null; //TODO: open file with import mode and return with openmode
             //otherwise, it seems file is v1.6 and higher
             //trying to open via iTextSharp to
         }
         return PdfReader.Open(GetCompartibleStream(filename), openmode);
     }
 }
Esempio n. 5
0
 /// <summary>
 /// Opens an existing PDF document.
 /// </summary>
 public static PdfDocument Open(Stream stream, PdfDocumentOpenMode openmode)
 {
   return Open(stream, null, openmode);
 }
Esempio n. 6
0
 /// <summary>
 /// Opens an existing PDF document.
 /// </summary>
 public static PdfDocument Open(Stream stream, PdfDocumentOpenMode openmode, PdfPasswordProvider passwordProvider)
 {
   return Open(stream, null, openmode);
 }
Esempio n. 7
0
 /// <summary>
 /// Opens an existing PDF document.
 /// </summary>
 public static PdfDocument Open(string path, PdfDocumentOpenMode openmode, PdfPasswordProvider provider)
 {
   return Open(path, null, openmode, provider);
 }
Esempio n. 8
0
 /// <summary>
 /// Opens an existing PDF document.
 /// </summary>
 public static PdfDocument Open(string path, string password, PdfDocumentOpenMode openmode, PdfPasswordProvider provider)
 {
   PdfDocument document;
   Stream stream = new FileStream(path, FileMode.Open, FileAccess.Read);
   try
   {
     document = PdfReader.Open(stream, password, openmode, provider);
     if (document != null)
     {
       document.fullPath = Path.GetFullPath(path);
     }
   }
   finally
   {
     if (stream != null)
       stream.Close();
   }
   return document;
 }
Esempio n. 9
0
        static PdfDocument readExistingPDFDocument(String filename, String generatedFilename, String password, PdfDocumentOpenMode mode)
        {
            PdfDocument dstDoc = null;

            try
            {
                dstDoc = PdfReader.Open(filename, mode);
            }
            catch (PdfReaderException)
            {
                if (password.Length != 0)
                {
                    try
                    {
                        dstDoc = PdfReader.Open(filename, password, mode);
                    }
                    catch (PdfReaderException)
                    {
                        if (File.Exists(generatedFilename))
                        {
                            File.Delete(generatedFilename);
                        }
                        Console.WriteLine("Unable to modify a protected PDF. Invalid owner password given.");
                        // Return the general failure code and the specific failure code
                        Environment.Exit((int)ExitCode.PDFProtectedDocument);
                    }
                }
                else
                {
                    // There is no owner password, we can not open this
                    if (File.Exists(generatedFilename))
                    {
                        File.Delete(generatedFilename);
                    }
                    Console.WriteLine("Unable to modify a protected PDF. Use the /pdf_owner_pass option to specify an owner password.");
                    // Return the general failure code and the specific failure code
                    Environment.Exit((int)ExitCode.PDFProtectedDocument);
                }
            }
            return(dstDoc);
        }
Esempio n. 10
0
 /// <summary>
 /// Opens an existing PDF document.
 /// </summary>
 public static PdfDocument Open(string path, PdfDocumentOpenMode openmode)
 {
   return Open(path, null, openmode, null);
 }
Esempio n. 11
0
 /// <summary>
 /// Opens an existing PDF document.
 /// </summary>
 public static PdfDocument Open(string path, PdfDocumentOpenMode openmode, PdfPasswordProvider provider)
 {
     return(Open(path, null, openmode, provider));
 }
Esempio n. 12
0
 /// <summary>
 /// uses itextsharp 4.1.6 to convert any pdf to 1.4 compatible pdf, called instead of PdfReader.open
 /// </summary>
 public static PdfDocument Open(byte[] fileArray, PdfDocumentOpenMode openmode)
 {
     return Open(new MemoryStream(fileArray), openmode);
 }
Esempio n. 13
0
 /// <summary>
 /// Opens an existing PDF document.
 /// </summary>
 public static PdfDocument Open(Stream stream, string password, PdfDocumentOpenMode openmode)
 {
   return Open(stream, password, openmode, null);
 }
        /// <summary>
        /// Opens an existing PDF document asynchronously.
        /// </summary>
        public static async Task <PdfDocument> OpenAsync(
            Stream stream,
            string password = null,
            PdfDocumentOpenMode openmode         = PdfDocumentOpenMode.Modify,
            PdfPasswordProvider passwordProvider = null)
        {
            PdfDocument document;

            try
            {
                Lexer lexer = new Lexer(stream);
                document           = new PdfDocument(lexer);
                document._state   |= DocumentState.Imported;
                document._openMode = openmode;
                document._fileSize = stream.Length;

                // Get file version.
                byte[] header = new byte[1024];
                stream.Position = 0;
                stream.Read(header, 0, 1024);
                document._version = GetPdfFileVersion(header);
                if (document._version == 0)
                {
                    throw new InvalidOperationException(PSSR.InvalidPdf);
                }

                document._irefTable.IsUnderConstruction = true;
                Parser parser = new Parser(document);
                // Read all trailers or cross-reference streams, but no objects.
                document._trailer = await parser.ReadTrailerAsync();

                if (document._trailer == null)
                {
                    ParserDiagnostics.ThrowParserException("Invalid PDF file: no trailer found."); // TODO L10N using PSSR.
                }
                Debug.Assert(document._irefTable.IsUnderConstruction);
                document._irefTable.IsUnderConstruction = false;

                // Is document encrypted?
                PdfReference xrefEncrypt = document._trailer.Elements[PdfTrailer.Keys.Encrypt] as PdfReference;
                if (xrefEncrypt != null)
                {
                    PdfObject encrypt = await parser.ReadObjectAsync(null, xrefEncrypt.ObjectID, false, false);

                    encrypt.Reference = xrefEncrypt;
                    xrefEncrypt.Value = encrypt;
                    PdfStandardSecurityHandler securityHandler = document.SecurityHandler;
TryAgain:
                    PasswordValidity validity = securityHandler.ValidatePassword(password);
                    if (validity == PasswordValidity.Invalid)
                    {
                        if (passwordProvider != null)
                        {
                            PdfPasswordProviderArgs args = new PdfPasswordProviderArgs();
                            passwordProvider(args);
                            if (args.Abort)
                            {
                                return(null);
                            }
                            password = args.Password;
                            goto TryAgain;
                        }
                        else
                        {
                            if (password == null)
                            {
                                throw new PdfReaderException(PSSR.PasswordRequired);
                            }
                            else
                            {
                                throw new PdfReaderException(PSSR.InvalidPassword);
                            }
                        }
                    }
                    else if (validity == PasswordValidity.UserPassword && openmode == PdfDocumentOpenMode.Modify)
                    {
                        if (passwordProvider != null)
                        {
                            PdfPasswordProviderArgs args = new PdfPasswordProviderArgs();
                            passwordProvider(args);
                            if (args.Abort)
                            {
                                return(null);
                            }
                            password = args.Password;
                            goto TryAgain;
                        }
                        else
                        {
                            throw new PdfReaderException(PSSR.OwnerPasswordRequired);
                        }
                    }
                }
                else
                {
                    if (password != null)
                    {
                        // Password specified but document is not encrypted.
                        // ignore
                    }
                }

                PdfReference[] irefs2 = document._irefTable.AllReferences;
                int            count2 = irefs2.Length;

                // 3rd: Create iRefs for all compressed objects.
                Dictionary <int, object> objectStreams = new Dictionary <int, object>();
                for (int idx = 0; idx < count2; idx++)
                {
                    PdfReference iref = irefs2[idx];
                    if (iref.Value is PdfCrossReferenceStream xrefStream)
                    {
                        for (int idx2 = 0; idx2 < xrefStream.Entries.Count; idx2++)
                        {
                            PdfCrossReferenceStream.CrossReferenceStreamEntry item = xrefStream.Entries[idx2];
                            // Is type xref to compressed object?
                            if (item.Type == 2)
                            {
                                //PdfReference irefNew = parser.ReadCompressedObject(new PdfObjectID((int)item.Field2), (int)item.Field3);
                                //document._irefTable.Add(irefNew);
                                int objectNumber = (int)item.Field2;
                                if (!objectStreams.ContainsKey(objectNumber))
                                {
                                    objectStreams.Add(objectNumber, null);
                                    PdfObjectID objectID = new PdfObjectID((int)item.Field2);
                                    parser.ReadIRefsFromCompressedObject(objectID);
                                }
                            }
                        }
                    }
                }

                // 4th: Read compressed objects.
                for (int idx = 0; idx < count2; idx++)
                {
                    PdfReference iref = irefs2[idx];
                    if (iref.Value is PdfCrossReferenceStream xrefStream)
                    {
                        for (int idx2 = 0; idx2 < xrefStream.Entries.Count; idx2++)
                        {
                            PdfCrossReferenceStream.CrossReferenceStreamEntry item = xrefStream.Entries[idx2];
                            // Is type xref to compressed object?
                            if (item.Type == 2)
                            {
                                PdfReference irefNew = parser.ReadCompressedObject(new PdfObjectID((int)item.Field2),
                                                                                   (int)item.Field3);
                                Debug.Assert(document._irefTable.Contains(iref.ObjectID));
                                //document._irefTable.Add(irefNew);
                            }
                        }
                    }
                }


                PdfReference[] irefs = document._irefTable.AllReferences;
                int            count = irefs.Length;

                // Read all indirect objects.
                for (int idx = 0; idx < count; idx++)
                {
                    PdfReference iref = irefs[idx];
                    if (iref.Value == null)
                    {
#if DEBUG_
                        if (iref.ObjectNumber == 1074)
                        {
                            iref.GetType();
                        }
#endif
                        try
                        {
                            Debug.Assert(document._irefTable.Contains(iref.ObjectID));
                            PdfObject pdfObject = await parser.ReadObjectAsync(null, iref.ObjectID, false, false);

                            Debug.Assert(pdfObject.Reference == iref);
                            pdfObject.Reference = iref;
                            Debug.Assert(pdfObject.Reference.Value != null, "Something went wrong.");
                        }
                        catch (Exception ex)
                        {
                            Debug.WriteLine(ex.Message);
                            // 4STLA rethrow exception to notify caller.
                            throw;
                        }
                    }
                    else
                    {
                        Debug.Assert(document._irefTable.Contains(iref.ObjectID));
                        //iref.GetType();
                    }
                    // Set maximum object number.
                    document._irefTable._maxObjectNumber = Math.Max(document._irefTable._maxObjectNumber,
                                                                    iref.ObjectNumber);
                }

                // Decrypt all objects.
                if (xrefEncrypt != null)
                {
                    document.SecurityHandler.DecryptDocument();
                }

                // Fix references of trailer values and then objects and irefs are consistent.
                document._trailer.Finish();

#if DEBUG_
                // Some tests...
                PdfReference[] reachables = document.xrefTable.TransitiveClosure(document.trailer);
                reachables.GetType();
                reachables = document.xrefTable.AllXRefs;
                document.xrefTable.CheckConsistence();
#endif

                if (openmode == PdfDocumentOpenMode.Modify)
                {
                    // Create new or change existing document IDs.
                    if (document.Internals.SecondDocumentID == "")
                    {
                        document._trailer.CreateNewDocumentIDs();
                    }
                    else
                    {
                        byte[] agTemp = Guid.NewGuid().ToByteArray();
                        document.Internals.SecondDocumentID = PdfEncoders.RawEncoding.GetString(agTemp, 0, agTemp.Length);
                    }

                    // Change modification date
                    document.Info.ModificationDate = DateTime.Now;

                    // Remove all unreachable objects
                    int removed = document._irefTable.Compact();
                    if (removed != 0)
                    {
                        Debug.WriteLine("Number of deleted unreachable objects: " + removed);
                    }

                    // Force flattening of page tree
                    PdfPages pages = document.Pages;
                    Debug.Assert(pages != null);

                    //bool b = document.irefTable.Contains(new PdfObjectID(1108));
                    //b.GetType();

                    document._irefTable.CheckConsistence();
                    document._irefTable.Renumber();
                    document._irefTable.CheckConsistence();
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                throw;
            }
            return(document);
        }
Esempio n. 15
0
 public bool OpenPDF(PdfDocumentOpenMode openmode)
 {
     //Открываем файл
     OpenFileDialog dlg = new OpenFileDialog();
     dlg.Filter = "PDF files|*.pdf";
     if (true == dlg.ShowDialog())
     {
         this.filename = dlg.FileName;
         this.openMode = openmode;
         this.document = PDFDoc.LoadPDF(filename, openmode);
         if (document == null)
         {
             return false;
         }
         return true;
     }
     else
     {
         return false;
     }
 }
Esempio n. 16
0
        /// <summary>
        /// Opens an existing PDF document.
        /// </summary>
        public static PdfDocument Open(Stream stream, string password, PdfDocumentOpenMode openmode, PdfPasswordProvider passwordProvider)
        {
            PdfDocument document;
            try
            {
                Lexer lexer = new Lexer(stream);
                document = new PdfDocument(lexer);
                document._state |= DocumentState.Imported;
                document._openMode = openmode;
                document._fileSize = stream.Length;

                // Get file version.
                byte[] header = new byte[1024];
                stream.Position = 0;
                stream.Read(header, 0, 1024);
                document._version = GetPdfFileVersion(header);
                if (document._version == 0)
                    throw new InvalidOperationException(PSSR.InvalidPdf);

                document._irefTable.IsUnderConstruction = true;
                Parser parser = new Parser(document);
                // Read all trailers or cross-reference streams, but no objects.
                document._trailer = parser.ReadTrailer();

                Debug.Assert(document._irefTable.IsUnderConstruction);
                document._irefTable.IsUnderConstruction = false;

                // Is document encrypted?
                PdfReference xrefEncrypt = document._trailer.Elements[PdfTrailer.Keys.Encrypt] as PdfReference;
                if (xrefEncrypt != null)
                {
                    //xrefEncrypt.Value = parser.ReadObject(null, xrefEncrypt.ObjectID, false);
                    PdfObject encrypt = parser.ReadObject(null, xrefEncrypt.ObjectID, false, false);

                    encrypt.Reference = xrefEncrypt;
                    xrefEncrypt.Value = encrypt;
                    PdfStandardSecurityHandler securityHandler = document.SecurityHandler;
                    TryAgain:
                    PasswordValidity validity = securityHandler.ValidatePassword(password);
                    if (validity == PasswordValidity.Invalid)
                    {
                        if (passwordProvider != null)
                        {
                            PdfPasswordProviderArgs args = new PdfPasswordProviderArgs();
                            passwordProvider(args);
                            if (args.Abort)
                                return null;
                            password = args.Password;
                            goto TryAgain;
                        }
                        else
                        {
                            if (password == null)
                                throw new PdfReaderException(PSSR.PasswordRequired);
                            else
                                throw new PdfReaderException(PSSR.InvalidPassword);
                        }
                    }
                    else if (validity == PasswordValidity.UserPassword && openmode == PdfDocumentOpenMode.Modify)
                    {
                        if (passwordProvider != null)
                        {
                            PdfPasswordProviderArgs args = new PdfPasswordProviderArgs();
                            passwordProvider(args);
                            if (args.Abort)
                                return null;
                            password = args.Password;
                            goto TryAgain;
                        }
                        else
                            throw new PdfReaderException(PSSR.OwnerPasswordRequired);
                    }
                }
                else
                {
                    if (password != null)
                    {
                        // Password specified but document is not encrypted.
                        // ignore
                    }
                }

                PdfReference[] irefs2 = document._irefTable.AllReferences;
                int count2 = irefs2.Length;

                // 3rd: Create iRefs for all compressed objects.
                Dictionary<int, object> objectStreams = new Dictionary<int, object>();
                for (int idx = 0; idx < count2; idx++)
                {
                    PdfReference iref = irefs2[idx];
                    PdfCrossReferenceStream xrefStream = iref.Value as PdfCrossReferenceStream;
                    if (xrefStream != null)
                    {
                        for (int idx2 = 0; idx2 < xrefStream.Entries.Count; idx2++)
                        {
                            PdfCrossReferenceStream.CrossReferenceStreamEntry item = xrefStream.Entries[idx2];
                            // Is type xref to compressed object?
                            if (item.Type == 2)
                            {
                                //PdfReference irefNew = parser.ReadCompressedObject(new PdfObjectID((int)item.Field2), (int)item.Field3);
                                //document._irefTable.Add(irefNew);
                                int objectNumber = (int)item.Field2;
                                if (!objectStreams.ContainsKey(objectNumber))
                                {
                                    objectStreams.Add(objectNumber, null);
                                    PdfObjectID objectID = new PdfObjectID((int)item.Field2);
                                    parser.ReadIRefsFromCompressedObject(objectID);
                                }
                            }
                        }
                    }
                }

                // 4th: Read compressed objects.
                for (int idx = 0; idx < count2; idx++)
                {
                    PdfReference iref = irefs2[idx];
                    PdfCrossReferenceStream xrefStream = iref.Value as PdfCrossReferenceStream;
                    if (xrefStream != null)
                    {
                        for (int idx2 = 0; idx2 < xrefStream.Entries.Count; idx2++)
                        {
                            PdfCrossReferenceStream.CrossReferenceStreamEntry item = xrefStream.Entries[idx2];
                            // Is type xref to compressed object?
                            if (item.Type == 2)
                            {
                                PdfReference irefNew = parser.ReadCompressedObject(new PdfObjectID((int)item.Field2),
                                    (int)item.Field3);
                                Debug.Assert(document._irefTable.Contains(iref.ObjectID));
                                //document._irefTable.Add(irefNew);
                            }
                        }
                    }
                }


                PdfReference[] irefs = document._irefTable.AllReferences;
                int count = irefs.Length;

                // Read all indirect objects.
                for (int idx = 0; idx < count; idx++)
                {
                    PdfReference iref = irefs[idx];
                    if (iref.Value == null)
                    {
#if DEBUG_
                        if (iref.ObjectNumber == 1074)
                            iref.GetType();
#endif
                        try
                        {
                            Debug.Assert(document._irefTable.Contains(iref.ObjectID));
                            PdfObject pdfObject = parser.ReadObject(null, iref.ObjectID, false, false);
                            Debug.Assert(pdfObject.Reference == iref);
                            pdfObject.Reference = iref;
                            Debug.Assert(pdfObject.Reference.Value != null, "Something went wrong.");
                        }
                        catch (Exception ex)
                        {
                            Debug.WriteLine(ex.Message);
                            // 4STLA rethrow exception to notify caller.
                            throw;
                        }
                    }
                    else
                    {
                        Debug.Assert(document._irefTable.Contains(iref.ObjectID));
                        //iref.GetType();
                    }
                    // Set maximum object number.
                    document._irefTable._maxObjectNumber = Math.Max(document._irefTable._maxObjectNumber,
                        iref.ObjectNumber);
                }

                // Encrypt all objects.
                if (xrefEncrypt != null)
                {
                    document.SecurityHandler.EncryptDocument();
                }

                // Fix references of trailer values and then objects and irefs are consistent.
                document._trailer.Finish();

#if DEBUG_
    // Some tests...
                PdfReference[] reachables = document.xrefTable.TransitiveClosure(document.trailer);
                reachables.GetType();
                reachables = document.xrefTable.AllXRefs;
                document.xrefTable.CheckConsistence();
#endif

                if (openmode == PdfDocumentOpenMode.Modify)
                {
                    // Create new or change existing document IDs.
                    if (document.Internals.SecondDocumentID == "")
                        document._trailer.CreateNewDocumentIDs();
                    else
                    {
                        byte[] agTemp = Guid.NewGuid().ToByteArray();
                        document.Internals.SecondDocumentID = PdfEncoders.RawEncoding.GetString(agTemp, 0, agTemp.Length);
                    }

                    // Change modification date
                    document.Info.ModificationDate = DateTime.Now;

                    // Remove all unreachable objects
                    int removed = document._irefTable.Compact();
                    if (removed != 0)
                        Debug.WriteLine("Number of deleted unreachable objects: " + removed);

                    // Force flattening of page tree
                    PdfPages pages = document.Pages;
                    Debug.Assert(pages != null);

                    //bool b = document.irefTable.Contains(new PdfObjectID(1108));
                    //b.GetType();

                    document._irefTable.CheckConsistence();
                    document._irefTable.Renumber();
                    document._irefTable.CheckConsistence();
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                throw;
            }
            return document;
        }
Esempio n. 17
0
        /// <summary>
        /// Opens an existing PDF document.
        /// </summary>
        public static PdfDocument Open(string path, string password, PdfDocumentOpenMode openmode, PdfPasswordProvider provider)
        {
#if !NETFX_CORE
            PdfDocument document;
            Stream stream = null;
            try
            {
                stream = new FileStream(path, FileMode.Open, FileAccess.Read);
                document = Open(stream, password, openmode, provider);
                if (document != null)
                {
                    document._fullPath = Path.GetFullPath(path);
                }
            }
            finally
            {
                if (stream != null)
#if !UWP
                    stream.Close();
#else
                    stream.Dispose();
#endif
            }
            return document;
#else
                    return null;
#endif
        }
Esempio n. 18
0
 /// <summary>
 /// Opens an existing PDF document.
 /// </summary>
 public static PdfDocument Open(Stream stream, PdfDocumentOpenMode openmode)
 {
     return(Open(stream, null, openmode));
 }
Esempio n. 19
0
 /// <summary>
 /// Opens an existing PDF document.
 /// </summary>
 public static PdfDocument Open(string path, string password, PdfDocumentOpenMode openmode)
 {
     return(Open(path, password, openmode, null));
 }
Esempio n. 20
0
    /// <summary>
    /// Opens an existing PDF document.
    /// </summary>
    public static PdfDocument Open(Stream stream, string password, PdfDocumentOpenMode openmode, PdfPasswordProvider passwordProvider)
    {
      PdfDocument document = null;
      try
      {
        Lexer lexer = new Lexer(stream);
        document = new PdfDocument(lexer);
        document.state |= DocumentState.Imported;
        document.openMode = openmode;
        document.fileSize = stream.Length;

        // Get file version
        byte[] header = new byte[1024];
        stream.Position = 0;
        stream.Read(header, 0, 1024);
        document.version = GetPdfFileVersion(header);
        if (document.version == 0)
          throw new InvalidOperationException(PSSR.InvalidPdf);

        // Read all trailers
        document.irefTable.IsUnderConstruction = true;
        Parser parser = new Parser(document);
        document.trailer = parser.ReadTrailer();

        document.irefTable.IsUnderConstruction = false;

        // Is document encrypted?
        PdfReference xrefEncrypt = document.trailer.Elements[PdfTrailer.Keys.Encrypt] as PdfReference;
        if (xrefEncrypt != null)
        {
          //xrefEncrypt.Value = parser.ReadObject(null, xrefEncrypt.ObjectID, false);
          PdfObject encrypt = parser.ReadObject(null, xrefEncrypt.ObjectID, false);

          encrypt.Reference = xrefEncrypt;
          xrefEncrypt.Value = encrypt;
          PdfStandardSecurityHandler securityHandler = document.SecurityHandler;
        TryAgain:
          PasswordValidity validity = securityHandler.ValidatePassword(password);
          if (validity == PasswordValidity.Invalid)
          {
            if (passwordProvider != null)
            {
              PdfPasswordProviderArgs args = new PdfPasswordProviderArgs();
              passwordProvider(args);
              if (args.Abort)
                return null;
              password = args.Password;
              goto TryAgain;
            }
            else
            {
              if (password == null)
                throw new PdfReaderException(PSSR.PasswordRequired);
              else
                throw new PdfReaderException(PSSR.InvalidPassword);
            }
          }
          else if (validity == PasswordValidity.UserPassword && openmode == PdfDocumentOpenMode.Modify)
          {
            if (passwordProvider != null)
            {
              PdfPasswordProviderArgs args = new PdfPasswordProviderArgs();
              passwordProvider(args);
              if (args.Abort)
                return null;
              password = args.Password;
              goto TryAgain;
            }
            else
              throw new PdfReaderException(PSSR.OwnerPasswordRequired);
          }
        }
        else
        {
          if (password != null)
          {
            // Password specified but document is not encrypted.
            // ignore
          }
        }

        PdfReference[] irefs = document.irefTable.AllReferences;
        int count = irefs.Length;

        // Read all indirect objects
        for (int idx = 0; idx < count; idx++)
        {
          PdfReference iref = irefs[idx];
          if (iref.Value == null)
          {
            try
            {
              Debug.Assert(document.irefTable.Contains(iref.ObjectID));
              PdfObject pdfObject = parser.ReadObject(null, iref.ObjectID, false);
              Debug.Assert(pdfObject.Reference == iref);
              pdfObject.Reference = iref;
              Debug.Assert(pdfObject.Reference.Value != null, "something got wrong");
            }
            catch (Exception ex)
            {
              Debug.WriteLine(ex.Message);
            }
          }
          else
          {
            Debug.Assert(document.irefTable.Contains(iref.ObjectID));
            iref.GetType();
          }
          // Set maximum object number
          document.irefTable.maxObjectNumber = Math.Max(document.irefTable.maxObjectNumber, iref.ObjectNumber);
        }
        // Encrypt all objects
        if (xrefEncrypt != null)
        {
          document.SecurityHandler.EncryptDocument();
        }

        // Fix references of trailer values and then objects and irefs are consistent.
        document.trailer.Finish();

#if DEBUG_
        // Some tests...
        PdfReference[] reachables = document.xrefTable.TransitiveClosure(document.trailer);
        reachables.GetType();
        reachables = document.xrefTable.AllXRefs;
        document.xrefTable.CheckConsistence();
#endif

        if (openmode == PdfDocumentOpenMode.Modify)
        {
          // Create new or change existing document IDs
          if (document.Internals.SecondDocumentID == "")
            document.trailer.CreateNewDocumentIDs();
          else
          {
            byte[] agTemp = Guid.NewGuid().ToByteArray();
            document.Internals.SecondDocumentID = PdfEncoders.RawEncoding.GetString(agTemp, 0, agTemp.Length);
          }

          // Change modification date
          document.Info.ModificationDate = DateTime.Now;

          // Remove all unreachable objects
          int removed = document.irefTable.Compact();
          if (removed != 0)
            Debug.WriteLine("Number of deleted unreachable objects: " + removed);

          // Force flattening of page tree
          PdfPages pages = document.Pages;

          //bool b = document.irefTable.Contains(new PdfObjectID(1108));
          //b.GetType();

          document.irefTable.CheckConsistence();
          document.irefTable.Renumber();
          document.irefTable.CheckConsistence();
        }
      }
      finally
      {
        //if (filestream != null)
        //  filestream.Close();
      }
      return document;
    }
 /// <summary>
 /// Opens an existing PDF document.
 /// </summary>
 public static PdfDocument Open(Stream stream, PdfDocumentOpenMode openmode, PdfPasswordProvider passwordProvider = null)
 {
     return(Open(stream, null, openmode, passwordProvider));
 }