VS2017 uses the NuGet plug-in to convert CSV format files into pdf files using iTextSharp and Aspose.Words.

NuGet download and installation reference: Visual Studio NuGet offline installation package_visual studionuget package offline download_Su Shoukun’s Blog-CSDN Blog

iTextSharp installation:

first step:

Step two:

Aspose.Words installation:

It is also downloaded in NuGet.

The above are all operations for finding files. For specific installation operations, see here. This uses iTextSharp as an example, and Aspose.Words will no longer prompt for examples.

First of all, the red box is the plug-in to be selected, the green box is the project directory you want to follow, and the black box is the version you want to install.

If the installation is successful, there will be a prompt. Here’s a tip from Aspose.Words.

code:

Write data in csv format:

public bool keepDataCsv(List<ClassCSV> list)
        {
            Dictionary<int, string> defectMapping = new Dictionary<int, string>();
            foreach (var item in Globals. DefectPriorityConfigs)
            {
                defectMapping.Add(item.Number, item.Name);
            }
            if (list. IsValid())
            {
                string time = DateTime.Now.ToString("yyyy-MM-dd");
                string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $@"SpotCheckFile\{time + "\"}");
                if (!Directory. Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                //StreamWriter writer = new StreamWriter();

                string filePath = path + "SPOTCHECK.csv";

                StreamWriter writer = null;

                try
                {
                    if (File. Exists(filePath))
                    {
                        writer = new StreamWriter(filePath, true, Encoding.UTF8); // append mode
                    }
                    else
                    {
                        writer = new StreamWriter(filePath, true, Encoding.UTF8); // create a file and write
                    }

                    // write data to the file
                    if (titleindex)
                        writer.WriteLine("2D Barcode, Date, Time, AIM Name, AIM Unique #, OK(1)/NG(0), inspector, inspection result, Defect");//Header
                    //List<string> defectName = new List<string>();
                    string defectName = "";
                    string now = DateTime.Now.ToString("HH:mm:ss");
                    string name = LoginName. Text;
                    for (int i = 1; i <= Globals. DefectPriorityConfigs. Count; i ++ )
                    {
                        if (list[0].GetPropertyValue("DEFECT_" + i + "_PHOTOID").ToString() != "0")
                        {
                            defectName += defectMapping[i];
                            defectName += ", ";
                        }
                    }
                    if (!string.IsNullOrEmpty(defectName))
                    {
                        defectName = defectName.Substring(0, defectName.Length - 1);
                    }
                    string newstr = list[0].QRCODE + "," + list[0].Q_CSV_DATE.ToString("yyyy-MM-dd") + "," + now + "," + "TC ANO AOI" + "," + "1#" + "," + list[0].RESULT + "," + name + "," + spotCheckStr + "," + defectName;
                    writer. WriteLine(newstr);
                }
                catch (Exception ex)
                {
                    // handle exception
                    writer. Close();
                    return false;
                }
                finally
                {
                    if (writer != null)
                    {
                        writer. Close();
                    }
                }

                return true;
            }
            return false;
        }

Export to pdf:

public void resultToPdf(string str)
        {
            string time = DateTime.Now.ToString("yyyy-MM-dd");
            string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $@"SpotCheckFile\{time + "\"}");
            if (!Directory. Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            string filePath = path + "SPOTCHECK.csv";

            StreamWriter writer = null;

            if (File. Exists(filePath))
            {
                writer = new StreamWriter(filePath, true, Encoding.UTF8); // append mode
            }
            else
            {
                writer = new StreamWriter(filePath, true, Encoding.UTF8); // create a file and write
            }
            writer. WriteLine(str);
            writer. Close();

            // Define the Word file path
            string wordFilePath = filePath;

            // Define the PDF file path
            string pdfFilePath = filePath.Replace(".csv", ".pdf");

            // create PDF document object
            Document document = new Document();

            // Define the PDF output stream
            //PdfWriter writer2 = PdfWriter.GetInstance(document, new FileStream(pdfFilePath, FileMode.Create));

            // open the PDF document
            document. Open();

            // Create a Word document object
            Aspose.Words.Document wordDoc = new Aspose.Words.Document(wordFilePath);

            // Convert Word document to PDF format and write to PDF document
            wordDoc.Save(pdfFilePath, Aspose.Words.SaveFormat.Pdf);

            // close the PDF document
            document. Close();
            //writer2.Close();
        }

This article is for reference only, and the individual is recorded as a skill skill.