1. 程式人生 > >C# Aspose拷貝Word

C# Aspose拷貝Word

using Aspose;

private void btn_Click(object sender, EventArgs e)
{
    OpenFileDialog OFD = new OpenFileDialog();

    if (OFD.ShowDialog() == DialogResult.OK)
    {
        Aspose.Words.Document doc = new Aspose.Words.Document(OFD.FileName);

        if (doc != null)
        {
            //拷貝到桌面        
            string fileName = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop) + "\\" + Path.GetFileNameWithoutExtension(OFD.FileName) + " - 復件" + Path.GetExtension(textBox1.Text);
            if(!File.Exists(fileName))
            {
                File.Create(fileName).Close(); //建立後必須關閉,不然會資源佔用
            }

            Aspose.Words.Document destinationDoc = doc.Clone();

           if(Path.GetExtension(OFD.FileName) == ".doc")
           {
               destinationDoc.Save(fileName, Aspose.Words.SaveFormat.Doc);
           }
           else if (Path.GetExtension(OFD.FileName) == ".docx")
           {
               destinationDoc.Save(fileName, Aspose.Words.SaveFormat.Docx);
           }

           MessageBox.Show("拷貝成功!", "結果", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
}