2017-06-15 13 views
6
 connetionString = ConfigurationManager.ConnectionStrings["conString"].ToString(); 
     sql = "select Id,employeeName,employeePosition from Employee"; 
     connection = new SqlConnection(connetionString); 
     connection.Open(); 
     command = new SqlCommand(sql, connection); 
     adapter.SelectCommand = command; 
     adapter.Fill(ds); 
     connection.Close(); 

     PdfDocument pdf = new PdfDocument(); 
     pdf.Info.Title = "Database to PDF"; 
     PdfPage pdfPage = pdf.AddPage(); 
     XGraphics graph = XGraphics.FromPdfPage(pdfPage); 
     XFont font = new XFont("Verdana", 20, XFontStyle.Regular); 

     yPoint = yPoint + 100; 

     for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++) 
     { 
      pubname = ds.Tables[0].Rows[i].ItemArray[0].ToString(); 
      city = ds.Tables[0].Rows[i].ItemArray[1].ToString(); 
      state = ds.Tables[0].Rows[i].ItemArray[2].ToString(); 

      graph.DrawString(pubname, font, XBrushes.Black, new XRect(40, yPoint, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.TopLeft); 

      graph.DrawString(city, font, XBrushes.Black, new XRect(120, yPoint, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.TopLeft); 

      graph.DrawString(state, font, XBrushes.Black, new XRect(400, yPoint, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.TopLeft); 

      yPoint = yPoint + 40; 
     } 


     string pdfFilename = "dbtopdf.pdf"; 
     pdf.Save(pdfFilename); 

के साथ पीडीएफ फ़ाइल की रक्षा करें मैंने डेटाबेस से सीधे एक पीडीएफ फाइल बनाई है। मुझे पासवर्ड के साथ पीडीएफ फ़ाइल की रक्षा करने की आवश्यकता है।पासवर्ड सी #

using (MemoryStream ObjememoryStream = new MemoryStream()) 
     { 
      PdfWriter.GetInstance(pdfDoc, ObjememoryStream); 
      pdfDoc.Open(); 
      htmlworker.Parse(sr); 
      pdfDoc.Close(); 
      byte[] Filebytes = ObjememoryStream.ToArray(); 
      ObjememoryStream.Close(); 
      using (MemoryStream inputData = new MemoryStream(Filebytes)) 
      { 
       using (MemoryStream outputData = new MemoryStream()) 
       { 
        string PDFFileword = txtPassword.Text;//you can also generate Dynamic word 
        PdfReader reader = new PdfReader(inputData); 
        PdfEncryptor.Encrypt(reader, outputData, true, PDFFileword, PDFFileword, PdfWriter.ALLOW_SCREENREADERS); 
        Filebytes = outputData.ToArray(); 
        File.WriteAllBytes(destPath, Filebytes); 
        //Response.ContentType = "application/pdf"; 
        //Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf"); 
        //Response.Cache.SetCacheability(HttpCacheability.NoCache); 
        //Response.BinaryWrite(Filebytes); 
        //Response.End(); 
        GridView1.AllowPaging = true; 
        GridView1.DataBind(); 
       } 
      } 
     } 

मैं ऊपर कोड के साथ पासवर्ड के साथ pdf फ़ाइल की रक्षा करने में कामयाब रहे, लेकिन यह केवल पीडीएफ फाइल जो एक gridview से उत्पन्न है के साथ काम करता है। क्या कोई मुझे दिखा सकता है कि पीडीएफ फ़ाइल को पासवर्ड के साथ कैसे सुरक्षित किया जाए, जो कि मेरे दूसरे कोड के समान कुछ विधि के साथ उत्पन्न होता है?

उत्तर

3

SecuritySettings

pdf.SecuritySettings.UserPassword = "your password"; 
0

में उपयोगकर्ता पासवर्ड सेट web.config में, add key तत्व रास्तों और मैं Date of Birth एक PDF Password के रूप में जोड़ें। आप इसके स्थान पर anything का उपयोग कर सकते हैं।

<add key="Inputfile" value=”Path of pdf file where it is getting saved…”> 
     <add key="Outputfile" value=”Path of pdf file where it has to be saved after getting password protected…”> 


     protected void passwordProtect(DateTime DateofBirth) 
       { 
        string yourpdf = ConfigurationManager.AppSettings["Inputfile"]; 
        string pdfWithPasswordA = ConfigurationManager.AppSettings["Outputfile"]; 
        string InputFileA = yourpdf; 
        string OutputFileA = pdfWithPasswordA; 

        using (Stream input = new FileStream(InputFileA, FileMode.Open, FileAccess.Read, FileShare.Read)) 
        { 
         using (Stream output = new FileStream(OutputFileA, FileMode.Create, FileAccess.Write, FileShare.None)) 
         { 
          PdfReader reader = new PdfReader(input); 
          PdfEncryptor.Encrypt(reader, output, true, DateofBirth.ToString("yyyyMMdd"), "secret", PdfWriter.ALLOW_PRINTING | PdfWriter.ALLOW_COPY); 
         } 
        } 



      } 
संबंधित मुद्दे