简体   繁体   English

如何使用C#复制以数据库中副本数量为值的pdf文件?

[英]How to copy a pdf file with the amount of copies in the database as the value using C#?

I need to get the amount of copies from my local database and use that value to make copies of the pdf file.我需要从我的本地数据库中获取副本数量并使用该值来制作 pdf 文件的副本。

         public Boolean getAmountOfCopies()
         {
             string connectionString = null;
             MySqlConnection cnn;
             connectionString = "databaseInformation";
             cnn = new MySqlConnection(connectionString);
             try
             {
                 cnn.Open();
                 Console.WriteLine("Connection Open");
                 const string sql = "SELECT numberofcopies FROM orders";
                 MySqlCommand cmd = new MySqlCommand(sql, cnn);
                 MySqlDataReader rdr = cmd.ExecuteReader();
        
                 while (rdr.Read())
                 {
                     if (rdr[0] == null)
                     {
                         Console.WriteLine("There is no value, it is null.");
                         return false;
                     }
                     else
                     {
                         Console.WriteLine(rdr[0]);
                         var ImageFiles = Directory.EnumerateFiles(Directory.GetCurrentDirectory()).Where(f => f.EndsWith(".jpg") || f.EndsWith(".JPG") || f.EndsWith(".jpeg") || f.EndsWith(".jfif"));
                         List<string> images = ImageFiles.ToList();
                         PdfDocument[] copyOfPDF = new PdfDocument[images.Count()];

                         // Code goes here to make the copy
                     }
                 }
                 cnn.Close();
                 Console.WriteLine("Connection Closed");
                 return true;
             }
             catch (Exception ex)
             {
                 Console.WriteLine(ex);
                 return false;
             }
         }

The rdr[0] is the value being taken from the database in numberofcopies column. rdr[0] 是从数据库中的 numberofcopies 列中获取的值。 I need to use that value to make the amount of copies based on that value.我需要使用该值来根据该值制作副本数量。

Hopefully it is not too confusing, if you need clarification please let me know and i will provide further information.希望这不会太混乱,如果您需要澄清,请告诉我,我会提供更多信息。

Thank you.谢谢你。

Made it work by adding this.通过添加它使其工作。

while (rdr.Read())
                 {
                     //Checks if the value is null
                     if (rdr[0] == null)
                     {
                         //If it is null execute the Console.WriteLine
                         Console.WriteLine("There is no value, it is null.");
                         return false;
                     }
                     else
                     {
                         //Converting object to int
                         int numberOfCopies = Convert.ToInt32(rdr[0]);
                         //Source directory for the PDF
                         string sourceDir = @"C:/Orders/0931617841_13_05_22/";
                         //Source directory for the new folder directory
                         string backupDir = @"C:/Orders/AmountOfCopies";

                         //If not null
                         try
                         {
                             //Gets the file composite.pdf inside the 'sourceDir' variable, which is instantiated above
                             string[] pdfList = Directory.GetFiles(sourceDir, "*.pdf");

                             // Copy pdf file.
                             foreach (string f in pdfList)
                             {
                                 //For loop to change the file name using 'i'
                                 for (int i = 0; i < numberOfCopies; i++)
                                 {
                                     string oldPath = @"C:/Orders/0931617841_13_05_22/composite.pdf";
                                     string newpath = @"C:/Orders/AmountOfCopies/";
                                     string newFileName = "composite" + i;
                                     FileInfo f1 = new FileInfo(oldPath);
                                     if(f1.Exists)
                                     {
                                         if(!Directory.Exists(newpath))
                                         {
                                             Directory.CreateDirectory(newpath); 
                                         }
                                         f1.CopyTo(string.Format("{0}{1}{2}", newpath, newFileName, f1.Extension));
                                     }
                                 }
                             }
                         }
                        //If no directory as that name is found execute exception
                         catch (DirectoryNotFoundException dirNotFound)
                         {
                             Console.WriteLine(dirNotFound.Message);
                         }
                     }
                 }

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

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