欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#實(shí)現(xiàn)word和pdf格式互轉(zhuǎn)

 更新時(shí)間:2024年10月30日 09:40:51   作者:昔舍  
這篇文章主要為大家詳細(xì)介紹了如何通過C#實(shí)現(xiàn)word和pdf格式互轉(zhuǎn)功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

1、word轉(zhuǎn)pdf

使用nuget:

 Microsoft.Office.Interop.Word

winform頁面:

后端代碼:

//using Spire.Doc;
//using Spire.Pdf;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//using Aspose.Words;
using Microsoft.Office.Interop.Word;
using System.Windows.Forms;
using Application = Microsoft.Office.Interop.Word.Application;
 
namespace file_operations
{
    public partial class word轉(zhuǎn)PDF : Form
    {
        public word轉(zhuǎn)PDF()
        {
            InitializeComponent();
            //窗體居中
            this.StartPosition = FormStartPosition.CenterScreen;
            //無邊框
            this.FormBorderStyle = FormBorderStyle.None;
            //放大無效
            this.MaximizeBox = false;
            //版權(quán)
            label4.Text = "該應(yīng)用由昔舍版權(quán)所有,如修改源碼請聯(lián)系15574296763@163.com,侵權(quán)后果自負(fù)!!!";
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            if(openFileDialog.ShowDialog() == DialogResult.OK)
            {
                string file = openFileDialog.FileName.ToLower();
                //獲取文件擴(kuò)展名
                string extension = System.IO.Path.GetExtension(file);
                if(extension != ".doc" && extension != ".docx")
                {
                    MessageBox.Show("請選擇word文件", "錯(cuò)誤提示");
                }
                else {
                    textBox1.Text = file;
 
                }
            }
        }
 
        private void button2_Click(object sender, EventArgs e)
        {
           FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
            if(folderBrowserDialog.ShowDialog() == DialogResult.OK)
            {
                textBox2.Text = folderBrowserDialog.SelectedPath+"\\";
            }
        }
 
        //保存為PDF
        private void button3_Click(object sender, EventArgs e)
        {
            if (textBox1.Text.Length == 0 && textBox2.Text.Length == 0 && textBox3.Text.Length ==0)
            {
                MessageBox.Show("請選擇要轉(zhuǎn)換的原文件和要保存的路徑", "錯(cuò)誤提示");
            }
            else
            {
                try
                {
                    //創(chuàng)建一個(gè)word實(shí)例
                    Application wordapp = new Application();
                //創(chuàng)建一個(gè)word文檔對象,并打開word文件
                Document wordDoc = wordapp.Documents.Open(textBox1.Text);
                    //獲取文件擴(kuò)展名
                    string extension = System.IO.Path.GetExtension(textBox2.Text);
                    //設(shè)置保存路徑,保存文件名稱和文件格式
                    if (extension !=".pdf")
                    {
                        try
                        {
                            string savePath = textBox2.Text + textBox3.Text + ".pdf";
                            wordDoc.SaveAs2(savePath, WdSaveFormat.wdFormatPDF);
                        }
                        catch
                        {
                            MessageBox.Show("請檢查選擇的文件是否有效,保存的路徑是否存在", "錯(cuò)誤提示");
                        }
                    }
                    else
                    {
                        try
                        {
                            string savePath = textBox2.Text + textBox3.Text;
                            wordDoc.SaveAs2(savePath, WdSaveFormat.wdFormatPDF);
                        }
                        catch
                        {
                            MessageBox.Show("請檢查選擇的文件是否有效,保存的路徑是否存在", "錯(cuò)誤提示");
                        }
 
                    }
                    //保存以后打開文件路徑
                    string openfilePath = textBox2.Text;
                    System.Diagnostics.Process.Start(openfilePath);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("請檢查選擇的文件是否有效,保存的路徑是否存在", "錯(cuò)誤提示");
                }
 
            }
        }
 
            private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            PDF轉(zhuǎn)word pDF = new PDF轉(zhuǎn)word();
            //隱藏本窗體
            this.Hide();
            //打開PDF轉(zhuǎn)word
            pDF.Show();
        }
 
        private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            this.Close();
            PDF轉(zhuǎn)word pDF = new PDF轉(zhuǎn)word();
            pDF.Close();
        }
    }
}

2、pdf轉(zhuǎn)word功能實(shí)現(xiàn):

使用nuget:

破解的Spire.pdf

下載地址:crack-spire/手動破解Spire.PDF,已破解下載鏈接在底部.md at main · zhjunbai/crack-spire · GitHub

winform頁面:

后端代碼:

using Spire.Pdf;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Office.Interop.Word;
using Application = Microsoft.Office.Interop.Word.Application;
using System.Threading;
 
namespace file_operations
{
    public partial class PDF轉(zhuǎn)word : Form
    {
        public PDF轉(zhuǎn)word()
        {
            InitializeComponent();
            //窗體居中
            this.StartPosition = FormStartPosition.CenterScreen;
            //無邊框
            this.FormBorderStyle = FormBorderStyle.None;
            //放大無效
            this.MaximizeBox = false;
            //版權(quán)
            label4.Text = "該應(yīng)用由昔舍版權(quán)所有,如修改源碼請聯(lián)系15574296763@163.com,侵權(quán)后果自負(fù)!!!";
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            //獲取PDF文件
            OpenFileDialog openFileDialog = new OpenFileDialog();
            if(openFileDialog.ShowDialog() == DialogResult.OK)
            {
                //獲取文件名
                string files = openFileDialog.FileName.ToLower();
                //獲取文件擴(kuò)展名
                string extension = System.IO.Path.GetExtension(files);
                if(extension != ".pdf")
                {
                    MessageBox.Show("請選擇PDF文件", "錯(cuò)誤提示");
                }
                else
                {
                    pdftext.Text = files;
                }
 
            }
        }
 
        private void button2_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog openFileDialog = new FolderBrowserDialog();
            if(openFileDialog.ShowDialog() == DialogResult.OK) {
                wordPath.Text = openFileDialog.SelectedPath + "\\";
            }
        }
 
        private void button3_Click(object sender, EventArgs e)
        {
 
           //初始化pdfDocument實(shí)例
           PdfDocument doc = new PdfDocument();
            try
            {
                //加載PDF文檔
                doc.LoadFromFile(pdftext.Text);
                //保存為DOC格式文檔
                string savePath = wordPath.Text + wordname.Text + ".DOC";
                doc.SaveToFile(savePath, FileFormat.DOC);
                Thread.Sleep(3000);
                //保存以后打開文件路徑
                string openfilePath = wordPath.Text;
                System.Diagnostics.Process.Start(openfilePath);
            }
            catch
            {
                MessageBox.Show("請確定文件選擇正確", "錯(cuò)誤提示");
            }
        }
 
        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            this.Close();
            word轉(zhuǎn)PDF word = new word轉(zhuǎn)PDF();
            word.Close();
        }
 
        private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            word轉(zhuǎn)PDF word = new word轉(zhuǎn)PDF();
            //隱藏本窗體
            this.Hide();
            word.Show();
        }
    }
}

到此這篇關(guān)于C#實(shí)現(xiàn)word和pdf格式互轉(zhuǎn)的文章就介紹到這了,更多相關(guān)C# word和pdf互轉(zhuǎn)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論