הפורמטים הנפוצים והנפוץ ביותר של עיבוד תמלילים הם DOC, DOCX ו-ODT. ה-Microsoft Word ו-OpenOffice Writer המפורסמים תומכים בפורמטים הללו, ובדרך כלל אנו משתמשים בפורמטים הללו לניסוח המסמכים. לכן, כמפתחים, אנו צריכים באופן נרחב לערוך מסמכי Word ביישומים שלנו באופן פרוגרמטי. במאמר זה, נדון כיצד לערוך מסמכי Word ב-C# באמצעות ה-.NET API לעריכת מסמכים.

להלן הנושאים הנידונים בקצרה במאמר זה:

NET API עבור עריכת מסמכי Word ואוטומציה

במאמר זה אשתמש ב-GroupDocs.Editor for .NET בדוגמאות C#, שהוא ממשק ה-API לעריכת מסמכים ומאפשר למפתחים לטעון, לערוך ולשמור פורמטים שונים של מסמכים באמצעות עורכי WYSIWYG HTML. בנוסף לפורמטים של מסמכי עיבוד התמלילים, ה-API תומך בעריכת גיליונות אלקטרוניים, מצגות, פורמטים של HTML, XML, TXT, DSV, TSV ו-CSV.

הורד את קובצי ה-DLL או ה-MSI מתקין מסעיף ההורדות או התקן את ה-API באפליקציית NET שלך דרך NuGet.

PM> Install-Package GroupDocs.Editor

עריכת מסמכי Word ב-C#

מיד לאחר הגדרת ה-API, אתה יכול לעבור במהירות לעבר עריכת מסמך Word. השלבים הבאים יאפשרו לך לערוך את מסמך עיבוד התמלילים.

  • טען את מסמך ה-Word.
  • ערוך בהתאם עם אפשרויות.
  • שמור את המסמך הערוך.

טען את מסמך ה-Word

ראשית, טען את המסמך על ידי מתן נתיב המסמך והסיסמה, אם המסמך מוגן.

using (FileStream fs = File.OpenRead(inputFilePath))
{
    Options.WordProcessingLoadOptions loadOptions = new WordProcessingLoadOptions();
    loadOptions.Password = "password-if-any";
}

ערוך את מסמך ה-Word

לאחר הטעינה, אתה יכול לערוך את המסמך שנטען לפי הדרישה שלך. כאן אני מחליף את כל המופעים של המילה “מסמך” ב"מסמך הערוך" במסמך Word באמצעות קוד C# להלן.

    using (Editor editor = new Editor(delegate { return fs; }, delegate { return loadOptions; }))
    {
        Options.WordProcessingEditOptions editOptions = new WordProcessingEditOptions();
        editOptions.FontExtraction = FontExtractionOptions.ExtractEmbeddedWithoutSystem;
        editOptions.EnableLanguageInformation = true;
        editOptions.EnablePagination = true;
        using (EditableDocument beforeEdit = editor.Edit(editOptions))
        {
            string originalContent = beforeEdit.GetContent();
            List<IHtmlResource> allResources = beforeEdit.AllResources;
            string editedContent = originalContent.Replace("document", "edited document");
        }
    }

שמור את מסמך Word הערוך עם אפשרויות

לבסוף, בזמן שמירת תוכן המסמך הערוך, תוכל להגדיר עוד אפשרויות שונות. אפשרויות אלו כוללות; הגדרות עימוד, הגדרת סיסמה, מיקום, הגנה או אופטימיזציה של זיכרון. אני מגדיר את האפשרויות לעיל בקוד המוזכר למטה ושומר את המסמך הערוך כקובץ DOCX מוגן בסיסמה וקריאה בלבד.

using (EditableDocument afterEdit = EditableDocument.FromMarkup(editedContent, allResources))
{
    Options.WordProcessingSaveOptions saveOptions = new WordProcessingSaveOptions(WordProcessingFormats.Docx);
    saveOptions.EnablePagination = true;
    saveOptions.Locale = System.Globalization.CultureInfo.GetCultureInfo("en-US");
    saveOptions.OptimizeMemoryUsage = true;
    saveOptions.Password = "password";
    saveOptions.Protection = new WordProcessingProtection(WordProcessingProtectionType.ReadOnly, "write\_password");
    using (FileStream outputStream = File.Create("filepath/editedDocument.docx"))
    {
        editor.Save(afterEdit, outputStream, saveOptions);
    }
}

קוד מלא

לנוחיותך, אני מראה את הדוגמה המלאה של C# המוסברת לעיל והיא עורכת את מסמך ה-Word ולאחר מכן שומרת אותו בפורמט DOCX.

// ערוך מסמך Word ב-C# באמצעות עריכת מסמכים של GroupDocs ואוטומציה API
using (FileStream fs = File.OpenRead("filepath/document.docx"))
{   // Load Document
    Options.WordProcessingLoadOptions loadOptions = new WordProcessingLoadOptions();
    loadOptions.Password = "password-if-any";
    // ערוך מסמך
    using (Editor editor = new Editor(delegate { return fs; }, delegate { return loadOptions; }))
    {
        Options.WordProcessingEditOptions editOptions = new WordProcessingEditOptions();
        editOptions.FontExtraction = FontExtractionOptions.ExtractEmbeddedWithoutSystem;
        editOptions.EnableLanguageInformation = true;
        editOptions.EnablePagination = true;

        using (EditableDocument beforeEdit = editor.Edit(editOptions))
        {
            string originalContent = beforeEdit.GetContent();
            List<IHtmlResource> allResources = beforeEdit.AllResources;

            string editedContent = originalContent.Replace("document", "edited document");
            // שמור מסמך
            using (EditableDocument afterEdit = EditableDocument.FromMarkup(editedContent, allResources))
            {
                WordProcessingFormats docxFormat = WordProcessingFormats.Docx;
                Options.WordProcessingSaveOptions saveOptions = new WordProcessingSaveOptions(docxFormat);
                            
                saveOptions.EnablePagination = true;
                saveOptions.Locale = System.Globalization.CultureInfo.GetCultureInfo("en-US");
                saveOptions.OptimizeMemoryUsage = true;
                saveOptions.Password = "password";
                saveOptions.Protection = new WordProcessingProtection(WordProcessingProtectionType.ReadOnly, "write_password");

                using (FileStream outputStream = File.Create("filepath/editedDocument.docx"))
                {
                    editor.Save(afterEdit, outputStream, saveOptions);
                }
            }
        }
    }
}

להלן מסמך הפלט שבו כל המופעים מוחלפים באמצעות הקוד לעיל.

מסמך docx ערוך באמצעות עורך API

מסמך פלט - כל המופעים מוחלפים

סיכום

לסיום, דנו בעריכת מסמכי Word ב-C# באמצעות API לעריכת מסמכים עבור יישומי NET. אתה יכול להשתמש ב-API עם עורכי WYSIWYG לעריכה חזותית של המסמכים שלך. לאחר מכן, תוכל להתקדם ולבנות עורך מסמכים משלך. באופן דומה, אתה יכול גם לשלב את תכונת העריכה בתוך אפליקציית ה-.NET שלך.

לפרטים נוספים, אפשרויות ודוגמאות, אתה יכול לבקר במאגר תיעוד ומאגר GitHub. לשאלות נוספות, צור קשר עם התמיכה בפורום.

מאמרים קשורים

ראה גם