Baybars Bay

Baybars Bay

  • 2k
  • 69
  • 1.3k

Data Transfer from listview to word and pdf

Oct 11 2021 9:16 PM

I can transfer data to pdf and word from textbox in windows form, but I cannot transfer data to word and pdf from listview

I am using spire doc for transfer process

can i export all lines of listview to word and pdf

//sample file path
string samplePath = Application.StartupPath + Path.DirectorySeparatorChar + "demo.docx";
//result docs paths
string docxPath = Application.StartupPath + Path.DirectorySeparatorChar + "Result.docx";
string pdfPath = Application.StartupPath + Path.DirectorySeparatorChar + "Result.pdf";

//word document object
Document document = null;

private void ToViewFile(string fileName)
{
    try
    {
        System.Diagnostics.Process.Start(fileName);
    }
    catch { }
}
Dictionary<string, string> GetReplaceDictionary()
{
    {
        Dictionary<string, string> replaceDict = new Dictionary<string, string>();
        replaceDict.Add("#DATE#", textBox1.Text.Trim());
        replaceDict.Add("#PRODUCT#", textBox2.Text.Trim());
        replaceDict.Add("#MODEL#", textBox3.Text.Trim());
        replaceDict.Add("#SERIALNO#", textBox4.Text.Trim());
        replaceDict.Add("#BARCODE#", textBox5.Text);
        replaceDict.Add("#ABOUT#", textBox6.Text.Trim());
        replaceDict.Add("#LIST#", listView1.Text.Trim());
        return replaceDict;
    }
}

private void button2_Click(object sender, EventArgs e)
{

    //initialize word object
    document = new Document();
    document.LoadFromFile(samplePath);
    //get strings to replace
    Dictionary<string, string> dictReplace = GetReplaceDictionary();
    //Replace text
    foreach (KeyValuePair<string, string> kvp in dictReplace)
    {
        document.Replace(kvp.Key, kvp.Value, true, true);
    }
    //Save doc file.
    document.SaveToFile(docxPath, FileFormat.Docx);
    //Convert to PDF
    document.SaveToFile(pdfPath, FileFormat.PDF);
    MessageBox.Show("All tasks are finished.", "doc processing", MessageBoxButtons.OK, MessageBoxIcon.Information);
    document.Close();
}

 


Answers (3)