简体   繁体   English

从 Oracle 数据库中检索 blob pdf 文件,为其添加密码并更新数据库中的 blob 列

[英]Retrieving blob pdf file from Oracle database, adding password to it and updating blob column on the database

My case is:我的情况是:

  1. Retrieve blob file from Oracle database which is a pdf file从 Oracle 数据库中检索 blob 文件,它是一个 pdf 文件
  2. Add password to retrieved pdf file为检索到的 pdf 文件添加密码
  3. Update the record on the Oracle database which is the same blob column.更新 Oracle 数据库中属于同一 blob 列的记录。

That's what i did but I'm not sure if that's ok... I would be very grateful for any help with that...那就是我所做的,但我不确定那是否可以......我将非常感谢任何帮助......

public Stream encryptPdf(int id_file)
        {

            string connStr = "User Id=user; Password=pass; Data Source=oracle; Pooling=false";
            
            OracleConnection con = new OracleConnection(connStr);
            try
            {
                con.Open();
            }
            catch (OracleException ex)
            {
               throw new Exception(ex.Message);
            }

            string sql = "select blob from table";
            

            OracleCommand cmd = new OracleCommand(sql, con);

            cmd.InitialLOBFetchSize = 8192;

            OracleDataReader dr = cmd.ExecuteReader();

            try
            {
                dr.Read();
            }
            catch (OracleException ex)
            {
                throw new Exception(ex.Message);
            }

            OracleBlob blob = dr.GetOracleBlob(0);

            MemoryStream ms = new MemoryStream();           
            ms.Write(blob.Value, 0, blob.Value.Length);


            PdfDocument document = PdfReader.Open(ms);
            PdfSecuritySettings securitySettings = document.SecuritySettings;
            securitySettings.UserPassword = "user";
            securitySettings.OwnerPassword = "12345";
            securitySettings.PermitAccessibilityExtractContent = false;
            securitySettings.PermitAnnotations = false;
            securitySettings.PermitAssembleDocument = false;
            securitySettings.PermitExtractContent = false;
            securitySettings.PermitFormsFill = true;
            securitySettings.PermitFullQualityPrint = false;
            securitySettings.PermitModifyDocument = true;
            securitySettings.PermitPrint = false;

            //BinaryReader binaryReader = new BinaryReader(ms);
            //byte[] data = binaryReader.ReadBytes((int)ms.Length);

            document.Save(ms);            
            ms.Close();
           
            return ms;
        }

I was thinking that MemoryStream would be a good solution but right now I just don't know what do:(我当时认为MemoryStream会是一个很好的解决方案,但现在我只是不知道该怎么做:(

I was able to get the effect of reading the blob and updating it to add a password to the pdf file.我能够获得读取 blob 并更新它以将密码添加到 pdf 文件的效果。

The only thing that is currently worrying is that the speed of this method is very slow.目前唯一让人担心的是这种方法的速度很慢。 Maybe you would have some tip on how to speed it up?也许您会对如何加快速度有一些提示?

     public Stream encryptPdf(int id)
    {

        string constr = "User Id=user;Password=pass;Data Source=test";
        OracleConnection con = new OracleConnection(constr);
        con.Open();
        
        // Get the ByteCodes for blob file
        string cmdstr = string.Format(@"select blob_file from table where blob = {0}", id);            
        OracleCommand cmd = new OracleCommand(cmdstr, con);

        // Since we are going to update the OracleBlob object, we will
        //have to create a transaction
        OracleTransaction txn = con.BeginTransaction();

        // Get the reader
        OracleDataReader reader = cmd.ExecuteReader();

        // Declare the variables to retrieve the data in EmpInfo
        OracleBlob byteCodesBlob;

        // Read the first row
        reader.Read();
        if (!reader.IsDBNull(0))
        {
            byteCodesBlob = reader.GetOracleBlobForUpdate(0);

            PdfDocument document = PdfReader.Open(byteCodesBlob);
            PdfSecuritySettings securitySettings = document.SecuritySettings;
            securitySettings.UserPassword = "user";
            securitySettings.OwnerPassword = "1234";
            securitySettings.PermitAccessibilityExtractContent = false;
            securitySettings.PermitAnnotations = false;
            securitySettings.PermitAssembleDocument = false;
            securitySettings.PermitExtractContent = false;
            securitySettings.PermitFormsFill = true;
            securitySettings.PermitFullQualityPrint = false;
            securitySettings.PermitModifyDocument = true;
            securitySettings.PermitPrint = false;

            document.Save(byteCodesBlob);
            
            // Close the reader
            reader.Close();

            // Update the ByteCodes object
            BinaryReader binaryReader = new BinaryReader(byteCodesBlob);
            byte[] data = binaryReader.ReadBytes((int)byteCodesBlob.Length);
            
            byteCodesBlob.Append(data, 0, data.Length);

            // Now commit the transaction
            txn.Commit();
            Console.WriteLine("Blob Column successfully updated");
        }
        else
            reader.Dispose();

        // Close the connection
        con.Dispose();
        return null;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM