iT邦幫忙

0

C#程式設計怎樣把一個文件裡面的文字置換以後再輸出?哪一種方式較好?

  • 分享至 

  • xImage

我現在正在做一個網頁功能,要把一個文件裡面的文字置換,然後匯出,格式可以是Word或是pdf檔皆可,請問用哪一種方式比較好呢?謝謝。

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
2
JamesDoge
iT邦高手 1 級 ‧ 2023-02-03 07:56:20
最佳解答

我提供範例程式,給你選

  1. 透過 Microsoft.Office.Interop.Word 的範例:
using System;
using System.IO;
using Microsoft.Office.Interop.Word;

namespace ReplaceTextInWord
{
    class Program
    {
        static void Main(string[] args)
        {
            string sourceFile = @"C:\Example\Source.docx";
            string targetFile = @"C:\Example\Target";
            string fileFormat = "pdf"; // 更改 "pdf" 為 "docx" 以輸出 Word 格式

            // 創建一個 Word 應用程式物件
            Application wordApp = new Application();

            // 開啟源文件
            Document doc = wordApp.Documents.Open(sourceFile);

            // 替換文字
            doc.Content.Find.Execute(FindText: "old text", ReplaceWith: "new text", Replace: WdReplace.wdReplaceAll);

            // 保存目標文件
            if (fileFormat == "pdf")
            {
                targetFile += ".pdf";
                doc.SaveAs2(targetFile, WdSaveFormat.wdFormatPDF);
            }
            else
            {
                targetFile += ".docx";
                doc.SaveAs2(targetFile, WdSaveFormat.wdFormatDocument);
            }

            // 關閉文件並退出 Word 應用程式
            doc.Close();
            wordApp.Quit();
        }
    }
}

  1. 透過 Novacode.DocX 的範例:
using System;
using System.IO;
using Novacode;

namespace ReplaceTextAndExport
{
    class Program
    {
        static void Main(string[] args)
        {
            string sourceFile = @"C:\Example\Source.docx";
            string targetFile = @"C:\Example\Target";
            string fileFormat = "pdf"; // 更改 "pdf" 為 "docx" 以輸出 Word 格式

            // 開啟源文件
            using (DocX document = DocX.Load(sourceFile))
            {
                // 替換文字
                document.ReplaceText("old text", "new text");

                // 保存目標文件
                if (fileFormat == "pdf")
                {
                    targetFile += ".pdf";
                    document.SaveAs(targetFile, Format.PDF);
                }
                else
                {
                    targetFile += ".docx";
                    document.SaveAs(targetFile);
                }
            }
        }
    }
}

3.透過 Spire.Doc for .NET 的範例:

using System;
using Spire.Doc;

namespace ReplaceTextAndExport
{
    class Program
    {
        static void Main(string[] args)
        {
            string sourceFile = @"C:\Example\Source.docx";
            string targetFile = @"C:\Example\Target";
            string fileFormat = "pdf"; // 更改 "pdf" 為 "docx" 以輸出 Word 格式

            // 開啟源文件
            using (Document document = new Document())
            {
                document.LoadFromFile(sourceFile);

                // 替換文字
                document.Replace("old text", "new text", false, true);

                // 保存目標文件
                if (fileFormat == "pdf")
                {
                    targetFile += ".pdf";
                    document.SaveToFile(targetFile, FileFormat.PDF);
                }
                else
                {
                    targetFile += ".docx";
                    document.SaveToFile(targetFile, FileFormat.Docx);
                }
            }
        }
    }
}

  1. 透過 Aspose.Words for .NET的範例:
using System;
using Aspose.Words;

namespace ReplaceTextAndExport
{
    class Program
    {
        static void Main(string[] args)
        {
            string sourceFile = @"C:\Example\Source.docx";
            string targetFile = @"C:\Example\Target";
            string fileFormat = "pdf"; // 更改 "pdf" 為 "docx" 以輸出 Word 格式

            // 開啟源文件
            Document document = new Document(sourceFile);

            // 替換文字
            document.Range.Replace("old text", "new text", new FindReplaceOptions { MatchCase = false });

            // 保存目標文件
            if (fileFormat == "pdf")
            {
                targetFile += ".pdf";
                document.Save(targetFile, SaveFormat.Pdf);
            }
            else
            {
                targetFile += ".docx";
                document.Save(targetFile, SaveFormat.Docx);
            }
        }
    }
}

  1. 透過 GemBox.Document的範例:
using System;
using GemBox.Document;

namespace ReplaceTextAndExport
{
    class Program
    {
        static void Main(string[] args)
        {
            // 註冊 GemBox.Document
            ComponentInfo.SetLicense("FREE-LIMITED-KEY");

            string sourceFile = @"C:\Example\Source.docx";
            string targetFile = @"C:\Example\Target";
            string fileFormat = "pdf"; // 更改 "pdf" 為 "docx" 以輸出 Word 格式

            // 開啟源文件
            DocumentModel document = DocumentModel.Load(sourceFile);

            // 替換文字
            document.Content.Replace("old text", "new text", true, true);

            // 保存目標文件
            if (fileFormat == "pdf")
            {
                targetFile += ".pdf";
                document.Save(targetFile, SaveOptions.PdfDefault);
            }
            else
            {
                targetFile += ".docx";
                document.Save(targetFile, SaveOptions.DocxDefault);
            }
        }
    }
}

你可以先比較一下哪種寫法
比較合你胃口 再選擇
僅供參考

0
Juro十六
iT邦新手 3 級 ‧ 2023-02-02 13:59:02

word can using Microsoft.Office.Interop.Word
pdf can using iTextSharp.text.pdf
前端可以用表單把東西丟到後端
後端解析你的文件後編譯成word或pdf
再回傳給前端連結下載

0
Yaowen
iT邦研究生 4 級 ‧ 2023-02-02 16:40:07

具體你想匯出甚麼內容 word的話 Spire.Doc 套件很好用

我要發表回答

立即登入回答