diff --git a/words/arabic/net/ai-powered-document-processing/_index.md b/words/arabic/net/ai-powered-document-processing/_index.md index a216bdfe7b..41a1cf276c 100644 --- a/words/arabic/net/ai-powered-document-processing/_index.md +++ b/words/arabic/net/ai-powered-document-processing/_index.md @@ -42,6 +42,7 @@ | [العمل مع نموذج الذكاء الاصطناعي من Google](./working-with-google-ai-model/) |قم بترقية معالجة المستندات لديك باستخدام Aspose.Words for .NET وGoogle AI لإنشاء ملخصات موجزة بسهولة. | | [العمل مع نموذج الذكاء الاصطناعي المفتوح](./working-with-open-ai-model/) | أطلق العنان لتلخيص المستندات بكفاءة باستخدام Aspose.Words لـ .NET مع نماذج OpenAI القوية. انغمس في هذا الدليل الشامل الآن. | | [العمل مع خيارات التلخيص](./working-with-summarize-options/) | تعلم كيفية تلخيص مستندات Word بشكل فعال باستخدام Aspose.Words for .NET من خلال دليلنا خطوة بخطوة حول دمج نماذج الذكاء الاصطناعي للحصول على رؤى سريعة. | +| [الاتصال بنموذج لغة كبير محلي في C# – دليل برمجة كامل](./connect-to-local-llm-in-c-complete-programming-guide/) | تعلم كيفية ربط Aspose.Words بـ LLM محلي باستخدام C# في دليل برمجة شامل. | {{< /blocks/products/pf/tutorial-page-section >}} diff --git a/words/arabic/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/_index.md b/words/arabic/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/_index.md new file mode 100644 index 0000000000..29d7be14ba --- /dev/null +++ b/words/arabic/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/_index.md @@ -0,0 +1,282 @@ +--- +category: general +date: 2026-04-28 +description: الاتصال بـ LLM المحلي من C# وتحفيز نموذج اللغة الكبير لتحميل مستند Word، + استدعاء الـ LLM المحلي وإعادة كتابة النص تلقائيًا. يتضمن كود خطوة بخطوة. +draft: false +keywords: +- connect to local llm +- prompt large language model +- load word document +- call local llm +- rewrite text automatically +language: ar +og_description: اتصل بـ LLM المحلي من C# وتعرّف على كيفية توجيه نموذج اللغة الضخم، + تحميل مستند Word، استدعاء الـ LLM المحلي وإعادة كتابة النص تلقائيًا في دقائق. +og_title: الاتصال بـ LLM المحلي في C# – دليل البرمجة الكامل +tags: +- Aspose.Words +- C# +- LLM +- AI Automation +title: الاتصال بـ LLM المحلي في C# – دليل برمجة شامل +url: /ar/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/ +--- + +{{< blocks/products/pf/main-wrap-class >}} +{{< blocks/products/pf/main-container >}} +{{< blocks/products/pf/tutorial-page-section >}} + +# الاتصال بـ LLM المحلي في C# – دليل برمجي كامل + +هل احتجت يومًا إلى **الاتصال بـ llm المحلي** من تطبيق .NET وتساءلت كيف تجعلها تتعامل مع ملف Word؟ لست وحدك. في هذا الدليل سنستعرض العملية بالكامل — الاتصال بـ llm المحلي، **تحفيز نموذج اللغة الكبيرة**، تحميل مستند Word، **استدعاء llm المحلي**، وأخيرًا **إعادة كتابة النص تلقائيًا**. في النهاية ستحصل على مثال قابل للتنفيذ يحول أي فقرة إلى نبرة رسمية دون الحاجة إلى مفاتيح API خارجية. + +## ما يغطيه هذا الدرس + +سنبدأ بتثبيت حزم NuGet الضرورية، ثم تشغيل نقطة نهاية LLM محلية بسيطة (فكر في Ollama على المنفذ 11434). بعد ذلك سنحمّل ملف `.docx` باستخدام Aspose.Words، نرسل فقرة إلى LLM، نستقبل نسخة معاد صياغتها، ونكتبها مرة أخرى في نفس المستند. ستشاهد أيضًا كيفية التعامل مع المشكلات الشائعة — فقرات فارغة، إلغاء الموارد بشكل غير متزامن، ومشكلات الترميز — بحيث يعمل الكود في بيئة الإنتاج وليس مجرد عرض توضيحي. + +### المتطلبات المسبقة + +- .NET 6.0 SDK أو أحدث (يمكنك أيضًا استخدام .NET 8 إذا رغبت) +- Visual Studio 2022 أو VS Code مع امتداد C# +- **Aspose.Words for .NET** (الإصدار التجريبي المجاني يعمل جيدًا) +- LLM مستضاف محليًا يدعم عقد `/api/generate` (مثل Ollama، LMStudio) +- إلمام أساسي بـ async/await في C# + +> **نصيحة احترافية:** إذا لم تقم بتثبيت Ollama بعد، شغّل `ollama serve` واسحب نموذجًا باستخدام `ollama pull llama3`. ستكون نقطة النهاية HTTP الافتراضية `http://localhost:11434/api/generate`. + +--- + +## الخطوة 1: تثبيت الحزم المطلوبة + +أولاً، أضف حزم NuGet الخاصة بـ Aspose.Words و Aspose.Words.AI إلى مشروعك. + +```bash +dotnet add package Aspose.Words +dotnet add package Aspose.Words.AI +``` + +هذه المكتبات تمنحنا القدرة على **تحميل مستند Word** وتغليفًا خفيفًا لـ **استدعاء llm المحلي** دون الحاجة إلى كتابة طلبات HTTP يدويًا. + +--- + +## الخطوة 2: الاتصال بنقطة نهاية LLM المحلي + +الاتصال بنموذج مستضاف محليًا بسيط مثل إنشاء كائن `LocalLargeLanguageModel`. يتوقع المُنشئ عنوان URL الكامل لنقطة توليد النص. + +```csharp +using Aspose.Words.AI; +using Aspose.Words; +using System.Threading.Tasks; + +// Create a client that talks to the LLM running on localhost +var localLlm = new LocalLargeLanguageModel("http://localhost:11434/api/generate"); +``` + +لماذا نغلف النقطة النهاية في فئة؟ فئة `LocalLargeLanguageModel` تتعامل مع تسلسل JSON، وإعادة المحاولات، وتدفق الاستجابات نيابةً عنك — بحيث يمكنك التركيز على منطق التحفيز بدلاً من العبث بـ `HttpClient`. + +--- + +## الخطوة 3: تحميل مستند Word المصدر + +بعد ذلك، نجلب المستند إلى الذاكرة. يدعم Aspose.Words تقريبًا كل تنسيقات Word، لذا سيقوم `Document` بتحليل `input.docx` دون الحاجة إلى تثبيت Office. + +```csharp +// Path to the source file – adjust as needed +string inputPath = Path.Combine(Environment.CurrentDirectory, "input.docx"); + +// Load the document; throws if the file is missing or corrupted +Document sourceDocument = new Document(inputPath); +``` + +إذا احتجت للعمل مع تدفق (مثلاً ملف تم رفعه عبر ASP.NET)، استبدل مسار الملف بـ `MemoryStream` ومرره إلى مُنشئ `Document`. + +--- + +## الخطوة 4: استخراج نص الفقرة الحالية + +سنستخدم `DocumentBuilder` للتنقل داخل المستند. في هذا المثال نعيد صياغة **الفقرة الأولى**، لكن يمكنك التكرار على `sourceDocument.GetChildNodes(NodeType.Paragraph, true)` لمعالجة عدة فقرات. + +```csharp +// Builder gives us a cursor inside the document +DocumentBuilder docBuilder = new DocumentBuilder(sourceDocument); + +// Grab the text of the paragraph where the builder is currently positioned +string originalParagraph = docBuilder.CurrentParagraph?.GetText() ?? string.Empty; + +// Safety check – avoid sending empty strings to the LLM +if (string.IsNullOrWhiteSpace(originalParagraph)) +{ + Console.WriteLine("No paragraph found at the current cursor position."); + return; +} +``` + +عامل `?.` يمنع حدوث `NullReferenceException` إذا كان المستند فارغًا. هذه واحدة من **حالات الحافة** التي تُربك المبتدئين. + +--- + +## الخطوة 5: تحفيز LLM لإعادة صياغة الفقرة + +الآن نقوم فعليًا بـ **تحفيز نموذج اللغة الكبيرة**. النص التحفيزي هو إنجليزي بسيط؛ التغليف سيُرسله كـ JSON إلى النقطة النهاية المحلية. + +```csharp +// Build a friendly instruction for the model +string prompt = $"Rewrite the following sentence in a more formal tone:\n{originalParagraph}"; + +// Await the model's response – this is an async call +string rewrittenParagraph = await localLlm.PromptAsync(prompt); +``` + +لماذا نصيغ الطلب بهذه الطريقة؟ تستجيب LLMs بشكل أفضل لتعليمات واضحة ومحددة المهمة. إضافة سطر جديد بعد النقطتين تفصل بين التعليمات والمحتوى، مما يقلل من احتمال أن يُعيد النموذج النص التحفيزي نفسه. + +**الناتج المتوقع** – إذا كان `originalParagraph` هو `"Hey, what's up?"`، قد يُعيد LLM: + +> “Good day, how may I assist you?” + +يمكنك التحقق من النتيجة بطباعتها: + +```csharp +Console.WriteLine("Original: " + originalParagraph); +Console.WriteLine("Rewritten: " + rewrittenParagraph); +``` + +--- + +## الخطوة 6: إدراج النص المعاد صياغته مرة أخرى في المستند + +مع النص الجديد في المتناول، نستبدل الفقرة القديمة. `DocumentBuilder.Writeln` يكتب سطرًا جديدًا ويحرك المؤشر للأمام، وهو مثالي للإضافة. إذا أردت *استبدال* الفقرة نفسها تمامًا، يمكنك استخدام `docBuilder.CurrentParagraph.RemoveAllChildren()` قبل الكتابة. + +```csharp +// Option A – Append a new paragraph (keeps the original) +docBuilder.Writeln(rewrittenParagraph); + +// Option B – Replace the existing paragraph (uncomment to use) +// docBuilder.CurrentParagraph.RemoveAllChildren(); +// docBuilder.CurrentParagraph.AppendChild(new Run(docBuilder.Document, rewrittenParagraph)); +``` + +تم عرض كلا النهجين لتختار ما يناسب سير عملك. + +--- + +## الخطوة 7: حفظ المستند المحدث + +أخيرًا، نحفظ التغييرات في ملف جديد. يختار Aspose.Words التنسيق تلقائيًا بناءً على امتداد الملف. + +```csharp +string outputPath = Path.Combine(Environment.CurrentDirectory, "output.docx"); +sourceDocument.Save(outputPath); + +Console.WriteLine($"Document saved to {outputPath}"); +``` + +افتح `output.docx` في Word، وسترى أن الفقرة الآن مكتوبة بنبرة رسمية. + +--- + +## مثال كامل يعمل + +فيما يلي **البرنامج الكامل المستقل**. انسخه إلى مشروع Console، استعد حزم NuGet، وشغّله — لا تحتاج إلى أي إعدادات إضافية سوى تشغيل LLM المحلي. + +```csharp +using Aspose.Words.AI; +using Aspose.Words; +using System; +using System.IO; +using System.Threading.Tasks; + +class Program +{ + static async Task Main() + { + // ------------------------------------------------- + // Step 1: Connect to the locally hosted LLM endpoint + // ------------------------------------------------- + var localLlm = new LocalLargeLanguageModel("http://localhost:11434/api/generate"); + + // ------------------------------------------------- + // Step 2: Load the source Word document + // ------------------------------------------------- + string inputPath = Path.Combine(Environment.CurrentDirectory, "input.docx"); + Document sourceDocument = new Document(inputPath); + + // ------------------------------------------------- + // Step 3: Retrieve the text of the current paragraph + // ------------------------------------------------- + DocumentBuilder docBuilder = new DocumentBuilder(sourceDocument); + string originalParagraph = docBuilder.CurrentParagraph?.GetText() ?? string.Empty; + + if (string.IsNullOrWhiteSpace(originalParagraph)) + { + Console.WriteLine("No paragraph found at the current cursor position."); + return; + } + + // ------------------------------------------------- + // Step 4: Ask the LLM to rewrite the paragraph in a formal tone + // ------------------------------------------------- + string prompt = $"Rewrite the following sentence in a more formal tone:\n{originalParagraph}"; + string rewrittenParagraph = await localLlm.PromptAsync(prompt); + + // ------------------------------------------------- + // Step 5: Insert the rewritten text back into the document + // ------------------------------------------------- + docBuilder.Writeln(rewrittenParagraph); + + // ------------------------------------------------- + // Step 6: Save the updated document + // ------------------------------------------------- + string outputPath = Path.Combine(Environment.CurrentDirectory, "output.docx"); + sourceDocument.Save(outputPath); + + Console.WriteLine("Original paragraph:"); + Console.WriteLine(originalParagraph); + Console.WriteLine("\nRewritten paragraph:"); + Console.WriteLine(rewrittenParagraph); + Console.WriteLine($"\nDocument saved to {outputPath}"); + } +} +``` + +### ما تتوقعه عند تشغيله + +1. يطبع الكونسول الفقرة الأصلية والفقرة المعاد صياغتها. +2. يظهر `output.docx` بجوار `input.docx`. +3. عند فتح الملف، ستجد الفقرة الرسمية الجديدة مضافة بعد الأصل (أو مستبدلة إذا استخدمت الكود البديل). + +--- + +## التعامل مع حالات الحافة الشائعة + +| الحالة | الحل | +|-----------|----------| +| **فقرة فارغة أو تحتوي على مسافات فقط** | تحقق من `string.IsNullOrWhiteSpace` قبل التحفيز (انظر الخطوة 3). | +| **LLM يُعيد خطأ أو سلسلة فارغة** | غلف `PromptAsync` بـ `try/catch` وارجع إلى النص الأصلي كبديل. | +| **ضرورة إعادة صياغة فقرات متعددة** | كرّر عبر `sourceDocument.GetChildNodes(NodeType.Paragraph, true)` وطبق منطق التحفيز نفسه. | +| **المستندات الكبيرة تُسبب تأخيرًا** | اجمع الفقرات وأرسلها في طلب واحد (حدّ التحفيز حتى 4 KB لكل استدعاء). | +| **حروف غير ASCII تظهر مشوهة** | تأكد من أن نقطة النهاية للـ LLM تستخدم UTF‑8 (معظم النماذج الحديثة تفعل ذلك). | + +--- + +## الخطوات التالية والمواضيع ذات الصلة + +- **تحفيز نموذج اللغة الكبيرة** بتعليمات أكثر تفصيلاً (مثل أدلة الأسلوب، حدود الطول). +- استخدم **استدعاء llm المحلي** في Web API لتوفير أتمتة المستندات كخدمة. +- استكشف **تحميل مستند Word** عبر تدفقات متوازية لسيناريوهات عالية الإنتاجية. +- دمج هذا النهج مع **إعادة كتابة النص تلقائيًا** لتوليد رسائل بريد جماعية أو توحيد تقارير. + +للتعمق أكثر، راجع توثيق Aspose حول **دمج المستندات** ومرجع Ollama API للحصول على معلمات العينة المخصصة. + +--- + +## الخلاصة + +لقد أظهرنا لك كيفية **الاتصال بـ llm المحلي** من C#، **تحفيز نموذج اللغة الكبيرة**، **تحميل مستند Word**، **استدعاء llm المحلي**، و**إعادة كتابة النص تلقائيًا** — كل ذلك في تطبيق Console واحد قابل للتنفيذ. النمط قابل للتوسيع: غيّر التحفيز، كرّر على الفقرات، أو قدّم المنطق عبر نقطة نهاية ASP.NET. الفكرة الأساسية هي أن نماذج الذكاء الاصطناعي المحلية يمكن دمجها بعمق مع مكتبات معالجة المستندات التقليدية، لتوفر أتمتة قوية دون مغادرة بيئتك الموثوقة داخل المؤسسة. + +هل لديك أسئلة حول الـ threading، + +{{< /blocks/products/pf/tutorial-page-section >}} +{{< /blocks/products/pf/main-container >}} +{{< /blocks/products/pf/main-wrap-class >}} +{{< blocks/products/products-backtop-button >}} \ No newline at end of file diff --git a/words/arabic/net/programming-with-markdownsaveoptions/_index.md b/words/arabic/net/programming-with-markdownsaveoptions/_index.md index e4bfc46653..8c94b316ac 100644 --- a/words/arabic/net/programming-with-markdownsaveoptions/_index.md +++ b/words/arabic/net/programming-with-markdownsaveoptions/_index.md @@ -41,9 +41,11 @@ ### [كيفية حفظ Markdown من DOCX – دليل خطوة بخطوة](./how-to-save-markdown-from-docx-step-by-step-guide/) ### [كيفية تصدير Markdown من DOCX – دليل كامل](./how-to-export-markdown-from-docx-complete-guide/) + ### [كيفية تصدير LaTeX من Word: تحويل DOCX إلى Markdown باستخدام Aspose](./how-to-export-latex-from-word-convert-docx-to-markdown-with/) ### [تحويل Word إلى Markdown – تضمين الصور كـ Base64](./convert-word-to-markdown-embed-images-as-base64/) + ### [كيفية حفظ Markdown من Word – دليل C# كامل](./how-to-save-markdown-from-word-complete-c-guide/) ### [كيفية حفظ Markdown من Word – دليل كامل](./how-to-save-markdown-from-word-complete-guide/) @@ -58,6 +60,8 @@ ### [تحويل Word إلى Markdown في C# – دليل كامل مع استخراج الصور](./convert-word-to-markdown-in-c-full-guide-with-image-extracti/) +### [مسار الصورة النسبي في Markdown – تحويل Word إلى Markdown](./markdown-image-relative-path-convert-word-to-markdown/) + {{< /blocks/products/pf/tutorial-page-section >}} diff --git a/words/arabic/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/_index.md b/words/arabic/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/_index.md new file mode 100644 index 0000000000..0c75dd1e50 --- /dev/null +++ b/words/arabic/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/_index.md @@ -0,0 +1,205 @@ +--- +category: general +date: 2026-04-28 +description: تعلم كيفية تعيين مسار نسبي لصورة markdown عند تحويل Word إلى markdown، + واستخراج الصور من Word، وإنشاء مجلد موارد للصور المصدرة. +draft: false +keywords: +- markdown image relative path +- convert word to markdown +- extract images from word +- create resources folder +- export images from docx +language: ar +og_description: حدد مسارًا نسبيًا لصورة Markdown أثناء تحويل Word إلى Markdown، استخرج + الصور من Word، وأنشئ مجلد موارد للصور المصدرة. +og_title: مسار الصورة النسبي في ماركداون – تحويل Word إلى ماركداون +tags: +- Aspose.Words +- C# +- Markdown +- Image Export +title: مسار صورة ماركداون النسبي – تحويل Word إلى ماركداون +url: /ar/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/ +--- + +{{< blocks/products/pf/main-wrap-class >}} +{{< blocks/products/pf/main-container >}} +{{< blocks/products/pf/tutorial-page-section >}} + +# مسار صورة markdown النسبي – تحويل Word إلى Markdown + +هل احتجت إلى **مسار صورة markdown نسبي** أثناء **تحويل Word إلى markdown**؟ لست وحدك. يواجه معظم المطورين مشكلة عندما تشير ملفات Markdown المُولدة إلى الصور في مجلد مسطح، مما يكسر بنية الروابط النسبية التي تتوقعها في موقع ثابت أو مستودع GitHub. + +في هذا الدرس سنستعرض حلاً كاملاً من البداية إلى النهاية ي **يستخرج الصور من Word**، **ينشئ مجلد موارد**، ويعيد كتابة مراجع الصور بحيث تستخدم *مسار صورة markdown نسبي* نظيف. في النهاية ستحصل على ملف `.md` جاهز للنشر ومجلد `Resources` منظم يحتوي على كل صورة تم استخراجها من ملف `.docx` الأصلي. + +> **ما ستحصل عليه:** برنامج C# واحد (بدون سكريبتات خارجية)، شرح واضح *لماذا* كل جزء مهم، وبعض النصائح العملية التي يمكنك نسخها ولصقها في مشاريعك الخاصة. + +--- + +## المتطلبات المسبقة + +قبل أن نغوص في الكود، تأكد من وجود ما يلي: + +- **.NET 6.0** أو أحدث مثبت (يمكنك أيضًا استهداف .NET Framework 4.7+، لكن .NET 6 هو الخيار المثالي للمشاريع الجديدة). +- **Aspose.Words for .NET** (أحدث حزمة NuGet وقت كتابة هذا الدرس، الإصدار 23.12). ثبّتها باستخدام: + ```bash + dotnet add package Aspose.Words + ``` +- مستند Word يحتوي فعليًا على صور—سنسميه `WithImages.docx`. +- مجلد تريد أن يعيش فيه ملف الـ markdown الناتج والصور، مثال: `C:\Projects\MarkdownExport`. + +لا توجد مكتبات إضافية مطلوبة؛ كل شيء آخر يتم التعامل معه بواسطة Aspose.Words. + +--- + +## الخطوة 1: تحميل مستند Word المصدر (نقطة الانطلاق لتحويل Word إلى markdown) + +```csharp +using System; +using System.IO; +using Aspose.Words; +using Aspose.Words.Saving; + +class Program +{ + static void Main() + { + // Adjust the path to point at your own .docx file. + string sourcePath = @"C:\Projects\MarkdownExport\WithImages.docx"; + + // Load the document – this is where Aspose.Words parses the Word file. + Document doc = new Document(sourcePath); + + // The rest of the workflow follows… + } +} +``` + +*لماذا هذا مهم:* تحميل المستند يمنحنا الوصول إلى شجرة العقد الداخلية، التي تشمل أجزاء الصور التي نحتاج لاحقًا إلى **تصدير الصور من docx**. إذا فشل التحميل، لن تُنفّذ أي خطوة لاحقة، لذا تأكد من صحة المسار وأذونات الملف. + +--- + +## الخطوة 2: تكوين `MarkdownSaveOptions` مع رد نداء مخصص (قلب إنشاء مجلد الموارد) + +يتيح لنا `ResourceSavingCallback` التدخل في كل مرة تريد Aspose.Words كتابة ملف صورة. داخل رد النداء سن **ننشئ مجلد فرعي Resources** ونضبط المرجع بحيث يستخدم الـ markdown مسار صورة *markdown image relative path*. + +```csharp +// Inside Main(), after loading the document: +string outputFolder = @"C:\Projects\MarkdownExport"; +string resourcesFolder = Path.Combine(outputFolder, "Resources"); + +// Make sure the folder exists before we start saving anything. +Directory.CreateDirectory(resourcesFolder); + +// Set up the Markdown save options. +MarkdownSaveOptions mdOptions = new MarkdownSaveOptions +{ + // Hook that runs for every image resource. + ResourceSavingCallback = new MyMarkdownResourceCallback(resourcesFolder) +}; + +// Save the document as Markdown. +string markdownPath = Path.Combine(outputFolder, "Doc.md"); +doc.Save(markdownPath, mdOptions); +``` + +لاحظ أننا مررنا `resourcesFolder` إلى مُنشئ رد النداء—هذا يبقي مسار المجلد مرنًا ويتجنب كتابة السلاسل النصية صراحةً في جميع أنحاء الكود. + +--- + +## الخطوة 3: تنفيذ رد النداء الذي **ينشئ مجلد الموارد** ويعيد كتابة المسار + +```csharp +/// +/// Handles image extraction and path rewriting for markdown export. +/// +class MyMarkdownResourceCallback : IResourceSavingCallback +{ + private readonly string _resourcesFolder; + + public MyMarkdownResourceCallback(string resourcesFolder) + { + _resourcesFolder = resourcesFolder; + } + + public void ResourceSaving(ResourceSavingArgs args) + { + // 1️⃣ Build the full file system path where the image will be stored. + string targetPath = Path.Combine(_resourcesFolder, args.ResourceFileName); + + // 2️⃣ Ensure the directory exists (in case Aspose creates sub‑folders). + Directory.CreateDirectory(Path.GetDirectoryName(targetPath)); + + // 3️⃣ Write the image stream to disk. + using (FileStream fileStream = File.Create(targetPath)) + { + args.Stream.CopyTo(fileStream); + } + + // 4️⃣ Update the markdown reference to use a relative path. + // This is the crucial line that gives us the markdown image relative path. + args.ResourceFileName = Path.Combine("Resources", args.ResourceFileName); + } +} +``` + +*لماذا هذا يعمل:* يحتوي `args.Stream` على بايتات الصورة الخام. بنسخه إلى ملف داخل مجلد `Resources` ن **نصدر الصور من docx** بأمان. ثم نستبدل `args.ResourceFileName` بعنوان URL نسبي (`Resources/image.png`). عندما تكتب Aspose.Words الـ markdown لاحقًا، ستُدرج تلك السلسلة بالضبط، مما يمنحنا مسار صورة markdown النسبي المطلوب. + +--- + +## الخطوة 4: التحقق من الـ Markdown المُولد (ما يبدو عليه الإخراج النهائي) + +افتح `Doc.md` في أي محرر نصوص. يجب أن ترى شيئًا مشابهًا لـ: + +```markdown +# Sample Heading + +Here is an inline picture: + +![Image 0](Resources/Image_0.png) + +And a picture inside a table: + +![Image 1](Resources/Image_1.jpg) +``` + +الجزء المهم هو أن كل مرجع صورة يشير إلى `Resources/...` – هذا هو **مسار صورة markdown النسبي** الذي كنا نبحث عنه. + +![مثال على مسار صورة markdown النسبي](example.png "مثال على مسار صورة markdown النسبي") + +*نصيحة:* إذا فتحت الـ markdown في عارض يحترم الروابط النسبية (معاينة VS Code، GitHub، أو مولد موقع ثابت)، ستظهر الصور بشكل صحيح دون أي إعداد إضافي. + +--- + +## الخطوة 5: المشكلات الشائعة ونصائح المتخصصين + +| المشكلة | لماذا يحدث | كيفية الإصلاح | +|-------|----------------|---------------| +| الصور تنتهي في المجلد الجذر بدلاً من `Resources` | لم يتم ربط رد النداء أو لم يتم استبدال `args.ResourceFileName`. | تأكد من ضبط `ResourceSavingCallback` **قبل** استدعاء `doc.Save`. | +| أسماء الملفات تحتوي على أحرف غير صالحة | أحيانًا يطلق Word أسماء للصور تحتوي على مسافات أو رموز يونيكود. | استخدم `Path.GetInvalidFileNameChars()` لتطهير `args.ResourceFileName` داخل رد النداء. | +| المستندات الكبيرة تستغرق وقتًا طويلاً للمعالجة | كل صورة تُكتب بشكل متزامن. | انتقل إلى I/O غير متزامن (`await args.Stream.CopyToAsync(fileStream)`) إذا كنت على .NET 6+ وتحتاج إلى أداء أعلى. | +| الروابط النسبية تنكسر عندما يُنقل الـ markdown | المسار نسبي لموقع ملف الـ markdown. | حافظ على وجود `Doc.md` ومجلد `Resources` معًا، أو عدل رد النداء لاستخدام بادئة نسبية مختلفة (مثل `../assets`). | + +--- + +## الخطوة 6: توسيع الحل (ماذا لو احتجت إلى مزيد من التحكم؟) + +- **تنسيقات إخراج متعددة:** استبدل `MarkdownSaveOptions` بـ `HtmlSaveOptions` أو `PdfSaveOptions` مع الحفاظ على نفس رد النداء—ستستدعي Aspose.Words رد النداء لكل صورة بغض النظر عن التنسيق. +- **تسمية الصور مخصصًا:** إذا أردت إعادة تسمية الصور (مثال: `figure-01.png`)، عدل `args.ResourceFileName` داخل رد النداء قبل كتابة الملف. +- **تضمين الصور كـ Base64:** اضبط `args.ResourceFileName` إلى URI بيانات (`data:image/png;base64,...`) وتجاوز كتابة الملف. هذا مفيد لتصدير markdown كملف واحد. + +--- + +## الخاتمة + +أصبح لديك الآن برنامج C# كامل الوظائف **يحول Word إلى markdown**، **يستخرج الصور من word**، **ينشئ مجلد موارد**، ويضمن **مسار صورة markdown نسبي** نظيف لكل صورة. الكود مستقل، يعمل مع أحدث نسخة من Aspose.Words، ويمكن إدراجه في أي مشروع .NET بأقل جهد. + +ما الخطوة التالية؟ جرّب إمداد الـ markdown المُولد إلى مولد موقع ثابت مثل Hugo أو Jekyll، أو جرب تعديل رد النداء لتضمين الصور مباشرة كـ Base64. إذا صادفت حالات خاصة—مثل صور SVG أو ملفات ضخمة جدًا—ارجع إلى جدول “المشكلات الشائعة”؛ تعديل بسيط عادةً ما يحل المشكلة. + +برمجة سعيدة، ولتظل روابط الـ markdown دائمًا تشير إلى المجلد الصحيح! + +{{< /blocks/products/pf/tutorial-page-section >}} +{{< /blocks/products/pf/main-container >}} +{{< /blocks/products/pf/main-wrap-class >}} +{{< blocks/products/products-backtop-button >}} \ No newline at end of file diff --git a/words/arabic/net/programming-with-officemath/_index.md b/words/arabic/net/programming-with-officemath/_index.md index 48e01744dc..033f61519e 100644 --- a/words/arabic/net/programming-with-officemath/_index.md +++ b/words/arabic/net/programming-with-officemath/_index.md @@ -22,8 +22,8 @@ | --- | --- | | [معادلات الرياضيات](./math-equations/) | تعلّم كيفية إعداد المعادلات الرياضية في مستندات Word باستخدام Aspose.Words لـ .NET. دليل خطوة بخطوة مع أمثلة وأسئلة شائعة والمزيد. | | [حفظ ملف docx كملف txt – تصدير معادلات Word إلى LaTeX باستخدام C#](./save-docx-as-txt-export-word-math-to-latex-with-c/) | تعلّم كيفية حفظ مستندات docx كملفات txt وتصدير معادلات Word إلى تنسيق LaTeX باستخدام C#. | - | [حفظ المستند كملف Txt – تصدير معادلات Word إلى LaTeX في C#](./save-document-as-txt-export-word-math-to-latex-in-c/) | تعلم كيفية حفظ مستند Word كملف نصي وتصدير المعادلات إلى LaTeX باستخدام C# و Aspose.Words. | +| [تحويل DOCX إلى TXT – تصدير معادلات Word إلى LaTeX في C#](./convert-docx-to-txt-export-word-equations-to-latex-in-c/) | تعلم كيفية تحويل ملفات DOCX إلى TXT وتصدير معادلات Word إلى LaTeX باستخدام C#. | {{< /blocks/products/pf/tutorial-page-section >}} diff --git a/words/arabic/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/_index.md b/words/arabic/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/_index.md new file mode 100644 index 0000000000..abd1581d1f --- /dev/null +++ b/words/arabic/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/_index.md @@ -0,0 +1,207 @@ +--- +category: general +date: 2026-04-28 +description: تحويل DOCX إلى TXT وتصدير معادلات Word إلى LaTeX باستخدام Aspose.Words. + تعلّم كيفية حفظ Word كملف TXT ومعالجة كائنات الرياضيات في بضع خطوات. +draft: false +keywords: +- convert docx to txt +- convert word equations to latex +- convert word to plain text +- save word as txt +- export equations as latex +language: ar +og_description: حوّل ملفات DOCX إلى TXT وصدر معادلات Word إلى LaTeX باستخدام مقتطف + C# بسيط. دليل كامل، كود، ونصائح. +og_title: تحويل DOCX إلى TXT – تصدير معادلات Word إلى LaTeX +tags: +- C# +- Aspose.Words +- Document Conversion +title: تحويل DOCX إلى TXT – تصدير معادلات Word إلى LaTeX في C# +url: /ar/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/ +--- + +{{< blocks/products/pf/main-wrap-class >}} +{{< blocks/products/pf/main-container >}} +{{< blocks/products/pf/tutorial-page-section >}} + +# تحويل DOCX إلى TXT – تصدير معادلات Word إلى LaTeX + +هل احتجت يومًا إلى **convert docx to txt** لكنك كنت قلقًا من أن تتحول المعادلات في ملف Word إلى فوضى غير مقروءة؟ لست وحدك. في العديد من المشاريع الهندسية أو الأكاديمية، المستند الأصلي يكون بامتداد .docx، بينما الأدوات اللاحقة لا تفهم إلا النص العادي أو LaTeX. الخبر السار؟ باستخدام بضع أسطر من C# و Aspose.Words يمكنك **convert docx to txt** *و* الحفاظ على كل معادلة ككود LaTeX نظيف. + +في هذا الدرس سنستعرض العملية بالكامل: تحميل ملف .docx، ضبط خيارات الحفظ بحيث تتحول كائنات Office Math إلى LaTeX، وأخيرًا كتابة النتيجة إلى ملف .txt. بنهاية الدرس ستعرف كيف **save word as txt**، **convert word to plain text**، و **export equations as latex** دون الحاجة للبحث في وثائق الـ API. + +## ما ستتعلمه + +- الاستدعاءات الدقيقة للـ API اللازمة لـ **convert docx to txt** مع الحفاظ على المعادلات. +- لماذا اختيار `OfficeMathExportMode.LaTeX` هو الطريقة الموصى بها لـ **convert word equations to latex**. +- كيفية التعامل مع الحالات الطرفية الشائعة مثل الخطوط المفقودة أو ميزات المعادلات غير المدعومة. +- برنامج C# كامل جاهز للتنفيذ يمكنك إدراجه في أي مشروع .NET. + +### المتطلبات المسبقة + +- .NET 6.0 أو أحدث (الكود يعمل أيضًا على .NET Framework 4.7+). +- ترخيص لـ Aspose.Words for .NET (الإصدار التجريبي المجاني يكفي للتقييم). +- مستند Word (`input.docx`) يحتوي على كائن Office Math واحد على الأقل. + +إذا كان لديك هذه المتطلبات، هيا نبدأ. + +## الخطوة 1: تثبيت Aspose.Words + +قبل تشغيل أي كود تحتاج إلى المكتبة. افتح الطرفية في مجلد مشروعك ونفّذ: + +```bash +dotnet add package Aspose.Words +``` + +## الخطوة 2: تحميل المستند المصدر + +أول شيء نقوم به هو قراءة ملف .docx إلى كائن `Document`. هذا الكائن يمنحنا وصولًا كاملًا إلى بنية الملف، بما في ذلك مقاطع النص، الصور، وكائنات الرياضيات. + +```csharp +using Aspose.Words; +using Aspose.Words.Saving; + +class Program +{ + static void Main() + { + // Step 2: Load the source document + Document doc = new Document(@"YOUR_DIRECTORY\input.docx"); +``` + +> **لماذا هذا مهم:** تحميل المستند ينشئ تمثيلًا في الذاكرة، بحيث يمكننا لاحقًا تعديل طريقة كتابة كل عنصر. إذا لم يُعثر على الملف، ستطرح Aspose استثناء `FileNotFoundException`، وقد ترغب في التقاطه في كود الإنتاج. + +## الخطوة 3: ضبط خيارات حفظ TXT للرياضيات بصيغة LaTeX + +افتراضيًا، `Document.Save` يكتب نصًا عاديًا و**يتجاهل** أي Office Math. للحفاظ على تلك المعادلات، نضبط `OfficeMathExportMode` إلى `LaTeX`. هذا يخبر المُصدّر بترجمة كل معادلة إلى ما يعادلها في LaTeX. + +```csharp + // Step 3: Configure TXT save options to export Office Math as LaTeX + TxtSaveOptions txtOptions = new TxtSaveOptions + { + OfficeMathExportMode = OfficeMathExportMode.LaTeX, + // Optional: preserve line breaks as they appear in the original Word file + PreserveTableLayout = true + }; +``` + +> **نصيحة احترافية:** إذا كنت تحتاج فقط إلى الأحرف Unicode الخام للمعادلة (مثلاً لعرض سريع)، يمكنك استخدام `OfficeMathExportMode.Text`. لكن لمعظم خطوط الأنابيب العلمية، يعتبر `LaTeX` المعيار الذهبي لأنه مفهوم عالميًا من قبل معالجات LaTeX. + +## الخطوة 4: حفظ المستند كنص عادي + +الآن نكتب المحتوى المُحوّل إلى ملف `.txt`. سيحتوي الملف على فقرات عادية، نقاط تعداد، وبفضل الخطوة السابقة—مقتطفات LaTeX لكل معادلة. + +```csharp + // Step 4: Save the document as plain‑text using the configured options + doc.Save(@"YOUR_DIRECTORY\Math.txt", txtOptions); + } +} +``` + +عند فتح `Math.txt` ستظهر لك شيء مشابه لـ: + +``` +In this report we derive the quadratic formula: +\[ +x = \frac{-b \pm \sqrt{b^{2} - 4ac}}{2a} +\] + +The end. +``` + +هل لاحظت delimiters `\[` … `\]`؟ هذه هي كتل الرياضيات في LaTeX التي تم توليدها تلقائيًا. + +## الخطوة 5: التحقق من الناتج (اختياري لكن موصى به) + +من السهل أن تغفل عن مشكلة تحويل دقيقة، خاصةً عندما تحتوي المعادلات على رموز مخصصة. فحص سريع هو تمرير ملف `.txt` المُولد إلى مُصرّف LaTeX (مثل `pdflatex`) ورؤية ما إذا كان يُترجم بدون أخطاء. + +```bash +pdflatex -interaction=nonstopmode Math.txt +``` + +إذا نجح التجميع، فقد نجحت فعليًا في **convert word equations to latex** و **convert docx to txt** في خطوة واحدة. إذا واجهت أخطاء، ابحث عن رسائل حول أوامر غير معرفة—هذه عادةً ما تشير إلى ميزة معادلة لا يمكن لـ Aspose.Words ترجمتها (مثل بعض صيغ المصفوفات). في مثل هذه الحالات، يمكنك الرجوع إلى `OfficeMathExportMode.MathML` ثم تحويل MathML إلى LaTeX بأداة أخرى. + +## المشكلات الشائعة وكيفية تجنبها + +| المشكلة | لماذا يحدث | الحل | +|-------|----------------|-----| +| الخطوط المفقودة | Aspose.Words يحتاج الخط لتصوير الرموز بشكل صحيح. | ثبّت الخط المفقود على الجهاز أو أدمجه في ملف .docx. | +| عدم تصدير المعادلات المعقدة | بعض ميزات Office Math الحديثة لم تُطابق بعد إلى LaTeX. | استخدم `OfficeMathExportMode.MathML` ثم حوّل باستخدام مكتبة MathML‑to‑LaTeX. | +| سطور فارغة إضافية | حافظة النص العادي تحتفظ بفواصل الفقرات، مما قد يضيف مسافات بيضاء. | اضبط `txtOptions.AddBidiMarks = false` أو عالج الملف ببرنامج بسيط. | + +## مثال كامل جاهز للتنفيذ (نسخ‑لصق) + +فيما يلي البرنامج الكامل، جاهز للترجمة. استبدل `YOUR_DIRECTORY` بالمجلد الذي يحتوي على `input.docx`. + +```csharp +using System; +using Aspose.Words; +using Aspose.Words.Saving; + +namespace DocxToTxtWithLatex +{ + class Program + { + static void Main() + { + try + { + // Load the source document + Document doc = new Document(@"C:\Docs\input.docx"); + + // Configure save options: export equations as LaTeX + TxtSaveOptions txtOptions = new TxtSaveOptions + { + OfficeMathExportMode = OfficeMathExportMode.LaTeX, + PreserveTableLayout = true, + AddBidiMarks = false + }; + + // Save as plain‑text + string outputPath = @"C:\Docs\Math.txt"; + doc.Save(outputPath, txtOptions); + + Console.WriteLine($"Successfully converted DOCX to TXT. Output at: {outputPath}"); + } + catch (Exception ex) + { + Console.Error.WriteLine($"Conversion failed: {ex.Message}"); + } + } + } +} +``` + +تشغيل هذا البرنامج سيقوم **save word as txt** مع تحويل كل كتلة Office Math إلى LaTeX، مما يمنحك ملف نص عادي نظيف وقابل للبحث. + +## الخطوات التالية والمواضيع ذات الصلة + +- **تحويل دفعي:** غلف المنطق أعلاه داخل حلقة `foreach` لمعالجة مجلد كامل من ملفات .docx. +- **دمج مع توليد PDF:** بعد الحصول على مقتطفات LaTeX، مررها إلى خط أنابيب PDF (مثل `PdfSharp` + `MiKTeX`) لإنتاج تقارير PDF. +- **تصدير المعادلات كـ latex** لتنسيقات أخرى: يدعم Aspose.Words أيضًا `SaveFormat.Markdown`، الذي يمكنه تضمين LaTeX تلقائيًا. +- **تحسين الأداء:** للمستندات الضخمة، أعد استخدام نفس كائن `TxtSaveOptions` وعطّل الميزات غير الضرورية مثل `AddBidiMarks`. + +--- + +### مثال صورة (اختياري) + +إذا كنت تفضّل إشارة بصرية، إليك لقطة شاشة لملف الإخراج في Notepad++. + +![مخرجات تحويل docx إلى txt تظهر معادلات LaTeX](convert-docx-to-txt-output.png) + +*(نص بديل: “convert docx to txt output showing LaTeX equations” – يفي بمتطلبات الكلمة المفتاحية الأساسية.)* + +## الخلاصة + +لقد عرضنا طريقة موثوقة لـ **convert docx to txt** مع الحفاظ على كل معادلة كـ LaTeX نظيف. المفتاح هو علم `OfficeMathExportMode.LaTeX`، الذي يحول تنسيق الرياضيات الخاص بـ Word إلى شيء يفهمه أي محرك LaTeX. باستخدام عينة الكود الكاملة أعلاه يمكنك **save word as txt**، **convert word to plain text**، و **export equations as latex** في تشغيل واحد متكامل. + +لا تتردد في التجربة—غيّر امتداد الإخراج إلى `.md` للـ Markdown، أو دمج المقتطف في خط أنابيب معالجة مستندات أكبر. إذا صادفت أي شذوذ، اترك تعليقًا أدناه؛ سأكون سعيدًا بالمساعدة في حل المشكلات. + +برمجة سعيدة! + +{{< /blocks/products/pf/tutorial-page-section >}} +{{< /blocks/products/pf/main-container >}} +{{< /blocks/products/pf/main-wrap-class >}} +{{< blocks/products/products-backtop-button >}} \ No newline at end of file diff --git a/words/chinese/net/ai-powered-document-processing/_index.md b/words/chinese/net/ai-powered-document-processing/_index.md index 79c133fcd9..699e589dd0 100644 --- a/words/chinese/net/ai-powered-document-processing/_index.md +++ b/words/chinese/net/ai-powered-document-processing/_index.md @@ -42,6 +42,7 @@ | [使用 Google AI 模型](./working-with-google-ai-model/) 使用 Aspose.Words for .NET 和 Google AI 提升您的文档处理能力,轻松创建简洁的摘要。| | [使用开放的人工智能模型](./working-with-open-ai-model/) 使用 Aspose.Words for .NET 和 OpenAI 强大的模型,解锁高效的文档摘要功能。立即深入了解这份全面的指南。| | [使用汇总选项](./working-with-summarize-options/) 学习使用 Aspose.Words for .NET 有效地总结 Word 文档,并遵循我们关于集成 AI 模型以获得快速洞察的分步指南。| +| [在 C# 中连接本地 LLM – 完整编程指南](./connect-to-local-llm-in-c-complete-programming-guide/) 学习如何在 C# 项目中集成本地大型语言模型,实现离线 AI 文档处理。| {{< /blocks/products/pf/tutorial-page-section >}} diff --git a/words/chinese/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/_index.md b/words/chinese/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/_index.md new file mode 100644 index 0000000000..2c738abc00 --- /dev/null +++ b/words/chinese/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/_index.md @@ -0,0 +1,280 @@ +--- +category: general +date: 2026-04-28 +description: 从 C# 连接本地 LLM,并提示大型语言模型加载 Word 文档,调用本地 LLM 自动重写文本。包括逐步代码示例。 +draft: false +keywords: +- connect to local llm +- prompt large language model +- load word document +- call local llm +- rewrite text automatically +language: zh +og_description: 使用 C# 连接本地 LLM,了解如何提示大型语言模型,加载 Word 文档,调用本地 LLM 并在几分钟内自动重写文本。 +og_title: 在 C# 中连接本地 LLM – 完整编程指南 +tags: +- Aspose.Words +- C# +- LLM +- AI Automation +title: 在 C# 中连接本地 LLM – 完整编程指南 +url: /zh/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/ +--- + +{{< blocks/products/pf/main-wrap-class >}} +{{< blocks/products/pf/main-container >}} +{{< blocks/products/pf/tutorial-page-section >}} + +# 在 C# 中连接本地 LLM – 完整编程指南 + +是否曾经需要在 .NET 应用中 **connect to local llm**,并想知道如何让它与 Word 文件交互?你并不孤单。在本指南中,我们将完整演示整个过程——connect to local llm、**prompt large language model**、加载 Word 文档、**call local llm**,以及最终 **rewrite text automatically**。完成后,你将拥有一个可运行的示例,能够将任意段落转换为正式语气,且无需任何外部 API 密钥。 + +## 本教程涵盖内容 + +我们将首先安装必要的 NuGet 包,然后启动一个简单的本地 LLM 端点(例如运行在 11434 端口的 Ollama)。随后我们会使用 Aspose.Words 加载 `.docx` 文件,将段落发送给 LLM,获取改写后的版本,并写回同一文档。你还将看到如何处理常见的陷阱——空段落、异步释放以及编码问题——确保代码在生产环境而非仅演示中也能正常工作。 + +### 先决条件 + +- .NET 6.0 SDK 或更高(如果愿意也可以使用 .NET 8) +- Visual Studio 2022 或带 C# 扩展的 VS Code +- **Aspose.Words for .NET**(免费试用即可) +- 本地托管的 LLM,遵循 `/api/generate` 合约(例如 Ollama、LMStudio) +- 对 C# 中的 async/await 有基本了解 + +> **专业提示:** 如果尚未安装 Ollama,请运行 `ollama serve` 并使用 `ollama pull llama3` 拉取模型。默认的 HTTP 端点为 `http://localhost:11434/api/generate`。 + +--- + +## 步骤 1:安装必需的包 + +First, add the Aspose.Words and Aspose.Words.AI NuGet packages to your project. + +```bash +dotnet add package Aspose.Words +dotnet add package Aspose.Words.AI +``` + +这些库为我们提供了 **load word document** 功能,以及一个轻量包装器,可 **call local llm** 而无需手动编写 HTTP 请求。 + +--- + +## 步骤 2:连接本地 LLM 端点 + +Connecting to a locally hosted model is as simple as instantiating `LocalLargeLanguageModel`. The constructor expects the full URL of the generation endpoint. + +```csharp +using Aspose.Words.AI; +using Aspose.Words; +using System.Threading.Tasks; + +// Create a client that talks to the LLM running on localhost +var localLlm = new LocalLargeLanguageModel("http://localhost:11434/api/generate"); +``` + +为什么要把端点包装成一个类?`LocalLargeLanguageModel` 为你处理 JSON 序列化、重试以及流式响应——这样你可以专注于提示逻辑,而无需摆弄 `HttpClient`。 + +--- + +## 步骤 3:加载源 Word 文档 + +Next, we bring the document into memory. Aspose.Words supports virtually every Word format, so `Document` will parse `input.docx` without needing Office installed. + +```csharp +// Path to the source file – adjust as needed +string inputPath = Path.Combine(Environment.CurrentDirectory, "input.docx"); + +// Load the document; throws if the file is missing or corrupted +Document sourceDocument = new Document(inputPath); +``` + +如果需要使用流(例如通过 ASP.NET 上传的文件),只需将文件路径替换为 `MemoryStream` 并传入 `Document` 构造函数即可。 + +--- + +## 步骤 4:提取当前段落文本 + +We’ll use `DocumentBuilder` to navigate the document. In this example we rewrite **the first paragraph**, but you can iterate over `sourceDocument.GetChildNodes(NodeType.Paragraph, true)` to process many. + +```csharp +// Builder gives us a cursor inside the document +DocumentBuilder docBuilder = new DocumentBuilder(sourceDocument); + +// Grab the text of the paragraph where the builder is currently positioned +string originalParagraph = docBuilder.CurrentParagraph?.GetText() ?? string.Empty; + +// Safety check – avoid sending empty strings to the LLM +if (string.IsNullOrWhiteSpace(originalParagraph)) +{ + Console.WriteLine("No paragraph found at the current cursor position."); + return; +} +``` + +`?.` 运算符可以防止在文档为空时抛出 `NullReferenceException`。这就是会让初学者踩坑的 **edge cases** 之一。 + +--- + +## 步骤 5:提示 LLM 改写段落 + +Now we actually **prompt large language model**. The prompt is plain English; the wrapper will send it as JSON to the local endpoint. + +```csharp +// Build a friendly instruction for the model +string prompt = $"Rewrite the following sentence in a more formal tone:\n{originalParagraph}"; + +// Await the model's response – this is an async call +string rewrittenParagraph = await localLlm.PromptAsync(prompt); +``` + +为什么要这样表述请求?LLM 对清晰、单一任务的指令响应最佳。在冒号后添加换行可以将指令与内容分离,降低模型回显提示的可能性。 + +**预期输出** – 如果 `originalParagraph` 为 `"Hey, what's up?"`,LLM 可能返回: + +> “Good day, how may I assist you?” + +你可以通过打印来验证结果: + +```csharp +Console.WriteLine("Original: " + originalParagraph); +Console.WriteLine("Rewritten: " + rewrittenParagraph); +``` + +--- + +## 步骤 6:将改写后的文本写回文档 + +With the new text in hand, we replace the old paragraph. `DocumentBuilder.Writeln` writes a new line and moves the cursor forward, which is perfect for appending. If you need to *replace* the exact same paragraph, you can use `docBuilder.CurrentParagraph.RemoveAllChildren()` before writing. + +```csharp +// Option A – Append a new paragraph (keeps the original) +docBuilder.Writeln(rewrittenParagraph); + +// Option B – Replace the existing paragraph (uncomment to use) +// docBuilder.CurrentParagraph.RemoveAllChildren(); +// docBuilder.CurrentParagraph.AppendChild(new Run(docBuilder.Document, rewrittenParagraph)); +``` + +这里展示了两种方法,你可以根据工作流选择合适的方式。 + +--- + +## 步骤 7:保存更新后的文档 + +Finally, we persist the changes to a new file. Aspose.Words automatically chooses the format based on the file extension. + +```csharp +string outputPath = Path.Combine(Environment.CurrentDirectory, "output.docx"); +sourceDocument.Save(outputPath); + +Console.WriteLine($"Document saved to {outputPath}"); +``` + +在 Word 中打开 `output.docx`,你会看到段落已以正式语气呈现。 + +--- + +## 完整工作示例 + +Below is the **complete, self‑contained program**. Copy‑paste it into a console project, restore NuGet packages, and run it—no extra configuration required beyond a running local LLM. + +```csharp +using Aspose.Words.AI; +using Aspose.Words; +using System; +using System.IO; +using System.Threading.Tasks; + +class Program +{ + static async Task Main() + { + // ------------------------------------------------- + // Step 1: Connect to the locally hosted LLM endpoint + // ------------------------------------------------- + var localLlm = new LocalLargeLanguageModel("http://localhost:11434/api/generate"); + + // ------------------------------------------------- + // Step 2: Load the source Word document + // ------------------------------------------------- + string inputPath = Path.Combine(Environment.CurrentDirectory, "input.docx"); + Document sourceDocument = new Document(inputPath); + + // ------------------------------------------------- + // Step 3: Retrieve the text of the current paragraph + // ------------------------------------------------- + DocumentBuilder docBuilder = new DocumentBuilder(sourceDocument); + string originalParagraph = docBuilder.CurrentParagraph?.GetText() ?? string.Empty; + + if (string.IsNullOrWhiteSpace(originalParagraph)) + { + Console.WriteLine("No paragraph found at the current cursor position."); + return; + } + + // ------------------------------------------------- + // Step 4: Ask the LLM to rewrite the paragraph in a formal tone + // ------------------------------------------------- + string prompt = $"Rewrite the following sentence in a more formal tone:\n{originalParagraph}"; + string rewrittenParagraph = await localLlm.PromptAsync(prompt); + + // ------------------------------------------------- + // Step 5: Insert the rewritten text back into the document + // ------------------------------------------------- + docBuilder.Writeln(rewrittenParagraph); + + // ------------------------------------------------- + // Step 6: Save the updated document + // ------------------------------------------------- + string outputPath = Path.Combine(Environment.CurrentDirectory, "output.docx"); + sourceDocument.Save(outputPath); + + Console.WriteLine("Original paragraph:"); + Console.WriteLine(originalParagraph); + Console.WriteLine("\nRewritten paragraph:"); + Console.WriteLine(rewrittenParagraph); + Console.WriteLine($"\nDocument saved to {outputPath}"); + } +} +``` + +### 运行时的预期结果 + +1. 控制台会打印原始段落和改写后的段落。 +2. `output.docx` 会出现在 `input.docx` 旁边。 +3. 打开文件后会看到新的正式段落插入在原始段落之后(如果使用了替代代码,则会替换原段落)。 + +--- + +## 处理常见的边缘情况 + +| 情况 | 解决方案 | +|-----------|----------| +| **仅为空或仅包含空白的段落** | 在提示前检查 `string.IsNullOrWhiteSpace`(见步骤 3)。 | +| **LLM 返回错误或空字符串** | 将 `PromptAsync` 包裹在 `try/catch` 中,并回退使用原始文本。 | +| **需要改写多个段落** | 遍历 `sourceDocument.GetChildNodes(NodeType.Paragraph, true)` 并应用相同的提示逻辑。 | +| **大型文档导致延迟** | 将段落批量处理,在一次请求中发送(每次提示最多约 4 KB)。 | +| **非 ASCII 字符出现乱码** | 确保 LLM 端点使用 UTF‑8(大多数现代模型都是如此)。 | + +--- + +## 后续步骤与相关主题 + +- 使用更丰富的指令对 **Prompt large language model**(例如风格指南、长度限制)。 +- 在 Web API 中使用 **call local llm**,将文档自动化作为服务暴露。 +- 探索在并行流中 **load word document** 以实现高吞吐场景。 +- 将此方法与 **rewrite text automatically** 结合,用于批量邮件生成或报告标准化。 + +如果想深入了解,请查阅 Aspose 关于 **document merging** 的文档以及 Ollama API 参考以获取自定义采样参数。 + +--- + +## 结论 + +我们刚刚演示了如何在 C# 中 **connect to local llm**、**prompt large language model**、**load word document**、**call local llm**,以及 **rewrite text automatically**——全部在一个可运行的控制台应用中完成。该模式具有可扩展性:可以更换提示、遍历段落,或通过 ASP.NET 端点公开逻辑。关键点在于,本地 AI 模型可以与传统文档处理库紧密结合,为你提供强大的自动化能力,而无需离开可信的本地环境。 + +如有关于线程的问题, + +{{< /blocks/products/pf/tutorial-page-section >}} +{{< /blocks/products/pf/main-container >}} +{{< /blocks/products/pf/main-wrap-class >}} +{{< blocks/products/products-backtop-button >}} \ No newline at end of file diff --git a/words/chinese/net/programming-with-markdownsaveoptions/_index.md b/words/chinese/net/programming-with-markdownsaveoptions/_index.md index b59df0a2f7..167146fdc5 100644 --- a/words/chinese/net/programming-with-markdownsaveoptions/_index.md +++ b/words/chinese/net/programming-with-markdownsaveoptions/_index.md @@ -70,6 +70,10 @@ 使用 Aspose.Words for .NET 将 Word 文档转换为 Markdown,并提取嵌入的图像,提供完整的 C# 示例和详细步骤指南。 +### [markdown 图像相对路径 – 将 Word 转换为 Markdown](./markdown-image-relative-path-convert-word-to-markdown/) + +使用 Aspose.Words for .NET 将 Word 文档转换为 Markdown,并使用相对路径保存图像。 + {{< /blocks/products/pf/tutorial-page-section >}} diff --git a/words/chinese/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/_index.md b/words/chinese/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/_index.md new file mode 100644 index 0000000000..e2a95a1731 --- /dev/null +++ b/words/chinese/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/_index.md @@ -0,0 +1,185 @@ +--- +category: general +date: 2026-04-28 +description: 了解在将 Word 转换为 Markdown 时如何设置 Markdown 图像的相对路径、从 Word 中提取图像以及为导出的图像创建资源文件夹。 +draft: false +keywords: +- markdown image relative path +- convert word to markdown +- extract images from word +- create resources folder +- export images from docx +language: zh +og_description: 在将 Word 转换为 Markdown 时,设置 Markdown 图片的相对路径,提取 Word 中的图片,并为导出的图片创建资源文件夹。 +og_title: Markdown 图像相对路径 – 将 Word 转换为 Markdown +tags: +- Aspose.Words +- C# +- Markdown +- Image Export +title: Markdown 图像相对路径 – 将 Word 转换为 Markdown +url: /zh/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/ +--- + +{{< blocks/products/pf/main-wrap-class >}} +{{< blocks/products/pf/main-container >}} +{{< blocks/products/pf/tutorial-page-section >}} + +# markdown image relative path – 将 Word 转换为 Markdown + +是否曾在 **markdown image relative path** 的同时 **convert Word to markdown** 时遇到需求?你并不孤单。大多数开发者在生成的 Markdown 将图片指向平铺文件夹时会卡住,这会破坏你在静态站点或 GitHub 仓库中期望的相对链接结构。 + +在本教程中,我们将逐步演示一个完整的端到端解决方案,**extracts images from Word**,**creates a resources folder**,并重写图片引用,使其使用干净的 *markdown image relative path*。完成后,你将拥有一个可直接发布的 `.md` 文件以及一个整齐组织的 `Resources` 目录,包含从原始 `.docx` 中提取的所有图片。 + +> **你将获得:** 一个单文件 C# 程序(无需外部脚本),对每个部分 *why* 重要性的清晰解释,以及一些可直接复制粘贴到自己项目中的实用技巧。 + +## 前置条件 + +- **.NET 6.0** 或更高版本已安装(你也可以针对 .NET Framework 4.7+,但 .NET 6 是新项目的最佳选择)。 +- **Aspose.Words for .NET**(撰写时的最新 NuGet 包,版本 23.12)。使用以下方式安装: + ```bash + dotnet add package Aspose.Words + ``` +- 一个实际包含图片的 Word 文档——我们称之为 `WithImages.docx`。 +- 一个用于存放输出 markdown 和图片的文件夹,例如 `C:\Projects\MarkdownExport`。 + +不需要额外的库;其他所有工作均由 Aspose.Words 处理。 + +## 步骤 1:加载源 Word 文档(convert word to markdown 的起点) + +```csharp +using System; +using System.IO; +using Aspose.Words; +using Aspose.Words.Saving; + +class Program +{ + static void Main() + { + // Adjust the path to point at your own .docx file. + string sourcePath = @"C:\Projects\MarkdownExport\WithImages.docx"; + + // Load the document – this is where Aspose.Words parses the Word file. + Document doc = new Document(sourcePath); + + // The rest of the workflow follows… + } +} +``` + +*为什么重要:* 加载文档后我们可以访问内部节点树,其中包含后续需要 **export images from docx** 的图片部分。如果加载失败,后续步骤都不会执行,请再次检查路径和文件权限。 + +## 步骤 2:使用自定义回调配置 `MarkdownSaveOptions`(create resources folder 的核心) + +`ResourceSavingCallback` 让我们在 Aspose.Words 每次尝试写入图片文件时进行干预。在回调内部,我们将 **create a Resources sub‑folder** 并调整引用,使生成的 markdown 使用 *markdown image relative path*。 + +```csharp +// Inside Main(), after loading the document: +string outputFolder = @"C:\Projects\MarkdownExport"; +string resourcesFolder = Path.Combine(outputFolder, "Resources"); + +// Make sure the folder exists before we start saving anything. +Directory.CreateDirectory(resourcesFolder); + +// Set up the Markdown save options. +MarkdownSaveOptions mdOptions = new MarkdownSaveOptions +{ + // Hook that runs for every image resource. + ResourceSavingCallback = new MyMarkdownResourceCallback(resourcesFolder) +}; + +// Save the document as Markdown. +string markdownPath = Path.Combine(outputFolder, "Doc.md"); +doc.Save(markdownPath, mdOptions); +``` + +请注意我们将 `resourcesFolder` 传入回调的构造函数——这使文件夹路径保持灵活,避免在代码中硬编码字符串。 + +## 步骤 3:实现回调以 **creates resources folder** 并重写路径 + +```csharp +/// +/// Handles image extraction and path rewriting for markdown export. +/// +class MyMarkdownResourceCallback : IResourceSavingCallback +{ + private readonly string _resourcesFolder; + + public MyMarkdownResourceCallback(string resourcesFolder) + { + _resourcesFolder = resourcesFolder; + } + + public void ResourceSaving(ResourceSavingArgs args) + { + // 1️⃣ Build the full file system path where the image will be stored. + string targetPath = Path.Combine(_resourcesFolder, args.ResourceFileName); + + // 2️⃣ Ensure the directory exists (in case Aspose creates sub‑folders). + Directory.CreateDirectory(Path.GetDirectoryName(targetPath)); + + // 3️⃣ Write the image stream to disk. + using (FileStream fileStream = File.Create(targetPath)) + { + args.Stream.CopyTo(fileStream); + } + + // 4️⃣ Update the markdown reference to use a relative path. + // This is the crucial line that gives us the markdown image relative path. + args.ResourceFileName = Path.Combine("Resources", args.ResourceFileName); + } +} +``` + +*为什么可行:* `args.Stream` 包含原始图片字节。将其复制到 `Resources` 文件夹中的文件时,我们安全地 **export images from docx**。随后我们将 `args.ResourceFileName` 替换为相对 URL(`Resources/image.png`)。当 Aspose.Words 稍后写入 markdown 时,它会注入该字符串,从而得到期望的 *markdown image relative path*。 + +## 步骤 4:验证生成的 Markdown(最终输出的样子) + +在任意文本编辑器中打开 `Doc.md`。你应该会看到类似如下内容: + +```markdown +# Sample Heading + +Here is an inline picture: + +![Image 0](Resources/Image_0.png) + +And a picture inside a table: + +![Image 1](Resources/Image_1.jpg) +``` + +重要的是每个图片引用都指向 `Resources/...` ——这正是我们想要的 **markdown image relative path**。 + +![markdown image relative path 示例](example.png "markdown image relative path 示例") + +*提示:* 如果在支持相对链接的查看器中打开 markdown(如 VS Code 预览、GitHub 或静态站点生成器),图片将会正确渲染,无需额外配置。 + +## 步骤 5:常见陷阱与专业提示 + +| 问题 | 为什么会发生 | 如何解决 | +|------|--------------|----------| +| 图片最终位于根文件夹而不是 `Resources` | 回调未附加或 `args.ResourceFileName` 未被覆盖。 | 再次确认在调用 `doc.Save` 之前已设置 **ResourceSavingCallback**。 | +| 文件名包含非法字符 | Word 有时会使用空格或 Unicode 符号为图片命名。 | 在回调中使用 `Path.GetInvalidFileNameChars()` 清理 `args.ResourceFileName`。 | +| 大型文档处理时间较长 | 每个图片都是同步写入的。 | 如果使用 .NET 6+ 并需要性能,可切换为异步 I/O(`await args.Stream.CopyToAsync(fileStream)`)。 | +| 当 markdown 被移动时相对路径会失效 | 路径相对于 markdown 文件的位置。 | 保持 `Doc.md` 与 `Resources` 文件夹在同一目录,或在回调中使用不同的相对前缀(例如 `../assets`)。 | + +## 步骤 6:扩展解决方案(如果需要更多控制怎么办?) + +- **Multiple output formats:** 将 `MarkdownSaveOptions` 替换为 `HtmlSaveOptions` 或 `PdfSaveOptions`,同时保持相同的回调——Aspose.Words 将对每个图片调用它,无论格式为何。 +- **Custom image naming:** 如果想重命名图片(例如 `figure-01.png`),在写入文件前于回调中修改 `args.ResourceFileName`。 +- **Embedding images as Base64:** 将 `args.ResourceFileName` 设置为 data URI(`data:image/png;base64,...`),并跳过文件写入。这对单文件 markdown 导出非常有用。 + +## 结论 + +现在你拥有一个完整的 C# 程序,能够 **converts Word to markdown**,**extracts images from word**,**creates a resources folder**,并为每张图片保证干净的 **markdown image relative path**。代码自包含,兼容最新的 Aspose.Words 版本,可轻松嵌入任何 .NET 项目。 + +下一步?尝试将生成的 markdown 输入到 Hugo 或 Jekyll 等静态站点生成器,或实验回调以直接将图片嵌入为 Base64 字符串。如果遇到边缘情况——例如 SVG 图片或异常大的文件——请参考 “Common pitfalls” 表格;通常只需微调即可解决。 + +祝编码愉快,愿你的 markdown 永远指向正确的文件夹! + +{{< /blocks/products/pf/tutorial-page-section >}} +{{< /blocks/products/pf/main-container >}} +{{< /blocks/products/pf/main-wrap-class >}} +{{< blocks/products/products-backtop-button >}} \ No newline at end of file diff --git a/words/chinese/net/programming-with-officemath/_index.md b/words/chinese/net/programming-with-officemath/_index.md index ac126b3799..edfa901fae 100644 --- a/words/chinese/net/programming-with-officemath/_index.md +++ b/words/chinese/net/programming-with-officemath/_index.md @@ -23,6 +23,7 @@ Aspose.Words for .NET 教程“使用 OfficeMath 进行文字处理”将指导 | [数学方程式](./math-equations/) 了解如何使用 Aspose.Words for .NET 在 Word 文档中配置数学公式。包含示例、常见问题解答等内容的分步指南。| | [将 docx 保存为 txt – 使用 C# 将 Word Math 导出为 LaTeX](./save-docx-as-txt-export-word-math-to-latex-with-c/) 了解如何使用 C# 将 Word 文档中的数学公式导出为 LaTeX,并将 docx 文件保存为 txt 格式。| | [将文档另存为 Txt – 在 C# 中将 Word 数学导出为 LaTeX](./save-document-as-txt-export-word-math-to-latex-in-c/) 了解如何使用 Aspose.Words for .NET 将 Word 文档保存为 txt,并将其中的数学公式导出为 LaTeX 格式。| +| [将 DOCX 转换为 TXT – 使用 C# 将 Word 方程式导出为 LaTeX](./convert-docx-to-txt-export-word-equations-to-latex-in-c/) 了解如何使用 C# 将 DOCX 文件转换为 TXT,并将其中的 Word 方程式导出为 LaTeX。| {{< /blocks/products/pf/tutorial-page-section >}} diff --git a/words/chinese/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/_index.md b/words/chinese/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/_index.md new file mode 100644 index 0000000000..ceb99c01e3 --- /dev/null +++ b/words/chinese/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/_index.md @@ -0,0 +1,208 @@ +--- +category: general +date: 2026-04-28 +description: 使用 Aspose.Words 将 DOCX 转换为 TXT 并将 Word 方程导出为 LaTeX。了解如何将 Word 保存为 TXT + 并在几步内处理数学对象。 +draft: false +keywords: +- convert docx to txt +- convert word equations to latex +- convert word to plain text +- save word as txt +- export equations as latex +language: zh +og_description: 使用简易的 C# 代码片段将 DOCX 转换为 TXT,并将 Word 方程导出为 LaTeX。完整指南、代码和技巧。 +og_title: 将 DOCX 转换为 TXT – 导出 Word 方程为 LaTeX +tags: +- C# +- Aspose.Words +- Document Conversion +title: 将 DOCX 转换为 TXT – 在 C# 中导出 Word 方程为 LaTeX +url: /zh/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/ +--- + +{{< blocks/products/pf/main-wrap-class >}} +{{< blocks/products/pf/main-container >}} +{{< blocks/products/pf/tutorial-page-section >}} + +# 将 DOCX 转换为 TXT – 导出 Word 方程为 LaTeX + +是否曾经需要 **convert docx to txt**,但担心 Word 文件中的数学公式会变成乱码?你并不孤单。在许多工程或学术项目中,源文档是 .docx,但下游工具只能理解纯文本或 LaTeX。好消息是,只需几行 C# 和 Aspose.Words,你就可以 **convert docx to txt** *并且* 将每个公式保持为干净的 LaTeX 代码。 + +在本教程中,我们将完整演示整个过程:加载 .docx,配置保存选项以使 Office Math 对象转换为 LaTeX,最后将结果写入 .txt 文件。完成后,你将了解如何 **save word as txt**、**convert word to plain text**,以及 **export equations as latex**,而无需在 API 文档中四处查找。 + +## 你将学到 + +- 实现 **convert docx to txt** 并保留公式所需的确切 API 调用。 +- 为什么选择 `OfficeMathExportMode.LaTeX` 是推荐的 **convert word equations to latex** 方法。 +- 如何处理常见的边缘情况,例如缺少字体或不受支持的公式特性。 +- 一个完整的、可直接运行的 C# 程序,可放入任何 .NET 项目中。 + +### 前置条件 + +- .NET 6.0 或更高版本(代码同样适用于 .NET Framework 4.7+)。 +- Aspose.Words for .NET 的许可证(免费试用可用于评估)。 +- 包含至少一个 Office Math 对象的 Word 文档(`input.docx`)。 + +如果你已经准备好这些,让我们开始吧。 + +## 步骤 1:安装 Aspose.Words + +在运行任何代码之前,你需要先获取该库。在项目文件夹中打开终端并执行: + +```bash +dotnet add package Aspose.Words +``` + +这将获取最新的稳定版本(截至 2026‑04‑28 的 v24.12)。无需额外的 DLL。 + +## 步骤 2:加载源文档 + +我们首先要做的是将 .docx 文件读取到 `Document` 对象中。该对象让我们能够完整访问文件结构,包括文本运行、图像和数学对象。 + +```csharp +using Aspose.Words; +using Aspose.Words.Saving; + +class Program +{ + static void Main() + { + // Step 2: Load the source document + Document doc = new Document(@"YOUR_DIRECTORY\input.docx"); +``` + +> **为什么这很重要:** 加载文档会创建一个内存中的表示,这样后续我们就可以调整每个元素的写出方式。如果文件未找到,Aspose 会抛出 `FileNotFoundException`,在生产代码中你可能需要捕获它。 + +## 步骤 3:为 LaTeX 数学配置 TXT 保存选项 + +默认情况下,`Document.Save` 会写入纯文本并 **丢弃** 所有 Office Math。为了保留这些公式,我们将 `OfficeMathExportMode` 设置为 `LaTeX`。这会指示导出器将每个公式翻译为对应的 LaTeX 代码。 + +```csharp + // Step 3: Configure TXT save options to export Office Math as LaTeX + TxtSaveOptions txtOptions = new TxtSaveOptions + { + OfficeMathExportMode = OfficeMathExportMode.LaTeX, + // Optional: preserve line breaks as they appear in the original Word file + PreserveTableLayout = true + }; +``` + +> **专业提示:** 如果你只需要公式的原始 Unicode 字符(例如快速预览),可以使用 `OfficeMathExportMode.Text`。但对于大多数科学工作流,`LaTeX` 是金标准,因为它被所有 LaTeX 处理器普遍支持。 + +## 步骤 4:将文档保存为纯文本 + +现在我们将转换后的内容写入 `.txt` 文件。该文件将包含普通段落、项目符号列表,并且——多亏上一步——为每个公式生成 LaTeX 代码片段。 + +```csharp + // Step 4: Save the document as plain‑text using the configured options + doc.Save(@"YOUR_DIRECTORY\Math.txt", txtOptions); + } +} +``` + +打开 `Math.txt` 时,你会看到类似以下内容: + +``` +In this report we derive the quadratic formula: +\[ +x = \frac{-b \pm \sqrt{b^{2} - 4ac}}{2a} +\] + +The end. +``` + +注意到 `\[` … `\]` 分隔符了吗?它们是自动生成的 LaTeX 数学块。 + +## 步骤 5:验证输出(可选但推荐) + +很容易忽略细微的转换问题,尤其是公式中包含自定义符号时。一个快速的检查方法是将生成的 `.txt` 输入到 LaTeX 编译器(例如 `pdflatex`),查看是否能够无错误编译。 + +```bash +pdflatex -interaction=nonstopmode Math.txt +``` + +如果编译成功,你就已经一次性 **convert word equations to latex** 并 **convert docx to txt**。如果出现错误,请留意未定义命令的提示——这通常表明某些公式特性 Aspose.Words 无法翻译(例如特定的矩阵表示)。在这种情况下,你可以回退到 `OfficeMathExportMode.MathML`,并使用其他工具将 MathML 后处理为 LaTeX。 + +## 常见陷阱及规避方法 + +| Issue | Why it Happens | Fix | +|-------|----------------|-----| +| Missing fonts | Aspose.Words 需要相应的字体来正确渲染符号。 | 在机器上安装缺失的字体或将其嵌入 .docx 中。 | +| Complex equations not exported | 某些较新的 Office Math 功能尚未映射到 LaTeX。 | 使用 `OfficeMathExportMode.MathML`,然后使用 MathML‑to‑LaTeX 库进行转换。 | +| Extra blank lines | 纯文本保存器会保留段落换行,可能导致额外的空白。 | 将 `txtOptions.AddBidiMarks = false`,或使用简单脚本后处理文件。 | + +## 完整可运行示例(复制粘贴即用) + +下面是完整的程序代码,已准备好编译。将 `YOUR_DIRECTORY` 替换为存放 `input.docx` 的文件夹路径。 + +```csharp +using System; +using Aspose.Words; +using Aspose.Words.Saving; + +namespace DocxToTxtWithLatex +{ + class Program + { + static void Main() + { + try + { + // Load the source document + Document doc = new Document(@"C:\Docs\input.docx"); + + // Configure save options: export equations as LaTeX + TxtSaveOptions txtOptions = new TxtSaveOptions + { + OfficeMathExportMode = OfficeMathExportMode.LaTeX, + PreserveTableLayout = true, + AddBidiMarks = false + }; + + // Save as plain‑text + string outputPath = @"C:\Docs\Math.txt"; + doc.Save(outputPath, txtOptions); + + Console.WriteLine($"Successfully converted DOCX to TXT. Output at: {outputPath}"); + } + catch (Exception ex) + { + Console.Error.WriteLine($"Conversion failed: {ex.Message}"); + } + } + } +} +``` + +运行此程序将 **save word as txt**,并将每个 Office Math 块转换为 LaTeX,为你提供一个干净、可搜索的纯文本文件。 + +## 后续步骤及相关主题 + +- **Batch conversion:** 将上述逻辑包装在 `foreach` 循环中,以处理整个文件夹的 .docx 文件。 +- **Combine with PDF generation:** 获得 LaTeX 代码片段后,将其输入 PDF 流程(例如 `PdfSharp` + `MiKTeX`),生成 PDF 报告。 +- **Export equations as latex** for other formats: Aspose.Words 还支持 `SaveFormat.Markdown`,可自动嵌入 LaTeX。 +- **Performance tuning:** 对于大型文档,复用同一个 `TxtSaveOptions` 实例,并关闭诸如 `AddBidiMarks` 等不必要的功能。 + +--- + +### 图片示例(可选) + +如果你更喜欢直观的示例,这里是一张在 Notepad++ 中打开输出文件的截图。 + +![convert docx to txt output showing LaTeX equations](convert-docx-to-txt-output.png) + +(Alt text: “convert docx to txt output showing LaTeX equations” – 满足主要关键词要求。) + +## 结论 + +我们已经演示了一种可靠的方式来 **convert docx to txt**,同时将每个公式保留为干净的 LaTeX。关键在于 `OfficeMathExportMode.LaTeX` 标志,它将 Word 专有的数学格式转换为任何 LaTeX 引擎都能理解的形式。使用上面的完整代码示例,你可以在一次自包含的运行中 **save word as txt**、**convert word to plain text**,以及 **export equations as latex**。 + +欢迎自行尝试——将输出扩展名改为 `.md` 以生成 Markdown,或将代码片段集成到更大的文档处理流水线中。如果遇到任何问题,请在下方留言,我很乐意帮助排查。 + +祝编码愉快! + +{{< /blocks/products/pf/tutorial-page-section >}} +{{< /blocks/products/pf/main-container >}} +{{< /blocks/products/pf/main-wrap-class >}} +{{< blocks/products/products-backtop-button >}} \ No newline at end of file diff --git a/words/czech/net/ai-powered-document-processing/_index.md b/words/czech/net/ai-powered-document-processing/_index.md index f01dc2b734..69181574ef 100644 --- a/words/czech/net/ai-powered-document-processing/_index.md +++ b/words/czech/net/ai-powered-document-processing/_index.md @@ -42,6 +42,7 @@ Nakonec nezapomeňte se podívat na naše [Práce s možnostmi shrnutí](./worki | [Práce s modelem umělé inteligence Google](./working-with-google-ai-model/) Zlepšete zpracování dokumentů s Aspose.Words pro .NET a Google AI a vytvářejte stručné shrnutí bez námahy. | | [Práce s modelem otevřené umělé inteligence](./working-with-open-ai-model/) | Získejte efektivní sumarizaci dokumentů pomocí Aspose.Words pro .NET s výkonnými modely OpenAI. Ponořte se do tohoto komplexního průvodce hned teď. | | [Práce s možnostmi shrnutí](./working-with-summarize-options/) | Naučte se efektivně shrnovat dokumenty Wordu pomocí Aspose.Words pro .NET s naším podrobným návodem k integraci modelů umělé inteligence pro rychlý přehled. | +| [Připojení k lokálnímu LLM v C# – Kompletní programovací průvodce](./connect-to-local-llm-in-c-complete-programming-guide/) | Naučte se, jak v C# připojit a využívat lokální model LLM pro generování textu s Aspose.Words. | {{< /blocks/products/pf/tutorial-page-section >}} diff --git a/words/czech/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/_index.md b/words/czech/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/_index.md new file mode 100644 index 0000000000..20a9ad96d3 --- /dev/null +++ b/words/czech/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/_index.md @@ -0,0 +1,284 @@ +--- +category: general +date: 2026-04-28 +description: Připojte se k lokálnímu LLM z C# a požádejte velký jazykový model, aby + načetl Word dokument, zavolejte lokální LLM a automaticky přepište text. Kód krok + po kroku je zahrnut. +draft: false +keywords: +- connect to local llm +- prompt large language model +- load word document +- call local llm +- rewrite text automatically +language: cs +og_description: Připojte se k lokálnímu LLM z C# a zjistěte, jak zadat prompt velkému + jazykovému modelu, načíst Word dokument, zavolat lokální LLM a automaticky přepsat + text během několika minut. +og_title: Připojení k lokálnímu LLM v C# – Kompletní programovací průvodce +tags: +- Aspose.Words +- C# +- LLM +- AI Automation +title: Připojení k lokálnímu LLM v C# – Kompletní programovací průvodce +url: /cs/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/ +--- + +{{< blocks/products/pf/main-wrap-class >}} +{{< blocks/products/pf/main-container >}} +{{< blocks/products/pf/tutorial-page-section >}} + +# Připojení k lokálnímu LLM v C# – Kompletní programovací průvodce + +Už jste někdy potřebovali **připojit se k lokálnímu llm** z .NET aplikace a přemýšleli, jak ji nechat komunikovat se souborem Word? Nejste v tom sami. V tomto průvodci projdeme celý proces – připojení k lokálnímu llm, **prompt large language model**, načtení Word dokumentu, **call local llm** a nakonec **rewrite text automatically**. Na konci budete mít spustitelný příklad, který přemění libovolný odstavec do formálního tónu bez jakýchkoli externích API klíčů. + +## Co tento tutoriál pokrývá + +Začneme instalací potřebných NuGet balíčků, poté spustíme jednoduchý lokální LLM endpoint (např. Ollama na portu 11434). Pak načteme soubor `.docx` pomocí Aspose.Words, pošleme odstavec LLM, získáme přepsanou verzi a zapíšeme ji zpět do stejného dokumentu. Také uvidíte, jak řešit běžné úskalí – prázdné odstavce, asynchronní uvolňování a zvláštnosti kódování – aby kód fungoval v produkci, ne jen jako demo. + +### Požadavky + +- .NET 6.0 SDK nebo novější (můžete také použít .NET 8, pokud chcete) +- Visual Studio 2022 nebo VS Code s rozšířením C# +- **Aspose.Words for .NET** (bezplatná zkušební verze funguje dobře) +- Lokálně hostovaný LLM, který podporuje kontrakt `/api/generate` (např. Ollama, LMStudio) +- Základní znalost async/await v C# + +> **Pro tip:** Pokud jste ještě nenainstalovali Ollama, spusťte `ollama serve` a stáhněte model pomocí `ollama pull llama3`. Výchozí HTTP endpoint bude `http://localhost:11434/api/generate`. + +--- + +## Krok 1: Instalace potřebných balíčků + +Nejprve přidejte do projektu NuGet balíčky Aspose.Words a Aspose.Words.AI. + +```bash +dotnet add package Aspose.Words +dotnet add package Aspose.Words.AI +``` + +Tyto knihovny nám poskytují schopnost **load word document** a tenký obal pro **call local llm** bez ručního sestavování HTTP požadavků. + +--- + +## Krok 2: Připojení k lokálnímu LLM endpointu + +Připojení k lokálně hostovanému modelu je tak jednoduché, jako vytvořit instanci `LocalLargeLanguageModel`. Konstruktor očekává úplnou URL generovacího endpointu. + +```csharp +using Aspose.Words.AI; +using Aspose.Words; +using System.Threading.Tasks; + +// Create a client that talks to the LLM running on localhost +var localLlm = new LocalLargeLanguageModel("http://localhost:11434/api/generate"); +``` + +Proč obalujeme endpoint do třídy? `LocalLargeLanguageModel` za vás řeší JSON serializaci, opakování požadavků a streamování odpovědí – takže se můžete soustředit na logiku promptu místo manipulace s `HttpClient`. + +--- + +## Krok 3: Načtení zdrojového Word dokumentu + +Dále načteme dokument do paměti. Aspose.Words podporuje prakticky každý Word formát, takže `Document` zpracuje `input.docx` bez nutnosti mít nainstalovaný Office. + +```csharp +// Path to the source file – adjust as needed +string inputPath = Path.Combine(Environment.CurrentDirectory, "input.docx"); + +// Load the document; throws if the file is missing or corrupted +Document sourceDocument = new Document(inputPath); +``` + +Pokud potřebujete pracovat se streamem (např. soubor nahraný přes ASP.NET), stačí nahradit cestu k souboru objektem `MemoryStream` a předat jej konstruktoru `Document`. + +--- + +## Krok 4: Extrahování textu aktuálního odstavce + +Použijeme `DocumentBuilder` k procházení dokumentu. V tomto příkladu přepisujeme **první odstavec**, ale můžete iterovat přes `sourceDocument.GetChildNodes(NodeType.Paragraph, true)` a zpracovat více. + +```csharp +// Builder gives us a cursor inside the document +DocumentBuilder docBuilder = new DocumentBuilder(sourceDocument); + +// Grab the text of the paragraph where the builder is currently positioned +string originalParagraph = docBuilder.CurrentParagraph?.GetText() ?? string.Empty; + +// Safety check – avoid sending empty strings to the LLM +if (string.IsNullOrWhiteSpace(originalParagraph)) +{ + Console.WriteLine("No paragraph found at the current cursor position."); + return; +} +``` + +Operátor `?.` zabraňuje `NullReferenceException`, pokud je dokument prázdný. Jedná se o jeden z těch **edge cases**, který nováčky často překvapí. + +--- + +## Krok 5: Prompt LLM pro přepsání odstavce + +Nyní skutečně **prompt large language model**. Prompt je v prosté angličtině; obal jej pošle jako JSON na lokální endpoint. + +```csharp +// Build a friendly instruction for the model +string prompt = $"Rewrite the following sentence in a more formal tone:\n{originalParagraph}"; + +// Await the model's response – this is an async call +string rewrittenParagraph = await localLlm.PromptAsync(prompt); +``` + +Proč formulovat požadavek tímto způsobem? LLM reagují nejlépe na jasné, jednorázové instrukce. Přidání nového řádku po dvojtečce oddělí instrukci od obsahu a snižuje šanci, že model vrátí samotný prompt. + +**Očekávaný výstup** – Pokud `originalParagraph` byl `"Hey, what's up?"`, LLM může vrátit: + +> “Good day, how may I assist you?” + +Výsledek můžete ověřit vytištěním: + +```csharp +Console.WriteLine("Original: " + originalParagraph); +Console.WriteLine("Rewritten: " + rewrittenParagraph); +``` + +--- + +## Krok 6: Vložení přepsaného textu zpět do dokumentu + +S novým textem v ruce nahradíme starý odstavec. `DocumentBuilder.Writeln` zapíše nový řádek a posune kurzor dopředu, což je ideální pro přidání. Pokud potřebujete *nahradit* přesně ten samý odstavec, můžete před zápisem použít `docBuilder.CurrentParagraph.RemoveAllChildren()`. + +```csharp +// Option A – Append a new paragraph (keeps the original) +docBuilder.Writeln(rewrittenParagraph); + +// Option B – Replace the existing paragraph (uncomment to use) +// docBuilder.CurrentParagraph.RemoveAllChildren(); +// docBuilder.CurrentParagraph.AppendChild(new Run(docBuilder.Document, rewrittenParagraph)); +``` + +Oba přístupy jsou ukázány, abyste si mohli vybrat ten, který lépe odpovídá vašemu workflow. + +--- + +## Krok 7: Uložení aktualizovaného dokumentu + +Nakonec změny uložíme do nového souboru. Aspose.Words automaticky zvolí formát podle přípony souboru. + +```csharp +string outputPath = Path.Combine(Environment.CurrentDirectory, "output.docx"); +sourceDocument.Save(outputPath); + +Console.WriteLine($"Document saved to {outputPath}"); +``` + +Otevřete `output.docx` ve Wordu a uvidíte, že odstavec nyní zní formálně. + +--- + +## Kompletní funkční příklad + +Níže je **complete, self‑contained program**. Zkopírujte jej do konzolového projektu, obnovte NuGet balíčky a spusťte – žádná další konfigurace není potřeba kromě běžícího lokálního LLM. + +```csharp +using Aspose.Words.AI; +using Aspose.Words; +using System; +using System.IO; +using System.Threading.Tasks; + +class Program +{ + static async Task Main() + { + // ------------------------------------------------- + // Step 1: Connect to the locally hosted LLM endpoint + // ------------------------------------------------- + var localLlm = new LocalLargeLanguageModel("http://localhost:11434/api/generate"); + + // ------------------------------------------------- + // Step 2: Load the source Word document + // ------------------------------------------------- + string inputPath = Path.Combine(Environment.CurrentDirectory, "input.docx"); + Document sourceDocument = new Document(inputPath); + + // ------------------------------------------------- + // Step 3: Retrieve the text of the current paragraph + // ------------------------------------------------- + DocumentBuilder docBuilder = new DocumentBuilder(sourceDocument); + string originalParagraph = docBuilder.CurrentParagraph?.GetText() ?? string.Empty; + + if (string.IsNullOrWhiteSpace(originalParagraph)) + { + Console.WriteLine("No paragraph found at the current cursor position."); + return; + } + + // ------------------------------------------------- + // Step 4: Ask the LLM to rewrite the paragraph in a formal tone + // ------------------------------------------------- + string prompt = $"Rewrite the following sentence in a more formal tone:\n{originalParagraph}"; + string rewrittenParagraph = await localLlm.PromptAsync(prompt); + + // ------------------------------------------------- + // Step 5: Insert the rewritten text back into the document + // ------------------------------------------------- + docBuilder.Writeln(rewrittenParagraph); + + // ------------------------------------------------- + // Step 6: Save the updated document + // ------------------------------------------------- + string outputPath = Path.Combine(Environment.CurrentDirectory, "output.docx"); + sourceDocument.Save(outputPath); + + Console.WriteLine("Original paragraph:"); + Console.WriteLine(originalParagraph); + Console.WriteLine("\nRewritten paragraph:"); + Console.WriteLine(rewrittenParagraph); + Console.WriteLine($"\nDocument saved to {outputPath}"); + } +} +``` + +### Co očekávat při spuštění + +1. Konzole vypíše původní a přepsaný odstavec. +2. `output.docx` se objeví vedle `input.docx`. +3. Po otevření souboru uvidíte nový formální odstavec vložený za původní (nebo nahrazený, pokud jste použili alternativní kód). + +--- + +## Řešení běžných okrajových případů + +| Situace | Řešení | +|-----------|----------| +| **Prázdný nebo jen s bílými znaky odstavec** | Zkontrolujte `string.IsNullOrWhiteSpace` před odesláním promptu (viz Krok 3). | +| **LLM vrátí chybu nebo prázdný řetězec** | Zabalte `PromptAsync` do `try/catch` a v případě chyby použijte původní text. | +| **Více odstavců vyžaduje přepsání** | Procházejte `sourceDocument.GetChildNodes(NodeType.Paragraph, true)` a aplikujte stejnou logiku promptu. | +| **Velké dokumenty způsobují latenci** | Sesbírejte odstavce do dávky a pošlete je v jednom požadavku (prompt až 4 KB na volání). | +| **Ne‑ASCII znaky se zkomolí** | Ujistěte se, že LLM endpoint používá UTF-8 (většina moderních modelů to dělá). | + +--- + +## Další kroky a související témata + +- **Prompt large language model** s podrobnějšími instrukcemi (např. stylové příručky, omezení délky). +- Použijte **call local llm** ve webovém API k vystavení automatizace dokumentů jako služby. +- Prozkoumejte **load word document** v paralelních streamech pro scénáře s vysokou propustností. +- Kombinujte tento přístup s **rewrite text automatically** pro hromadnou generaci e‑mailů nebo standardizaci reportů. + +Pokud se chcete ponořit hlouběji, podívejte se na dokumentaci Aspose k **document merging** a na Ollama API reference pro vlastní parametry vzorkování. + +--- + +## Závěr + +Právě jsme vám ukázali, jak **connect to local llm** z C#, **prompt large language model**, **load word document**, **call local llm** a **rewrite text automatically** – vše v jedné spustitelné konzolové aplikaci. Tento vzor je škálovatelný: můžete měnit prompt, iterovat přes odstavce nebo exponovat logiku přes ASP.NET endpoint. Hlavní výsledek je, že lokální AI modely lze úzce integrovat s klasickými knihovnami pro zpracování dokumentů, což vám poskytne výkonnou automatizaci bez opuštění důvěryhodného on‑prem prostředí. + +Máte otázky ohledně vláken, + +{{< /blocks/products/pf/tutorial-page-section >}} +{{< /blocks/products/pf/main-container >}} +{{< /blocks/products/pf/main-wrap-class >}} +{{< blocks/products/products-backtop-button >}} \ No newline at end of file diff --git a/words/czech/net/programming-with-markdownsaveoptions/_index.md b/words/czech/net/programming-with-markdownsaveoptions/_index.md index 315442e3d0..09cbb07753 100644 --- a/words/czech/net/programming-with-markdownsaveoptions/_index.md +++ b/words/czech/net/programming-with-markdownsaveoptions/_index.md @@ -42,6 +42,7 @@ Zpracování textu pomocí MarkdownSaveOptions je podrobný návod, který vás | [Jak exportovat Markdown z DOCX – Kompletní průvodce](./how-to-export-markdown-from-docx-complete-guide/) | Naučte se, jak exportovat Markdown z DOCX pomocí kompletního průvodce. | | [Uložení docx jako markdown s Aspose.Words – Kompletní průvodce v C#](./save-docx-as-markdown-with-aspose-words-full-c-guide/) | Naučte se, jak pomocí Aspose.Words převést soubory DOCX do formátu Markdown v kompletním průvodci v C#. | | [Uložení obrázků Word – převod Wordu do Markdownu s Aspose](./save-word-images-convert-word-to-markdown-with-aspose/) | Naučte se ukládat obrázky z dokumentů Word a převádět je do formátu Markdown pomocí Aspose.Words pro .NET. | +| [Relativní cesta k obrázku v Markdownu – převod Wordu do Markdownu](./markdown-image-relative-path-convert-word-to-markdown/) | Naučte se, jak při konverzi Wordu do Markdownu nastavit relativní cesty k obrázkům pomocí Aspose.Words pro .NET. | {{< /blocks/products/pf/tutorial-page-section >}} diff --git a/words/czech/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/_index.md b/words/czech/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/_index.md new file mode 100644 index 0000000000..012b32b475 --- /dev/null +++ b/words/czech/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/_index.md @@ -0,0 +1,207 @@ +--- +category: general +date: 2026-04-28 +description: Naučte se nastavit relativní cestu k obrázku v markdownu při převodu + Wordu na markdown, extrahovat obrázky z Wordu a vytvořit složku resources pro exportované + obrázky. +draft: false +keywords: +- markdown image relative path +- convert word to markdown +- extract images from word +- create resources folder +- export images from docx +language: cs +og_description: Nastavte relativní cestu k obrázku v markdownu při převodu Wordu na + markdown, extrahujte obrázky z Wordu a vytvořte složku resources pro exportované + obrázky. +og_title: Relativní cesta k obrázku v markdownu – Převést Word do Markdownu +tags: +- Aspose.Words +- C# +- Markdown +- Image Export +title: relativní cesta k obrázku v markdownu – převod Wordu do Markdownu +url: /cs/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/ +--- + +{{< blocks/products/pf/main-wrap-class >}} +{{< blocks/products/pf/main-container >}} +{{< blocks/products/pf/tutorial-page-section >}} + +# relativní cesta k obrázku v markdown – Převod Wordu do markdown + +Už jste někdy potřebovali **relativní cestu k obrázku v markdown** při **převodu Wordu do markdown**? Nejste v tom sami. Většina vývojářů narazí na problém, když vygenerovaný Markdown odkazuje na obrázky v jedné složce, čímž se naruší struktura relativních odkazů, kterou očekáváte u statického webu nebo v repozitáři GitHub. + +V tomto tutoriálu projdeme kompletní, end‑to‑end řešení, které **extrahuje obrázky z Wordu**, **vytvoří složku resources** a přepíše odkazy na obrázky tak, aby používaly čistou *relativní cestu k obrázku v markdown*. Na konci budete mít připravený k publikaci soubor `.md` a přehledně uspořádaný adresář `Resources` obsahující každý obrázek extrahovaný z původního `.docx`. + +> **Co získáte:** jediný C# program (žádné externí skripty), jasné vysvětlení *proč* je každá část důležitá a několik praktických tipů, které můžete zkopírovat a vložit do svých projektů. + +--- + +## Prerequisites + +Než se pustíme do kódu, ujistěte se, že máte: + +- **.NET 6.0** nebo novější nainstalovaný (můžete také cílit na .NET Framework 4.7+, ale .NET 6 je ideální pro nové projekty). +- **Aspose.Words for .NET** (nejnovější NuGet balíček v době psaní, verze 23.12). Nainstalujte jej pomocí: + ```bash + dotnet add package Aspose.Words + ``` +- Word dokument, který skutečně obsahuje obrázky — nazveme ho `WithImages.docx`. +- Složku, kam chcete uložit výstupní markdown a obrázky, např. `C:\Projects\MarkdownExport`. + +Žádné další knihovny nejsou potřeba; vše ostatní zajišťuje Aspose.Words. + +--- + +## Step 1: Load the source Word document (the starting point for convert word to markdown) + +```csharp +using System; +using System.IO; +using Aspose.Words; +using Aspose.Words.Saving; + +class Program +{ + static void Main() + { + // Adjust the path to point at your own .docx file. + string sourcePath = @"C:\Projects\MarkdownExport\WithImages.docx"; + + // Load the document – this is where Aspose.Words parses the Word file. + Document doc = new Document(sourcePath); + + // The rest of the workflow follows… + } +} +``` + +*Proč je to důležité:* Načtení dokumentu nám poskytuje přístup k internímu stromu uzlů, který zahrnuje části s obrázky, které později potřebujeme **exportovat obrázky z docx**. Pokud načtení selže, žádný z následujících kroků se neprovede, takže dvojitě zkontrolujte cestu a oprávnění k souboru. + +--- + +## Step 2: Configure `MarkdownSaveOptions` with a custom callback (the heart of create resources folder) + +`ResourceSavingCallback` nám umožňuje zasáhnout pokaždé, když Aspose.Words chce zapsat soubor s obrázkem. V rámci callbacku **vytvoříme podsložku Resources** a upravíme odkaz tak, aby vygenerovaný markdown používal *relativní cestu k obrázku v markdown*. + +```csharp +// Inside Main(), after loading the document: +string outputFolder = @"C:\Projects\MarkdownExport"; +string resourcesFolder = Path.Combine(outputFolder, "Resources"); + +// Make sure the folder exists before we start saving anything. +Directory.CreateDirectory(resourcesFolder); + +// Set up the Markdown save options. +MarkdownSaveOptions mdOptions = new MarkdownSaveOptions +{ + // Hook that runs for every image resource. + ResourceSavingCallback = new MyMarkdownResourceCallback(resourcesFolder) +}; + +// Save the document as Markdown. +string markdownPath = Path.Combine(outputFolder, "Doc.md"); +doc.Save(markdownPath, mdOptions); +``` + +Všimněte si, že jsme do konstruktoru callbacku předali `resourcesFolder` — tím udržujeme cestu ke složce flexibilní a vyhneme se pevně zakódovaným řetězcům v celém kódu. + +--- + +## Step 3: Implement the callback that **creates resources folder** and rewrites the path + +```csharp +/// +/// Handles image extraction and path rewriting for markdown export. +/// +class MyMarkdownResourceCallback : IResourceSavingCallback +{ + private readonly string _resourcesFolder; + + public MyMarkdownResourceCallback(string resourcesFolder) + { + _resourcesFolder = resourcesFolder; + } + + public void ResourceSaving(ResourceSavingArgs args) + { + // 1️⃣ Build the full file system path where the image will be stored. + string targetPath = Path.Combine(_resourcesFolder, args.ResourceFileName); + + // 2️⃣ Ensure the directory exists (in case Aspose creates sub‑folders). + Directory.CreateDirectory(Path.GetDirectoryName(targetPath)); + + // 3️⃣ Write the image stream to disk. + using (FileStream fileStream = File.Create(targetPath)) + { + args.Stream.CopyTo(fileStream); + } + + // 4️⃣ Update the markdown reference to use a relative path. + // This is the crucial line that gives us the markdown image relative path. + args.ResourceFileName = Path.Combine("Resources", args.ResourceFileName); + } +} +``` + +*Proč to funguje:* `args.Stream` obsahuje surová data obrázku. Zkopírováním do souboru uvnitř naší složky `Resources` **exportujeme obrázky z docx** bezpečně. Pak nahradíme `args.ResourceFileName` relativní URL (`Resources/image.png`). Když Aspose.Words později zapíše markdown, vloží právě tento řetězec, čímž získáme požadovanou *relativní cestu k obrázku v markdown*. + +--- + +## Step 4: Verify the generated Markdown (what the final output looks like) + +Otevřete `Doc.md` v libovolném textovém editoru. Měli byste vidět něco podobného: + +```markdown +# Sample Heading + +Here is an inline picture: + +![Image 0](Resources/Image_0.png) + +And a picture inside a table: + +![Image 1](Resources/Image_1.jpg) +``` + +Důležitá část je, že každý odkaz na obrázek ukazuje na `Resources/...` — to je **relativní cesta k obrázku v markdown**, kterou jsme chtěli. + +![příklad relativní cesty k obrázku v markdown](example.png "příklad relativní cesty k obrázku v markdown") + +*Tip:* Pokud otevřete markdown v prohlížeči, který respektuje relativní odkazy (náhled ve VS Code, GitHub nebo generátor statických stránek), obrázky se vykreslí správně bez jakékoli další konfigurace. + +--- + +## Step 5: Common pitfalls and pro‑tips + +| Problém | Proč se to děje | Jak to opravit | +|---------|----------------|----------------| +| Obrázky končí v kořenové složce místo `Resources` | Callback nebyl připojen nebo `args.ResourceFileName` nebyl přepsán. | Zkontrolujte, že `ResourceSavingCallback` je nastaven **před** voláním `doc.Save`. | +| Název souboru obsahuje neplatné znaky | Word někdy pojmenovává obrázky mezerami nebo Unicode symboly. | Použijte `Path.GetInvalidFileNameChars()` k sanitaci `args.ResourceFileName` uvnitř callbacku. | +| Velké dokumenty zpracovávají dlouho | Každý obrázek se zapisuje synchronně. | Přepněte na asynchronní I/O (`await args.Stream.CopyToAsync(fileStream)`) pokud používáte .NET 6+ a potřebujete výkon. | +| Relativní cesty se rozbijí po přesunu markdownu | Cesta je relativní k umístění souboru markdown. | Udržujte `Doc.md` a složku `Resources` spolu, nebo upravte callback tak, aby používal jiný relativní prefix (např. `../assets`). | + +--- + +## Step 6: Extending the solution (what if you need more control?) + +- **Multiple output formats:** Nahraďte `MarkdownSaveOptions` za `HtmlSaveOptions` nebo `PdfSaveOptions`, přičemž zachováte stejný callback — Aspose.Words ho zavolá pro každý obrázek bez ohledu na formát. +- **Custom image naming:** Pokud chcete přejmenovat obrázky (např. `figure-01.png`), upravte `args.ResourceFileName` v callbacku před zápisem souboru. +- **Embedding images as Base64:** Nastavte `args.ResourceFileName` na data URI (`data:image/png;base64,...`) a vynechejte zápis souboru. To je užitečné pro exporty markdownu do jediného souboru. + +--- + +## Conclusion + +Nyní máte plně funkční C# program, který **převádí Word do markdown**, **extrahuje obrázky z word**, **vytváří složku resources** a zaručuje čistou **relativní cestu k obrázku v markdown** pro každý obrázek. Kód je samostatný, funguje s nejnovější verzí Aspose.Words a lze jej vložit do libovolného .NET projektu s minimálním úsilím. + +Další kroky? Zkuste nasadit vygenerovaný markdown do generátoru statických stránek jako Hugo nebo Jekyll, nebo experimentujte s callbackem a vkládejte obrázky přímo jako Base64 řetězce. Pokud narazíte na okrajové případy — např. SVG obrázky nebo neobvykle velké soubory — vrátíte se k tabulce „Common pitfalls“; malá úprava obvykle problém vyřeší. + +Šťastné kódování a ať vaše markdown vždy ukazuje na správnou složku! + +{{< /blocks/products/pf/tutorial-page-section >}} +{{< /blocks/products/pf/main-container >}} +{{< /blocks/products/pf/main-wrap-class >}} +{{< blocks/products/products-backtop-button >}} \ No newline at end of file diff --git a/words/czech/net/programming-with-officemath/_index.md b/words/czech/net/programming-with-officemath/_index.md index d01ad08c38..6f2f8e7af0 100644 --- a/words/czech/net/programming-with-officemath/_index.md +++ b/words/czech/net/programming-with-officemath/_index.md @@ -23,7 +23,7 @@ Výukové programy Aspose.Words pro .NET o zpracování textu v OfficeMath vás | [Matematické rovnice](./math-equations/) | Naučte se, jak konfigurovat matematické rovnice v dokumentech Wordu pomocí Aspose.Words pro .NET. Podrobný návod s příklady, nejčastějšími dotazy a dalšími informacemi. | | [Uložit docx jako txt – Exportovat Word Math do LaTeXu pomocí C#](./save-docx-as-txt-export-word-math-to-latex-with-c/) | Naučte se, jak uložit soubor DOCX jako TXT a exportovat rovnice Word Math do LaTeXu pomocí C# a Aspose.Words. | | [Uložit dokument jako Txt – Exportovat matematiku Word do LaTeXu v C#](./save-document-as-txt-export-word-math-to-latex-in-c/) | Naučte se, jak uložit dokument jako TXT a exportovat rovnice Word do LaTeXu pomocí Aspose.Words pro .NET v C#. | - +| [Převést docx na txt – Exportovat rovnice Word do LaTeXu v C#](./convert-docx-to-txt-export-word-equations-to-latex-in-c/) | Naučte se, jak převést soubor DOCX na TXT a exportovat rovnice Word do LaTeXu pomocí C# a Aspose.Words. | {{< /blocks/products/pf/tutorial-page-section >}} diff --git a/words/czech/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/_index.md b/words/czech/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/_index.md new file mode 100644 index 0000000000..d10887fad2 --- /dev/null +++ b/words/czech/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/_index.md @@ -0,0 +1,212 @@ +--- +category: general +date: 2026-04-28 +description: Převod DOCX na TXT a export rovnic z Wordu do LaTeXu pomocí Aspose.Words. + Naučte se, jak uložit Word jako TXT a zpracovat matematické objekty během několika + kroků. +draft: false +keywords: +- convert docx to txt +- convert word equations to latex +- convert word to plain text +- save word as txt +- export equations as latex +language: cs +og_description: Převést DOCX na TXT a exportovat rovnice Wordu do LaTeXu pomocí jednoduchého + C# úryvku. Kompletní průvodce, kód a tipy. +og_title: Převést DOCX na TXT – Exportovat rovnice Wordu do LaTeXu +tags: +- C# +- Aspose.Words +- Document Conversion +title: Převod DOCX na TXT – Export rovnic z Wordu do LaTeXu v C# +url: /cs/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/ +--- + +{{< blocks/products/pf/main-wrap-class >}} +{{< blocks/products/pf/main-container >}} +{{< blocks/products/pf/tutorial-page-section >}} + +# Převod DOCX na TXT – Export rovnic Word do LaTeXu + +Už jste někdy potřebovali **convert docx to txt**, ale obávali se, že matematika ve vašem souboru Word se změní v nečitelný chaos? Nejste v tom sami. V mnoha inženýrských nebo akademických projektech je zdrojový dokument ve formátu .docx, zatímco následné nástroje rozumí jen prostému textu nebo LaTeXu. Dobrá zpráva? Několika řádky C# a Aspose.Words můžete **convert docx to txt** *a* zachovat každou rovnici jako čistý LaTeX kód. + +V tomto tutoriálu projdeme celý proces: načtení .docx, nastavení možností uložení tak, aby se Office Math objekty převedly na LaTeX, a nakonec zápis výsledku do souboru .txt. Na konci budete vědět, jak **save word as txt**, **convert word to plain text**, a **export equations as latex** bez nutnosti procházet dokumentaci API. + +## Co se naučíte + +- Přesné volání API potřebné k **convert docx to txt** při zachování rovnic. +- Proč je volba `OfficeMathExportMode.LaTeX` doporučeným způsobem, jak **convert word equations to latex**. +- Jak řešit běžné okrajové případy, jako chybějící fonty nebo nepodporované funkce rovnic. +- Kompletní, připravený C# program, který můžete vložit do libovolného .NET projektu. + +### Požadavky + +- .NET 6.0 nebo novější (kód funguje také na .NET Framework 4.7+). +- Licence pro Aspose.Words for .NET (zdarma zkušební verze stačí pro hodnocení). +- Dokument Word (`input.docx`) obsahující alespoň jeden Office Math objekt. + +Pokud máte vše připravené, pojďme na to. + +## Krok 1: Instalace Aspose.Words + +Než se spustí jakýkoli kód, potřebujete knihovnu. Otevřete terminál ve složce projektu a spusťte: + +```bash +dotnet add package Aspose.Words +``` + +Tím se stáhne nejnovější stabilní verze (k 28. 04. 2026 v24.12). Žádné další DLL nejsou potřeba. + +## Krok 2: Načtení zdrojového dokumentu + +Prvním krokem je načíst .docx soubor do objektu `Document`. Tento objekt poskytuje plný přístup ke struktuře souboru, včetně textových běhů, obrázků a matematických objektů. + +```csharp +using Aspose.Words; +using Aspose.Words.Saving; + +class Program +{ + static void Main() + { + // Step 2: Load the source document + Document doc = new Document(@"YOUR_DIRECTORY\input.docx"); +``` + +> **Proč je to důležité:** Načtení dokumentu vytvoří reprezentaci v paměti, takže později můžeme upravit, jak se jednotlivé elementy zapíšou. Pokud soubor není nalezen, Aspose vyhodí `FileNotFoundException`, který můžete v produkčním kódu zachytit. + +## Krok 3: Nastavení možností uložení TXT pro LaTeX matematiku + +Ve výchozím nastavení `Document.Save` zapisuje prostý text a **zahazuje** veškerý Office Math. Abychom rovnice zachovali, nastavíme `OfficeMathExportMode` na `LaTeX`. Tím řekneme exportéru, aby každou rovnici převedl na její LaTeX ekvivalent. + +```csharp + // Step 3: Configure TXT save options to export Office Math as LaTeX + TxtSaveOptions txtOptions = new TxtSaveOptions + { + OfficeMathExportMode = OfficeMathExportMode.LaTeX, + // Optional: preserve line breaks as they appear in the original Word file + PreserveTableLayout = true + }; +``` + +> **Tip:** Pokud potřebujete jen surové Unicode znaky rovnice (např. pro rychlý náhled), můžete použít `OfficeMathExportMode.Text`. Pro většinu vědeckých pipeline je však `LaTeX` zlatým standardem, protože je univerzálně pochopen LaTeX procesory. + +## Krok 4: Uložení dokumentu jako prostý text + +Nyní zapíšeme transformovaný obsah do souboru `.txt`. Soubor bude obsahovat běžné odstavce, odrážky a — díky předchozímu kroku — LaTeX úryvky pro každou rovnici. + +```csharp + // Step 4: Save the document as plain‑text using the configured options + doc.Save(@"YOUR_DIRECTORY\Math.txt", txtOptions); + } +} +``` + +Po otevření `Math.txt` uvidíte něco jako: + +``` +In this report we derive the quadratic formula: +\[ +x = \frac{-b \pm \sqrt{b^{2} - 4ac}}{2a} +\] + +The end. +``` + +Všimněte si delimitérů `\[` … `\]`? Jedná se o LaTeX matematické bloky generované automaticky. + +## Krok 5: Ověření výstupu (volitelné, ale doporučené) + +Je snadné přehlédnout drobný problém při konverzi, zejména když rovnice obsahují vlastní symboly. Rychlá kontrola spočívá v předání vygenerovaného `.txt` LaTeX kompilátoru (např. `pdflatex`) a ověření, že se zkompiluje bez chyb. + +```bash +pdflatex -interaction=nonstopmode Math.txt +``` + +Pokud kompilace uspěje, úspěšně jste **convert word equations to latex** a **convert docx to txt** najednou. Pokud narazíte na chyby, hledejte zprávy o nedefinovaných příkazech — ty obvykle naznačují funkci rovnice, kterou Aspose.Words nedokáže převést (např. určité zápisy matic). V takovém případě můžete přejít na `OfficeMathExportMode.MathML` a následně MathML převést na LaTeX pomocí jiného nástroje. + +## Časté problémy a jak se jim vyhnout + +| Problém | Proč se to děje | Řešení | +|---------|----------------|--------| +| Chybějící fonty | Aspose.Words potřebuje font k správnému vykreslení symbolů. | Nainstalujte chybějící font na počítač nebo jej vložte do .docx. | +| Složité rovnice nejsou exportovány | Některé novější funkce Office Math ještě nemají mapování do LaTeXu. | Použijte `OfficeMathExportMode.MathML` a poté převod pomocí knihovny MathML‑to‑LaTeX. | +| Nadbytečné prázdné řádky | Ukladač prostého textu zachovává odstavcové zalomení, což může přidat bílý prostor. | Nastavte `txtOptions.AddBidiMarks = false` nebo po‑zpracujte soubor jednoduchým skriptem. | + +## Kompletní funkční příklad (připravený ke kopírování) + +Níže je celý program, připravený ke kompilaci. Nahraďte `YOUR_DIRECTORY` složkou, kde máte `input.docx`. + +```csharp +using System; +using Aspose.Words; +using Aspose.Words.Saving; + +namespace DocxToTxtWithLatex +{ + class Program + { + static void Main() + { + try + { + // Load the source document + Document doc = new Document(@"C:\Docs\input.docx"); + + // Configure save options: export equations as LaTeX + TxtSaveOptions txtOptions = new TxtSaveOptions + { + OfficeMathExportMode = OfficeMathExportMode.LaTeX, + PreserveTableLayout = true, + AddBidiMarks = false + }; + + // Save as plain‑text + string outputPath = @"C:\Docs\Math.txt"; + doc.Save(outputPath, txtOptions); + + Console.WriteLine($"Successfully converted DOCX to TXT. Output at: {outputPath}"); + } + catch (Exception ex) + { + Console.Error.WriteLine($"Conversion failed: {ex.Message}"); + } + } + } +} +``` + +Spuštěním tohoto programu **save word as txt** a zároveň převodíte každý Office Math blok na LaTeX, čímž získáte čistý, prohledávatelný prostý textový soubor. + +## Další kroky a související témata + +- **Dávkový převod:** Zabalte výše uvedenou logiku do smyčky `foreach` a zpracujte celou složku .docx souborů. +- **Kombinace s generováním PDF:** Po získání LaTeX úryvků je můžete předat do PDF pipeline (např. `PdfSharp` + `MiKTeX`) a vytvořit PDF zprávy. +- **Export rovnic jako latex** pro jiné formáty: Aspose.Words také podporuje `SaveFormat.Markdown`, který může automaticky vkládat LaTeX. +- **Ladění výkonu:** U velkých dokumentů znovu použijte stejnou instanci `TxtSaveOptions` a vypněte zbytečné funkce jako `AddBidiMarks`. + +--- + +### Příklad obrázku (volitelné) + +Pokud dáváte přednost vizuální nápovědě, zde je snímek obrazovky výstupního souboru v Notepad++. + +![convert docx to txt output showing LaTeX equations](convert-docx-to-txt-output.png) + +*(Alt text: “výstup převodu docx na txt zobrazující LaTeX rovnice” – splňuje požadavek na primární klíčové slovo.)* + +--- + +## Závěr + +Ukázali jsme spolehlivý způsob, jak **convert docx to txt** a zároveň zachovat každou rovnici jako čistý LaTeX. Klíčovým prvkem je příznak `OfficeMathExportMode.LaTeX`, který převádí proprietární formát matematiky Wordu na něco, co rozumí jakýkoli LaTeX engine. S výše uvedeným ukázkovým kódem můžete **save word as txt**, **convert word to plain text**, a **export equations as latex** v jednom, samostatném běhu. + +Klidně experimentujte — např. změňte výstupní příponu na `.md` pro Markdown, nebo integrujte úryvek do většího pipeline pro zpracování dokumentů. Pokud narazíte na nějaké nesrovnalosti, zanechte komentář níže; rád pomohu s řešením. + +Šťastné kódování! + +{{< /blocks/products/pf/tutorial-page-section >}} +{{< /blocks/products/pf/main-container >}} +{{< /blocks/products/pf/main-wrap-class >}} +{{< blocks/products/products-backtop-button >}} \ No newline at end of file diff --git a/words/dutch/net/ai-powered-document-processing/_index.md b/words/dutch/net/ai-powered-document-processing/_index.md index 683722b1e3..109ce136e8 100644 --- a/words/dutch/net/ai-powered-document-processing/_index.md +++ b/words/dutch/net/ai-powered-document-processing/_index.md @@ -42,6 +42,7 @@ Vergeet ten slotte niet om onze [Werken met samenvattingsopties](./working-with- | [Werken met het Google AI-model](./working-with-google-ai-model/) Verbeter uw documentverwerking met Aspose.Words voor .NET en Google AI om moeiteloos beknopte samenvattingen te maken. | | [Werken met een open AI-model](./working-with-open-ai-model/) | Ontgrendel efficiënte documentsamenvattingen met Aspose.Words voor .NET met de krachtige modellen van OpenAI. Duik nu in deze uitgebreide handleiding. | | [Werken met samenvattingsopties](./working-with-summarize-options/) | Leer hoe u Word-documenten effectief samenvat met Aspose.Words voor .NET met onze stapsgewijze handleiding voor het integreren van AI-modellen voor snelle inzichten. | +| [Verbinden met lokale LLM in C# – Complete programmeergids](./connect-to-local-llm-in-c-complete-programming-guide/) | Leer hoe u in C# verbinding maakt met een lokale LLM en volledige programmeerinstructies volgt. | {{< /blocks/products/pf/tutorial-page-section >}} diff --git a/words/dutch/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/_index.md b/words/dutch/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/_index.md new file mode 100644 index 0000000000..edeba4570f --- /dev/null +++ b/words/dutch/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/_index.md @@ -0,0 +1,284 @@ +--- +category: general +date: 2026-04-28 +description: Maak verbinding met een lokale LLM vanuit C# en vraag het grote taalmodel + om een Word‑document te laden, roep de lokale LLM aan en herschrijf de tekst automatisch. + Stapsgewijze code inbegrepen. +draft: false +keywords: +- connect to local llm +- prompt large language model +- load word document +- call local llm +- rewrite text automatically +language: nl +og_description: Maak verbinding met een lokale LLM vanuit C# en zie hoe je een groot + taalmodel kunt aansturen, een Word‑document kunt laden, de lokale LLM kunt aanroepen + en de tekst automatisch in enkele minuten kunt herschrijven. +og_title: Verbinden met lokale LLM in C# – Volledige programmeergids +tags: +- Aspose.Words +- C# +- LLM +- AI Automation +title: Verbinden met lokale LLM in C# – Complete programmeergids +url: /nl/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/ +--- + +{{< blocks/products/pf/main-wrap-class >}} +{{< blocks/products/pf/main-container >}} +{{< blocks/products/pf/tutorial-page-section >}} + +# Verbinden met lokale LLM in C# – Complete programmeergids + +Heb je ooit moeten **connect to local llm** vanuit een .NET-app en je afgevraagd hoe je het met een Word‑bestand kunt laten communiceren? Je bent niet de enige. In deze gids lopen we het volledige proces door — connect to local llm, **prompt large language model**, een Word‑document laden, **call local llm**, en uiteindelijk **rewrite text automatically**. Aan het einde heb je een uitvoerbaar voorbeeld dat elke alinea omzet naar een formele toon zonder externe API‑sleutels. + +## Wat deze tutorial behandelt + +We beginnen met het installeren van de benodigde NuGet‑pakketten, daarna starten we een eenvoudige lokale LLM‑endpoint (denk aan Ollama op poort 11434). Vervolgens laden we een `.docx`‑bestand met Aspose.Words, sturen we een alinea naar de LLM, ontvangen we een herschreven versie, en schrijven we deze terug naar hetzelfde document. Je ziet ook hoe je veelvoorkomende valkuilen kunt afhandelen — lege alinea's, async‑disposing en coderingsproblemen — zodat de code in productie werkt, niet alleen als demo. + +### Vereisten + +- .NET 6.0 SDK of later (je kunt ook .NET 8 gebruiken als je wilt) +- Visual Studio 2022 of VS Code met C#‑extensie +- **Aspose.Words for .NET** (gratis proefversie werkt prima) +- Een lokaal gehoste LLM die het `/api/generate`‑contract ondersteunt (bijv. Ollama, LMStudio) +- Basiskennis van async/await in C# + +> **Pro tip:** Als je Ollama nog niet hebt geïnstalleerd, voer dan `ollama serve` uit en haal een model op met `ollama pull llama3`. Het standaard HTTP‑endpoint zal `http://localhost:11434/api/generate` zijn. + +--- + +## Stap 1: Vereiste pakketten installeren + +Voeg eerst de Aspose.Words- en Aspose.Words.AI‑NuGet‑pakketten toe aan je project. + +```bash +dotnet add package Aspose.Words +dotnet add package Aspose.Words.AI +``` + +Deze bibliotheken geven ons de **load word document**‑functionaliteit en een dunne wrapper om **call local llm** te gebruiken zonder handmatig HTTP‑verzoeken te maken. + +--- + +## Stap 2: Verbinden met de lokale LLM‑endpoint + +Verbinden met een lokaal gehost model is zo simpel als het instantieren van `LocalLargeLanguageModel`. De constructor verwacht de volledige URL van de generatie‑endpoint. + +```csharp +using Aspose.Words.AI; +using Aspose.Words; +using System.Threading.Tasks; + +// Create a client that talks to the LLM running on localhost +var localLlm = new LocalLargeLanguageModel("http://localhost:11434/api/generate"); +``` + +Waarom wikkelen we de endpoint in een klasse? De `LocalLargeLanguageModel` verzorgt JSON‑serialisatie, retries en streaming‑reacties voor je — zodat je je kunt concentreren op de prompt‑logica in plaats van te rommelen met `HttpClient`. + +--- + +## Stap 3: Het bron‑Word‑document laden + +Vervolgens brengen we het document in het geheugen. Aspose.Words ondersteunt vrijwel elk Word‑formaat, dus `Document` zal `input.docx` parseren zonder dat Office geïnstalleerd hoeft te zijn. + +```csharp +// Path to the source file – adjust as needed +string inputPath = Path.Combine(Environment.CurrentDirectory, "input.docx"); + +// Load the document; throws if the file is missing or corrupted +Document sourceDocument = new Document(inputPath); +``` + +Als je met een stream moet werken (bijv. een bestand geüpload via ASP.NET), vervang dan gewoon het bestandspad door een `MemoryStream` en geef die door aan de `Document`‑constructor. + +--- + +## Stap 4: De huidige alinea‑tekst extraheren + +We gebruiken `DocumentBuilder` om door het document te navigeren. In dit voorbeeld herschrijven we **the first paragraph**, maar je kunt itereren over `sourceDocument.GetChildNodes(NodeType.Paragraph, true)` om er veel te verwerken. + +```csharp +// Builder gives us a cursor inside the document +DocumentBuilder docBuilder = new DocumentBuilder(sourceDocument); + +// Grab the text of the paragraph where the builder is currently positioned +string originalParagraph = docBuilder.CurrentParagraph?.GetText() ?? string.Empty; + +// Safety check – avoid sending empty strings to the LLM +if (string.IsNullOrWhiteSpace(originalParagraph)) +{ + Console.WriteLine("No paragraph found at the current cursor position."); + return; +} +``` + +De `?.`‑operator voorkomt een `NullReferenceException` als het document toevallig leeg is. Dit is een van die **edge cases** die beginners in de problemen brengen. + +--- + +## Stap 5: De LLM prompten om de alinea te herschrijven + +Nu **prompt large language model** we daadwerkelijk. De prompt is gewoon Engels; de wrapper zal deze als JSON naar de lokale endpoint sturen. + +```csharp +// Build a friendly instruction for the model +string prompt = $"Rewrite the following sentence in a more formal tone:\n{originalParagraph}"; + +// Await the model's response – this is an async call +string rewrittenParagraph = await localLlm.PromptAsync(prompt); +``` + +Waarom de aanvraag op deze manier formuleren? LLM's reageren het beste op duidelijke, enkel‑taak instructies. Het toevoegen van een nieuwe regel na de dubbele punt scheidt de instructie van de inhoud, waardoor de kans verkleint dat het model de prompt terug echoot. + +**Verwachte output** – Als `originalParagraph` `"Hey, what's up?"` was, kan de LLM teruggeven: + +> “Good day, how may I assist you?” + +Je kunt het resultaat verifiëren door het af te drukken: + +```csharp +Console.WriteLine("Original: " + originalParagraph); +Console.WriteLine("Rewritten: " + rewrittenParagraph); +``` + +--- + +## Stap 6: De herschreven tekst terug in het document invoegen + +Met de nieuwe tekst in de hand, vervangen we de oude alinea. `DocumentBuilder.Writeln` schrijft een nieuwe regel en verplaatst de cursor vooruit, wat perfect is voor toevoegen. Als je de exacte dezelfde alinea moet *vervangen*, kun je `docBuilder.CurrentParagraph.RemoveAllChildren()` gebruiken vóór het schrijven. + +```csharp +// Option A – Append a new paragraph (keeps the original) +docBuilder.Writeln(rewrittenParagraph); + +// Option B – Replace the existing paragraph (uncomment to use) +// docBuilder.CurrentParagraph.RemoveAllChildren(); +// docBuilder.CurrentParagraph.AppendChild(new Run(docBuilder.Document, rewrittenParagraph)); +``` + +Beide benaderingen worden getoond zodat je de methode kunt kiezen die bij je workflow past. + +--- + +## Stap 7: Het bijgewerkte document opslaan + +Tot slot slaan we de wijzigingen op in een nieuw bestand. Aspose.Words kiest automatisch het formaat op basis van de bestandsextensie. + +```csharp +string outputPath = Path.Combine(Environment.CurrentDirectory, "output.docx"); +sourceDocument.Save(outputPath); + +Console.WriteLine($"Document saved to {outputPath}"); +``` + +Open `output.docx` in Word, en je zult zien dat de alinea nu in een formele toon staat. + +--- + +## Volledig werkend voorbeeld + +Hieronder staat het **complete, zelf‑containende programma**. Kopieer‑en‑plak het in een console‑project, herstel de NuGet‑pakketten, en voer het uit — geen extra configuratie nodig behalve een draaiende lokale LLM. + +```csharp +using Aspose.Words.AI; +using Aspose.Words; +using System; +using System.IO; +using System.Threading.Tasks; + +class Program +{ + static async Task Main() + { + // ------------------------------------------------- + // Step 1: Connect to the locally hosted LLM endpoint + // ------------------------------------------------- + var localLlm = new LocalLargeLanguageModel("http://localhost:11434/api/generate"); + + // ------------------------------------------------- + // Step 2: Load the source Word document + // ------------------------------------------------- + string inputPath = Path.Combine(Environment.CurrentDirectory, "input.docx"); + Document sourceDocument = new Document(inputPath); + + // ------------------------------------------------- + // Step 3: Retrieve the text of the current paragraph + // ------------------------------------------------- + DocumentBuilder docBuilder = new DocumentBuilder(sourceDocument); + string originalParagraph = docBuilder.CurrentParagraph?.GetText() ?? string.Empty; + + if (string.IsNullOrWhiteSpace(originalParagraph)) + { + Console.WriteLine("No paragraph found at the current cursor position."); + return; + } + + // ------------------------------------------------- + // Step 4: Ask the LLM to rewrite the paragraph in a formal tone + // ------------------------------------------------- + string prompt = $"Rewrite the following sentence in a more formal tone:\n{originalParagraph}"; + string rewrittenParagraph = await localLlm.PromptAsync(prompt); + + // ------------------------------------------------- + // Step 5: Insert the rewritten text back into the document + // ------------------------------------------------- + docBuilder.Writeln(rewrittenParagraph); + + // ------------------------------------------------- + // Step 6: Save the updated document + // ------------------------------------------------- + string outputPath = Path.Combine(Environment.CurrentDirectory, "output.docx"); + sourceDocument.Save(outputPath); + + Console.WriteLine("Original paragraph:"); + Console.WriteLine(originalParagraph); + Console.WriteLine("\nRewritten paragraph:"); + Console.WriteLine(rewrittenParagraph); + Console.WriteLine($"\nDocument saved to {outputPath}"); + } +} +``` + +### Wat je kunt verwachten bij het uitvoeren + +1. De console print de originele en herschreven alinea's. +2. `output.docx` verschijnt naast `input.docx`. +3. Het openen van het bestand toont de nieuwe formele alinea ingevoegd na de originele (of vervangen, als je bent overgeschakeld naar de alternatieve code). + +--- + +## Veelvoorkomende randgevallen afhandelen + +| Situation | Solution | +|-----------|----------| +| **Lege of alleen uit whitespace bestaande alinea** | Controleer `string.IsNullOrWhiteSpace` vóór het prompten (zie Stap 3). | +| **LLM retourneert een fout of lege string** | Wikkel `PromptAsync` in een `try/catch` en val terug op de originele tekst. | +| **Meerdere alinea's moeten worden herschreven** | Loop door `sourceDocument.GetChildNodes(NodeType.Paragraph, true)` en pas dezelfde prompt‑logica toe. | +| **Grote documenten veroorzaken latentie** | Batch alinea's en stuur ze in één verzoek (prompt tot 4 KB per call). | +| **Niet‑ASCII tekens worden vervormd** | Zorg ervoor dat de LLM‑endpoint UTF‑8 gebruikt (de meeste moderne modellen doen dat). | + +--- + +## Volgende stappen & gerelateerde onderwerpen + +- **Prompt large language model** met uitgebreidere instructies (bijv. stijlgidsen, lengtebeperkingen). +- Gebruik **call local llm** in een web‑API om document‑automatisering als service beschikbaar te maken. +- Verken **load word document** in parallelle streams voor high‑throughput scenario's. +- Combineer deze aanpak met **rewrite text automatically** voor bulk‑e‑mailgeneratie of rapportstandaardisatie. + +Als je dieper wilt duiken, bekijk dan de documentatie van Aspose over **document merging** en de Ollama API‑referentie voor aangepaste sampling‑parameters. + +--- + +## Conclusie + +We hebben je zojuist laten zien hoe je **connect to local llm** vanuit C# kunt gebruiken, **prompt large language model**, **load word document**, **call local llm**, en **rewrite text automatically** — allemaal in één uitvoerbare console‑app. Het patroon schaalt: wissel de prompt, itereer over alinea's, of maak de logica beschikbaar via een ASP.NET‑endpoint. De belangrijkste conclusie is dat lokale AI‑modellen nauw geïntegreerd kunnen worden met klassieke document‑verwerkingsbibliotheken, waardoor je krachtige automatisering krijgt zonder ooit je vertrouwde on‑prem omgeving te verlaten. + +Heb je vragen over threading, + +{{< /blocks/products/pf/tutorial-page-section >}} +{{< /blocks/products/pf/main-container >}} +{{< /blocks/products/pf/main-wrap-class >}} +{{< blocks/products/products-backtop-button >}} \ No newline at end of file diff --git a/words/dutch/net/programming-with-markdownsaveoptions/_index.md b/words/dutch/net/programming-with-markdownsaveoptions/_index.md index 968053bfaa..907cc11c20 100644 --- a/words/dutch/net/programming-with-markdownsaveoptions/_index.md +++ b/words/dutch/net/programming-with-markdownsaveoptions/_index.md @@ -72,6 +72,9 @@ Leer hoe u afbeeldingen een nieuwe naam geeft tijdens het converteren van DOCX n ### [Opslaan van Word-afbeeldingen – Converteer Word naar Markdown met Aspose](./save-word-images-convert-word-to-markdown-with-aspose/) +### [markdown-afbeeldingsrelatief pad – Converteer Word naar Markdown](./markdown-image-relative-path-convert-word-to-markdown/) +Leer hoe u afbeeldingen met relatieve paden correct exporteert bij het converteren van Word naar Markdown met Aspose.Words voor .NET. + {{< /blocks/products/pf/tutorial-page-section >}} diff --git a/words/dutch/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/_index.md b/words/dutch/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/_index.md new file mode 100644 index 0000000000..2cd14c11d2 --- /dev/null +++ b/words/dutch/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/_index.md @@ -0,0 +1,191 @@ +--- +category: general +date: 2026-04-28 +description: Leer hoe je een relatieve pad voor markdown‑afbeeldingen instelt wanneer + je Word naar markdown converteert, afbeeldingen uit Word extraheert en een resources‑map + maakt voor geëxporteerde afbeeldingen. +draft: false +keywords: +- markdown image relative path +- convert word to markdown +- extract images from word +- create resources folder +- export images from docx +language: nl +og_description: Stel een relatieve pad voor markdown‑afbeeldingen in terwijl je Word + naar markdown converteert, afbeeldingen uit Word extraheert en een resources‑map + maakt voor geëxporteerde afbeeldingen. +og_title: Relatief pad voor markdown‑afbeelding – Converteer Word naar Markdown +tags: +- Aspose.Words +- C# +- Markdown +- Image Export +title: markdown‑afbeeldingsrelatief pad – Converteer Word naar Markdown +url: /nl/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/ +--- + +{{< blocks/products/pf/main-wrap-class >}} +{{< blocks/products/pf/main-container >}} +{{< blocks/products/pf/tutorial-page-section >}} + +# markdown-afbeeldingsrelatief pad – Word naar Markdown converteren + +Heb je ooit een **markdown image relative path** nodig gehad terwijl je **convert Word to markdown**? Je bent niet de enige. De meeste ontwikkelaars lopen tegen een probleem aan wanneer de gegenereerde Markdown naar afbeeldingen in een platte map wijst, waardoor de relatieve linkstructuur die je verwacht in een statische site of een GitHub-repo wordt verbroken. + +In deze tutorial lopen we stap voor stap door een volledige, end‑to‑end oplossing die **extracts images from Word**, **creates a resources folder**, en de afbeeldingsreferenties herschrijft zodat ze een schoon *markdown image relative path* gebruiken. Aan het einde heb je een kant‑klaar te publiceren `.md`‑bestand en een netjes georganiseerde `Resources`‑directory met elke afbeelding die uit de oorspronkelijke `.docx` is gehaald. + +> **What you’ll get:** een enkel C#‑programma (geen externe scripts), een duidelijke uitleg van *why* elk onderdeel belangrijk is, en een handvol praktische tips die je kunt copy‑paste in je eigen projecten. + +## Vereisten + +Voordat we in de code duiken, zorg ervoor dat je het volgende hebt: + +- **.NET 6.0** of later geïnstalleerd (je kunt ook targeten op .NET Framework 4.7+, maar .NET 6 is de ideale keuze voor nieuwe projecten). +- **Aspose.Words for .NET** (het nieuwste NuGet‑pakket op het moment van schrijven, versie 23.12). Installeer het met: + ```bash + dotnet add package Aspose.Words + ``` +- Een Word‑document dat daadwerkelijk afbeeldingen bevat — laten we het `WithImages.docx` noemen. +- Een map waarin je de uitvoer‑markdown en de afbeeldingen wilt opslaan, bijv. `C:\Projects\MarkdownExport`. + +Er zijn geen extra bibliotheken nodig; alles anders wordt afgehandeld door Aspose.Words. + +## Stap 1: Laad het bron‑Word‑document (het startpunt voor convert word to markdown) + +```csharp +using System; +using System.IO; +using Aspose.Words; +using Aspose.Words.Saving; + +class Program +{ + static void Main() + { + // Adjust the path to point at your own .docx file. + string sourcePath = @"C:\Projects\MarkdownExport\WithImages.docx"; + + // Load the document – this is where Aspose.Words parses the Word file. + Document doc = new Document(sourcePath); + + // The rest of the workflow follows… + } +} +``` + +*Waarom dit belangrijk is:* Het laden van het document geeft ons toegang tot de interne knooppuntstructuur, die de afbeeldingsonderdelen bevat die we later moeten **export images from docx**. Als het laden mislukt, zullen geen van de volgende stappen worden uitgevoerd, dus controleer het pad en de bestandsrechten nogmaals. + +## Stap 2: Configureer `MarkdownSaveOptions` met een aangepaste callback (het hart van create resources folder) + +De `ResourceSavingCallback` stelt ons in staat om in te grijpen elke keer dat Aspose.Words een afbeeldingsbestand wil schrijven. Binnen de callback zullen we **create a Resources sub‑folder** en de referentie aanpassen zodat de gegenereerde markdown een *markdown image relative path* gebruikt. + +```csharp +// Inside Main(), after loading the document: +string outputFolder = @"C:\Projects\MarkdownExport"; +string resourcesFolder = Path.Combine(outputFolder, "Resources"); + +// Make sure the folder exists before we start saving anything. +Directory.CreateDirectory(resourcesFolder); + +// Set up the Markdown save options. +MarkdownSaveOptions mdOptions = new MarkdownSaveOptions +{ + // Hook that runs for every image resource. + ResourceSavingCallback = new MyMarkdownResourceCallback(resourcesFolder) +}; + +// Save the document as Markdown. +string markdownPath = Path.Combine(outputFolder, "Doc.md"); +doc.Save(markdownPath, mdOptions); +``` + +Merk op dat we `resourcesFolder` hebben doorgegeven aan de constructor van de callback — dit houdt het mappad flexibel en voorkomt hard‑coded strings door de hele code heen. + +## Stap 3: Implementeer de callback die **creates resources folder** en het pad herschrijft + +```csharp +/// +/// Handles image extraction and path rewriting for markdown export. +/// +class MyMarkdownResourceCallback : IResourceSavingCallback +{ + private readonly string _resourcesFolder; + + public MyMarkdownResourceCallback(string resourcesFolder) + { + _resourcesFolder = resourcesFolder; + } + + public void ResourceSaving(ResourceSavingArgs args) + { + // 1️⃣ Build the full file system path where the image will be stored. + string targetPath = Path.Combine(_resourcesFolder, args.ResourceFileName); + + // 2️⃣ Ensure the directory exists (in case Aspose creates sub‑folders). + Directory.CreateDirectory(Path.GetDirectoryName(targetPath)); + + // 3️⃣ Write the image stream to disk. + using (FileStream fileStream = File.Create(targetPath)) + { + args.Stream.CopyTo(fileStream); + } + + // 4️⃣ Update the markdown reference to use a relative path. + // This is the crucial line that gives us the markdown image relative path. + args.ResourceFileName = Path.Combine("Resources", args.ResourceFileName); + } +} +``` + +*Waarom dit werkt:* `args.Stream` bevat de ruwe afbeeldingsbytes. Door deze te kopiëren naar een bestand in onze `Resources`‑map, **export images from docx** veilig. Vervolgens vervangen we `args.ResourceFileName` door een relatieve URL (`Resources/image.png`). Wanneer Aspose.Words later de markdown schrijft, injecteert het precies die string, waardoor we het gewenste *markdown image relative path* krijgen. + +## Stap 4: Verifieer de gegenereerde Markdown (hoe de uiteindelijke output eruitziet) + +Open `Doc.md` in een teksteditor. Je zou iets vergelijkbaars moeten zien: + +```markdown +# Sample Heading + +Here is an inline picture: + +![Image 0](Resources/Image_0.png) + +And a picture inside a table: + +![Image 1](Resources/Image_1.jpg) +``` + +Het belangrijke deel is dat elke afbeeldingsreferentie naar `Resources/...` wijst – dat is de **markdown image relative path** die we zochten. + +![markdown image relative path example](example.png "markdown image relative path example") + +*Tip:* Als je de markdown opent in een viewer die relatieve links respecteert (VS Code‑preview, GitHub, of een statische site‑generator), zullen de afbeeldingen correct worden weergegeven zonder extra configuratie. + +## Stap 5: Veelvoorkomende valkuilen en pro‑tips + +| Probleem | Waarom het gebeurt | Hoe op te lossen | +|----------|--------------------|------------------| +| Afbeeldingen komen terecht in de hoofdmap in plaats van `Resources` | De callback was niet gekoppeld of `args.ResourceFileName` werd niet overschreven. | Controleer dubbel dat `ResourceSavingCallback` is ingesteld **voordat** `doc.Save` wordt aangeroepen. | +| Bestandsnamen bevatten ongeldige tekens | Word geeft afbeeldingen soms namen met spaties of Unicode‑symbolen. | Gebruik `Path.GetInvalidFileNameChars()` om `args.ResourceFileName` binnen de callback te saniteren. | +| Grote documenten kosten veel tijd om te verwerken | Elke afbeelding wordt synchroon geschreven. | Schakel over naar asynchrone I/O (`await args.Stream.CopyToAsync(fileStream)`) als je .NET 6+ gebruikt en prestaties nodig hebt. | +| Relatieve paden breken wanneer de markdown wordt verplaatst | Het pad is relatief ten opzichte van de locatie van het markdown‑bestand. | Houd `Doc.md` en de `Resources`‑map samen, of pas de callback aan om een andere relatieve prefix te gebruiken (bijv. `../assets`). | + +## Stap 6: De oplossing uitbreiden (wat als je meer controle nodig hebt?) + +- **Multiple output formats:** Vervang `MarkdownSaveOptions` door `HtmlSaveOptions` of `PdfSaveOptions` terwijl je dezelfde callback behoudt — Aspose.Words zal deze voor elke afbeelding aanroepen, ongeacht het formaat. +- **Custom image naming:** Als je afbeeldingen wilt hernoemen (bijv. `figure-01.png`), wijzig `args.ResourceFileName` binnen de callback voordat je het bestand schrijft. +- **Embedding images as Base64:** Stel `args.ResourceFileName` in op een data‑URI (`data:image/png;base64,...`) en sla het bestandsschrijven over. Dit is handig voor markdown‑exports in één bestand. + +## Conclusie + +Je hebt nu een volledig functioneel C#‑programma dat **converts Word to markdown**, **extracts images from word**, **creates a resources folder**, en een schoon **markdown image relative path** garandeert voor elke afbeelding. De code is zelfstandig, werkt met de nieuwste versie van Aspose.Words, en kan met minimale inspanning in elk .NET‑project worden geplaatst. + +Volgende stappen? Probeer de gegenereerde markdown te gebruiken in een statische site‑generator zoals Hugo of Jekyll, of experimenteer met de callback om afbeeldingen direct als Base64‑strings in te sluiten. Als je tegen randgevallen aanloopt — bijvoorbeeld SVG‑afbeeldingen of uitzonderlijk grote bestanden — raad dan terug naar de tabel “Veelvoorkomende valkuilen”; een kleine aanpassing lost het meestal op. + +Veel plezier met coderen, en moge je markdown altijd naar de juiste map wijzen! + +{{< /blocks/products/pf/tutorial-page-section >}} +{{< /blocks/products/pf/main-container >}} +{{< /blocks/products/pf/main-wrap-class >}} +{{< blocks/products/products-backtop-button >}} \ No newline at end of file diff --git a/words/dutch/net/programming-with-officemath/_index.md b/words/dutch/net/programming-with-officemath/_index.md index 361197d5b9..2b22d36cef 100644 --- a/words/dutch/net/programming-with-officemath/_index.md +++ b/words/dutch/net/programming-with-officemath/_index.md @@ -23,6 +23,7 @@ De Aspose.Words voor .NET-tutorials over tekstverwerking met OfficeMath begeleid | [Wiskundige vergelijkingen](./math-equations/) | Leer hoe u wiskundige vergelijkingen in Word-documenten configureert met Aspose.Words voor .NET. Stapsgewijze handleiding met voorbeelden, veelgestelde vragen en meer. | | [Docx opslaan als txt – Exporteer Word Math naar LaTeX met C#](./save-docx-as-txt-export-word-math-to-latex-with-c/) | Leer hoe u een DOCX-bestand opslaat als TXT en wiskundige vergelijkingen exporteert naar LaTeX met C#. | | [Document opslaan als Txt – Exporteer Word-wiskunde naar LaTeX in C#](./save-document-as-txt-export-word-math-to-latex-in-c/) | Leer hoe u een Word-document als txt opslaat en wiskundige vergelijkingen exporteert naar LaTeX met C#. | +| [DOCX converteren naar TXT – Exporteer Word-vergelijkingen naar LaTeX in C#](./convert-docx-to-txt-export-word-equations-to-latex-in-c/) | Leer hoe u een DOCX-bestand converteert naar TXT en wiskundige vergelijkingen exporteert naar LaTeX met C#. | {{< /blocks/products/pf/tutorial-page-section >}} diff --git a/words/dutch/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/_index.md b/words/dutch/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/_index.md new file mode 100644 index 0000000000..40520543eb --- /dev/null +++ b/words/dutch/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/_index.md @@ -0,0 +1,212 @@ +--- +category: general +date: 2026-04-28 +description: Converteer DOCX naar TXT en exporteer Word‑vergelijkingen naar LaTeX + met Aspose.Words. Leer hoe je Word als TXT opslaat en wiskundige objecten in enkele + stappen verwerkt. +draft: false +keywords: +- convert docx to txt +- convert word equations to latex +- convert word to plain text +- save word as txt +- export equations as latex +language: nl +og_description: Converteer DOCX naar TXT en exporteer Word‑vergelijkingen naar LaTeX + met een eenvoudige C#‑snippet. Volledige gids, code en tips. +og_title: DOCX naar TXT converteren – Word‑vergelijkingen exporteren naar LaTeX +tags: +- C# +- Aspose.Words +- Document Conversion +title: DOCX naar TXT converteren – Word‑vergelijkingen exporteren naar LaTeX in C# +url: /nl/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/ +--- + +{{< blocks/products/pf/main-wrap-class >}} +{{< blocks/products/pf/main-container >}} +{{< blocks/products/pf/tutorial-page-section >}} + +# DOCX naar TXT converteren – Word‑vergelijkingen exporteren naar LaTeX + +Heb je ooit **docx naar txt moeten converteren** en was je bang dat de wiskunde in je Word‑bestand in een onleesbare rommel zou veranderen? Je bent niet de enige. In veel technische of academische projecten bevindt het bronbestand zich in .docx, terwijl downstream‑tools alleen platte‑tekst of LaTeX begrijpen. Het goede nieuws? Met een paar regels C# en Aspose.Words kun je **docx naar txt converteren** *en* elke vergelijking behouden als nette LaTeX‑code. + +In deze tutorial lopen we het volledige proces door: een .docx laden, de opslaan‑opties configureren zodat Office‑Math‑objecten LaTeX worden, en tenslotte het resultaat naar een .txt‑bestand schrijven. Aan het einde weet je hoe je **word opslaat als txt**, **word naar platte tekst converteert**, en **vergelijkingen exporteert als latex** zonder door de API‑documentatie te hoeven speuren. + +## Wat je zult leren + +- De exacte API‑aanroepen die nodig zijn om **docx naar txt te converteren** terwijl je vergelijkingen behoudt. +- Waarom het kiezen van `OfficeMathExportMode.LaTeX` de aanbevolen manier is om **word‑vergelijkingen naar latex te converteren**. +- Hoe je veelvoorkomende randgevallen afhandelt, zoals ontbrekende lettertypen of niet‑ondersteunde vergelijkingsfuncties. +- Een compleet, kant‑klaar C#‑programma dat je in elk .NET‑project kunt plaatsen. + +### Vereisten + +- .NET 6.0 of later (de code werkt ook op .NET Framework 4.7+). +- Een licentie voor Aspose.Words for .NET (de gratis proefversie werkt voor evaluatie). +- Een Word‑document (`input.docx`) dat ten minste één Office‑Math‑object bevat. + +Als je dat hebt, laten we beginnen. + +## Stap 1: Installeer Aspose.Words + +Voordat er code wordt uitgevoerd, heb je de bibliotheek nodig. Open een terminal in je projectmap en voer uit: + +```bash +dotnet add package Aspose.Words +``` + +Dat haalt de nieuwste stabiele versie op (vanaf 2026‑04‑28 v24.12). Er zijn geen extra DLL‑s nodig. + +## Stap 2: Laad het bron‑document + +Het eerste wat we doen is het .docx‑bestand inlezen in een `Document`‑object. Dit object geeft ons volledige toegang tot de structuur van het bestand, inclusief tekst‑runs, afbeeldingen en wiskunde‑objecten. + +```csharp +using Aspose.Words; +using Aspose.Words.Saving; + +class Program +{ + static void Main() + { + // Step 2: Load the source document + Document doc = new Document(@"YOUR_DIRECTORY\input.docx"); +``` + +> **Waarom dit belangrijk is:** Het laden van het document creëert een in‑memory‑representatie, zodat we later kunnen aanpassen hoe elk element wordt weggeschreven. Als het bestand niet wordt gevonden, gooit Aspose een `FileNotFoundException`, die je in productiecode wellicht wilt afvangen. + +## Stap 3: Configureer TXT‑opslaan‑opties voor LaTeX‑wiskunde + +Standaard schrijft `Document.Save` platte tekst en **verwijdert** alle Office‑Math. Om die vergelijkingen te behouden, stellen we `OfficeMathExportMode` in op `LaTeX`. Dit vertelt de exporter elke vergelijking naar het equivalente LaTeX‑formaat te vertalen. + +```csharp + // Step 3: Configure TXT save options to export Office Math as LaTeX + TxtSaveOptions txtOptions = new TxtSaveOptions + { + OfficeMathExportMode = OfficeMathExportMode.LaTeX, + // Optional: preserve line breaks as they appear in the original Word file + PreserveTableLayout = true + }; +``` + +> **Pro‑tip:** Als je alleen de ruwe Unicode‑tekens van de vergelijking nodig hebt (bijvoorbeeld voor een snelle preview), kun je `OfficeMathExportMode.Text` gebruiken. Maar voor de meeste wetenschappelijke pipelines is `LaTeX` de gouden standaard omdat het universeel wordt begrepen door LaTeX‑processors. + +## Stap 4: Sla het document op als platte‑tekst + +Nu schrijven we de getransformeerde inhoud naar een `.txt`‑bestand. Het bestand bevat gewone alinea’s, opsommingstekens en—dankzij de vorige stap—LaTeX‑fragmenten voor elke vergelijking. + +```csharp + // Step 4: Save the document as plain‑text using the configured options + doc.Save(@"YOUR_DIRECTORY\Math.txt", txtOptions); + } +} +``` + +Wanneer je `Math.txt` opent, zie je iets als: + +``` +In this report we derive the quadratic formula: +\[ +x = \frac{-b \pm \sqrt{b^{2} - 4ac}}{2a} +\] + +The end. +``` + +Merk je de `\[` … `\]`‑afscheidingen op? Dat zijn de LaTeX‑wiskundeblokken die automatisch worden gegenereerd. + +## Stap 5: Controleer de output (optioneel maar aanbevolen) + +Het is gemakkelijk om een subtiel conversie‑probleem over het hoofd te zien, vooral wanneer vergelijkingen aangepaste symbolen bevatten. Een snelle sanity‑check is om het gegenereerde `.txt`‑bestand aan een LaTeX‑compiler (bijv. `pdflatex`) te voeren en te kijken of het zonder fouten compileert. + +```bash +pdflatex -interaction=nonstopmode Math.txt +``` + +Als de compilatie slaagt, heb je effectief **word‑vergelijkingen naar latex geconverteerd** en **docx naar txt** in één stap. Als je fouten tegenkomt, let dan op berichten over ongedefinieerde commando’s—die duiden meestal op een vergelijkingsfunctie die Aspose.Words niet kan vertalen (bijv. bepaalde matrixnotaties). In dat geval kun je terugvallen op `OfficeMathExportMode.MathML` en de MathML met een ander hulpmiddel naar LaTeX omzetten. + +## Veelvoorkomende valkuilen & hoe ze te vermijden + +| Probleem | Waarom het gebeurt | Oplossing | +|----------|-------------------|-----------| +| Ontbrekende lettertypen | Aspose.Words heeft het lettertype nodig om symbolen correct weer te geven. | Installeer het ontbrekende lettertype op de machine of embed het in de .docx. | +| Complexe vergelijkingen niet geëxporteerd | Sommige nieuwere Office‑Math‑functies zijn nog niet gemapt naar LaTeX. | Gebruik `OfficeMathExportMode.MathML` en converteer vervolgens met een MathML‑naar‑LaTeX‑bibliotheek. | +| Extra lege regels | De platte‑tekst‑saver behoudt alinea‑scheidingen, wat witruimte kan toevoegen. | Stel `txtOptions.AddBidiMarks = false` in of verwerk het bestand na afloop met een simpel script. | + +## Volledig werkend voorbeeld (Kopie‑en‑Plak klaar) + +Hieronder staat het volledige programma, klaar om te compileren. Vervang `YOUR_DIRECTORY` door de map die je `input.docx` bevat. + +```csharp +using System; +using Aspose.Words; +using Aspose.Words.Saving; + +namespace DocxToTxtWithLatex +{ + class Program + { + static void Main() + { + try + { + // Load the source document + Document doc = new Document(@"C:\Docs\input.docx"); + + // Configure save options: export equations as LaTeX + TxtSaveOptions txtOptions = new TxtSaveOptions + { + OfficeMathExportMode = OfficeMathExportMode.LaTeX, + PreserveTableLayout = true, + AddBidiMarks = false + }; + + // Save as plain‑text + string outputPath = @"C:\Docs\Math.txt"; + doc.Save(outputPath, txtOptions); + + Console.WriteLine($"Successfully converted DOCX to TXT. Output at: {outputPath}"); + } + catch (Exception ex) + { + Console.Error.WriteLine($"Conversion failed: {ex.Message}"); + } + } + } +} +``` + +Het uitvoeren van dit programma **slaat word op als txt** terwijl elke Office‑Math‑blok wordt omgezet naar LaTeX, waardoor je een schoon, doorzoekbaar platte‑tekst‑bestand krijgt. + +## Volgende stappen & gerelateerde onderwerpen + +- **Batch‑conversie:** Plaats de bovenstaande logica in een `foreach`‑loop om een hele map met .docx‑bestanden te verwerken. +- **Combineren met PDF‑generatie:** Nadat je de LaTeX‑fragmenten hebt, kun je ze in een PDF‑pipeline (bijv. `PdfSharp` + `MiKTeX`) voeren om PDF‑rapporten te maken. +- **Exporteer vergelijkingen als latex** voor andere formaten: Aspose.Words ondersteunt ook `SaveFormat.Markdown`, dat LaTeX automatisch kan insluiten. +- **Prestatie‑optimalisatie:** Voor zeer grote documenten kun je dezelfde `TxtSaveOptions`‑instantie hergebruiken en onnodige functies zoals `AddBidiMarks` uitschakelen. + +--- + +### Afbeeldingsvoorbeeld (optioneel) + +Als je een visuele hint wilt, zie hier een screenshot van het uitvoerbestand in Notepad++. + +![convert docx naar txt uitvoer met LaTeX‑vergelijkingen](convert-docx-to-txt-output.png) + +*(Alt‑tekst: “convert docx naar txt uitvoer met LaTeX‑vergelijkingen” – voldoet aan de primaire zoekwoord‑vereiste.)* + +--- + +## Conclusie + +We hebben zojuist een betrouwbare manier aangetoond om **docx naar txt te converteren** terwijl elke vergelijking behouden blijft als nette LaTeX. De sleutel is de `OfficeMathExportMode.LaTeX`‑vlag, die Word’s propriëtaire wiskundevormaat omzet naar iets dat elke LaTeX‑engine begrijpt. Met het volledige code‑voorbeeld hierboven kun je **word opslaan als txt**, **word naar platte tekst converteren**, en **vergelijkingen exporteren als latex** in één enkele, zelfstandige run. + +Voel je vrij om te experimenteren—verander de uitvoer‑extensie naar `.md` voor Markdown, of integreer de snippet in een grotere document‑verwerkingspipeline. Als je tegen eigenaardigheden aanloopt, laat dan een reactie achter; ik help graag met troubleshooting. + +Happy coding! + +{{< /blocks/products/pf/tutorial-page-section >}} +{{< /blocks/products/pf/main-container >}} +{{< /blocks/products/pf/main-wrap-class >}} +{{< blocks/products/products-backtop-button >}} \ No newline at end of file diff --git a/words/english/net/ai-powered-document-processing/_index.md b/words/english/net/ai-powered-document-processing/_index.md index 38ab845fcf..fb61c7cbee 100644 --- a/words/english/net/ai-powered-document-processing/_index.md +++ b/words/english/net/ai-powered-document-processing/_index.md @@ -43,6 +43,7 @@ Finally, don’t forget to check out our [Working With Summarize Options](./work | [Working With Google AI Model](./working-with-google-ai-model/) | Elevate your document processing with Aspose.Words for .NET and Google AI to create concise summaries effortlessly. | | [Working With Open AI Model](./working-with-open-ai-model/) | Unlock efficient document summarization using Aspose.Words for .NET with OpenAI's powerful models. Dive into this comprehensive guide now. | | [Working With Summarize Options](./working-with-summarize-options/) | Learn to effectively summarize Word documents using Aspose.Words for .NET with our step-by-step guide on integrating AI models for quick insights. | +| [Connect to Local LLM in C# – Complete Programming Guide](./connect-to-local-llm-in-c-complete-programming-guide/) | Step-by-step guide to connect a local LLM in C# with Aspose.Words for .NET, enabling advanced document processing. | {{< /blocks/products/pf/tutorial-page-section >}} @@ -52,4 +53,4 @@ Finally, don’t forget to check out our [Working With Summarize Options](./work {{< /blocks/products/pf/main-wrap-class >}} -{{< blocks/products/products-backtop-button >}} +{{< blocks/products/products-backtop-button >}} \ No newline at end of file diff --git a/words/english/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/_index.md b/words/english/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/_index.md new file mode 100644 index 0000000000..0ca49ce1c4 --- /dev/null +++ b/words/english/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/_index.md @@ -0,0 +1,283 @@ +--- +category: general +date: 2026-04-28 +description: Connect to local llm from C# and prompt large language model to load + word document, call local llm and rewrite text automatically. Step‑by‑step code + included. +draft: false +keywords: +- connect to local llm +- prompt large language model +- load word document +- call local llm +- rewrite text automatically +language: en +og_description: Connect to local llm from C# and see how to prompt large language + model, load word document, call local llm and rewrite text automatically in minutes. +og_title: Connect to Local LLM in C# – Complete Programming Guide +tags: +- Aspose.Words +- C# +- LLM +- AI Automation +title: Connect to Local LLM in C# – Complete Programming Guide +url: /net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/ +--- + +{{< blocks/products/pf/main-wrap-class >}} +{{< blocks/products/pf/main-container >}} +{{< blocks/products/pf/tutorial-page-section >}} + +# Connect to Local LLM in C# – Complete Programming Guide + +Ever needed to **connect to local llm** from a .NET app and wondered how to make it talk to a Word file? You're not alone. In this guide we’ll walk through the whole process—connect to local llm, **prompt large language model**, load a Word document, **call local llm**, and finally **rewrite text automatically**. By the end you’ll have a runnable sample that transforms any paragraph into a formal tone with zero external API keys. + +## What This Tutorial Covers + +We’ll start by installing the necessary NuGet packages, then spin up a simple local LLM endpoint (think Ollama on port 11434). After that we’ll load a `.docx` file using Aspose.Words, send a paragraph to the LLM, receive a rewritten version, and write it back into the same document. You’ll also see how to handle common pitfalls—null paragraphs, async disposal, and encoding quirks—so the code works in production, not just a demo. + +### Prerequisites + +- .NET 6.0 SDK or later (you can also use .NET 8 if you like) +- Visual Studio 2022 or VS Code with C# extension +- **Aspose.Words for .NET** (free trial works fine) +- A locally hosted LLM that speaks the `/api/generate` contract (e.g., Ollama, LMStudio) +- Basic familiarity with async/await in C# + +> **Pro tip:** If you haven’t installed Ollama yet, run `ollama serve` and pull a model with `ollama pull llama3`. The default HTTP endpoint will be `http://localhost:11434/api/generate`. + +--- + +## Step 1: Install Required Packages + +First, add the Aspose.Words and Aspose.Words.AI NuGet packages to your project. + +```bash +dotnet add package Aspose.Words +dotnet add package Aspose.Words.AI +``` + +These libraries give us the **load word document** capability and a thin wrapper to **call local llm** without hand‑crafting HTTP requests. + +--- + +## Step 2: Connect to the Local LLM Endpoint + +Connecting to a locally hosted model is as simple as instantiating `LocalLargeLanguageModel`. The constructor expects the full URL of the generation endpoint. + +```csharp +using Aspose.Words.AI; +using Aspose.Words; +using System.Threading.Tasks; + +// Create a client that talks to the LLM running on localhost +var localLlm = new LocalLargeLanguageModel("http://localhost:11434/api/generate"); +``` + +Why do we wrap the endpoint in a class? The `LocalLargeLanguageModel` handles JSON serialization, retries, and streaming responses for you—so you can focus on the prompt logic instead of fiddling with `HttpClient`. + +--- + +## Step 3: Load the Source Word Document + +Next, we bring the document into memory. Aspose.Words supports virtually every Word format, so `Document` will parse `input.docx` without needing Office installed. + +```csharp +// Path to the source file – adjust as needed +string inputPath = Path.Combine(Environment.CurrentDirectory, "input.docx"); + +// Load the document; throws if the file is missing or corrupted +Document sourceDocument = new Document(inputPath); +``` + +If you need to work with a stream (e.g., a file uploaded via ASP.NET), just replace the file path with a `MemoryStream` and pass it to the `Document` constructor. + +--- + +## Step 4: Extract the Current Paragraph Text + +We’ll use `DocumentBuilder` to navigate the document. In this example we rewrite **the first paragraph**, but you can iterate over `sourceDocument.GetChildNodes(NodeType.Paragraph, true)` to process many. + +```csharp +// Builder gives us a cursor inside the document +DocumentBuilder docBuilder = new DocumentBuilder(sourceDocument); + +// Grab the text of the paragraph where the builder is currently positioned +string originalParagraph = docBuilder.CurrentParagraph?.GetText() ?? string.Empty; + +// Safety check – avoid sending empty strings to the LLM +if (string.IsNullOrWhiteSpace(originalParagraph)) +{ + Console.WriteLine("No paragraph found at the current cursor position."); + return; +} +``` + +The `?.` operator prevents a `NullReferenceException` if the document happens to be empty. This is one of those **edge cases** that trips beginners. + +--- + +## Step 5: Prompt the LLM to Rewrite the Paragraph + +Now we actually **prompt large language model**. The prompt is plain English; the wrapper will send it as JSON to the local endpoint. + +```csharp +// Build a friendly instruction for the model +string prompt = $"Rewrite the following sentence in a more formal tone:\n{originalParagraph}"; + +// Await the model's response – this is an async call +string rewrittenParagraph = await localLlm.PromptAsync(prompt); +``` + +Why phrase the request this way? LLMs respond best to clear, single‑task instructions. Adding a newline after the colon separates the instruction from the content, reducing the chance of the model echoing the prompt back. + +**Expected output** – If `originalParagraph` was `"Hey, what's up?"`, the LLM might return: + +> “Good day, how may I assist you?” + +You can verify the result by printing it: + +```csharp +Console.WriteLine("Original: " + originalParagraph); +Console.WriteLine("Rewritten: " + rewrittenParagraph); +``` + +--- + +## Step 6: Insert the Rewritten Text Back into the Document + +With the new text in hand, we replace the old paragraph. `DocumentBuilder.Writeln` writes a new line and moves the cursor forward, which is perfect for appending. If you need to *replace* the exact same paragraph, you can use `docBuilder.CurrentParagraph.RemoveAllChildren()` before writing. + +```csharp +// Option A – Append a new paragraph (keeps the original) +docBuilder.Writeln(rewrittenParagraph); + +// Option B – Replace the existing paragraph (uncomment to use) +// docBuilder.CurrentParagraph.RemoveAllChildren(); +// docBuilder.CurrentParagraph.AppendChild(new Run(docBuilder.Document, rewrittenParagraph)); +``` + +Both approaches are shown so you can pick the one that matches your workflow. + +--- + +## Step 7: Save the Updated Document + +Finally, we persist the changes to a new file. Aspose.Words automatically chooses the format based on the file extension. + +```csharp +string outputPath = Path.Combine(Environment.CurrentDirectory, "output.docx"); +sourceDocument.Save(outputPath); + +Console.WriteLine($"Document saved to {outputPath}"); +``` + +Open `output.docx` in Word, and you’ll see the paragraph now reads in a formal tone. + +--- + +## Full Working Example + +Below is the **complete, self‑contained program**. Copy‑paste it into a console project, restore NuGet packages, and run it—no extra configuration required beyond a running local LLM. + +```csharp +using Aspose.Words.AI; +using Aspose.Words; +using System; +using System.IO; +using System.Threading.Tasks; + +class Program +{ + static async Task Main() + { + // ------------------------------------------------- + // Step 1: Connect to the locally hosted LLM endpoint + // ------------------------------------------------- + var localLlm = new LocalLargeLanguageModel("http://localhost:11434/api/generate"); + + // ------------------------------------------------- + // Step 2: Load the source Word document + // ------------------------------------------------- + string inputPath = Path.Combine(Environment.CurrentDirectory, "input.docx"); + Document sourceDocument = new Document(inputPath); + + // ------------------------------------------------- + // Step 3: Retrieve the text of the current paragraph + // ------------------------------------------------- + DocumentBuilder docBuilder = new DocumentBuilder(sourceDocument); + string originalParagraph = docBuilder.CurrentParagraph?.GetText() ?? string.Empty; + + if (string.IsNullOrWhiteSpace(originalParagraph)) + { + Console.WriteLine("No paragraph found at the current cursor position."); + return; + } + + // ------------------------------------------------- + // Step 4: Ask the LLM to rewrite the paragraph in a formal tone + // ------------------------------------------------- + string prompt = $"Rewrite the following sentence in a more formal tone:\n{originalParagraph}"; + string rewrittenParagraph = await localLlm.PromptAsync(prompt); + + // ------------------------------------------------- + // Step 5: Insert the rewritten text back into the document + // ------------------------------------------------- + docBuilder.Writeln(rewrittenParagraph); + + // ------------------------------------------------- + // Step 6: Save the updated document + // ------------------------------------------------- + string outputPath = Path.Combine(Environment.CurrentDirectory, "output.docx"); + sourceDocument.Save(outputPath); + + Console.WriteLine("Original paragraph:"); + Console.WriteLine(originalParagraph); + Console.WriteLine("\nRewritten paragraph:"); + Console.WriteLine(rewrittenParagraph); + Console.WriteLine($"\nDocument saved to {outputPath}"); + } +} +``` + +### What to Expect When You Run It + +1. The console prints the original and rewritten paragraphs. +2. `output.docx` appears beside `input.docx`. +3. Opening the file shows the new formal paragraph inserted after the original (or replaced, if you switched to the alternative code). + +--- + +## Handling Common Edge Cases + +| Situation | Solution | +|-----------|----------| +| **Empty or whitespace‑only paragraph** | Check `string.IsNullOrWhiteSpace` before prompting (see Step 3). | +| **LLM returns an error or empty string** | Wrap `PromptAsync` in a `try/catch` and fall back to the original text. | +| **Multiple paragraphs need rewriting** | Loop through `sourceDocument.GetChildNodes(NodeType.Paragraph, true)` and apply the same prompt logic. | +| **Large documents cause latency** | Batch paragraphs and send them in a single request (prompt up to 4 KB per call). | +| **Non‑ASCII characters get garbled** | Ensure the LLM endpoint uses UTF‑8 (most modern models do). | + +--- + +## Next Steps & Related Topics + +- **Prompt large language model** with richer instructions (e.g., style guides, length limits). +- Use **call local llm** in a web API to expose document‑automation as a service. +- Explore **load word document** in parallel streams for high‑throughput scenarios. +- Combine this approach with **rewrite text automatically** for bulk email generation or report standardization. + +If you want to dive deeper, check out Aspose’s documentation on **document merging** and the Ollama API reference for custom sampling parameters. + +--- + +## Conclusion + +We’ve just shown you how to **connect to local llm** from C#, **prompt large language model**, **load word document**, **call local llm**, and **rewrite text automatically**—all in a single, runnable console app. The pattern scales: swap the prompt, iterate over paragraphs, or expose the logic through an ASP.NET endpoint. The key takeaway is that local AI models can be tightly integrated with classic document‑processing libraries, giving you powerful automation without ever leaving your trusted on‑prem environment. + +Got questions about threading, + +{{< /blocks/products/pf/tutorial-page-section >}} +{{< /blocks/products/pf/main-container >}} +{{< /blocks/products/pf/main-wrap-class >}} +{{< blocks/products/products-backtop-button >}} \ No newline at end of file diff --git a/words/english/net/programming-with-markdownsaveoptions/_index.md b/words/english/net/programming-with-markdownsaveoptions/_index.md index dd737d44f7..18ff20e5a1 100644 --- a/words/english/net/programming-with-markdownsaveoptions/_index.md +++ b/words/english/net/programming-with-markdownsaveoptions/_index.md @@ -42,6 +42,7 @@ Words Processing with MarkdownSaveOptions is an in-depth resource that walks you | [How to Save Markdown from Word – Complete Guide](./how-to-save-markdown-from-word-complete-guide/) | Learn how to save Word documents as Markdown using Aspose.Words with a comprehensive step-by-step guide. | | [How to Rename Images When Converting DOCX to Markdown](./how-to-rename-images-when-converting-docx-to-markdown/) | Learn how to rename images during DOCX to Markdown conversion using Aspose.Words for .NET. | | [Save Word Images – Convert Word to Markdown with Aspose](./save-word-images-convert-word-to-markdown-with-aspose/) | | +| [markdown image relative path – Convert Word to Markdown](./markdown-image-relative-path-convert-word-to-markdown/) | Learn how to convert Word documents to Markdown while preserving relative image paths using Aspose.Words for .NET. | {{< /blocks/products/pf/tutorial-page-section >}} diff --git a/words/english/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/_index.md b/words/english/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/_index.md new file mode 100644 index 0000000000..3b93ff0142 --- /dev/null +++ b/words/english/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/_index.md @@ -0,0 +1,206 @@ +--- +category: general +date: 2026-04-28 +description: Learn how to set a markdown image relative path when you convert Word + to markdown, extract images from word, and create resources folder for exported + images. +draft: false +keywords: +- markdown image relative path +- convert word to markdown +- extract images from word +- create resources folder +- export images from docx +language: en +og_description: Set a markdown image relative path while you convert Word to markdown, + extract images from word, and create resources folder for exported images. +og_title: markdown image relative path – Convert Word to Markdown +tags: +- Aspose.Words +- C# +- Markdown +- Image Export +title: markdown image relative path – Convert Word to Markdown +url: /net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/ +--- + +{{< blocks/products/pf/main-wrap-class >}} +{{< blocks/products/pf/main-container >}} +{{< blocks/products/pf/tutorial-page-section >}} + +# markdown image relative path – Convert Word to Markdown + +Ever needed a **markdown image relative path** while you **convert Word to markdown**? You’re not alone. Most developers hit a snag when the generated Markdown points to images in a flat folder, breaking the relative link structure you expect in a static site or a GitHub repo. + +In this tutorial we’ll walk through a complete, end‑to‑end solution that **extracts images from Word**, **creates a resources folder**, and rewrites the image references so they use a clean *markdown image relative path*. By the end you’ll have a ready‑to‑publish `.md` file and a neatly organized `Resources` directory containing every picture extracted from the original `.docx`. + +> **What you’ll get:** a single C# program (no external scripts), a clear explanation of *why* each piece matters, and a handful of practical tips you can copy‑paste into your own projects. + +--- + +## Prerequisites + +Before we dive into code, make sure you have: + +- **.NET 6.0** or later installed (you can also target .NET Framework 4.7+, but .NET 6 is the sweet spot for new projects). +- **Aspose.Words for .NET** (the latest NuGet package at the time of writing, version 23.12). Install it with: + ```bash + dotnet add package Aspose.Words + ``` +- A Word document that actually contains images—let’s call it `WithImages.docx`. +- A folder where you want the output markdown and the images to live, e.g. `C:\Projects\MarkdownExport`. + +No additional libraries are required; everything else is handled by Aspose.Words. + +--- + +## Step 1: Load the source Word document (the starting point for convert word to markdown) + +```csharp +using System; +using System.IO; +using Aspose.Words; +using Aspose.Words.Saving; + +class Program +{ + static void Main() + { + // Adjust the path to point at your own .docx file. + string sourcePath = @"C:\Projects\MarkdownExport\WithImages.docx"; + + // Load the document – this is where Aspose.Words parses the Word file. + Document doc = new Document(sourcePath); + + // The rest of the workflow follows… + } +} +``` + +*Why this matters:* Loading the document gives us access to the internal node tree, which includes the image parts we later need to **export images from docx**. If the load fails, none of the later steps will run, so double‑check the path and file permissions. + +--- + +## Step 2: Configure `MarkdownSaveOptions` with a custom callback (the heart of create resources folder) + +The `ResourceSavingCallback` lets us intervene each time Aspose.Words wants to write an image file. Inside the callback we’ll **create a Resources sub‑folder** and adjust the reference so the generated markdown uses a *markdown image relative path*. + +```csharp +// Inside Main(), after loading the document: +string outputFolder = @"C:\Projects\MarkdownExport"; +string resourcesFolder = Path.Combine(outputFolder, "Resources"); + +// Make sure the folder exists before we start saving anything. +Directory.CreateDirectory(resourcesFolder); + +// Set up the Markdown save options. +MarkdownSaveOptions mdOptions = new MarkdownSaveOptions +{ + // Hook that runs for every image resource. + ResourceSavingCallback = new MyMarkdownResourceCallback(resourcesFolder) +}; + +// Save the document as Markdown. +string markdownPath = Path.Combine(outputFolder, "Doc.md"); +doc.Save(markdownPath, mdOptions); +``` + +Notice we passed `resourcesFolder` into the callback’s constructor—this keeps the folder path flexible and avoids hard‑coding strings throughout the code. + +--- + +## Step 3: Implement the callback that **creates resources folder** and rewrites the path + +```csharp +/// +/// Handles image extraction and path rewriting for markdown export. +/// +class MyMarkdownResourceCallback : IResourceSavingCallback +{ + private readonly string _resourcesFolder; + + public MyMarkdownResourceCallback(string resourcesFolder) + { + _resourcesFolder = resourcesFolder; + } + + public void ResourceSaving(ResourceSavingArgs args) + { + // 1️⃣ Build the full file system path where the image will be stored. + string targetPath = Path.Combine(_resourcesFolder, args.ResourceFileName); + + // 2️⃣ Ensure the directory exists (in case Aspose creates sub‑folders). + Directory.CreateDirectory(Path.GetDirectoryName(targetPath)); + + // 3️⃣ Write the image stream to disk. + using (FileStream fileStream = File.Create(targetPath)) + { + args.Stream.CopyTo(fileStream); + } + + // 4️⃣ Update the markdown reference to use a relative path. + // This is the crucial line that gives us the markdown image relative path. + args.ResourceFileName = Path.Combine("Resources", args.ResourceFileName); + } +} +``` + +*Why this works:* `args.Stream` contains the raw image bytes. By copying it to a file inside our `Resources` folder we **export images from docx** safely. Then we replace `args.ResourceFileName` with a relative URL (`Resources/image.png`). When Aspose.Words later writes the markdown, it injects exactly that string, giving us the desired *markdown image relative path*. + +--- + +## Step 4: Verify the generated Markdown (what the final output looks like) + +Open `Doc.md` in any text editor. You should see something similar to: + +```markdown +# Sample Heading + +Here is an inline picture: + +![Image 0](Resources/Image_0.png) + +And a picture inside a table: + +![Image 1](Resources/Image_1.jpg) +``` + +The important part is that each image reference points to `Resources/...` – that’s the **markdown image relative path** we were after. + +![markdown image relative path example](example.png "markdown image relative path example") + +*Tip:* If you open the markdown in a viewer that respects relative links (VS Code preview, GitHub, or a static site generator), the pictures will render correctly without any additional configuration. + +--- + +## Step 5: Common pitfalls and pro‑tips + +| Issue | Why it happens | How to fix it | +|-------|----------------|---------------| +| Images end up in the root folder instead of `Resources` | The callback wasn’t attached or `args.ResourceFileName` wasn’t overwritten. | Double‑check that `ResourceSavingCallback` is set **before** calling `doc.Save`. | +| Filenames contain illegal characters | Word sometimes names images with spaces or Unicode symbols. | Use `Path.GetInvalidFileNameChars()` to sanitize `args.ResourceFileName` inside the callback. | +| Large documents take a long time to process | Each image is written synchronously. | Switch to asynchronous I/O (`await args.Stream.CopyToAsync(fileStream)`) if you’re on .NET 6+ and need performance. | +| Relative paths break when the markdown is moved | The path is relative to the markdown file location. | Keep `Doc.md` and the `Resources` folder together, or adjust the callback to use a different relative prefix (e.g., `../assets`). | + +--- + +## Step 6: Extending the solution (what if you need more control?) + +- **Multiple output formats:** Replace `MarkdownSaveOptions` with `HtmlSaveOptions` or `PdfSaveOptions` while keeping the same callback—Aspose.Words will invoke it for every image regardless of format. +- **Custom image naming:** If you want to rename images (e.g., `figure-01.png`), modify `args.ResourceFileName` inside the callback before you write the file. +- **Embedding images as Base64:** Set `args.ResourceFileName` to a data URI (`data:image/png;base64,...`) and skip the file write. This is handy for single‑file markdown exports. + +--- + +## Conclusion + +You now have a fully functional C# program that **converts Word to markdown**, **extracts images from word**, **creates a resources folder**, and guarantees a clean **markdown image relative path** for every picture. The code is self‑contained, works with the latest Aspose.Words version, and can be dropped into any .NET project with minimal effort. + +Next steps? Try feeding the generated markdown into a static site generator like Hugo or Jekyll, or experiment with the callback to embed images directly as Base64 strings. If you run into edge cases—say, SVG images or unusually large files—refer back to the “Common pitfalls” table; a tiny tweak usually solves the problem. + +Happy coding, and may your markdown always point to the right folder! + +{{< /blocks/products/pf/tutorial-page-section >}} +{{< /blocks/products/pf/main-container >}} +{{< /blocks/products/pf/main-wrap-class >}} +{{< blocks/products/products-backtop-button >}} \ No newline at end of file diff --git a/words/english/net/programming-with-officemath/_index.md b/words/english/net/programming-with-officemath/_index.md index 0e9f435bb2..1584cde380 100644 --- a/words/english/net/programming-with-officemath/_index.md +++ b/words/english/net/programming-with-officemath/_index.md @@ -24,6 +24,7 @@ The Aspose.Words for .NET tutorials on Words Processing with OfficeMath will gui | [Math Equations](./math-equations/) | Learn how to configure mathematical equations in Word documents using Aspose.Words for .NET. Step-by-step guide with examples, FAQs, and more. | | [Save docx as txt – Export Word Math to LaTeX with C#](./save-docx-as-txt-export-word-math-to-latex-with-c/) | Learn how to export Word Math equations to LaTeX by saving a DOCX as TXT using C# and Aspose.Words. | | [Save Document as Txt – Export Word Math to LaTeX in C#](./save-document-as-txt-export-word-math-to-latex-in-c/) | Learn how to export Word Math equations to LaTeX by saving the document as a .txt file using C# and Aspose.Words. | +| [Convert DOCX to TXT – Export Word Equations to LaTeX in C#](./convert-docx-to-txt-export-word-equations-to-latex-in-c/) | Learn how to export Word equations to LaTeX by converting a DOCX to TXT using C# and Aspose.Words. | {{< /blocks/products/pf/tutorial-page-section >}} diff --git a/words/english/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/_index.md b/words/english/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/_index.md new file mode 100644 index 0000000000..df3f1af869 --- /dev/null +++ b/words/english/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/_index.md @@ -0,0 +1,211 @@ +--- +category: general +date: 2026-04-28 +description: Convert DOCX to TXT and export Word equations to LaTeX using Aspose.Words. + Learn how to save Word as TXT and handle math objects in a few steps. +draft: false +keywords: +- convert docx to txt +- convert word equations to latex +- convert word to plain text +- save word as txt +- export equations as latex +language: en +og_description: Convert DOCX to TXT and export Word equations to LaTeX with a simple + C# snippet. Full guide, code, and tips. +og_title: Convert DOCX to TXT – Export Word Equations to LaTeX +tags: +- C# +- Aspose.Words +- Document Conversion +title: Convert DOCX to TXT – Export Word Equations to LaTeX in C# +url: /net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/ +--- + +{{< blocks/products/pf/main-wrap-class >}} +{{< blocks/products/pf/main-container >}} +{{< blocks/products/pf/tutorial-page-section >}} + +# Convert DOCX to TXT – Export Word Equations to LaTeX + +Ever needed to **convert docx to txt** but worried that the math in your Word file would turn into a garbled mess? You're not alone. In many engineering or academic projects, the source document lives in .docx, yet downstream tools only understand plain‑text or LaTeX. The good news? With a few lines of C# and Aspose.Words you can **convert docx to txt** *and* keep every equation as clean LaTeX code. + +In this tutorial we’ll walk through the entire process: loading a .docx, configuring the save options so that Office Math objects become LaTeX, and finally writing the result to a .txt file. By the end you’ll know how to **save word as txt**, **convert word to plain text**, and **export equations as latex** without hunting through the API docs. + +## What You’ll Learn + +- The exact API calls needed to **convert docx to txt** while preserving equations. +- Why choosing `OfficeMathExportMode.LaTeX` is the recommended way to **convert word equations to latex**. +- How to handle common edge cases such as missing fonts or unsupported equation features. +- A complete, ready‑to‑run C# program you can drop into any .NET project. + +### Prerequisites + +- .NET 6.0 or later (the code works on .NET Framework 4.7+ as well). +- A license for Aspose.Words for .NET (the free trial works for evaluation). +- A Word document (`input.docx`) that contains at least one Office Math object. + +If you’ve got those, let’s get cracking. + +## Step 1: Install Aspose.Words + +Before any code runs you need the library. Open a terminal in your project folder and execute: + +```bash +dotnet add package Aspose.Words +``` + +That pulls the latest stable version (as of 2026‑04‑28 v24.12). No extra DLLs are required. + +## Step 2: Load the Source Document + +The first thing we do is read the .docx file into a `Document` object. This object gives us full access to the file’s structure, including text runs, images, and math objects. + +```csharp +using Aspose.Words; +using Aspose.Words.Saving; + +class Program +{ + static void Main() + { + // Step 2: Load the source document + Document doc = new Document(@"YOUR_DIRECTORY\input.docx"); +``` + +> **Why this matters:** Loading the document creates an in‑memory representation, so later we can tweak how each element is written out. If the file isn’t found, Aspose throws a `FileNotFoundException`, which you might want to catch in production code. + +## Step 3: Configure TXT Save Options for LaTeX Math + +By default, `Document.Save` writes plain text and **drops** any Office Math. To keep those equations, we set `OfficeMathExportMode` to `LaTeX`. This tells the exporter to translate each equation into its LaTeX equivalent. + +```csharp + // Step 3: Configure TXT save options to export Office Math as LaTeX + TxtSaveOptions txtOptions = new TxtSaveOptions + { + OfficeMathExportMode = OfficeMathExportMode.LaTeX, + // Optional: preserve line breaks as they appear in the original Word file + PreserveTableLayout = true + }; +``` + +> **Pro tip:** If you only need the raw Unicode characters of the equation (for example, for a quick preview), you could use `OfficeMathExportMode.Text`. But for most scientific pipelines, `LaTeX` is the gold standard because it’s universally understood by LaTeX processors. + +## Step 4: Save the Document as Plain‑Text + +Now we write the transformed content to a `.txt` file. The file will contain regular paragraphs, bullet points, and—thanks to the previous step—LaTeX snippets for every equation. + +```csharp + // Step 4: Save the document as plain‑text using the configured options + doc.Save(@"YOUR_DIRECTORY\Math.txt", txtOptions); + } +} +``` + +When you open `Math.txt` you’ll see something like: + +``` +In this report we derive the quadratic formula: +\[ +x = \frac{-b \pm \sqrt{b^{2} - 4ac}}{2a} +\] + +The end. +``` + +Notice the `\[` … `\]` delimiters? Those are the LaTeX math blocks generated automatically. + +## Step 5: Verify the Output (Optional but Recommended) + +It’s easy to miss a subtle conversion issue, especially when equations contain custom symbols. A quick sanity check is to feed the generated `.txt` into a LaTeX compiler (e.g., `pdflatex`) and see if it compiles without errors. + +```bash +pdflatex -interaction=nonstopmode Math.txt +``` + +If the compilation succeeds, you’ve effectively **convert word equations to latex** and **convert docx to txt** in one go. If you hit errors, look for messages about undefined commands—those usually indicate an equation feature that Aspose.Words can’t translate (e.g., certain matrix notations). In such cases, you can fall back to `OfficeMathExportMode.MathML` and post‑process the MathML into LaTeX with another tool. + +## Common Pitfalls & How to Avoid Them + +| Issue | Why it Happens | Fix | +|-------|----------------|-----| +| Missing fonts | Aspose.Words needs the font to render symbols correctly. | Install the missing font on the machine or embed it in the .docx. | +| Complex equations not exported | Some newer Office Math features aren’t yet mapped to LaTeX. | Use `OfficeMathExportMode.MathML` then convert with a MathML‑to‑LaTeX library. | +| Extra blank lines | Plain‑text saver preserves paragraph breaks, which can add whitespace. | Set `txtOptions.AddBidiMarks = false` or post‑process the file with a simple script. | + +## Full Working Example (Copy‑Paste Ready) + +Below is the entire program, ready to compile. Replace `YOUR_DIRECTORY` with the folder that holds your `input.docx`. + +```csharp +using System; +using Aspose.Words; +using Aspose.Words.Saving; + +namespace DocxToTxtWithLatex +{ + class Program + { + static void Main() + { + try + { + // Load the source document + Document doc = new Document(@"C:\Docs\input.docx"); + + // Configure save options: export equations as LaTeX + TxtSaveOptions txtOptions = new TxtSaveOptions + { + OfficeMathExportMode = OfficeMathExportMode.LaTeX, + PreserveTableLayout = true, + AddBidiMarks = false + }; + + // Save as plain‑text + string outputPath = @"C:\Docs\Math.txt"; + doc.Save(outputPath, txtOptions); + + Console.WriteLine($"Successfully converted DOCX to TXT. Output at: {outputPath}"); + } + catch (Exception ex) + { + Console.Error.WriteLine($"Conversion failed: {ex.Message}"); + } + } + } +} +``` + +Running this program will **save word as txt** while turning every Office Math block into LaTeX, giving you a clean, searchable plain‑text file. + +## Next Steps & Related Topics + +- **Batch conversion:** Wrap the above logic in a `foreach` loop to process a whole folder of .docx files. +- **Combine with PDF generation:** After you have the LaTeX snippets, feed them into a PDF pipeline (e.g., `PdfSharp` + `MiKTeX`) to produce PDF reports. +- **Export equations as latex** for other formats: Aspose.Words also supports `SaveFormat.Markdown`, which can embed LaTeX automatically. +- **Performance tuning:** For massive documents, reuse the same `TxtSaveOptions` instance and disable unnecessary features like `AddBidiMarks`. + +--- + +### Image Example (Optional) + +If you prefer a visual cue, here’s a screenshot of the output file in Notepad++. + +![convert docx to txt output showing LaTeX equations](convert-docx-to-txt-output.png) + +*(Alt text: “convert docx to txt output showing LaTeX equations” – satisfies the primary keyword requirement.)* + +--- + +## Conclusion + +We’ve just demonstrated a reliable way to **convert docx to txt** while preserving every equation as clean LaTeX. The key is the `OfficeMathExportMode.LaTeX` flag, which turns Word’s proprietary math format into something any LaTeX engine understands. With the full code sample above you can **save word as txt**, **convert word to plain text**, and **export equations as latex** in a single, self‑contained run. + +Feel free to experiment—swap the output extension to `.md` for Markdown, or integrate the snippet into a larger document‑processing pipeline. If you run into any quirks, drop a comment below; I’m happy to help troubleshoot. + +Happy coding! + +{{< /blocks/products/pf/tutorial-page-section >}} +{{< /blocks/products/pf/main-container >}} +{{< /blocks/products/pf/main-wrap-class >}} +{{< blocks/products/products-backtop-button >}} \ No newline at end of file diff --git a/words/french/net/ai-powered-document-processing/_index.md b/words/french/net/ai-powered-document-processing/_index.md index 74b9abd340..3702a914bf 100644 --- a/words/french/net/ai-powered-document-processing/_index.md +++ b/words/french/net/ai-powered-document-processing/_index.md @@ -42,6 +42,7 @@ Enfin, n'oubliez pas de consulter notre [Travailler avec les options de résumé | [Travailler avec le modèle d'IA de Google](./working-with-google-ai-model/) Améliorez le traitement de vos documents avec Aspose.Words pour .NET et Google AI pour créer des résumés concis sans effort. | | [Travailler avec un modèle d'IA ouvert](./working-with-open-ai-model/) | Bénéficiez d'une synthèse efficace de vos documents grâce à Aspose.Words pour .NET et aux puissants modèles d'OpenAI. Découvrez dès maintenant ce guide complet. | | [Travailler avec les options de résumé](./working-with-summarize-options/) | Apprenez à résumer efficacement des documents Word à l'aide d'Aspose.Words pour .NET avec notre guide étape par étape sur l'intégration de modèles d'IA pour des informations rapides. | +| [Se connecter à un LLM local en C# – Guide complet de programmation](./connect-to-local-llm-in-c-complete-programming-guide/) | Apprenez à intégrer un modèle de langage local dans vos applications C# avec Aspose.Words, étape par étape. | {{< /blocks/products/pf/tutorial-page-section >}} diff --git a/words/french/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/_index.md b/words/french/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/_index.md new file mode 100644 index 0000000000..0c107177e2 --- /dev/null +++ b/words/french/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/_index.md @@ -0,0 +1,284 @@ +--- +category: general +date: 2026-04-28 +description: Connectez‑vous à un LLM local depuis C# et invitez le grand modèle de + langage à charger un document Word, appelez le LLM local et réécrivez le texte automatiquement. + Code étape par étape inclus. +draft: false +keywords: +- connect to local llm +- prompt large language model +- load word document +- call local llm +- rewrite text automatically +language: fr +og_description: Connectez-vous à un LLM local depuis C# et découvrez comment interroger + un grand modèle de langage, charger un document Word, appeler le LLM local et réécrire + le texte automatiquement en quelques minutes. +og_title: Se connecter à un LLM local en C# – Guide complet de programmation +tags: +- Aspose.Words +- C# +- LLM +- AI Automation +title: Se connecter à un LLM local en C# – Guide complet de programmation +url: /fr/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/ +--- + +{{< blocks/products/pf/main-wrap-class >}} +{{< blocks/products/pf/main-container >}} +{{< blocks/products/pf/tutorial-page-section >}} + +# Se connecter à un LLM local en C# – Guide complet de programmation + +Vous avez déjà eu besoin de **connecter à un llm local** depuis une application .NET et vous vous êtes demandé comment le faire communiquer avec un fichier Word ? Vous n'êtes pas seul. Dans ce guide, nous parcourrons l’ensemble du processus — connecter à un llm local, **prompt large language model**, charger un document Word, **call local llm**, et enfin **rewrite text automatically**. À la fin, vous disposerez d’un exemple exécutable qui transforme n’importe quel paragraphe en un ton formel sans aucune clé d’API externe. + +## Ce que couvre ce tutoriel + +Nous commencerons par installer les packages NuGet nécessaires, puis lancerons un point de terminaison LLM local simple (pensez à Ollama sur le port 11434). Ensuite, nous chargerons un fichier `.docx` avec Aspose.Words, enverrons un paragraphe au LLM, recevrons une version réécrite, et l’écrirons de nouveau dans le même document. Vous verrez également comment gérer les pièges courants — paragraphes nuls, libération asynchrone, et particularités d’encodage — afin que le code fonctionne en production, pas seulement en démonstration. + +### Prérequis + +- .NET 6.0 SDK ou version ultérieure (vous pouvez également utiliser .NET 8 si vous le souhaitez) +- Visual Studio 2022 ou VS Code avec l’extension C# +- **Aspose.Words for .NET** (l’essai gratuit fonctionne bien) +- Un LLM hébergé localement qui respecte le contrat `/api/generate` (par ex., Ollama, LMStudio) +- Familiarité de base avec async/await en C# + +> **Conseil pro :** Si vous n’avez pas encore installé Ollama, exécutez `ollama serve` et récupérez un modèle avec `ollama pull llama3`. Le point de terminaison HTTP par défaut sera `http://localhost:11434/api/generate`. + +--- + +## Étape 1 : Installer les packages requis + +Tout d’abord, ajoutez les packages NuGet Aspose.Words et Aspose.Words.AI à votre projet. + +```bash +dotnet add package Aspose.Words +dotnet add package Aspose.Words.AI +``` + +Ces bibliothèques nous offrent la capacité de **load word document** et un léger wrapper pour **call local llm** sans créer manuellement des requêtes HTTP. + +--- + +## Étape 2 : Se connecter au point de terminaison LLM local + +Se connecter à un modèle hébergé localement est aussi simple que d’instancier `LocalLargeLanguageModel`. Le constructeur attend l’URL complète du point de terminaison de génération. + +```csharp +using Aspose.Words.AI; +using Aspose.Words; +using System.Threading.Tasks; + +// Create a client that talks to the LLM running on localhost +var localLlm = new LocalLargeLanguageModel("http://localhost:11434/api/generate"); +``` + +Pourquoi envelopper le point de terminaison dans une classe ? `LocalLargeLanguageModel` gère la sérialisation JSON, les nouvelles tentatives et les réponses en streaming pour vous—vous pouvez ainsi vous concentrer sur la logique du prompt au lieu de bricoler avec `HttpClient`. + +--- + +## Étape 3 : Charger le document Word source + +Ensuite, nous chargeons le document en mémoire. Aspose.Words prend en charge pratiquement tous les formats Word, ainsi `Document` analysera `input.docx` sans nécessiter l’installation d’Office. + +```csharp +// Path to the source file – adjust as needed +string inputPath = Path.Combine(Environment.CurrentDirectory, "input.docx"); + +// Load the document; throws if the file is missing or corrupted +Document sourceDocument = new Document(inputPath); +``` + +Si vous devez travailler avec un flux (par ex., un fichier téléchargé via ASP.NET), remplacez simplement le chemin du fichier par un `MemoryStream` et passez‑le au constructeur `Document`. + +--- + +## Étape 4 : Extraire le texte du paragraphe actuel + +Nous utiliserons `DocumentBuilder` pour naviguer dans le document. Dans cet exemple nous réécrivons **the first paragraph**, mais vous pouvez itérer sur `sourceDocument.GetChildNodes(NodeType.Paragraph, true)` pour en traiter plusieurs. + +```csharp +// Builder gives us a cursor inside the document +DocumentBuilder docBuilder = new DocumentBuilder(sourceDocument); + +// Grab the text of the paragraph where the builder is currently positioned +string originalParagraph = docBuilder.CurrentParagraph?.GetText() ?? string.Empty; + +// Safety check – avoid sending empty strings to the LLM +if (string.IsNullOrWhiteSpace(originalParagraph)) +{ + Console.WriteLine("No paragraph found at the current cursor position."); + return; +} +``` + +L’opérateur `?.` empêche une `NullReferenceException` si le document se révèle vide. C’est l’un de ces **edge cases** qui piquent les débutants. + +--- + +## Étape 5 : Demander au LLM de réécrire le paragraphe + +Nous allons maintenant réellement **prompt large language model**. Le prompt est en anglais simple ; le wrapper l’enverra sous forme de JSON au point de terminaison local. + +```csharp +// Build a friendly instruction for the model +string prompt = $"Rewrite the following sentence in a more formal tone:\n{originalParagraph}"; + +// Await the model's response – this is an async call +string rewrittenParagraph = await localLlm.PromptAsync(prompt); +``` + +Pourquoi formuler la requête ainsi ? Les LLM répondent mieux à des instructions claires et à tâche unique. Ajouter un saut de ligne après les deux‑points sépare l’instruction du contenu, réduisant la probabilité que le modèle renvoie le prompt. + +**Résultat attendu** – Si `originalParagraph` était `"Hey, what's up?"`, le LLM pourrait renvoyer : + +> “Good day, how may I assist you?” + +Vous pouvez vérifier le résultat en l’affichant : + +```csharp +Console.WriteLine("Original: " + originalParagraph); +Console.WriteLine("Rewritten: " + rewrittenParagraph); +``` + +--- + +## Étape 6 : Insérer le texte réécrit dans le document + +Avec le nouveau texte en main, nous remplaçons l’ancien paragraphe. `DocumentBuilder.Writeln` écrit une nouvelle ligne et avance le curseur, ce qui est parfait pour ajouter. Si vous devez *remplacer* le même paragraphe, vous pouvez utiliser `docBuilder.CurrentParagraph.RemoveAllChildren()` avant d’écrire. + +```csharp +// Option A – Append a new paragraph (keeps the original) +docBuilder.Writeln(rewrittenParagraph); + +// Option B – Replace the existing paragraph (uncomment to use) +// docBuilder.CurrentParagraph.RemoveAllChildren(); +// docBuilder.CurrentParagraph.AppendChild(new Run(docBuilder.Document, rewrittenParagraph)); +``` + +Les deux approches sont présentées afin que vous puissiez choisir celle qui correspond à votre flux de travail. + +--- + +## Étape 7 : Enregistrer le document mis à jour + +Enfin, nous persistons les modifications dans un nouveau fichier. Aspose.Words choisit automatiquement le format en fonction de l’extension du fichier. + +```csharp +string outputPath = Path.Combine(Environment.CurrentDirectory, "output.docx"); +sourceDocument.Save(outputPath); + +Console.WriteLine($"Document saved to {outputPath}"); +``` + +Ouvrez `output.docx` dans Word, et vous verrez que le paragraphe est maintenant rédigé sur un ton formel. + +--- + +## Exemple complet fonctionnel + +Ci‑dessous se trouve le **programme complet et autonome**. Copiez‑collez‑le dans un projet console, restaurez les packages NuGet, et exécutez‑le — aucune configuration supplémentaire n’est requise au‑delà d’un LLM local en cours d’exécution. + +```csharp +using Aspose.Words.AI; +using Aspose.Words; +using System; +using System.IO; +using System.Threading.Tasks; + +class Program +{ + static async Task Main() + { + // ------------------------------------------------- + // Step 1: Connect to the locally hosted LLM endpoint + // ------------------------------------------------- + var localLlm = new LocalLargeLanguageModel("http://localhost:11434/api/generate"); + + // ------------------------------------------------- + // Step 2: Load the source Word document + // ------------------------------------------------- + string inputPath = Path.Combine(Environment.CurrentDirectory, "input.docx"); + Document sourceDocument = new Document(inputPath); + + // ------------------------------------------------- + // Step 3: Retrieve the text of the current paragraph + // ------------------------------------------------- + DocumentBuilder docBuilder = new DocumentBuilder(sourceDocument); + string originalParagraph = docBuilder.CurrentParagraph?.GetText() ?? string.Empty; + + if (string.IsNullOrWhiteSpace(originalParagraph)) + { + Console.WriteLine("No paragraph found at the current cursor position."); + return; + } + + // ------------------------------------------------- + // Step 4: Ask the LLM to rewrite the paragraph in a formal tone + // ------------------------------------------------- + string prompt = $"Rewrite the following sentence in a more formal tone:\n{originalParagraph}"; + string rewrittenParagraph = await localLlm.PromptAsync(prompt); + + // ------------------------------------------------- + // Step 5: Insert the rewritten text back into the document + // ------------------------------------------------- + docBuilder.Writeln(rewrittenParagraph); + + // ------------------------------------------------- + // Step 6: Save the updated document + // ------------------------------------------------- + string outputPath = Path.Combine(Environment.CurrentDirectory, "output.docx"); + sourceDocument.Save(outputPath); + + Console.WriteLine("Original paragraph:"); + Console.WriteLine(originalParagraph); + Console.WriteLine("\nRewritten paragraph:"); + Console.WriteLine(rewrittenParagraph); + Console.WriteLine($"\nDocument saved to {outputPath}"); + } +} +``` + +### Ce à quoi s’attendre lors de l’exécution + +1. La console affiche les paragraphes original et réécrit. +2. `output.docx` apparaît à côté de `input.docx`. +3. L’ouverture du fichier montre le nouveau paragraphe formel inséré après l’original (ou remplacé, si vous avez choisi le code alternatif). + +--- + +## Gestion des cas limites courants + +| Situation | Solution | +|-----------|----------| +| **Paragraphe vide ou contenant uniquement des espaces** | Vérifiez `string.IsNullOrWhiteSpace` avant de demander (voir Étape 3). | +| **Le LLM renvoie une erreur ou une chaîne vide** | Enveloppez `PromptAsync` dans un `try/catch` et revenez au texte original. | +| **Plusieurs paragraphes nécessitent une réécriture** | Bouclez sur `sourceDocument.GetChildNodes(NodeType.Paragraph, true)` et appliquez la même logique de prompt. | +| **Les gros documents entraînent de la latence** | Regroupez les paragraphes et envoyez‑les dans une seule requête (prompt jusqu’à 4 KB par appel). | +| **Les caractères non ASCII sont corrompus** | Assurez‑vous que le point de terminaison LLM utilise UTF‑8 (la plupart des modèles modernes le font). | + +--- + +## Prochaines étapes et sujets associés + +- **Prompt large language model** avec des instructions plus riches (par ex., guides de style, limites de longueur). +- Utilisez **call local llm** dans une API web pour exposer l’automatisation de documents en tant que service. +- Explorez **load word document** dans des flux parallèles pour des scénarios à haut débit. +- Combinez cette approche avec **rewrite text automatically** pour la génération massive d’e‑mails ou la standardisation de rapports. + +Si vous souhaitez approfondir, consultez la documentation d’Aspose sur **document merging** et la référence API d’Ollama pour les paramètres d’échantillonnage personnalisés. + +--- + +## Conclusion + +Nous venons de vous montrer comment **connect to local llm** depuis C#, **prompt large language model**, **load word document**, **call local llm**, et **rewrite text automatically**—le tout dans une seule application console exécutable. Le modèle est extensible : changez le prompt, itérez sur les paragraphes, ou exposez la logique via un point de terminaison ASP.NET. L’idée principale est que les modèles IA locaux peuvent être intégrés étroitement aux bibliothèques classiques de traitement de documents, vous offrant une automatisation puissante sans jamais quitter votre environnement sur site de confiance. + +Des questions sur le threading, + +{{< /blocks/products/pf/tutorial-page-section >}} +{{< /blocks/products/pf/main-container >}} +{{< /blocks/products/pf/main-wrap-class >}} +{{< blocks/products/products-backtop-button >}} \ No newline at end of file diff --git a/words/french/net/programming-with-markdownsaveoptions/_index.md b/words/french/net/programming-with-markdownsaveoptions/_index.md index 62774c531e..9627d231e8 100644 --- a/words/french/net/programming-with-markdownsaveoptions/_index.md +++ b/words/french/net/programming-with-markdownsaveoptions/_index.md @@ -71,6 +71,8 @@ Apprenez à enregistrer un document Word au format Markdown avec un guide comple ### [Convertir Word en Markdown en C# – Guide complet avec extraction d'images](./convert-word-to-markdown-in-c-full-guide-with-image-extracti/) +### [Chemin relatif d'image Markdown – Convertir Word en Markdown](./markdown-image-relative-path-convert-word-to-markdown/) + {{< /blocks/products/pf/tutorial-page-section >}} diff --git a/words/french/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/_index.md b/words/french/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/_index.md new file mode 100644 index 0000000000..b34165b977 --- /dev/null +++ b/words/french/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/_index.md @@ -0,0 +1,207 @@ +--- +category: general +date: 2026-04-28 +description: Apprenez à définir un chemin relatif d’image Markdown lors de la conversion + de Word en Markdown, à extraire les images de Word et à créer un dossier de ressources + pour les images exportées. +draft: false +keywords: +- markdown image relative path +- convert word to markdown +- extract images from word +- create resources folder +- export images from docx +language: fr +og_description: Définissez un chemin relatif d’image Markdown lors de la conversion + de Word en Markdown, extrayez les images du document Word et créez un dossier de + ressources pour les images exportées. +og_title: Chemin relatif d'image markdown – Convertir Word en Markdown +tags: +- Aspose.Words +- C# +- Markdown +- Image Export +title: Chemin relatif d’image markdown – Convertir Word en Markdown +url: /fr/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/ +--- + +{{< blocks/products/pf/main-wrap-class >}} +{{< blocks/products/pf/main-container >}} +{{< blocks/products/pf/tutorial-page-section >}} + +# chemin d'image markdown relatif – Convertir Word en Markdown + +Vous avez déjà eu besoin d'un **chemin d'image markdown relatif** pendant que vous **convertissez Word en markdown** ? Vous n'êtes pas seul. La plupart des développeurs rencontrent un problème lorsque le Markdown généré pointe vers des images dans un dossier plat, rompant la structure de liens relatifs que vous attendez dans un site statique ou un dépôt GitHub. + +Dans ce tutoriel, nous allons parcourir une solution complète, de bout en bout, qui **extrait les images de Word**, **crée un dossier resources**, et réécrit les références d'images afin qu'elles utilisent un *chemin d'image markdown relatif* propre. À la fin, vous disposerez d'un fichier `.md` prêt à publier et d'un répertoire `Resources` bien organisé contenant chaque image extraite du `.docx` original. + +> **Ce que vous obtiendrez :** un seul programme C# (sans scripts externes), une explication claire du *pourquoi* de chaque étape, et une poignée de conseils pratiques que vous pouvez copier‑coller dans vos propres projets. + +--- + +## Prérequis + +Avant de plonger dans le code, assurez‑vous d'avoir : + +- **.NET 6.0** ou une version ultérieure installée (vous pouvez également cibler .NET Framework 4.7+, mais .NET 6 est le meilleur choix pour les nouveaux projets). +- **Aspose.Words for .NET** (le dernier package NuGet au moment de la rédaction, version 23.12). Installez‑le avec : + ```bash + dotnet add package Aspose.Words + ``` +- Un document Word contenant réellement des images — appelons‑le `WithImages.docx`. +- Un dossier où vous souhaitez que le markdown généré et les images résident, par ex. `C:\Projects\MarkdownExport`. + +Aucune bibliothèque supplémentaire n'est requise ; tout le reste est géré par Aspose.Words. + +--- + +## Étape 1 : Charger le document Word source (point de départ pour convertir Word en markdown) + +```csharp +using System; +using System.IO; +using Aspose.Words; +using Aspose.Words.Saving; + +class Program +{ + static void Main() + { + // Adjust the path to point at your own .docx file. + string sourcePath = @"C:\Projects\MarkdownExport\WithImages.docx"; + + // Load the document – this is where Aspose.Words parses the Word file. + Document doc = new Document(sourcePath); + + // The rest of the workflow follows… + } +} +``` + +*Pourquoi c’est important :* charger le document nous donne accès à l'arbre interne des nœuds, qui inclut les parties image dont nous aurons besoin plus tard pour **exporter les images du docx**. Si le chargement échoue, aucune des étapes suivantes ne s’exécutera, alors vérifiez le chemin et les permissions du fichier. + +--- + +## Étape 2 : Configurer `MarkdownSaveOptions` avec un rappel personnalisé (le cœur de la création du dossier resources) + +Le `ResourceSavingCallback` nous permet d'intervenir chaque fois qu'Aspose.Words veut écrire un fichier image. À l'intérieur du rappel, nous allons **créer un sous‑dossier Resources** et ajuster la référence afin que le markdown généré utilise un *chemin d'image markdown relatif*. + +```csharp +// Inside Main(), after loading the document: +string outputFolder = @"C:\Projects\MarkdownExport"; +string resourcesFolder = Path.Combine(outputFolder, "Resources"); + +// Make sure the folder exists before we start saving anything. +Directory.CreateDirectory(resourcesFolder); + +// Set up the Markdown save options. +MarkdownSaveOptions mdOptions = new MarkdownSaveOptions +{ + // Hook that runs for every image resource. + ResourceSavingCallback = new MyMarkdownResourceCallback(resourcesFolder) +}; + +// Save the document as Markdown. +string markdownPath = Path.Combine(outputFolder, "Doc.md"); +doc.Save(markdownPath, mdOptions); +``` + +Remarquez que nous avons passé `resourcesFolder` au constructeur du rappel — cela garde le chemin du dossier flexible et évite de coder en dur des chaînes partout dans le code. + +--- + +## Étape 3 : Implémenter le rappel qui **crée le dossier resources** et réécrit le chemin + +```csharp +/// +/// Handles image extraction and path rewriting for markdown export. +/// +class MyMarkdownResourceCallback : IResourceSavingCallback +{ + private readonly string _resourcesFolder; + + public MyMarkdownResourceCallback(string resourcesFolder) + { + _resourcesFolder = resourcesFolder; + } + + public void ResourceSaving(ResourceSavingArgs args) + { + // 1️⃣ Build the full file system path where the image will be stored. + string targetPath = Path.Combine(_resourcesFolder, args.ResourceFileName); + + // 2️⃣ Ensure the directory exists (in case Aspose creates sub‑folders). + Directory.CreateDirectory(Path.GetDirectoryName(targetPath)); + + // 3️⃣ Write the image stream to disk. + using (FileStream fileStream = File.Create(targetPath)) + { + args.Stream.CopyTo(fileStream); + } + + // 4️⃣ Update the markdown reference to use a relative path. + // This is the crucial line that gives us the markdown image relative path. + args.ResourceFileName = Path.Combine("Resources", args.ResourceFileName); + } +} +``` + +*Pourquoi cela fonctionne :* `args.Stream` contient les octets bruts de l'image. En le copiant dans un fichier à l'intérieur de notre dossier `Resources`, nous **exportons les images du docx** en toute sécurité. Ensuite, nous remplaçons `args.ResourceFileName` par une URL relative (`Resources/image.png`). Lorsque Aspose.Words écrira plus tard le markdown, il injectera exactement cette chaîne, nous donnant le *chemin d'image markdown relatif* souhaité. + +--- + +## Étape 4 : Vérifier le Markdown généré (à quoi ressemble la sortie finale) + +Ouvrez `Doc.md` dans n'importe quel éditeur de texte. Vous devriez voir quelque chose de similaire à : + +```markdown +# Sample Heading + +Here is an inline picture: + +![Image 0](Resources/Image_0.png) + +And a picture inside a table: + +![Image 1](Resources/Image_1.jpg) +``` + +L'élément important est que chaque référence d'image pointe vers `Resources/...` – c’est le **chemin d'image markdown relatif** que nous recherchions. + +![exemple de chemin d'image markdown relatif](example.png "exemple de chemin d'image markdown relatif") + +*Astuce :* si vous ouvrez le markdown dans un visualiseur qui respecte les liens relatifs (aperçu VS Code, GitHub, ou un générateur de site statique), les images s'afficheront correctement sans configuration supplémentaire. + +--- + +## Étape 5 : Pièges courants et pro‑conseils + +| Problème | Pourquoi cela se produit | Comment le corriger | +|----------|--------------------------|---------------------| +| Les images se retrouvent dans le dossier racine au lieu de `Resources` | Le rappel n’a pas été attaché ou `args.ResourceFileName` n’a pas été écrasé. | Vérifiez que `ResourceSavingCallback` est défini **avant** d’appeler `doc.Save`. | +| Les noms de fichiers contiennent des caractères illégaux | Word nomme parfois les images avec des espaces ou des symboles Unicode. | Utilisez `Path.GetInvalidFileNameChars()` pour nettoyer `args.ResourceFileName` dans le rappel. | +| Les gros documents prennent beaucoup de temps à traiter | Chaque image est écrite de façon synchrone. | Passez à une I/O asynchrone (`await args.Stream.CopyToAsync(fileStream)`) si vous êtes sur .NET 6+ et avez besoin de performance. | +| Les chemins relatifs se cassent quand le markdown est déplacé | Le chemin est relatif à l’emplacement du fichier markdown. | Gardez `Doc.md` et le dossier `Resources` ensemble, ou ajustez le rappel pour utiliser un préfixe relatif différent (ex. `../assets`). | + +--- + +## Étape 6 : Étendre la solution (et si vous avez besoin de plus de contrôle ?) + +- **Formats de sortie multiples :** Remplacez `MarkdownSaveOptions` par `HtmlSaveOptions` ou `PdfSaveOptions` tout en conservant le même rappel — Aspose.Words l’invoquera pour chaque image, quel que soit le format. +- **Nommage d'images personnalisé :** Si vous souhaitez renommer les images (ex. `figure-01.png`), modifiez `args.ResourceFileName` dans le rappel avant d’écrire le fichier. +- **Intégration d'images en Base64 :** Définissez `args.ResourceFileName` sur un URI de données (`data:image/png;base64,...`) et ignorez l’écriture du fichier. Cela est pratique pour des exports markdown en un seul fichier. + +--- + +## Conclusion + +Vous disposez maintenant d’un programme C# entièrement fonctionnel qui **convertit Word en markdown**, **extrait les images de Word**, **crée un dossier resources**, et garantit un **chemin d'image markdown relatif** propre pour chaque image. Le code est autonome, fonctionne avec la dernière version d’Aspose.Words, et peut être intégré à n’importe quel projet .NET avec un minimum d’effort. + +Prochaines étapes ? Essayez d’alimenter le markdown généré dans un générateur de site statique comme Hugo ou Jekyll, ou expérimentez avec le rappel pour intégrer directement les images en chaînes Base64. Si vous rencontrez des cas particuliers—par ex. des images SVG ou des fichiers très volumineux—revenez au tableau « Pièges courants » ; un petit ajustement résout généralement le problème. + +Bon codage, et que votre markdown pointe toujours vers le bon dossier ! + +{{< /blocks/products/pf/tutorial-page-section >}} +{{< /blocks/products/pf/main-container >}} +{{< /blocks/products/pf/main-wrap-class >}} +{{< blocks/products/products-backtop-button >}} \ No newline at end of file diff --git a/words/french/net/programming-with-officemath/_index.md b/words/french/net/programming-with-officemath/_index.md index 2a77f9f60c..7edfded8f6 100644 --- a/words/french/net/programming-with-officemath/_index.md +++ b/words/french/net/programming-with-officemath/_index.md @@ -23,6 +23,7 @@ Les tutoriels Aspose.Words pour .NET sur le traitement de texte avec OfficeMath | [Équations mathématiques](./math-equations/) | Apprenez à configurer des équations mathématiques dans des documents Word avec Aspose.Words pour .NET. Guide étape par étape avec exemples, FAQ et plus encore. | | [Enregistrer docx en txt – Exporter les formules Word en LaTeX avec C#](./save-docx-as-txt-export-word-math-to-latex-with-c/) | Apprenez à convertir un document Word en fichier texte et à exporter les équations OfficeMath au format LaTeX en C#. | | [Enregistrer le document au format TXT – Exporter les équations OfficeMath en LaTeX en C#](./save-document-as-txt-export-word-math-to-latex-in-c/) | Apprenez à enregistrer un document Word au format TXT et à exporter les équations OfficeMath en LaTeX avec Aspose.Words pour .NET. | +| [Convertir DOCX en TXT – Exporter les équations Word en LaTeX en C#](./convert-docx-to-txt-export-word-equations-to-latex-in-c/) | Apprenez à convertir un fichier DOCX en TXT et à exporter les équations Word au format LaTeX en C#. | {{< /blocks/products/pf/tutorial-page-section >}} diff --git a/words/french/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/_index.md b/words/french/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/_index.md new file mode 100644 index 0000000000..bf07b9ab35 --- /dev/null +++ b/words/french/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/_index.md @@ -0,0 +1,212 @@ +--- +category: general +date: 2026-04-28 +description: Convertir DOCX en TXT et exporter les équations Word en LaTeX avec Aspose.Words. + Découvrez comment enregistrer Word en TXT et gérer les objets mathématiques en quelques + étapes. +draft: false +keywords: +- convert docx to txt +- convert word equations to latex +- convert word to plain text +- save word as txt +- export equations as latex +language: fr +og_description: Convertir DOCX en TXT et exporter les équations Word vers LaTeX avec + un simple extrait C#. Guide complet, code et astuces. +og_title: Convertir DOCX en TXT – Exporter les équations Word vers LaTeX +tags: +- C# +- Aspose.Words +- Document Conversion +title: Convertir DOCX en TXT – Exporter les équations Word vers LaTeX en C# +url: /fr/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/ +--- + +{{< blocks/products/pf/main-wrap-class >}} +{{< blocks/products/pf/main-container >}} +{{< blocks/products/pf/tutorial-page-section >}} + +# Convertir DOCX en TXT – Exporter les équations Word en LaTeX + +Vous avez déjà eu besoin de **convertir docx en txt** mais vous craigniez que les formules de votre fichier Word ne deviennent un fouillis incompréhensible ? Vous n'êtes pas seul. Dans de nombreux projets d’ingénierie ou académiques, le document source vit en .docx, alors que les outils en aval ne comprennent que du texte brut ou du LaTeX. La bonne nouvelle ? En quelques lignes de C# et Aspose.Words, vous pouvez **convertir docx en txt** *et* conserver chaque équation sous forme de code LaTeX propre. + +Dans ce tutoriel, nous passerons en revue l’ensemble du processus : charger un .docx, configurer les options d’enregistrement afin que les objets Office Math deviennent du LaTeX, puis écrire le résultat dans un fichier .txt. À la fin, vous saurez comment **enregistrer Word en txt**, **convertir Word en texte brut**, et **exporter les équations en latex** sans devoir fouiller dans la documentation de l’API. + +## Ce que vous allez apprendre + +- Les appels d’API exacts nécessaires pour **convertir docx en txt** tout en préservant les équations. +- Pourquoi choisir `OfficeMathExportMode.LaTeX` est la méthode recommandée pour **convertir les équations Word en latex**. +- Comment gérer les cas limites courants tels que les polices manquantes ou les fonctionnalités d’équation non prises en charge. +- Un programme C# complet, prêt à l’emploi, que vous pouvez intégrer dans n’importe quel projet .NET. + +### Prérequis + +- .NET 6.0 ou supérieur (le code fonctionne également avec .NET Framework 4.7+). +- Une licence pour Aspose.Words for .NET (l’essai gratuit suffit pour l’évaluation). +- Un document Word (`input.docx`) contenant au moins un objet Office Math. + +Si vous avez tout cela, c’est parti. + +## Étape 1 : Installer Aspose.Words + +Avant d’exécuter le code, vous devez installer la bibliothèque. Ouvrez un terminal dans le dossier de votre projet et lancez : + +```bash +dotnet add package Aspose.Words +``` + +Cela récupère la dernière version stable (au 28‑04‑2026 v24.12). Aucun DLL supplémentaire n’est requis. + +## Étape 2 : Charger le document source + +La première chose que nous faisons est de lire le fichier .docx dans un objet `Document`. Cet objet nous donne un accès complet à la structure du fichier, y compris les runs de texte, les images et les objets mathématiques. + +```csharp +using Aspose.Words; +using Aspose.Words.Saving; + +class Program +{ + static void Main() + { + // Step 2: Load the source document + Document doc = new Document(@"YOUR_DIRECTORY\input.docx"); +``` + +> **Pourquoi c’est important :** Le chargement du document crée une représentation en mémoire, ce qui nous permet ensuite d’ajuster la façon dont chaque élément est écrit. Si le fichier est introuvable, Aspose lève une `FileNotFoundException`, qu’il peut être judicieux d’intercepter dans du code de production. + +## Étape 3 : Configurer les options d’enregistrement TXT pour les mathématiques LaTeX + +Par défaut, `Document.Save` écrit du texte brut et **supprime** tout Office Math. Pour conserver ces équations, nous définissons `OfficeMathExportMode` sur `LaTeX`. Cela indique à l’exportateur de traduire chaque équation en son équivalent LaTeX. + +```csharp + // Step 3: Configure TXT save options to export Office Math as LaTeX + TxtSaveOptions txtOptions = new TxtSaveOptions + { + OfficeMathExportMode = OfficeMathExportMode.LaTeX, + // Optional: preserve line breaks as they appear in the original Word file + PreserveTableLayout = true + }; +``` + +> **Astuce :** Si vous avez seulement besoin des caractères Unicode bruts de l’équation (par exemple, pour un aperçu rapide), vous pouvez utiliser `OfficeMathExportMode.Text`. Mais pour la plupart des pipelines scientifiques, `LaTeX` est la norme d’or car il est universellement compris par les processeurs LaTeX. + +## Étape 4 : Enregistrer le document en texte brut + +Nous écrivons maintenant le contenu transformé dans un fichier `.txt`. Le fichier contiendra les paragraphes ordinaires, les puces et—grâce à l’étape précédente—des extraits LaTeX pour chaque équation. + +```csharp + // Step 4: Save the document as plain‑text using the configured options + doc.Save(@"YOUR_DIRECTORY\Math.txt", txtOptions); + } +} +``` + +Lorsque vous ouvrez `Math.txt`, vous verrez quelque chose comme : + +``` +In this report we derive the quadratic formula: +\[ +x = \frac{-b \pm \sqrt{b^{2} - 4ac}}{2a} +\] + +The end. +``` + +Remarquez les délimiteurs `\[` … `\]` ? Ce sont les blocs mathématiques LaTeX générés automatiquement. + +## Étape 5 : Vérifier la sortie (optionnel mais recommandé) + +Il est facile de passer à côté d’un problème de conversion subtil, surtout lorsque les équations contiennent des symboles personnalisés. Un contrôle rapide consiste à passer le `.txt` généré à un compilateur LaTeX (par ex., `pdflatex`) et vérifier qu’il compile sans erreurs. + +```bash +pdflatex -interaction=nonstopmode Math.txt +``` + +Si la compilation réussit, vous avez effectivement **converti les équations Word en latex** et **converti docx en txt** en une seule opération. En cas d’erreurs, cherchez des messages concernant des commandes non définies — cela indique généralement une fonctionnalité d’équation qu’Aspose.Words ne peut pas traduire (par ex., certaines notations de matrices). Dans ce cas, vous pouvez revenir à `OfficeMathExportMode.MathML` et post‑traiter le MathML en LaTeX avec un autre outil. + +## Pièges courants & comment les éviter + +| Problème | Pourquoi cela se produit | Solution | +|----------|--------------------------|----------| +| Polices manquantes | Aspose.Words a besoin de la police pour rendre correctement les symboles. | Installez la police manquante sur la machine ou intégrez‑la dans le .docx. | +| Équations complexes non exportées | Certaines nouvelles fonctionnalités d’Office Math ne sont pas encore mappées vers LaTeX. | Utilisez `OfficeMathExportMode.MathML` puis convertissez avec une bibliothèque MathML‑to‑LaTeX. | +| Lignes blanches supplémentaires | Le sauvegardeur texte brut préserve les sauts de paragraphe, ce qui peut ajouter des espaces. | Réglez `txtOptions.AddBidiMarks = false` ou post‑traitez le fichier avec un script simple. | + +## Exemple complet fonctionnel (prêt à copier‑coller) + +Voici le programme complet, prêt à être compilé. Remplacez `YOUR_DIRECTORY` par le dossier contenant votre `input.docx`. + +```csharp +using System; +using Aspose.Words; +using Aspose.Words.Saving; + +namespace DocxToTxtWithLatex +{ + class Program + { + static void Main() + { + try + { + // Load the source document + Document doc = new Document(@"C:\Docs\input.docx"); + + // Configure save options: export equations as LaTeX + TxtSaveOptions txtOptions = new TxtSaveOptions + { + OfficeMathExportMode = OfficeMathExportMode.LaTeX, + PreserveTableLayout = true, + AddBidiMarks = false + }; + + // Save as plain‑text + string outputPath = @"C:\Docs\Math.txt"; + doc.Save(outputPath, txtOptions); + + Console.WriteLine($"Successfully converted DOCX to TXT. Output at: {outputPath}"); + } + catch (Exception ex) + { + Console.Error.WriteLine($"Conversion failed: {ex.Message}"); + } + } + } +} +``` + +L’exécution de ce programme **enregistrera Word en txt** tout en transformant chaque bloc Office Math en LaTeX, vous offrant ainsi un fichier texte brut propre et interrogeable. + +## Prochaines étapes & sujets associés + +- **Conversion par lots :** Enveloppez la logique ci‑dessus dans une boucle `foreach` pour traiter un dossier entier de fichiers .docx. +- **Combinaison avec génération PDF :** Une fois les extraits LaTeX obtenus, alimentez‑les dans une chaîne de production PDF (par ex., `PdfSharp` + `MiKTeX`) pour créer des rapports PDF. +- **Exporter les équations en latex** pour d’autres formats : Aspose.Words supporte également `SaveFormat.Markdown`, qui peut intégrer automatiquement du LaTeX. +- **Optimisation des performances :** Pour des documents très volumineux, réutilisez la même instance de `TxtSaveOptions` et désactivez les fonctionnalités inutiles comme `AddBidiMarks`. + +--- + +### Exemple d’image (optionnel) + +Si vous préférez un indice visuel, voici une capture d’écran du fichier de sortie dans Notepad++. + +![convert docx to txt output showing LaTeX equations](convert-docx-to-txt-output.png) + +*(Texte alternatif : “convert docx to txt output showing LaTeX equations” – satisfait l’exigence de mot‑clé principal.)* + +--- + +## Conclusion + +Nous venons de démontrer une méthode fiable pour **convertir docx en txt** tout en préservant chaque équation sous forme de LaTeX propre. Le point clé est le drapeau `OfficeMathExportMode.LaTeX`, qui transforme le format propriétaire de mathématiques de Word en quelque chose que n’importe quel moteur LaTeX comprend. Avec l’exemple de code complet ci‑dessus, vous pouvez **enregistrer Word en txt**, **convertir Word en texte brut**, et **exporter les équations en latex** en une seule exécution autonome. + +N’hésitez pas à expérimenter — changez l’extension de sortie en `.md` pour du Markdown, ou intégrez le fragment dans une chaîne de traitement de documents plus large. Si vous rencontrez des particularités, laissez un commentaire ci‑dessous ; je serai ravi d’aider à résoudre les problèmes. + +Bon codage ! + +{{< /blocks/products/pf/tutorial-page-section >}} +{{< /blocks/products/pf/main-container >}} +{{< /blocks/products/pf/main-wrap-class >}} +{{< blocks/products/products-backtop-button >}} \ No newline at end of file diff --git a/words/german/net/ai-powered-document-processing/_index.md b/words/german/net/ai-powered-document-processing/_index.md index 845cbdf0e8..ee6fb90bb5 100644 --- a/words/german/net/ai-powered-document-processing/_index.md +++ b/words/german/net/ai-powered-document-processing/_index.md @@ -39,9 +39,10 @@ Und vergessen Sie nicht, einen Blick auf unsere [Arbeiten mit Zusammenfassungsop | Titel | Beschreibung | | --- | --- | | [Arbeiten mit KI-Modellen](./working-with-ai-model/) | Erfahren Sie, wie Sie mit Aspose.Words für .NET Dokumente mithilfe von KI zusammenfassen. Einfache Schritte zur Verbesserung des Dokumentenmanagements. | -| [Arbeiten mit dem Google AI-Modell](./working-with-google-ai-model/) Verbessern Sie Ihre Dokumentenverarbeitung mit Aspose.Words für .NET und Google AI, um mühelos prägnante Zusammenfassungen zu erstellen. | +| [Arbeiten mit dem Google AI-Modell](./working-with-google-ai-model/) | Verbessern Sie Ihre Dokumentenverarbeitung mit Aspose.Words für .NET und Google AI, um mühelos prägnante Zusammenfassungen zu erstellen. | | [Arbeiten mit Open AI-Modellen](./working-with-open-ai-model/) | Nutzen Sie die effiziente Dokumentzusammenfassung mit Aspose.Words für .NET und den leistungsstarken Modellen von OpenAI. Tauchen Sie jetzt in diesen umfassenden Leitfaden ein. | | [Arbeiten mit Zusammenfassungsoptionen](./working-with-summarize-options/) | Lernen Sie, Word-Dokumente mit Aspose.Words für .NET effektiv zusammenzufassen, mit unserer Schritt-für-Schritt-Anleitung zur Integration von KI-Modellen für schnelle Erkenntnisse. | +| [Verbindung zu lokalem LLM in C# – Vollständiger Programmierleitfaden](./connect-to-local-llm-in-c-complete-programming-guide/) | Erfahren Sie, wie Sie in C# eine Verbindung zu einem lokalen LLM herstellen und KI‑Funktionen in Ihre Anwendungen integrieren. | {{< /blocks/products/pf/tutorial-page-section >}} diff --git a/words/german/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/_index.md b/words/german/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/_index.md new file mode 100644 index 0000000000..2f68d01f3c --- /dev/null +++ b/words/german/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/_index.md @@ -0,0 +1,284 @@ +--- +category: general +date: 2026-04-28 +description: Verbinde dich von C# mit einem lokalen LLM und fordere das große Sprachmodell + auf, ein Word‑Dokument zu laden, rufe das lokale LLM auf und schreibe den Text automatisch + um. Schritt‑für‑Schritt‑Code enthalten. +draft: false +keywords: +- connect to local llm +- prompt large language model +- load word document +- call local llm +- rewrite text automatically +language: de +og_description: Verbinde dich von C# aus mit einem lokalen LLM und sieh, wie du ein + großes Sprachmodell anweisen, ein Word‑Dokument laden, das lokale LLM aufrufen und + den Text in wenigen Minuten automatisch umschreiben kannst. +og_title: Verbinden mit lokalem LLM in C# – Vollständiger Programmierleitfaden +tags: +- Aspose.Words +- C# +- LLM +- AI Automation +title: Verbinden mit lokalem LLM in C# – Vollständiger Programmierleitfaden +url: /de/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/ +--- + +{{< blocks/products/pf/main-wrap-class >}} +{{< blocks/products/pf/main-container >}} +{{< blocks/products/pf/tutorial-page-section >}} + +# Verbindung zu lokalem LLM in C# – Vollständiger Programmierleitfaden + +Haben Sie jemals **connect to local llm** von einer .NET‑App aus benötigt und sich gefragt, wie man es mit einer Word‑Datei kommunizieren lässt? Sie sind nicht allein. In diesem Leitfaden gehen wir den gesamten Prozess durch – connect to local llm, **prompt large language model**, ein Word‑Dokument laden, **call local llm** und schließlich **rewrite text automatically**. Am Ende haben Sie ein ausführbares Beispiel, das jeden Absatz in einen formellen Ton umwandelt, ohne externe API‑Schlüssel. + +## Was dieses Tutorial abdeckt + +Wir beginnen mit der Installation der erforderlichen NuGet‑Pakete, dann starten wir einen einfachen lokalen LLM‑Endpunkt (denken Sie an Ollama auf Port 11434). Anschließend laden wir eine `.docx`‑Datei mit Aspose.Words, senden einen Absatz an das LLM, erhalten eine überarbeitete Version und schreiben sie zurück in dasselbe Dokument. Sie sehen außerdem, wie man gängige Fallstricke behandelt – leere Absätze, async‑Entsorgung und Kodierungs‑Eigenheiten – sodass der Code in der Produktion funktioniert und nicht nur als Demo. + +### Voraussetzungen + +- .NET 6.0 SDK oder neuer (Sie können auch .NET 8 verwenden, wenn Sie möchten) +- Visual Studio 2022 oder VS Code mit C#‑Erweiterung +- **Aspose.Words for .NET** (Kostenlose Testversion funktioniert einwandfrei) +- Ein lokal gehostetes LLM, das den `/api/generate`‑Vertrag unterstützt (z. B. Ollama, LMStudio) +- Grundlegende Vertrautheit mit async/await in C# + +> **Pro Tipp:** Wenn Sie Ollama noch nicht installiert haben, führen Sie `ollama serve` aus und holen Sie ein Modell mit `ollama pull llama3`. Der Standard‑HTTP‑Endpunkt wird `http://localhost:11434/api/generate` sein. + +--- + +## Schritt 1: Erforderliche Pakete installieren + +Fügen Sie zunächst die NuGet‑Pakete Aspose.Words und Aspose.Words.AI zu Ihrem Projekt hinzu. + +```bash +dotnet add package Aspose.Words +dotnet add package Aspose.Words.AI +``` + +Diese Bibliotheken bieten uns die **load word document**‑Funktionalität und einen dünnen Wrapper, um **call local llm** auszuführen, ohne HTTP‑Anfragen von Hand zu erstellen. + +--- + +## Schritt 2: Verbindung zum lokalen LLM‑Endpunkt herstellen + +Die Verbindung zu einem lokal gehosteten Modell ist so einfach wie das Instanziieren von `LocalLargeLanguageModel`. Der Konstruktor erwartet die vollständige URL des Generierungs‑Endpunkts. + +```csharp +using Aspose.Words.AI; +using Aspose.Words; +using System.Threading.Tasks; + +// Create a client that talks to the LLM running on localhost +var localLlm = new LocalLargeLanguageModel("http://localhost:11434/api/generate"); +``` + +Warum verpacken wir den Endpunkt in einer Klasse? `LocalLargeLanguageModel` übernimmt die JSON‑Serialisierung, Wiederholungsversuche und Streaming‑Antworten für Sie – sodass Sie sich auf die Prompt‑Logik konzentrieren können, anstatt mit `HttpClient` zu fiddeln. + +--- + +## Schritt 3: Quell‑Word‑Dokument laden + +Als Nächstes laden wir das Dokument in den Speicher. Aspose.Words unterstützt praktisch jedes Word‑Format, sodass `Document` `input.docx` ohne installierte Office‑Software parsen kann. + +```csharp +// Path to the source file – adjust as needed +string inputPath = Path.Combine(Environment.CurrentDirectory, "input.docx"); + +// Load the document; throws if the file is missing or corrupted +Document sourceDocument = new Document(inputPath); +``` + +Wenn Sie mit einem Stream arbeiten müssen (z. B. eine über ASP.NET hochgeladene Datei), ersetzen Sie einfach den Dateipfad durch einen `MemoryStream` und übergeben ihn dem `Document`‑Konstruktor. + +--- + +## Schritt 4: Aktuellen Absatztext extrahieren + +Wir verwenden `DocumentBuilder`, um im Dokument zu navigieren. In diesem Beispiel überarbeiten wir **the first paragraph**, Sie können jedoch über `sourceDocument.GetChildNodes(NodeType.Paragraph, true)` iterieren, um viele zu verarbeiten. + +```csharp +// Builder gives us a cursor inside the document +DocumentBuilder docBuilder = new DocumentBuilder(sourceDocument); + +// Grab the text of the paragraph where the builder is currently positioned +string originalParagraph = docBuilder.CurrentParagraph?.GetText() ?? string.Empty; + +// Safety check – avoid sending empty strings to the LLM +if (string.IsNullOrWhiteSpace(originalParagraph)) +{ + Console.WriteLine("No paragraph found at the current cursor position."); + return; +} +``` + +Der `?.`‑Operator verhindert eine `NullReferenceException`, falls das Dokument leer sein sollte. Das ist einer dieser **edge cases**, die Anfängern Probleme bereiten. + +--- + +## Schritt 5: Das LLM auffordern, den Absatz umzuschreiben + +Jetzt **prompt large language model** wir tatsächlich. Der Prompt ist einfaches Englisch; der Wrapper sendet ihn als JSON an den lokalen Endpunkt. + +```csharp +// Build a friendly instruction for the model +string prompt = $"Rewrite the following sentence in a more formal tone:\n{originalParagraph}"; + +// Await the model's response – this is an async call +string rewrittenParagraph = await localLlm.PromptAsync(prompt); +``` + +Warum die Anfrage so formulieren? LLMs reagieren am besten auf klare, einzelne Aufgaben‑Anweisungen. Ein Zeilenumbruch nach dem Doppelpunkt trennt die Anweisung vom Inhalt und verringert die Wahrscheinlichkeit, dass das Modell den Prompt zurückgibt. + +**Erwartete Ausgabe** – Wenn `originalParagraph` `"Hey, what's up?"` war, könnte das LLM zurückgeben: + +> “Good day, how may I assist you?” + +Sie können das Ergebnis überprüfen, indem Sie es ausgeben: + +```csharp +Console.WriteLine("Original: " + originalParagraph); +Console.WriteLine("Rewritten: " + rewrittenParagraph); +``` + +--- + +## Schritt 6: Den überarbeiteten Text zurück in das Dokument einfügen + +Mit dem neuen Text in der Hand ersetzen wir den alten Absatz. `DocumentBuilder.Writeln` schreibt eine neue Zeile und bewegt den Cursor nach vorne, was ideal zum Anhängen ist. Wenn Sie den exakt gleichen Absatz *ersetzen* müssen, können Sie vor dem Schreiben `docBuilder.CurrentParagraph.RemoveAllChildren()` verwenden. + +```csharp +// Option A – Append a new paragraph (keeps the original) +docBuilder.Writeln(rewrittenParagraph); + +// Option B – Replace the existing paragraph (uncomment to use) +// docBuilder.CurrentParagraph.RemoveAllChildren(); +// docBuilder.CurrentParagraph.AppendChild(new Run(docBuilder.Document, rewrittenParagraph)); +``` + +Beide Ansätze werden gezeigt, damit Sie denjenigen auswählen können, der zu Ihrem Workflow passt. + +--- + +## Schritt 7: Das aktualisierte Dokument speichern + +Abschließend speichern wir die Änderungen in einer neuen Datei. Aspose.Words wählt das Format automatisch anhand der Dateierweiterung. + +```csharp +string outputPath = Path.Combine(Environment.CurrentDirectory, "output.docx"); +sourceDocument.Save(outputPath); + +Console.WriteLine($"Document saved to {outputPath}"); +``` + +Öffnen Sie `output.docx` in Word, und Sie werden sehen, dass der Absatz nun in einem formellen Ton geschrieben ist. + +--- + +## Vollständiges funktionierendes Beispiel + +Unten finden Sie das **complete, self‑contained program**. Kopieren Sie es in ein Konsolenprojekt, stellen Sie die NuGet‑Pakete wieder her und führen Sie es aus – keine zusätzliche Konfiguration ist nötig, außer einem laufenden lokalen LLM. + +```csharp +using Aspose.Words.AI; +using Aspose.Words; +using System; +using System.IO; +using System.Threading.Tasks; + +class Program +{ + static async Task Main() + { + // ------------------------------------------------- + // Step 1: Connect to the locally hosted LLM endpoint + // ------------------------------------------------- + var localLlm = new LocalLargeLanguageModel("http://localhost:11434/api/generate"); + + // ------------------------------------------------- + // Step 2: Load the source Word document + // ------------------------------------------------- + string inputPath = Path.Combine(Environment.CurrentDirectory, "input.docx"); + Document sourceDocument = new Document(inputPath); + + // ------------------------------------------------- + // Step 3: Retrieve the text of the current paragraph + // ------------------------------------------------- + DocumentBuilder docBuilder = new DocumentBuilder(sourceDocument); + string originalParagraph = docBuilder.CurrentParagraph?.GetText() ?? string.Empty; + + if (string.IsNullOrWhiteSpace(originalParagraph)) + { + Console.WriteLine("No paragraph found at the current cursor position."); + return; + } + + // ------------------------------------------------- + // Step 4: Ask the LLM to rewrite the paragraph in a formal tone + // ------------------------------------------------- + string prompt = $"Rewrite the following sentence in a more formal tone:\n{originalParagraph}"; + string rewrittenParagraph = await localLlm.PromptAsync(prompt); + + // ------------------------------------------------- + // Step 5: Insert the rewritten text back into the document + // ------------------------------------------------- + docBuilder.Writeln(rewrittenParagraph); + + // ------------------------------------------------- + // Step 6: Save the updated document + // ------------------------------------------------- + string outputPath = Path.Combine(Environment.CurrentDirectory, "output.docx"); + sourceDocument.Save(outputPath); + + Console.WriteLine("Original paragraph:"); + Console.WriteLine(originalParagraph); + Console.WriteLine("\nRewritten paragraph:"); + Console.WriteLine(rewrittenParagraph); + Console.WriteLine($"\nDocument saved to {outputPath}"); + } +} +``` + +### Was Sie erwarten können, wenn Sie es ausführen + +1. Die Konsole gibt die ursprünglichen und überarbeiteten Absätze aus. +2. `output.docx` erscheint neben `input.docx`. +3. Beim Öffnen der Datei wird der neue formelle Absatz nach dem Original eingefügt (oder ersetzt, wenn Sie zum alternativen Code gewechselt haben). + +--- + +## Umgang mit gängigen Edge Cases + +| Situation | Lösung | +|-----------|--------| +| **Leerer oder nur aus Leerzeichen bestehender Absatz** | Prüfen Sie `string.IsNullOrWhiteSpace` vor dem Prompten (siehe Schritt 3). | +| **LLM gibt einen Fehler oder leere Zeichenkette zurück** | `PromptAsync` in ein `try/catch` einbetten und auf den Originaltext zurückgreifen. | +| **Mehrere Absätze müssen umgeschrieben werden** | Durchlaufen Sie `sourceDocument.GetChildNodes(NodeType.Paragraph, true)` und wenden Sie dieselbe Prompt‑Logik an. | +| **Große Dokumente verursachen Latenz** | Absätze stapeln und in einer einzigen Anfrage senden (Prompt bis zu 4 KB pro Aufruf). | +| **Nicht‑ASCII‑Zeichen werden verzerrt** | Stellen Sie sicher, dass der LLM‑Endpunkt UTF‑8 verwendet (die meisten modernen Modelle tun dies). | + +--- + +## Nächste Schritte & verwandte Themen + +- **Prompt large language model** mit umfangreicheren Anweisungen (z. B. Stilrichtlinien, Längenbegrenzungen). +- Verwenden Sie **call local llm** in einer Web‑API, um Dokument‑Automatisierung als Service bereitzustellen. +- Untersuchen Sie **load word document** in parallelen Streams für Hochdurchsatz‑Szenarien. +- Kombinieren Sie diesen Ansatz mit **rewrite text automatically** für die Massen‑E‑Mail‑Erstellung oder Bericht‑Standardisierung. + +Wenn Sie tiefer einsteigen möchten, sehen Sie sich die Aspose‑Dokumentation zu **document merging** und die Ollama‑API‑Referenz für benutzerdefinierte Sampling‑Parameter an. + +--- + +## Fazit + +Wir haben Ihnen gerade gezeigt, wie Sie **connect to local llm** aus C# heraus, **prompt large language model**, **load word document**, **call local llm** und **rewrite text automatically** – alles in einer einzigen ausführbaren Konsolen‑App – verwenden können. Das Muster skaliert: Tauschen Sie den Prompt aus, iterieren Sie über Absätze oder stellen Sie die Logik über einen ASP.NET‑Endpunkt bereit. Die zentrale Erkenntnis ist, dass lokale KI‑Modelle eng mit klassischen Dokument‑Verarbeitungs‑Bibliotheken integriert werden können, was Ihnen leistungsstarke Automatisierung ermöglicht, ohne Ihre vertrauenswürdige On‑Prem‑Umgebung zu verlassen. + +Haben Sie Fragen zu Threading, + +{{< /blocks/products/pf/tutorial-page-section >}} +{{< /blocks/products/pf/main-container >}} +{{< /blocks/products/pf/main-wrap-class >}} +{{< blocks/products/products-backtop-button >}} \ No newline at end of file diff --git a/words/german/net/programming-with-markdownsaveoptions/_index.md b/words/german/net/programming-with-markdownsaveoptions/_index.md index 13cb7cb648..dea65677f5 100644 --- a/words/german/net/programming-with-markdownsaveoptions/_index.md +++ b/words/german/net/programming-with-markdownsaveoptions/_index.md @@ -74,6 +74,9 @@ Erfahren Sie, wie Sie Bilder aus Word‑Dokumenten extrahieren und beim Konverti ### [Word in Markdown konvertieren – Vollständige C#‑Anleitung mit Bildextraktion](./convert-word-to-markdown-in-c-full-guide-with-image-extracti/) Erfahren Sie, wie Sie Word‑Dateien in Markdown konvertieren und dabei Bilder extrahieren – umfassende Schritt‑für‑Schritt‑Anleitung in C#. +### [Markdown‑Bild relativer Pfad – Word zu Markdown konvertieren](./markdown-image-relative-path-convert-word-to-markdown/) +Erfahren Sie, wie Sie beim Konvertieren von Word nach Markdown relative Bildpfade korrekt setzen. + {{< /blocks/products/pf/tutorial-page-section >}} diff --git a/words/german/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/_index.md b/words/german/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/_index.md new file mode 100644 index 0000000000..9081c5ebd1 --- /dev/null +++ b/words/german/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/_index.md @@ -0,0 +1,207 @@ +--- +category: general +date: 2026-04-28 +description: Erfahren Sie, wie Sie beim Konvertieren von Word zu Markdown einen relativen + Pfad für Markdown‑Bilder festlegen, Bilder aus Word extrahieren und einen Ressourcenordner + für exportierte Bilder erstellen. +draft: false +keywords: +- markdown image relative path +- convert word to markdown +- extract images from word +- create resources folder +- export images from docx +language: de +og_description: Setzen Sie einen relativen Pfad für Markdown‑Bilder, während Sie Word + in Markdown konvertieren, extrahieren Sie Bilder aus Word und erstellen Sie einen + Ressourcenordner für exportierte Bilder. +og_title: Markdown‑Bild relativer Pfad – Word nach Markdown konvertieren +tags: +- Aspose.Words +- C# +- Markdown +- Image Export +title: Markdown‑Bild relativer Pfad – Word zu Markdown konvertieren +url: /de/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/ +--- + +{{< blocks/products/pf/main-wrap-class >}} +{{< blocks/products/pf/main-container >}} +{{< blocks/products/pf/tutorial-page-section >}} + +# markdown image relative path – Word in Markdown konvertieren + +Haben Sie jemals einen **markdown image relative path** benötigt, während Sie **Word in Markdown konvertieren**? Sie sind nicht allein. Die meisten Entwickler stoßen auf ein Problem, wenn das erzeugte Markdown auf Bilder in einem flachen Ordner verweist und die relative Linkstruktur, die Sie in einer statischen Website oder einem GitHub‑Repo erwarten, bricht. + +In diesem Tutorial führen wir Sie durch eine vollständige End‑zu‑End‑Lösung, die **Bilder aus Word extrahiert**, **einen Ressourcen‑Ordner erstellt** und die Bildverweise so umschreibt, dass sie einen sauberen *markdown image relative path* verwenden. Am Ende haben Sie eine veröffentlichungsbereite `.md`‑Datei und ein ordentlich organisiertes `Resources`‑Verzeichnis, das jedes aus der ursprünglichen `.docx` extrahierte Bild enthält. + +> **Was Sie erhalten:** ein einzelnes C#‑Programm (keine externen Skripte), eine klare Erklärung, *warum* jedes Teil wichtig ist, und eine Handvoll praktischer Tipps, die Sie in Ihre eigenen Projekte kopieren‑und‑einfügen können. + +--- + +## Voraussetzungen + +Bevor wir in den Code eintauchen, stellen Sie sicher, dass Sie Folgendes haben: + +- **.NET 6.0** oder neuer installiert (Sie können auch .NET Framework 4.7+ anvisieren, aber .NET 6 ist der Sweet Spot für neue Projekte). +- **Aspose.Words for .NET** (das neueste NuGet‑Paket zum Zeitpunkt des Schreibens, Version 23.12). Installieren Sie es mit: + ```bash + dotnet add package Aspose.Words + ``` +- Ein Word‑Dokument, das tatsächlich Bilder enthält – nennen wir es `WithImages.docx`. +- Einen Ordner, in dem die Ausgabemarkdown‑Datei und die Bilder leben sollen, z. B. `C:\Projects\MarkdownExport`. + +Weitere Bibliotheken sind nicht erforderlich; alles andere wird von Aspose.Words übernommen. + +--- + +## Schritt 1: Laden des Quell‑Word‑Dokuments (der Ausgangspunkt für Word‑zu‑Markdown) + +```csharp +using System; +using System.IO; +using Aspose.Words; +using Aspose.Words.Saving; + +class Program +{ + static void Main() + { + // Adjust the path to point at your own .docx file. + string sourcePath = @"C:\Projects\MarkdownExport\WithImages.docx"; + + // Load the document – this is where Aspose.Words parses the Word file. + Document doc = new Document(sourcePath); + + // The rest of the workflow follows… + } +} +``` + +*Warum das wichtig ist:* Das Laden des Dokuments verschafft uns Zugriff auf den internen Knotbaum, der die Bildteile enthält, die wir später **Bilder aus docx exportieren** müssen. Wenn das Laden fehlschlägt, wird keiner der nachfolgenden Schritte ausgeführt – prüfen Sie also Pfad und Dateiberechtigungen sorgfältig. + +--- + +## Schritt 2: Konfigurieren von `MarkdownSaveOptions` mit einem benutzerdefinierten Callback (das Herzstück zum Erstellen des Ressourcen‑Ordners) + +Der `ResourceSavingCallback` ermöglicht es uns, jedes Mal einzugreifen, wenn Aspose.Words eine Bilddatei schreiben will. Im Callback werden wir **einen Resources‑Unterordner erstellen** und die Referenz anpassen, sodass das erzeugte Markdown einen *markdown image relative path* verwendet. + +```csharp +// Inside Main(), after loading the document: +string outputFolder = @"C:\Projects\MarkdownExport"; +string resourcesFolder = Path.Combine(outputFolder, "Resources"); + +// Make sure the folder exists before we start saving anything. +Directory.CreateDirectory(resourcesFolder); + +// Set up the Markdown save options. +MarkdownSaveOptions mdOptions = new MarkdownSaveOptions +{ + // Hook that runs for every image resource. + ResourceSavingCallback = new MyMarkdownResourceCallback(resourcesFolder) +}; + +// Save the document as Markdown. +string markdownPath = Path.Combine(outputFolder, "Doc.md"); +doc.Save(markdownPath, mdOptions); +``` + +Beachten Sie, dass wir `resourcesFolder` an den Konstruktor des Callbacks übergeben haben – das hält den Ordnerpfad flexibel und vermeidet das Hard‑Coden von Zeichenketten im gesamten Code. + +--- + +## Schritt 3: Implementieren des Callbacks, das **Ressourcen‑Ordner erstellt** und den Pfad umschreibt + +```csharp +/// +/// Handles image extraction and path rewriting for markdown export. +/// +class MyMarkdownResourceCallback : IResourceSavingCallback +{ + private readonly string _resourcesFolder; + + public MyMarkdownResourceCallback(string resourcesFolder) + { + _resourcesFolder = resourcesFolder; + } + + public void ResourceSaving(ResourceSavingArgs args) + { + // 1️⃣ Build the full file system path where the image will be stored. + string targetPath = Path.Combine(_resourcesFolder, args.ResourceFileName); + + // 2️⃣ Ensure the directory exists (in case Aspose creates sub‑folders). + Directory.CreateDirectory(Path.GetDirectoryName(targetPath)); + + // 3️⃣ Write the image stream to disk. + using (FileStream fileStream = File.Create(targetPath)) + { + args.Stream.CopyTo(fileStream); + } + + // 4️⃣ Update the markdown reference to use a relative path. + // This is the crucial line that gives us the markdown image relative path. + args.ResourceFileName = Path.Combine("Resources", args.ResourceFileName); + } +} +``` + +*Warum das funktioniert:* `args.Stream` enthält die rohen Bildbytes. Indem wir sie in eine Datei innerhalb unseres `Resources`‑Ordners kopieren, **exportieren wir Bilder aus docx** sicher. Anschließend ersetzen wir `args.ResourceFileName` durch eine relative URL (`Resources/image.png`). Wenn Aspose.Words später das Markdown schreibt, fügt es genau diesen String ein und liefert uns den gewünschten *markdown image relative path*. + +--- + +## Schritt 4: Überprüfen des erzeugten Markdown (wie die endgültige Ausgabe aussieht) + +Öffnen Sie `Doc.md` in einem beliebigen Texteditor. Sie sollten etwas Ähnliches sehen: + +```markdown +# Sample Heading + +Here is an inline picture: + +![Image 0](Resources/Image_0.png) + +And a picture inside a table: + +![Image 1](Resources/Image_1.jpg) +``` + +Der wichtige Teil ist, dass jeder Bildverweis auf `Resources/...` zeigt – das ist der **markdown image relative path**, den wir gesucht haben. + +![markdown image relative path example](example.png "markdown image relative path example") + +*Tipp:* Wenn Sie das Markdown in einem Viewer öffnen, der relative Links respektiert (VS Code‑Vorschau, GitHub oder ein statischer Site‑Generator), werden die Bilder korrekt dargestellt, ohne zusätzliche Konfiguration. + +--- + +## Schritt 5: Häufige Stolperfallen und Profi‑Tipps + +| Problem | Warum es passiert | Wie man es behebt | +|---------|-------------------|-------------------| +| Bilder landen im Stammordner statt in `Resources` | Der Callback war nicht angehängt oder `args.ResourceFileName` wurde nicht überschrieben. | Stellen Sie sicher, dass `ResourceSavingCallback` **vor** dem Aufruf von `doc.Save` gesetzt ist. | +| Dateinamen enthalten ungültige Zeichen | Word benennt Bilder manchmal mit Leerzeichen oder Unicode‑Symbolen. | Verwenden Sie `Path.GetInvalidFileNameChars()`, um `args.ResourceFileName` im Callback zu bereinigen. | +| Große Dokumente benötigen lange Verarbeitungszeit | Jedes Bild wird synchron geschrieben. | Wechseln Sie zu asynchronem I/O (`await args.Stream.CopyToAsync(fileStream)`), wenn Sie .NET 6+ nutzen und Leistung benötigen. | +| Relative Pfade brechen, wenn das Markdown verschoben wird | Der Pfad ist relativ zum Speicherort der Markdown‑Datei. | Halten Sie `Doc.md` und den `Resources`‑Ordner zusammen, oder passen Sie den Callback an, um ein anderes relatives Präfix zu verwenden (z. B. `../assets`). | + +--- + +## Schritt 6: Erweiterung der Lösung (was tun, wenn Sie mehr Kontrolle benötigen?) + +- **Mehrere Ausgabeformate:** Ersetzen Sie `MarkdownSaveOptions` durch `HtmlSaveOptions` oder `PdfSaveOptions`, während Sie denselben Callback beibehalten – Aspose.Words ruft ihn für jedes Bild unabhängig vom Format auf. +- **Benutzerdefinierte Bildbenennung:** Wenn Sie Bilder umbenennen möchten (z. B. `figure-01.png`), ändern Sie `args.ResourceFileName` im Callback, bevor Sie die Datei schreiben. +- **Einbetten von Bildern als Base64:** Setzen Sie `args.ResourceFileName` auf einen Data‑URI (`data:image/png;base64,...`) und überspringen Sie das Schreiben der Datei. Das ist praktisch für Markdown‑Exporte in einer einzigen Datei. + +--- + +## Fazit + +Sie haben nun ein voll funktionsfähiges C#‑Programm, das **Word in Markdown konvertiert**, **Bilder aus Word extrahiert**, **einen Ressourcen‑Ordner erstellt** und für jedes Bild einen sauberen **markdown image relative path** garantiert. Der Code ist eigenständig, funktioniert mit der neuesten Aspose.Words‑Version und lässt sich mit minimalem Aufwand in jedes .NET‑Projekt einbinden. + +Nächste Schritte? Füttern Sie das erzeugte Markdown in einen statischen Site‑Generator wie Hugo oder Jekyll, oder experimentieren Sie mit dem Callback, um Bilder direkt als Base64‑Strings einzubetten. Wenn Sie auf Sonderfälle stoßen – etwa SVG‑Bilder oder ungewöhnlich große Dateien – schauen Sie zurück in die Tabelle „Häufige Stolperfallen“; eine kleine Anpassung löst meist das Problem. + +Viel Spaß beim Coden, und möge Ihr Markdown immer auf den richtigen Ordner zeigen! + +{{< /blocks/products/pf/tutorial-page-section >}} +{{< /blocks/products/pf/main-container >}} +{{< /blocks/products/pf/main-wrap-class >}} +{{< blocks/products/products-backtop-button >}} \ No newline at end of file diff --git a/words/german/net/programming-with-officemath/_index.md b/words/german/net/programming-with-officemath/_index.md index 034f68be7e..42e9c829f8 100644 --- a/words/german/net/programming-with-officemath/_index.md +++ b/words/german/net/programming-with-officemath/_index.md @@ -23,6 +23,7 @@ Die Aspose.Words für .NET-Tutorials zur Textverarbeitung mit OfficeMath führen | [Mathematische Gleichungen](./math-equations/) | Erfahren Sie, wie Sie mit Aspose.Words für .NET mathematische Gleichungen in Word-Dokumenten konfigurieren. Schritt-für-Schritt-Anleitung mit Beispielen, FAQs und mehr. | | [DOCX als TXT speichern – Word-Mathematik nach LaTeX exportieren mit C#](./save-docx-as-txt-export-word-math-to-latex-with-c/) | Erfahren Sie, wie Sie DOCX-Dateien in TXT konvertieren und mathematische Formeln nach LaTeX exportieren, mithilfe von C# und Aspose.Words. | | [Dokument als Txt speichern – Word-Math in LaTeX exportieren in C#](./save-document-as-txt-export-word-math-to-latex-in-c/) | Erfahren Sie, wie Sie ein Word-Dokument als TXT speichern und mathematische Formeln nach LaTeX in C# exportieren. | +| [DOCX in TXT konvertieren – Word-Gleichungen nach LaTeX exportieren in C#](./convert-docx-to-txt-export-word-equations-to-latex-in-c/) | Erfahren Sie, wie Sie DOCX-Dateien in TXT umwandeln und mathematische Gleichungen nach LaTeX exportieren, mit C# und Aspose.Words. | {{< /blocks/products/pf/tutorial-page-section >}} diff --git a/words/german/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/_index.md b/words/german/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/_index.md new file mode 100644 index 0000000000..a4d96207b3 --- /dev/null +++ b/words/german/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/_index.md @@ -0,0 +1,212 @@ +--- +category: general +date: 2026-04-28 +description: Konvertieren Sie DOCX in TXT und exportieren Sie Word‑Gleichungen nach + LaTeX mit Aspose.Words. Erfahren Sie, wie Sie Word als TXT speichern und mathematische + Objekte in wenigen Schritten verarbeiten. +draft: false +keywords: +- convert docx to txt +- convert word equations to latex +- convert word to plain text +- save word as txt +- export equations as latex +language: de +og_description: Konvertieren Sie DOCX in TXT und exportieren Sie Word‑Gleichungen + nach LaTeX mit einem einfachen C#‑Snippet. Vollständige Anleitung, Code und Tipps. +og_title: DOCX nach TXT konvertieren – Word‑Gleichungen nach LaTeX exportieren +tags: +- C# +- Aspose.Words +- Document Conversion +title: DOCX nach TXT konvertieren – Word‑Gleichungen nach LaTeX exportieren in C# +url: /de/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/ +--- + +{{< blocks/products/pf/main-wrap-class >}} +{{< blocks/products/pf/main-container >}} +{{< blocks/products/pf/tutorial-page-section >}} + +# DOCX in TXT konvertieren – Word‑Gleichungen nach LaTeX exportieren + +Haben Sie schon einmal **docx in txt konvertieren** müssen, waren aber besorgt, dass die Formeln in Ihrer Word‑Datei zu einem Kauderwelsch werden? Sie sind nicht allein. In vielen Ingenieur‑ oder Forschungsprojekten liegt das Ausgangsdokument im .docx‑Format vor, während nachgelagerte Werkzeuge nur Klartext oder LaTeX verstehen. Die gute Nachricht? Mit ein paar Zeilen C# und Aspose.Words können Sie **docx in txt konvertieren** *und* jede Gleichung als sauberen LaTeX‑Code erhalten. + +In diesem Tutorial führen wir Sie durch den gesamten Prozess: Laden einer .docx, Konfigurieren der Speicheroptionen, sodass Office‑Math‑Objekte zu LaTeX werden, und schließlich das Schreiben des Ergebnisses in eine .txt‑Datei. Am Ende wissen Sie, wie Sie **Word als txt speichern**, **Word in Klartext konvertieren** und **Gleichungen als latex exportieren** können, ohne die API‑Dokumentation zu durchforsten. + +## Was Sie lernen werden + +- Die genauen API‑Aufrufe, die nötig sind, um **docx in txt zu konvertieren** und dabei Gleichungen zu erhalten. +- Warum die Wahl von `OfficeMathExportMode.LaTeX` der empfohlene Weg ist, um **Word‑Gleichungen in latex zu konvertieren**. +- Wie Sie gängige Randfälle wie fehlende Schriften oder nicht unterstützte Gleichungs‑Features behandeln. +- Ein vollständiges, sofort lauffähiges C#‑Programm, das Sie in jedes .NET‑Projekt einbinden können. + +### Voraussetzungen + +- .NET 6.0 oder höher (der Code funktioniert auch mit .NET Framework 4.7+). +- Eine Lizenz für Aspose.Words for .NET (die kostenlose Testversion reicht für die Evaluation). +- Ein Word‑Dokument (`input.docx`), das mindestens ein Office‑Math‑Objekt enthält. + +Wenn Sie das haben, legen wir los. + +## Schritt 1: Aspose.Words installieren + +Bevor irgendein Code ausgeführt wird, benötigen Sie die Bibliothek. Öffnen Sie ein Terminal im Projektordner und führen Sie aus: + +```bash +dotnet add package Aspose.Words +``` + +Damit wird die neueste stabile Version (Stand 2026‑04‑28 v24.12) heruntergeladen. Keine zusätzlichen DLLs nötig. + +## Schritt 2: Quelldokument laden + +Zuerst lesen wir die .docx‑Datei in ein `Document`‑Objekt ein. Dieses Objekt gibt uns vollen Zugriff auf die Dateistruktur, inklusive Text‑Runs, Bilder und Math‑Objekte. + +```csharp +using Aspose.Words; +using Aspose.Words.Saving; + +class Program +{ + static void Main() + { + // Step 2: Load the source document + Document doc = new Document(@"YOUR_DIRECTORY\input.docx"); +``` + +> **Warum das wichtig ist:** Das Laden des Dokuments erzeugt eine In‑Memory‑Repräsentation, sodass wir später beeinflussen können, wie jedes Element ausgegeben wird. Wird die Datei nicht gefunden, wirft Aspose eine `FileNotFoundException`, die Sie in Produktionscode abfangen sollten. + +## Schritt 3: TXT‑Speicheroptionen für LaTeX‑Math konfigurieren + +Standardmäßig schreibt `Document.Save` reinen Text und **verwirft** jede Office‑Math‑Formel. Um diese Gleichungen zu erhalten, setzen wir `OfficeMathExportMode` auf `LaTeX`. Damit wird der Exporter angewiesen, jede Gleichung in das entsprechende LaTeX‑Äquivalent zu übersetzen. + +```csharp + // Step 3: Configure TXT save options to export Office Math as LaTeX + TxtSaveOptions txtOptions = new TxtSaveOptions + { + OfficeMathExportMode = OfficeMathExportMode.LaTeX, + // Optional: preserve line breaks as they appear in the original Word file + PreserveTableLayout = true + }; +``` + +> **Pro‑Tipp:** Wenn Sie nur die rohen Unicode‑Zeichen der Gleichung benötigen (z. B. für eine schnelle Vorschau), können Sie `OfficeMathExportMode.Text` verwenden. Für die meisten wissenschaftlichen Pipelines ist jedoch `LaTeX` der Goldstandard, weil es von allen LaTeX‑Prozessoren verstanden wird. + +## Schritt 4: Dokument als Klartext speichern + +Jetzt schreiben wir den transformierten Inhalt in eine `.txt`‑Datei. Die Datei enthält reguläre Absätze, Aufzählungen und – dank des vorherigen Schritts – LaTeX‑Snippets für jede Gleichung. + +```csharp + // Step 4: Save the document as plain‑text using the configured options + doc.Save(@"YOUR_DIRECTORY\Math.txt", txtOptions); + } +} +``` + +Wenn Sie `Math.txt` öffnen, sehen Sie etwa Folgendes: + +``` +In this report we derive the quadratic formula: +\[ +x = \frac{-b \pm \sqrt{b^{2} - 4ac}}{2a} +\] + +The end. +``` + +Erkennen Sie die `\[` … `\]`‑Begrenzer? Das sind die automatisch erzeugten LaTeX‑Math‑Blöcke. + +## Schritt 5: Ausgabe prüfen (optional, aber empfohlen) + +Es ist leicht, subtile Konvertierungsprobleme zu übersehen, besonders wenn Gleichungen benutzerdefinierte Symbole enthalten. Ein schneller Plausibilitäts‑Check ist, die erzeugte `.txt`‑Datei in einen LaTeX‑Compiler (z. B. `pdflatex`) zu geben und zu schauen, ob sie fehlerfrei kompiliert. + +```bash +pdflatex -interaction=nonstopmode Math.txt +``` + +Gelingt die Kompilierung, haben Sie erfolgreich **Word‑Gleichungen in latex konvertiert** und **docx in txt konvertiert** – in einem Schritt. Bei Fehlermeldungen achten Sie auf Hinweise zu undefinierten Befehlen – das deutet meist auf ein Gleichungs‑Feature hin, das Aspose.Words nicht übersetzen kann (z. B. bestimmte Matrix‑Notation). In solchen Fällen können Sie zu `OfficeMathExportMode.MathML` zurückwechseln und das MathML mit einem anderen Tool nach LaTeX umwandeln. + +## Häufige Stolperfallen & wie man sie vermeidet + +| Problem | Warum es passiert | Lösung | +|---------|-------------------|--------| +| Fehlende Schriften | Aspose.Words benötigt die Schrift, um Symbole korrekt darzustellen. | Schrift auf dem Rechner installieren oder in die .docx einbetten. | +| Komplexe Gleichungen werden nicht exportiert | Einige neuere Office‑Math‑Features sind noch nicht auf LaTeX abgebildet. | `OfficeMathExportMode.MathML` verwenden und anschließend mit einer MathML‑zu‑LaTeX‑Bibliothek konvertieren. | +| Zusätzliche Leerzeilen | Der Klartext‑Saver bewahrt Absatzumbrüche, was zu zusätzlichem Whitespace führen kann. | `txtOptions.AddBidiMarks = false` setzen oder die Datei mit einem einfachen Skript nachbearbeiten. | + +## Vollständiges Beispiel (einfach kopier‑und‑einfügen) + +Unten finden Sie das komplette Programm, fertig zum Kompilieren. Ersetzen Sie `YOUR_DIRECTORY` durch den Ordner, der Ihre `input.docx` enthält. + +```csharp +using System; +using Aspose.Words; +using Aspose.Words.Saving; + +namespace DocxToTxtWithLatex +{ + class Program + { + static void Main() + { + try + { + // Load the source document + Document doc = new Document(@"C:\Docs\input.docx"); + + // Configure save options: export equations as LaTeX + TxtSaveOptions txtOptions = new TxtSaveOptions + { + OfficeMathExportMode = OfficeMathExportMode.LaTeX, + PreserveTableLayout = true, + AddBidiMarks = false + }; + + // Save as plain‑text + string outputPath = @"C:\Docs\Math.txt"; + doc.Save(outputPath, txtOptions); + + Console.WriteLine($"Successfully converted DOCX to TXT. Output at: {outputPath}"); + } + catch (Exception ex) + { + Console.Error.WriteLine($"Conversion failed: {ex.Message}"); + } + } + } +} +``` + +Wenn Sie dieses Programm ausführen, **speichert es Word als txt**, wobei jeder Office‑Math‑Block in LaTeX umgewandelt wird – Sie erhalten also eine saubere, durchsuchbare Klartextdatei. + +## Nächste Schritte & verwandte Themen + +- **Batch‑Konvertierung:** Packen Sie die obige Logik in eine `foreach`‑Schleife, um einen ganzen Ordner mit .docx‑Dateien zu verarbeiten. +- **Kombination mit PDF‑Erstellung:** Nachdem Sie die LaTeX‑Snippets haben, können Sie sie in eine PDF‑Pipeline (z. B. `PdfSharp` + `MiKTeX`) einspeisen, um PDF‑Berichte zu erzeugen. +- **Gleichungen als latex exportieren** für andere Formate: Aspose.Words unterstützt außerdem `SaveFormat.Markdown`, das LaTeX automatisch einbetten kann. +- **Performance‑Optimierung:** Bei sehr großen Dokumenten wiederverwenden Sie dieselbe `TxtSaveOptions`‑Instanz und deaktivieren Sie unnötige Features wie `AddBidiMarks`. + +--- + +### Bildbeispiel (optional) + +Falls Sie eine visuelle Orientierung bevorzugen, hier ein Screenshot der Ausgabedatei in Notepad++. + +![convert docx to txt output showing LaTeX equations](convert-docx-to-txt-output.png) + +*(Alt‑Text: “convert docx to txt output showing LaTeX equations” – erfüllt die primäre Keyword‑Anforderung.)* + +--- + +## Fazit + +Wir haben gezeigt, wie man zuverlässig **docx in txt konvertiert**, während jede Gleichung als sauberer LaTeX‑Code erhalten bleibt. Der Schlüssel ist das Flag `OfficeMathExportMode.LaTeX`, das das proprietäre Word‑Math‑Format in etwas verwandelt, das jeder LaTeX‑Engine versteht. Mit dem obigen vollständigen Code‑Beispiel können Sie **Word als txt speichern**, **Word in Klartext konvertieren** und **Gleichungen als latex exportieren** in einem einzigen, eigenständigen Durchlauf. + +Probieren Sie gern aus – ändern Sie die Ausgabe‑Erweiterung zu `.md` für Markdown oder integrieren Sie das Snippet in eine größere Dokumenten‑Verarbeitungspipeline. Wenn Sie auf Eigenheiten stoßen, hinterlassen Sie einen Kommentar unten; ich helfe gern beim Troubleshooting. + +Viel Spaß beim Coden! + +{{< /blocks/products/pf/tutorial-page-section >}} +{{< /blocks/products/pf/main-container >}} +{{< /blocks/products/pf/main-wrap-class >}} +{{< blocks/products/products-backtop-button >}} \ No newline at end of file diff --git a/words/greek/net/ai-powered-document-processing/_index.md b/words/greek/net/ai-powered-document-processing/_index.md index 6ec35473e6..e987d4c7e9 100644 --- a/words/greek/net/ai-powered-document-processing/_index.md +++ b/words/greek/net/ai-powered-document-processing/_index.md @@ -42,6 +42,7 @@ | [Εργασία με το μοντέλο τεχνητής νοημοσύνης της Google](./working-with-google-ai-model/) Αναβαθμίστε την επεξεργασία εγγράφων με το Aspose.Words για .NET και Google AI για να δημιουργήσετε συνοπτικές περιλήψεις χωρίς κόπο. | | [Εργασία με ανοιχτό μοντέλο τεχνητής νοημοσύνης](./working-with-open-ai-model/) | Ξεκλειδώστε αποτελεσματική σύνοψη εγγράφων χρησιμοποιώντας το Aspose.Words για .NET με τα ισχυρά μοντέλα του OpenAI. Βουτήξτε σε αυτόν τον ολοκληρωμένο οδηγό τώρα. | | [Εργασία με επιλογές σύνοψης](./working-with-summarize-options/) | Μάθετε να συνοψίζετε αποτελεσματικά έγγραφα Word χρησιμοποιώντας το Aspose.Words για .NET με τον αναλυτικό οδηγό μας για την ενσωμάτωση μοντέλων AI για γρήγορες πληροφορίες. | +| [Σύνδεση με τοπικό LLM σε C# – Πλήρης Οδηγός Προγραμματισμού](./connect-to-local-llm-in-c-complete-programming-guide/) | Μάθετε πώς να συνδέσετε ένα τοπικό μοντέλο γλώσσας σε C# χρησιμοποιώντας το Aspose.Words για .NET. | {{< /blocks/products/pf/tutorial-page-section >}} diff --git a/words/greek/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/_index.md b/words/greek/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/_index.md new file mode 100644 index 0000000000..d5770e5355 --- /dev/null +++ b/words/greek/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/_index.md @@ -0,0 +1,284 @@ +--- +category: general +date: 2026-04-28 +description: Συνδέστε το τοπικό LLM από C# και ζητήστε από το μεγάλο μοντέλο γλώσσας + να φορτώσει έγγραφο Word, καλέστε το τοπικό LLM και ξαναγράψτε το κείμενο αυτόματα. + Περιλαμβάνεται κώδικας βήμα‑βήμα. +draft: false +keywords: +- connect to local llm +- prompt large language model +- load word document +- call local llm +- rewrite text automatically +language: el +og_description: Συνδεθείτε με το τοπικό LLM από C# και δείτε πώς να δώσετε εντολή + σε μεγάλο μοντέλο γλώσσας, να φορτώσετε έγγραφο Word, να καλέσετε το τοπικό LLM + και να ξαναγράψετε το κείμενο αυτόματα σε λίγα λεπτά. +og_title: Σύνδεση με τοπικό LLM σε C# – Πλήρης Οδηγός Προγραμματισμού +tags: +- Aspose.Words +- C# +- LLM +- AI Automation +title: Σύνδεση με τοπικό LLM σε C# – Πλήρης Οδηγός Προγραμματισμού +url: /el/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/ +--- + +{{< blocks/products/pf/main-wrap-class >}} +{{< blocks/products/pf/main-container >}} +{{< blocks/products/pf/tutorial-page-section >}} + +# Σύνδεση με το Τοπικό LLM σε C# – Πλήρης Οδηγός Προγραμματισμού + +Έχετε ποτέ χρειαστεί να **συνδέσετε το τοπικό llm** από μια εφαρμογή .NET και να αναρωτηθείτε πώς να το κάνετε να «μιλήσει» με ένα αρχείο Word; Δεν είστε μόνοι. Σε αυτόν τον οδηγό θα περάσουμε από όλη τη διαδικασία — σύνδεση με το τοπικό llm, **prompt large language model**, φόρτωση ενός εγγράφου Word, **call local llm**, και τέλος **rewrite text automatically**. Στο τέλος θα έχετε ένα εκτελέσιμο παράδειγμα που μετατρέπει οποιαδήποτε παράγραφο σε επίσημο τόνο χωρίς εξωτερικά κλειδιά API. + +## Τι καλύπτει αυτό το tutorial + +Θα ξεκινήσουμε εγκαθιστώντας τα απαραίτητα πακέτα NuGet, μετά θα δημιουργήσουμε ένα απλό τοπικό LLM endpoint (σκεφτείτε το Ollama στη θύρα 11434). Στη συνέχεια θα φορτώσουμε ένα αρχείο `.docx` χρησιμοποιώντας το Aspose.Words, θα στείλουμε μια παράγραφο στο LLM, θα λάβουμε μια επανεγγραμμένη έκδοση και θα την γράψουμε πίσω στο ίδιο έγγραφο. Θα δείτε επίσης πώς να αντιμετωπίσετε κοινά προβλήματα — κενές παραγράφους, async διαχείριση πόρων, και ιδιαιτερότητες κωδικοποίησης — ώστε ο κώδικας να λειτουργεί σε παραγωγή, όχι μόνο σε demo. + +### Προαπαιτούμενα + +- .NET 6.0 SDK ή νεότερο (μπορείτε επίσης να χρησιμοποιήσετε .NET 8 αν θέλετε) +- Visual Studio 2022 ή VS Code με επέκταση C# +- **Aspose.Words for .NET** (η δωρεάν δοκιμή λειτουργεί καλά) +- Ένα τοπικά φιλοξενούμενο LLM που ακολουθεί το συμβόλαιο `/api/generate` (π.χ., Ollama, LMStudio) +- Βασική εξοικείωση με async/await σε C# + +> **Pro tip:** Αν δεν έχετε εγκαταστήσει ακόμα το Ollama, εκτελέστε `ollama serve` και κατεβάστε ένα μοντέλο με `ollama pull llama3`. Το προεπιλεγμένο HTTP endpoint θα είναι `http://localhost:11434/api/generate`. + +--- + +## Βήμα 1: Εγκατάσταση Απαιτούμενων Πακέτων + +Αρχικά, προσθέστε τα πακέτα NuGet Aspose.Words και Aspose.Words.AI στο πρόγραμμά σας. + +```bash +dotnet add package Aspose.Words +dotnet add package Aspose.Words.AI +``` + +Αυτές οι βιβλιοθήκες μας παρέχουν τη δυνατότητα **load word document** και ένα ελαφρύ wrapper για **call local llm** χωρίς να χρειάζεται να δημιουργήσουμε χειροκίνητα HTTP αιτήματα. + +--- + +## Βήμα 2: Σύνδεση με το Τοπικό LLM Endpoint + +Η σύνδεση με ένα τοπικά φιλοξενούμενο μοντέλο είναι τόσο απλή όσο η δημιουργία ενός αντικειμένου `LocalLargeLanguageModel`. Ο κατασκευαστής αναμένει το πλήρες URL του endpoint δημιουργίας. + +```csharp +using Aspose.Words.AI; +using Aspose.Words; +using System.Threading.Tasks; + +// Create a client that talks to the LLM running on localhost +var localLlm = new LocalLargeLanguageModel("http://localhost:11434/api/generate"); +``` + +Γιατί τυλίγουμε το endpoint σε μια κλάση; Η `LocalLargeLanguageModel` διαχειρίζεται τη σειριοποίηση JSON, τις επαναπροσπάθειες και τις ροές απαντήσεων για εσάς — ώστε να μπορείτε να εστιάσετε στη λογική του prompt αντί να ασχολείστε με το `HttpClient`. + +--- + +## Βήμα 3: Φόρτωση Πηγαίου Εγγράφου Word + +Στη συνέχεια, φέρνουμε το έγγραφο στη μνήμη. Το Aspose.Words υποστηρίζει σχεδόν κάθε μορφή Word, έτσι το `Document` θα αναλύσει το `input.docx` χωρίς να χρειάζεται εγκατεστημένο Office. + +```csharp +// Path to the source file – adjust as needed +string inputPath = Path.Combine(Environment.CurrentDirectory, "input.docx"); + +// Load the document; throws if the file is missing or corrupted +Document sourceDocument = new Document(inputPath); +``` + +Αν χρειάζεται να δουλέψετε με ροή (π.χ., ένα αρχείο που ανεβάστηκε μέσω ASP.NET), απλώς αντικαταστήστε τη διαδρομή αρχείου με ένα `MemoryStream` και περάστε το στον κατασκευαστή `Document`. + +--- + +## Βήμα 4: Εξαγωγή Κειμένου Τρέχουσας Παραγράφου + +Θα χρησιμοποιήσουμε το `DocumentBuilder` για να περιηγηθούμε στο έγγραφο. Σε αυτό το παράδειγμα επανεγγράφουμε **την πρώτη παράγραφο**, αλλά μπορείτε να επαναλάβετε το `sourceDocument.GetChildNodes(NodeType.Paragraph, true)` για να επεξεργαστείτε πολλές. + +```csharp +// Builder gives us a cursor inside the document +DocumentBuilder docBuilder = new DocumentBuilder(sourceDocument); + +// Grab the text of the paragraph where the builder is currently positioned +string originalParagraph = docBuilder.CurrentParagraph?.GetText() ?? string.Empty; + +// Safety check – avoid sending empty strings to the LLM +if (string.IsNullOrWhiteSpace(originalParagraph)) +{ + Console.WriteLine("No paragraph found at the current cursor position."); + return; +} +``` + +Ο τελεστής `?.` αποτρέπει ένα `NullReferenceException` αν το έγγραφο είναι κενό. Αυτό είναι ένα από εκείνα τα **edge cases** που παγιδεύουν τους αρχάριους. + +--- + +## Βήμα 5: Prompt το LLM για Επανεγγραφή της Παραγράφου + +Τώρα πραγματικά **prompt large language model**. Το prompt είναι απλή αγγλική; το wrapper θα το στείλει ως JSON στο τοπικό endpoint. + +```csharp +// Build a friendly instruction for the model +string prompt = $"Rewrite the following sentence in a more formal tone:\n{originalParagraph}"; + +// Await the model's response – this is an async call +string rewrittenParagraph = await localLlm.PromptAsync(prompt); +``` + +Γιατί διατυπώνουμε το αίτημα έτσι; Τα LLM ανταποκρίνονται καλύτερα σε σαφείς, μονο‑εργασίες οδηγίες. Η προσθήκη μιας νέας γραμμής μετά το άνω‑κάτω τελεία χωρίζει την οδηγία από το περιεχόμενο, μειώνοντας την πιθανότητα το μοντέλο να επαναλάβει το prompt. + +**Expected output** – Αν το `originalParagraph` ήταν `"Hey, what's up?"`, το LLM μπορεί να επιστρέψει: + +> “Καλημέρα, πώς μπορώ να σας βοηθήσω;” + +Μπορείτε να επαληθεύσετε το αποτέλεσμα εκτυπώνοντάς το: + +```csharp +Console.WriteLine("Original: " + originalParagraph); +Console.WriteLine("Rewritten: " + rewrittenParagraph); +``` + +--- + +## Βήμα 6: Εισαγωγή του Επανεγγραμμένου Κειμένου Πίσω στο Έγγραφο + +Με το νέο κείμενο στα χέρια, αντικαθιστούμε την παλιά παράγραφο. Η `DocumentBuilder.Writeln` γράφει μια νέα γραμμή και προχωρά το cursor, κάτι τέλειο για προσθήκη. Αν χρειάζεται να *αντικαταστήσετε* ακριβώς την ίδια παράγραφο, μπορείτε να χρησιμοποιήσετε `docBuilder.CurrentParagraph.RemoveAllChildren()` πριν τη γραφή. + +```csharp +// Option A – Append a new paragraph (keeps the original) +docBuilder.Writeln(rewrittenParagraph); + +// Option B – Replace the existing paragraph (uncomment to use) +// docBuilder.CurrentParagraph.RemoveAllChildren(); +// docBuilder.CurrentParagraph.AppendChild(new Run(docBuilder.Document, rewrittenParagraph)); +``` + +Και οι δύο προσεγγίσεις εμφανίζονται ώστε να μπορείτε να επιλέξετε αυτή που ταιριάζει στη ροή εργασίας σας. + +--- + +## Βήμα 7: Αποθήκευση του Ενημερωμένου Εγγράφου + +Τέλος, αποθηκεύουμε τις αλλαγές σε ένα νέο αρχείο. Το Aspose.Words επιλέγει αυτόματα τη μορφή βάσει της επέκτασης του αρχείου. + +```csharp +string outputPath = Path.Combine(Environment.CurrentDirectory, "output.docx"); +sourceDocument.Save(outputPath); + +Console.WriteLine($"Document saved to {outputPath}"); +``` + +Ανοίξτε το `output.docx` στο Word, και θα δείτε ότι η παράγραφος τώρα διαβάζεται σε επίσημο τόνο. + +--- + +## Πλήρες Παράδειγμα Λειτουργίας + +Παρακάτω βρίσκεται το **complete, self‑contained program**. Αντιγράψτε‑και‑επικολλήστε το σε ένα console project, επαναφέρετε τα πακέτα NuGet και τρέξτε το — χωρίς πρόσθετη ρύθμιση εκτός από ένα ενεργό τοπικό LLM. + +```csharp +using Aspose.Words.AI; +using Aspose.Words; +using System; +using System.IO; +using System.Threading.Tasks; + +class Program +{ + static async Task Main() + { + // ------------------------------------------------- + // Step 1: Connect to the locally hosted LLM endpoint + // ------------------------------------------------- + var localLlm = new LocalLargeLanguageModel("http://localhost:11434/api/generate"); + + // ------------------------------------------------- + // Step 2: Load the source Word document + // ------------------------------------------------- + string inputPath = Path.Combine(Environment.CurrentDirectory, "input.docx"); + Document sourceDocument = new Document(inputPath); + + // ------------------------------------------------- + // Step 3: Retrieve the text of the current paragraph + // ------------------------------------------------- + DocumentBuilder docBuilder = new DocumentBuilder(sourceDocument); + string originalParagraph = docBuilder.CurrentParagraph?.GetText() ?? string.Empty; + + if (string.IsNullOrWhiteSpace(originalParagraph)) + { + Console.WriteLine("No paragraph found at the current cursor position."); + return; + } + + // ------------------------------------------------- + // Step 4: Ask the LLM to rewrite the paragraph in a formal tone + // ------------------------------------------------- + string prompt = $"Rewrite the following sentence in a more formal tone:\n{originalParagraph}"; + string rewrittenParagraph = await localLlm.PromptAsync(prompt); + + // ------------------------------------------------- + // Step 5: Insert the rewritten text back into the document + // ------------------------------------------------- + docBuilder.Writeln(rewrittenParagraph); + + // ------------------------------------------------- + // Step 6: Save the updated document + // ------------------------------------------------- + string outputPath = Path.Combine(Environment.CurrentDirectory, "output.docx"); + sourceDocument.Save(outputPath); + + Console.WriteLine("Original paragraph:"); + Console.WriteLine(originalParagraph); + Console.WriteLine("\nRewritten paragraph:"); + Console.WriteLine(rewrittenParagraph); + Console.WriteLine($"\nDocument saved to {outputPath}"); + } +} +``` + +### Τι να Περιμένετε Όταν το Εκτελέσετε + +1. Η κονσόλα εκτυπώνει τις αρχικές και τις επανεγγραμμένες παραγράφους. +2. `output.docx` εμφανίζεται δίπλα στο `input.docx`. +3. Ανοίγοντας το αρχείο δείχνει τη νέα επίσημη παράγραφο που έχει εισαχθεί μετά την αρχική (ή αντικατασταθεί, αν χρησιμοποιήσατε τον εναλλακτικό κώδικα). + +--- + +## Διαχείριση Συνηθισμένων Edge Cases + +| Situation | Solution | +|-----------|----------| +| **Empty or whitespace‑only paragraph** | Ελέγξτε `string.IsNullOrWhiteSpace` πριν το prompt (δείτε το Βήμα 3). | +| **LLM returns an error or empty string** | Τυλίξτε το `PromptAsync` σε `try/catch` και επιστρέψτε το αρχικό κείμενο σε περίπτωση σφάλματος. | +| **Multiple paragraphs need rewriting** | Επανάληψη μέσω `sourceDocument.GetChildNodes(NodeType.Paragraph, true)` και εφαρμογή της ίδιας λογικής prompt. | +| **Large documents cause latency** | Ομαδοποιήστε παραγράφους και στείλτε τις σε ένα ενιαίο αίτημα (prompt έως 4 KB ανά κλήση). | +| **Non‑ASCII characters get garbled** | Βεβαιωθείτε ότι το endpoint του LLM χρησιμοποιεί UTF-8 (τα περισσότερα μοντέλα το κάνουν). | + +--- + +## Επόμενα Βήματα & Σχετικά Θέματα + +- **Prompt large language model** με πιο πλούσιες οδηγίες (π.χ., οδηγούς στυλ, όρια μήκους). +- Χρησιμοποιήστε **call local llm** σε web API για να εκθέσετε την αυτοματοποίηση εγγράφων ως υπηρεσία. +- Εξερευνήστε το **load word document** σε παράλληλες ροές για σενάρια υψηλής απόδοσης. +- Συνδυάστε αυτή την προσέγγιση με **rewrite text automatically** για μαζική δημιουργία email ή τυποποίηση αναφορών. + +Αν θέλετε να εμβαθύνετε, δείτε την τεκμηρίωση του Aspose για **document merging** και την αναφορά API του Ollama για προσαρμοσμένες παραμέτρους δειγματοληψίας. + +--- + +## Συμπέρασμα + +Μόλις σας δείξαμε πώς να **connect to local llm** από C#, **prompt large language model**, **load word document**, **call local llm**, και **rewrite text automatically** — όλα σε μια ενιαία, εκτελέσιμη εφαρμογή console. Το πρότυπο κλιμακώνεται: αλλάξτε το prompt, επαναλάβετε τις παραγράφους, ή εκθέστε τη λογική μέσω ενός endpoint ASP.NET. Το βασικό συμπέρασμα είναι ότι τα τοπικά μοντέλα AI μπορούν να ενσωματωθούν στενά με κλασικές βιβλιοθήκες επεξεργασίας εγγράφων, παρέχοντας ισχυρή αυτοματοποίηση χωρίς να αφήνετε το αξιόπιστο on‑prem περιβάλλον σας. + +Έχετε ερωτήσεις σχετικά με το threading, + +{{< /blocks/products/pf/tutorial-page-section >}} +{{< /blocks/products/pf/main-container >}} +{{< /blocks/products/pf/main-wrap-class >}} +{{< blocks/products/products-backtop-button >}} \ No newline at end of file diff --git a/words/greek/net/programming-with-markdownsaveoptions/_index.md b/words/greek/net/programming-with-markdownsaveoptions/_index.md index 86265e28a6..bbb7c27d5f 100644 --- a/words/greek/net/programming-with-markdownsaveoptions/_index.md +++ b/words/greek/net/programming-with-markdownsaveoptions/_index.md @@ -43,6 +43,7 @@ | [Πώς να μετονομάσετε εικόνες κατά τη μετατροπή DOCX σε Markdown](./how-to-rename-images-when-converting-docx-to-markdown/) | Μάθετε πώς να μετονομάσετε εικόνες κατά τη μετατροπή DOCX σε Markdown χρησιμοποιώντας Aspose.Words για .NET. | | [Αποθήκευση docx ως markdown με Aspose.Words – Πλήρης Οδηγός C#](./save-docx-as-markdown-with-aspose-words-full-c-guide/) | Μάθετε πώς να μετατρέψετε αρχεία docx σε markdown χρησιμοποιώντας Aspose.Words με πλήρη οδηγό C#. | | [Αποθήκευση εικόνων Word – Μετατροπή Word σε Markdown με Aspose](./save-word-images-convert-word-to-markdown-with-aspose/) | Μάθετε πώς να αποθηκεύσετε εικόνες από έγγραφα Word και να τα μετατρέψετε σε Markdown με Aspose.Words. | +| [σχετική διαδρομή εικόνας markdown – Μετατροπή Word σε Markdown](./markdown-image-relative-path-convert-word-to-markdown/) | Μάθετε πώς να ορίσετε σχετικές διαδρομές εικόνων κατά τη μετατροπή εγγράφων Word σε Markdown με Aspose.Words για .NET. | {{< /blocks/products/pf/tutorial-page-section >}} diff --git a/words/greek/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/_index.md b/words/greek/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/_index.md new file mode 100644 index 0000000000..575682815f --- /dev/null +++ b/words/greek/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/_index.md @@ -0,0 +1,206 @@ +--- +category: general +date: 2026-04-28 +description: Μάθετε πώς να ορίσετε σχετικό μονοπάτι εικόνας σε markdown όταν μετατρέπετε + το Word σε markdown, εξάγετε εικόνες από το Word και δημιουργήσετε φάκελο πόρων + για τις εξαγόμενες εικόνες. +draft: false +keywords: +- markdown image relative path +- convert word to markdown +- extract images from word +- create resources folder +- export images from docx +language: el +og_description: Ορίστε μια σχετική διαδρομή εικόνας markdown ενώ μετατρέπετε το Word + σε markdown, εξάγετε εικόνες από το Word και δημιουργήστε φάκελο resources για τις + εξαγόμενες εικόνες. +og_title: Σχετική διαδρομή εικόνας Markdown – Μετατροπή Word σε Markdown +tags: +- Aspose.Words +- C# +- Markdown +- Image Export +title: σχετικό μονοπάτι εικόνας markdown – Μετατροπή Word σε Markdown +url: /el/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/ +--- + +{{< blocks/products/pf/main-wrap-class >}} +{{< blocks/products/pf/main-container >}} +{{< blocks/products/pf/tutorial-page-section >}} + +# markdown image relative path – Μετατροπή Word σε Markdown + +Έχετε ποτέ χρειαστεί ένα **markdown image relative path** ενώ **μετατρέπετε Word σε markdown**; Δεν είστε μόνοι. Οι περισσότεροι προγραμματιστές αντιμετωπίζουν πρόβλημα όταν το παραγόμενο Markdown δείχνει σε εικόνες σε έναν επίπεδο φάκελο, σπάζοντας τη δομή των σχετικών συνδέσμων που περιμένετε σε έναν static site ή σε αποθετήριο GitHub. + +Σε αυτό το tutorial θα περάσουμε βήμα‑βήμα από μια πλήρη, end‑to‑end λύση που **εξάγει εικόνες από Word**, **δημιουργεί έναν φάκελο resources**, και ξαναγράφει τις αναφορές εικόνων ώστε να χρησιμοποιούν ένα καθαρό *markdown image relative path*. Στο τέλος θα έχετε ένα έτοιμο προς δημοσίευση αρχείο `.md` και έναν καλαίσθητα οργανωμένο φάκελο `Resources` που περιέχει κάθε εικόνα που εξήχθη από το αρχικό `.docx`. + +> **Τι θα πάρετε:** ένα μόνο πρόγραμμα C# (χωρίς εξωτερικά scripts), μια σαφή εξήγηση του *γιατί* κάθε μέρος είναι σημαντικό, και μια σειρά πρακτικών συμβουλών που μπορείτε να αντιγράψετε‑επικολλήσετε στα δικά σας projects. + +--- + +## Prerequisites + +Πριν βουτήξουμε στον κώδικα, βεβαιωθείτε ότι έχετε: + +- **.NET 6.0** ή νεότερη έκδοση εγκατεστημένη (μπορείτε επίσης να στοχεύσετε .NET Framework 4.7+, αλλά το .NET 6 είναι η ιδανική επιλογή για νέα projects). +- **Aspose.Words for .NET** (το πιο πρόσφατο πακέτο NuGet τη στιγμή της συγγραφής, έκδοση 23.12). Εγκαταστήστε το με: + ```bash + dotnet add package Aspose.Words + ``` +- Ένα έγγραφο Word που περιέχει πραγματικά εικόνες—ας το ονομάσουμε `WithImages.docx`. +- Έναν φάκελο όπου θέλετε να αποθηκευτούν το παραγόμενο markdown και οι εικόνες, π.χ. `C:\Projects\MarkdownExport`. +- Δεν απαιτούνται επιπλέον βιβλιοθήκες· όλα τα υπόλοιπα διαχειρίζεται το Aspose.Words. + +--- + +## Step 1: Load the source Word document (the starting point for convert word to markdown) + +```csharp +using System; +using System.IO; +using Aspose.Words; +using Aspose.Words.Saving; + +class Program +{ + static void Main() + { + // Adjust the path to point at your own .docx file. + string sourcePath = @"C:\Projects\MarkdownExport\WithImages.docx"; + + // Load the document – this is where Aspose.Words parses the Word file. + Document doc = new Document(sourcePath); + + // The rest of the workflow follows… + } +} +``` + +*Why this matters:* Η φόρτωση του εγγράφου μας δίνει πρόσβαση στο εσωτερικό δέντρο κόμβων, το οποίο περιλαμβάνει τα τμήματα εικόνων που αργότερα χρειάζεται να **export images from docx**. Αν η φόρτωση αποτύχει, κανένα από τα επόμενα βήματα δεν θα εκτελεστεί, οπότε ελέγξτε ξανά τη διαδρομή και τα δικαιώματα του αρχείου. + +--- + +## Step 2: Configure `MarkdownSaveOptions` with a custom callback (the heart of create resources folder) + +Το `ResourceSavingCallback` μας επιτρέπει να παρεμβαίνουμε κάθε φορά που το Aspose.Words θέλει να γράψει ένα αρχείο εικόνας. Μέσα στην callback θα **δημιουργήσουμε έναν υπο‑φάκελο Resources** και θα προσαρμόσουμε την αναφορά ώστε το παραγόμενο markdown να χρησιμοποιεί ένα *markdown image relative path*. + +```csharp +// Inside Main(), after loading the document: +string outputFolder = @"C:\Projects\MarkdownExport"; +string resourcesFolder = Path.Combine(outputFolder, "Resources"); + +// Make sure the folder exists before we start saving anything. +Directory.CreateDirectory(resourcesFolder); + +// Set up the Markdown save options. +MarkdownSaveOptions mdOptions = new MarkdownSaveOptions +{ + // Hook that runs for every image resource. + ResourceSavingCallback = new MyMarkdownResourceCallback(resourcesFolder) +}; + +// Save the document as Markdown. +string markdownPath = Path.Combine(outputFolder, "Doc.md"); +doc.Save(markdownPath, mdOptions); +``` + +Παρατηρήστε ότι περάσαμε το `resourcesFolder` στον κατασκευαστή της callback—αυτό κρατά τη διαδρομή του φακέλου ευέλικτη και αποφεύγει το hard‑coding συμβολοσειρών σε όλο τον κώδικα. + +--- + +## Step 3: Implement the callback that **creates resources folder** and rewrites the path + +```csharp +/// +/// Handles image extraction and path rewriting for markdown export. +/// +class MyMarkdownResourceCallback : IResourceSavingCallback +{ + private readonly string _resourcesFolder; + + public MyMarkdownResourceCallback(string resourcesFolder) + { + _resourcesFolder = resourcesFolder; + } + + public void ResourceSaving(ResourceSavingArgs args) + { + // 1️⃣ Build the full file system path where the image will be stored. + string targetPath = Path.Combine(_resourcesFolder, args.ResourceFileName); + + // 2️⃣ Ensure the directory exists (in case Aspose creates sub‑folders). + Directory.CreateDirectory(Path.GetDirectoryName(targetPath)); + + // 3️⃣ Write the image stream to disk. + using (FileStream fileStream = File.Create(targetPath)) + { + args.Stream.CopyTo(fileStream); + } + + // 4️⃣ Update the markdown reference to use a relative path. + // This is the crucial line that gives us the markdown image relative path. + args.ResourceFileName = Path.Combine("Resources", args.ResourceFileName); + } +} +``` + +*Why this works:* Το `args.Stream` περιέχει τα ακατέργαστα bytes της εικόνας. Αντιγράφοντάς το σε ένα αρχείο μέσα στον φάκελο `Resources` **export images from docx** με ασφάλεια. Στη συνέχεια αντικαθιστούμε το `args.ResourceFileName` με ένα σχετικό URL (`Resources/image.png`). Όταν το Aspose.Words γράψει αργότερα το markdown, θα ενσωματώσει ακριβώς αυτή τη συμβολοσειρά, δίνοντάς μας το επιθυμητό *markdown image relative path*. + +--- + +## Step 4: Verify the generated Markdown (what the final output looks like) + +Ανοίξτε το `Doc.md` σε οποιονδήποτε επεξεργαστή κειμένου. Θα πρέπει να δείτε κάτι παρόμοιο με: + +```markdown +# Sample Heading + +Here is an inline picture: + +![Image 0](Resources/Image_0.png) + +And a picture inside a table: + +![Image 1](Resources/Image_1.jpg) +``` + +Το σημαντικό είναι ότι κάθε αναφορά εικόνας δείχνει στο `Resources/...` – αυτό είναι το **markdown image relative path** που επιζητούσαμε. + +![παράδειγμα markdown image relative path](example.png "παράδειγμα markdown image relative path") + +*Tip:* Αν ανοίξετε το markdown σε έναν προβολέα που σέβεται τις σχετικές συνδέσεις (προεπισκόπηση VS Code, GitHub ή static site generator), οι εικόνες θα εμφανιστούν σωστά χωρίς επιπλέον ρυθμίσεις. + +--- + +## Step 5: Common pitfalls and pro‑tips + +| Πρόβλημα | Γιατί συμβαίνει | Πώς να το διορθώσετε | +|----------|----------------|----------------------| +| Οι εικόνες καταλήγουν στον ριζικό φάκελο αντί για `Resources` | Η callback δεν είχε προσαρτηθεί ή το `args.ResourceFileName` δεν αντικαταστάθηκε. | Ελέγξτε ξανά ότι το `ResourceSavingCallback` έχει οριστεί **πριν** την κλήση του `doc.Save`. | +| Τα ονόματα αρχείων περιέχουν μη έγκυρους χαρακτήρες | Το Word μερικές φορές ονομάζει τις εικόνες με κενά ή σύμβολα Unicode. | Χρησιμοποιήστε το `Path.GetInvalidFileNameChars()` για να καθαρίσετε το `args.ResourceFileName` μέσα στην callback. | +| Τα μεγάλα έγγραφα χρειάζονται πολύ χρόνο επεξεργασίας | Κάθε εικόνα γράφεται συγχρονισμένα. | Μεταβείτε σε ασύγχρονη I/O (`await args.Stream.CopyToAsync(fileStream)`) αν χρησιμοποιείτε .NET 6+ και χρειάζεστε απόδοση. | +| Οι σχετικές διαδρομές σπάζουν όταν το markdown μετακινείται | Η διαδρομή είναι σχετική με τη θέση του αρχείου markdown. | Διατηρήστε το `Doc.md` και το φάκελο `Resources` μαζί, ή προσαρμόστε την callback ώστε να χρησιμοποιεί διαφορετικό σχετικό πρόθεμα (π.χ., `../assets`). | + +--- + +## Step 6: Extending the solution (what if you need more control?) + +- **Multiple output formats:** Αντικαταστήστε το `MarkdownSaveOptions` με `HtmlSaveOptions` ή `PdfSaveOptions` διατηρώντας την ίδια callback—το Aspose.Words θα την καλέσει για κάθε εικόνα ανεξάρτητα από τη μορφή. +- **Custom image naming:** Αν θέλετε να μετονομάσετε τις εικόνες (π.χ., `figure-01.png`), τροποποιήστε το `args.ResourceFileName` μέσα στην callback πριν γράψετε το αρχείο. +- **Embedding images as Base64:** Ορίστε το `args.ResourceFileName` σε ένα data URI (`data:image/png;base64,...`) και παραλείψτε τη γραφή του αρχείου. Αυτό είναι χρήσιμο για εξαγωγές markdown σε ένα μόνο αρχείο. + +--- + +## Conclusion + +Τώρα έχετε ένα πλήρως λειτουργικό πρόγραμμα C# που **converts Word to markdown**, **extracts images from word**, **creates a resources folder**, και εγγυάται ένα καθαρό **markdown image relative path** για κάθε εικόνα. Ο κώδικας είναι αυτόνομος, λειτουργεί με την πιο πρόσφατη έκδοση του Aspose.Words, και μπορεί να ενσωματωθεί σε οποιοδήποτε .NET project με ελάχιστη προσπάθεια. + +Τι επόμενα; Δοκιμάστε να τροφοδοτήσετε το παραγόμενο markdown σε έναν static site generator όπως Hugo ή Jekyll, ή πειραματιστείτε με την callback για ενσωμάτωση εικόνων απευθείας ως Base64. Αν συναντήσετε ειδικές περιπτώσεις—π.χ., SVG εικόνες ή εξαιρετικά μεγάλα αρχεία—ανατρέξτε πίσω στον πίνακα “Common pitfalls”; μια μικρή προσαρμογή συνήθως λύνει το πρόβλημα. + +Καλή προγραμματιστική, και ας δείχνουν πάντα οι markdown συνδέσεις σας στον σωστό φάκελο! + +{{< /blocks/products/pf/tutorial-page-section >}} +{{< /blocks/products/pf/main-container >}} +{{< /blocks/products/pf/main-wrap-class >}} +{{< blocks/products/products-backtop-button >}} \ No newline at end of file diff --git a/words/greek/net/programming-with-officemath/_index.md b/words/greek/net/programming-with-officemath/_index.md index d5fe27ad47..ea9a70af1c 100644 --- a/words/greek/net/programming-with-officemath/_index.md +++ b/words/greek/net/programming-with-officemath/_index.md @@ -23,6 +23,7 @@ | [Μαθηματικές Εξισώσεις](./math-equations/) | Μάθετε πώς να ρυθμίζετε μαθηματικές εξισώσεις σε έγγραφα Word χρησιμοποιώντας το Aspose.Words για .NET. Οδηγός βήμα προς βήμα με παραδείγματα, συχνές ερωτήσεις και πολλά άλλα. | | [Αποθήκευση docx ως txt – Εξαγωγή Word Math σε LaTeX με C#](./save-docx-as-txt-export-word-math-to-latex-with-c/) | Μάθετε πώς να μετατρέπετε αρχεία docx σε txt και να εξάγετε μαθηματικά Word σε LaTeX χρησιμοποιώντας C#. | | [Αποθήκευση Εγγράφου ως Txt – Εξαγωγή Μαθηματικών Word σε LaTeX σε C#](./save-document-as-txt-export-word-math-to-latex-in-c/) | Μάθετε πώς να αποθηκεύετε έγγραφα ως αρχείο κειμένου και να εξάγετε μαθηματικά Word σε LaTeX χρησιμοποιώντας C#. | +| [Μετατροπή DOCX σε TXT – Εξαγωγή Εξισώσεων Word σε LaTeX με C#](./convert-docx-to-txt-export-word-equations-to-latex-in-c/) | Μάθετε πώς να μετατρέπετε αρχεία DOCX σε TXT και να εξάγετε τις εξισώσεις Word σε LaTeX χρησιμοποιώντας C#. | {{< /blocks/products/pf/tutorial-page-section >}} diff --git a/words/greek/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/_index.md b/words/greek/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/_index.md new file mode 100644 index 0000000000..a9ee3390b7 --- /dev/null +++ b/words/greek/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/_index.md @@ -0,0 +1,212 @@ +--- +category: general +date: 2026-04-28 +description: Μετατρέψτε DOCX σε TXT και εξάγετε τις εξισώσεις του Word σε LaTeX χρησιμοποιώντας + το Aspose.Words. Μάθετε πώς να αποθηκεύετε το Word ως TXT και να διαχειρίζεστε αντικείμενα + μαθηματικών σε λίγα βήματα. +draft: false +keywords: +- convert docx to txt +- convert word equations to latex +- convert word to plain text +- save word as txt +- export equations as latex +language: el +og_description: Μετατρέψτε DOCX σε TXT και εξάγετε εξισώσεις Word σε LaTeX με ένα + απλό απόσπασμα C#. Πλήρης οδηγός, κώδικας και συμβουλές. +og_title: Μετατροπή DOCX σε TXT – Εξαγωγή εξισώσεων Word σε LaTeX +tags: +- C# +- Aspose.Words +- Document Conversion +title: Μετατροπή DOCX σε TXT – Εξαγωγή εξισώσεων Word σε LaTeX με C# +url: /el/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/ +--- + +{{< blocks/products/pf/main-wrap-class >}} +{{< blocks/products/pf/main-container >}} +{{< blocks/products/pf/tutorial-page-section >}} + +# Μετατροπή DOCX σε TXT – Εξαγωγή Εξισώσεων Word σε LaTeX + +Έχετε ποτέ χρειαστεί να **convert docx to txt** αλλά ανησυχείτε ότι τα μαθηματικά στο αρχείο Word σας θα μετατραπούν σε ακατάστατο μπέρδεμα; Δεν είστε μόνοι. Σε πολλά έργα μηχανικής ή ακαδημαϊκά, το αρχικό έγγραφο βρίσκεται σε .docx, ενώ τα επόμενα εργαλεία καταλαβαίνουν μόνο plain‑text ή LaTeX. Τα καλά νέα; Με μερικές γραμμές C# και Aspose.Words μπορείτε να **convert docx to txt** *και* να διατηρήσετε κάθε εξίσωση ως καθαρό κώδικα LaTeX. + +Σε αυτό το tutorial θα περάσουμε από όλη τη διαδικασία: φόρτωση ενός .docx, ρύθμιση των επιλογών αποθήκευσης ώστε τα αντικείμενα Office Math να γίνουν LaTeX, και τελικά εγγραφή του αποτελέσματος σε αρχείο .txt. Στο τέλος θα ξέρετε πώς να **save word as txt**, **convert word to plain text**, και **export equations as latex** χωρίς να ψάχνετε στα API docs. + +## Τι θα μάθετε + +- Οι ακριβείς κλήσεις API που απαιτούνται για **convert docx to txt** διατηρώντας τις εξισώσεις. +- Γιατί η επιλογή `OfficeMathExportMode.LaTeX` είναι η προτεινόμενη μέθοδος για **convert word equations to latex**. +- Πώς να αντιμετωπίσετε κοινές περιπτώσεις άκρων όπως ελλιπείς γραμματοσειρές ή μη υποστηριζόμενα χαρακτηριστικά εξίσωσης. +- Ένα πλήρες, έτοιμο‑για‑εκτέλεση πρόγραμμα C# που μπορείτε να ενσωματώσετε σε οποιοδήποτε έργο .NET. + +### Προαπαιτούμενα + +- .NET 6.0 ή νεότερο (ο κώδικας λειτουργεί επίσης σε .NET Framework 4.7+). +- Άδεια για Aspose.Words for .NET (η δωρεάν δοκιμή λειτουργεί για αξιολόγηση). +- Ένα έγγραφο Word (`input.docx`) που περιέχει τουλάχιστον ένα αντικείμενο Office Math. + +Αν τα έχετε, ας ξεκινήσουμε. + +## Βήμα 1: Εγκατάσταση Aspose.Words + +Πριν τρέξει οποιοσδήποτε κώδικας χρειάζεστε τη βιβλιοθήκη. Ανοίξτε ένα τερματικό στο φάκελο του έργου σας και εκτελέστε: + +```bash +dotnet add package Aspose.Words +``` + +Αυτό κατεβάζει την πιο πρόσφατη σταθερή έκδοση (ως 2026‑04‑28 v24.12). Δεν απαιτούνται επιπλέον DLLs. + +## Βήμα 2: Φόρτωση του Πηγαίου Εγγράφου + +Το πρώτο που κάνουμε είναι να διαβάσουμε το αρχείο .docx σε ένα αντικείμενο `Document`. Αυτό το αντικείμενο μας δίνει πλήρη πρόσβαση στη δομή του αρχείου, συμπεριλαμβανομένων των τμημάτων κειμένου, εικόνων και αντικειμένων μαθηματικών. + +```csharp +using Aspose.Words; +using Aspose.Words.Saving; + +class Program +{ + static void Main() + { + // Step 2: Load the source document + Document doc = new Document(@"YOUR_DIRECTORY\input.docx"); +``` + +> **Γιατί αυτό είναι σημαντικό:** Η φόρτωση του εγγράφου δημιουργεί μια αναπαράσταση στη μνήμη, ώστε αργότερα να μπορούμε να ρυθμίσουμε πώς κάθε στοιχείο γράφεται. Εάν το αρχείο δεν βρεθεί, το Aspose ρίχνει ένα `FileNotFoundException`, το οποίο ίσως θέλετε να πιάσετε σε κώδικα παραγωγής. + +## Βήμα 3: Ρύθμιση επιλογών αποθήκευσης TXT για LaTeX Math + +Από προεπιλογή, το `Document.Save` γράφει plain text και **απορρίπτει** οποιοδήποτε Office Math. Για να διατηρήσετε αυτές τις εξισώσεις, ορίζουμε το `OfficeMathExportMode` σε `LaTeX`. Αυτό λέει στον εξαγωγέα να μεταφράσει κάθε εξίσωση στην αντίστοιχη LaTeX. + +```csharp + // Step 3: Configure TXT save options to export Office Math as LaTeX + TxtSaveOptions txtOptions = new TxtSaveOptions + { + OfficeMathExportMode = OfficeMathExportMode.LaTeX, + // Optional: preserve line breaks as they appear in the original Word file + PreserveTableLayout = true + }; +``` + +> **Συμβουλή:** Αν χρειάζεστε μόνο τους ακατέργαστους χαρακτήρες Unicode της εξίσωσης (π.χ. για γρήγορη προεπισκόπηση), μπορείτε να χρησιμοποιήσετε `OfficeMathExportMode.Text`. Αλλά για τις περισσότερες επιστημονικές αλυσίδες, το `LaTeX` είναι το χρυσό πρότυπο επειδή είναι παγκοσμίως κατανοητό από τους επεξεργαστές LaTeX. + +## Βήμα 4: Αποθήκευση του Εγγράφου ως Plain‑Text + +Τώρα γράφουμε το μετασχηματισμένο περιεχόμενο σε ένα αρχείο `.txt`. Το αρχείο θα περιέχει κανονικές παραγράφους, κουκίδες, και—ευχαριστώντας το προηγούμενο βήμα—αποσπάσματα LaTeX για κάθε εξίσωση. + +```csharp + // Step 4: Save the document as plain‑text using the configured options + doc.Save(@"YOUR_DIRECTORY\Math.txt", txtOptions); + } +} +``` + +Όταν ανοίξετε το `Math.txt` θα δείτε κάτι σαν: + +``` +In this report we derive the quadratic formula: +\[ +x = \frac{-b \pm \sqrt{b^{2} - 4ac}}{2a} +\] + +The end. +``` + +Παρατηρήσατε τα όρια `\[` … `\]`; Αυτά είναι τα μπλοκ μαθηματικών LaTeX που δημιουργούνται αυτόματα. + +## Βήμα 5: Επαλήθευση του Αποτελέσματος (Προαιρετικό αλλά Συνιστώμενο) + +Είναι εύκολο να χάσετε ένα λεπτό πρόβλημα μετατροπής, ειδικά όταν οι εξισώσεις περιέχουν προσαρμοσμένα σύμβολα. Μια γρήγορη έλεγχος λογικής είναι να τροφοδοτήσετε το παραγόμενο `.txt` σε έναν LaTeX compiler (π.χ., `pdflatex`) και να δείτε αν μεταγλωττίζεται χωρίς σφάλματα. + +```bash +pdflatex -interaction=nonstopmode Math.txt +``` + +Αν η μεταγλώττιση πετύχει, έχετε αποτελεσματικά **convert word equations to latex** και **convert docx to txt** σε ένα βήμα. Αν αντιμετωπίσετε σφάλματα, ψάξτε για μηνύματα σχετικά με ακαθόριστες εντολές—αυτά συνήθως υποδεικνύουν μια λειτουργία εξίσωσης που το Aspose.Words δεν μπορεί να μεταφράσει (π.χ., ορισμένες σημειώσεις πινάκων). Σε τέτοιες περιπτώσεις, μπορείτε να επιστρέψετε στο `OfficeMathExportMode.MathML` και να επεξεργαστείτε το MathML σε LaTeX με άλλο εργαλείο. + +## Συνηθισμένες Παγίδες & Πώς να τις Αποφύγετε + +| Πρόβλημα | Γιατί συμβαίνει | Διόρθωση | +|----------|----------------|----------| +| Missing fonts | Το Aspose.Words χρειάζεται τη γραμματοσειρά για να αποδώσει σωστά τα σύμβολα. | Εγκαταστήστε τη λείπουσα γραμματοσειρά στον υπολογιστή ή ενσωματώστε την στο .docx. | +| Complex equations not exported | Ορισμένες νεότερες λειτουργίες Office Math δεν έχουν ακόμη αντιστοιχιστεί σε LaTeX. | Χρησιμοποιήστε `OfficeMathExportMode.MathML` και στη συνέχεια μετατρέψτε με μια βιβλιοθήκη MathML‑to‑LaTeX. | +| Extra blank lines | Ο αποθηκευτής plain‑text διατηρεί τα διαχωριστικά παραγράφων, που μπορεί να προσθέσει κενά. | Ορίστε `txtOptions.AddBidiMarks = false` ή επεξεργαστείτε το αρχείο με ένα απλό script. | + +## Πλήρες Παράδειγμα Εργασίας (Έτοιμο για Αντιγραφή‑Επικόλληση) + +Παρακάτω βρίσκεται ολόκληρο το πρόγραμμα, έτοιμο για μεταγλώττιση. Αντικαταστήστε το `YOUR_DIRECTORY` με το φάκελο που περιέχει το `input.docx`. + +```csharp +using System; +using Aspose.Words; +using Aspose.Words.Saving; + +namespace DocxToTxtWithLatex +{ + class Program + { + static void Main() + { + try + { + // Load the source document + Document doc = new Document(@"C:\Docs\input.docx"); + + // Configure save options: export equations as LaTeX + TxtSaveOptions txtOptions = new TxtSaveOptions + { + OfficeMathExportMode = OfficeMathExportMode.LaTeX, + PreserveTableLayout = true, + AddBidiMarks = false + }; + + // Save as plain‑text + string outputPath = @"C:\Docs\Math.txt"; + doc.Save(outputPath, txtOptions); + + Console.WriteLine($"Successfully converted DOCX to TXT. Output at: {outputPath}"); + } + catch (Exception ex) + { + Console.Error.WriteLine($"Conversion failed: {ex.Message}"); + } + } + } +} +``` + +Η εκτέλεση αυτού του προγράμματος θα **save word as txt** ενώ θα μετατρέπει κάθε μπλοκ Office Math σε LaTeX, παρέχοντάς σας ένα καθαρό, αναζητήσιμο αρχείο plain‑text. + +## Επόμενα Βήματα & Σχετικά Θέματα + +- **Batch conversion:** Τυλίξτε τη λογική παραπάνω σε έναν βρόχο `foreach` για να επεξεργαστείτε ολόκληρο φάκελο αρχείων .docx. +- **Combine with PDF generation:** Αφού έχετε τα αποσπάσματα LaTeX, τροφοδοτήστε τα σε μια αλυσίδα PDF (π.χ., `PdfSharp` + `MiKTeX`) για να δημιουργήσετε αναφορές PDF. +- **Export equations as latex** για άλλες μορφές: Το Aspose.Words υποστηρίζει επίσης `SaveFormat.Markdown`, το οποίο μπορεί να ενσωματώνει LaTeX αυτόματα. +- **Performance tuning:** Για τεράστια έγγραφα, επαναχρησιμοποιήστε το ίδιο αντικείμενο `TxtSaveOptions` και απενεργοποιήστε περιττές λειτουργίες όπως `AddBidiMarks`. + +--- + +### Παράδειγμα Εικόνας (Προαιρετικό) + +Αν προτιμάτε μια οπτική ένδειξη, εδώ είναι ένα στιγμιότυπο της εξόδου στο Notepad++. + +![convert docx to txt output showing LaTeX equations](convert-docx-to-txt-output.png) + +*(Alt text: “convert docx to txt output showing LaTeX equations” – satisfies the primary keyword requirement.)* + +--- + +## Συμπέρασμα + +Μόλις δείξαμε έναν αξιόπιστο τρόπο για **convert docx to txt** διατηρώντας κάθε εξίσωση ως καθαρό LaTeX. Το κλειδί είναι η σημαία `OfficeMathExportMode.LaTeX`, η οποία μετατρέπει τη ιδιόκτητη μορφή μαθηματικών του Word σε κάτι που καταλαβαίνει οποιαδήποτε μηχανή LaTeX. Με το πλήρες δείγμα κώδικα παραπάνω μπορείτε να **save word as txt**, **convert word to plain text**, και **export equations as latex** σε μια ενιαία, αυτόνομη εκτέλεση. + +Νιώστε ελεύθεροι να πειραματιστείτε—αλλάξτε την επέκταση εξόδου σε `.md` για Markdown, ή ενσωματώστε το απόσπασμα σε μια μεγαλύτερη αλυσίδα επεξεργασίας εγγράφων. Αν αντιμετωπίσετε οποιεσδήποτε ιδιαιτερότητες, αφήστε ένα σχόλιο παρακάτω· θα χαρώ να βοηθήσω στην επίλυση. + +Καλό προγραμματισμό! + +{{< /blocks/products/pf/tutorial-page-section >}} +{{< /blocks/products/pf/main-container >}} +{{< /blocks/products/pf/main-wrap-class >}} +{{< blocks/products/products-backtop-button >}} \ No newline at end of file diff --git a/words/hindi/net/ai-powered-document-processing/_index.md b/words/hindi/net/ai-powered-document-processing/_index.md index f79ec9c470..ecb7a44a49 100644 --- a/words/hindi/net/ai-powered-document-processing/_index.md +++ b/words/hindi/net/ai-powered-document-processing/_index.md @@ -42,6 +42,7 @@ | [गूगल AI मॉडल के साथ काम करना](./working-with-google-ai-model/) .NET और Google AI के लिए Aspose.Words के साथ अपने दस्तावेज़ प्रसंस्करण को उन्नत करें और आसानी से संक्षिप्त सारांश बनाएं। | | [ओपन एआई मॉडल के साथ काम करना](./working-with-open-ai-model/) | OpenAI के शक्तिशाली मॉडल के साथ .NET के लिए Aspose.Words का उपयोग करके कुशल दस्तावेज़ सारांश अनलॉक करें। अब इस व्यापक गाइड में गोता लगाएँ। | | [सारांश विकल्पों के साथ कार्य करना](./working-with-summarize-options/) | त्वरित जानकारी के लिए AI मॉडल को एकीकृत करने पर हमारे चरण-दर-चरण मार्गदर्शिका के साथ .NET के लिए Aspose.Words का उपयोग करके Word दस्तावेज़ों को प्रभावी ढंग से सारांशित करना सीखें। | +| [C# में स्थानीय LLM से कनेक्ट करें – पूर्ण प्रोग्रामिंग गाइड](./connect-to-local-llm-in-c-complete-programming-guide/) | C# में स्थानीय बड़े भाषा मॉडल को जोड़ने और उपयोग करने के लिए चरण-दर-चरण मार्गदर्शिका। | {{< /blocks/products/pf/tutorial-page-section >}} diff --git a/words/hindi/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/_index.md b/words/hindi/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/_index.md new file mode 100644 index 0000000000..7069eb1f2a --- /dev/null +++ b/words/hindi/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/_index.md @@ -0,0 +1,284 @@ +--- +category: general +date: 2026-04-28 +description: C# से स्थानीय LLM से कनेक्ट करें और बड़े भाषा मॉडल को वर्ड दस्तावेज़ + लोड करने के लिए प्रॉम्प्ट करें, स्थानीय LLM को कॉल करें और टेक्स्ट को स्वचालित रूप + से पुनः लिखें। चरण‑दर‑चरण कोड शामिल है। +draft: false +keywords: +- connect to local llm +- prompt large language model +- load word document +- call local llm +- rewrite text automatically +language: hi +og_description: C# से स्थानीय LLM से कनेक्ट करें और देखें कैसे बड़े भाषा मॉडल को प्रॉम्प्ट + करें, वर्ड दस्तावेज़ लोड करें, स्थानीय LLM को कॉल करें और मिनटों में स्वचालित रूप + से टेक्स्ट को पुनर्लेखित करें। +og_title: C# में स्थानीय LLM से कनेक्ट करें – पूर्ण प्रोग्रामिंग गाइड +tags: +- Aspose.Words +- C# +- LLM +- AI Automation +title: C# में स्थानीय LLM से कनेक्ट करें – पूर्ण प्रोग्रामिंग गाइड +url: /hi/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/ +--- + +{{< blocks/products/pf/main-wrap-class >}} +{{< blocks/products/pf/main-container >}} +{{< blocks/products/pf/tutorial-page-section >}} + +# C# में स्थानीय LLM से कनेक्ट करें – पूर्ण प्रोग्रामिंग गाइड + +क्या आपको कभी .NET एप्लिकेशन से **स्थानीय llm** से कनेक्ट करने की ज़रूरत पड़ी और सोचा कि इसे Word फ़ाइल से कैसे बात करवाई जाए? आप अकेले नहीं हैं। इस गाइड में हम पूरी प्रक्रिया को समझेंगे—स्थानीय llm से कनेक्ट करना, **prompt large language model**, Word दस्तावेज़ लोड करना, **call local llm**, और अंत में **rewrite text automatically**। अंत तक आपके पास एक चलाने योग्य नमूना होगा जो किसी भी पैराग्राफ को शून्य बाहरी API कुंजियों के साथ औपचारिक स्वर में बदल देगा। + +## इस ट्यूटोरियल में क्या कवर किया गया है + +हम आवश्यक NuGet पैकेज स्थापित करके शुरू करेंगे, फिर एक सरल स्थानीय LLM एंडपॉइंट (जैसे Ollama पोर्ट 11434 पर) चलाएंगे। उसके बाद हम Aspose.Words का उपयोग करके एक `.docx` फ़ाइल लोड करेंगे, पैराग्राफ को LLM को भेजेंगे, पुनर्लिखित संस्करण प्राप्त करेंगे, और उसे उसी दस्तावेज़ में वापस लिखेंगे। आप सामान्य समस्याओं—null पैराग्राफ, async डिस्पोज़ल, और एन्कोडिंग क्विर्क्स—को कैसे संभालें, भी देखेंगे—ताकि कोड प्रोडक्शन में काम करे, सिर्फ डेमो नहीं। + +### आवश्यकताएँ + +- .NET 6.0 SDK या बाद का (आप .NET 8 भी उपयोग कर सकते हैं) +- Visual Studio 2022 या VS Code के साथ C# एक्सटेंशन +- **Aspose.Words for .NET** (फ्री ट्रायल ठीक काम करता है) +- `/api/generate` कॉन्ट्रैक्ट को सपोर्ट करने वाला स्थानीय रूप से होस्ट किया गया LLM (जैसे, Ollama, LMStudio) +- C# में async/await की बुनियादी समझ + +> **Pro tip:** यदि आपने अभी तक Ollama स्थापित नहीं किया है, तो `ollama serve` चलाएँ और `ollama pull llama3` से मॉडल पुल करें। डिफ़ॉल्ट HTTP एंडपॉइंट `http://localhost:11434/api/generate` होगा। + +--- + +## चरण 1: आवश्यक पैकेज स्थापित करें + +सबसे पहले, अपने प्रोजेक्ट में Aspose.Words और Aspose.Words.AI NuGet पैकेज जोड़ें। + +```bash +dotnet add package Aspose.Words +dotnet add package Aspose.Words.AI +``` + +ये लाइब्रेरी हमें **load word document** क्षमता देती हैं और **call local llm** के लिए एक हल्का रैपर प्रदान करती हैं, बिना हाथ से HTTP अनुरोध लिखे। + +--- + +## चरण 2: स्थानीय LLM एंडपॉइंट से कनेक्ट करें + +स्थानीय रूप से होस्ट किए गए मॉडल से कनेक्ट करना उतना ही सरल है जितना `LocalLargeLanguageModel` को इंस्टैंशिएट करना। कंस्ट्रक्टर को जेनरेशन एंडपॉइंट का पूरा URL चाहिए। + +```csharp +using Aspose.Words.AI; +using Aspose.Words; +using System.Threading.Tasks; + +// Create a client that talks to the LLM running on localhost +var localLlm = new LocalLargeLanguageModel("http://localhost:11434/api/generate"); +``` + +हम एंडपॉइंट को क्लास में क्यों रैप करते हैं? `LocalLargeLanguageModel` आपके लिए JSON सीरियलाइज़ेशन, रीट्राईज़, और स्ट्रीमिंग रिस्पॉन्स को संभालता है—ताकि आप `HttpClient` के साथ झंझट किए बिना प्रॉम्प्ट लॉजिक पर ध्यान दे सकें। + +--- + +## चरण 3: स्रोत Word दस्तावेज़ लोड करें + +अब हम दस्तावेज़ को मेमोरी में लाते हैं। Aspose.Words लगभग सभी Word फ़ॉर्मेट को सपोर्ट करता है, इसलिए `Document` `input.docx` को बिना Office इंस्टॉल किए पार्स कर लेगा। + +```csharp +// Path to the source file – adjust as needed +string inputPath = Path.Combine(Environment.CurrentDirectory, "input.docx"); + +// Load the document; throws if the file is missing or corrupted +Document sourceDocument = new Document(inputPath); +``` + +यदि आपको स्ट्रीम के साथ काम करना है (जैसे, ASP.NET के माध्यम से अपलोड की गई फ़ाइल), तो फ़ाइल पाथ को `MemoryStream` से बदलें और उसे `Document` कंस्ट्रक्टर में पास करें। + +--- + +## चरण 4: वर्तमान पैराग्राफ टेक्स्ट निकालें + +हम दस्तावेज़ को नेविगेट करने के लिए `DocumentBuilder` का उपयोग करेंगे। इस उदाहरण में हम **पहले पैराग्राफ** को पुनर्लिखते हैं, लेकिन आप कई पैराग्राफ प्रोसेस करने के लिए `sourceDocument.GetChildNodes(NodeType.Paragraph, true)` पर इटरेट कर सकते हैं। + +```csharp +// Builder gives us a cursor inside the document +DocumentBuilder docBuilder = new DocumentBuilder(sourceDocument); + +// Grab the text of the paragraph where the builder is currently positioned +string originalParagraph = docBuilder.CurrentParagraph?.GetText() ?? string.Empty; + +// Safety check – avoid sending empty strings to the LLM +if (string.IsNullOrWhiteSpace(originalParagraph)) +{ + Console.WriteLine("No paragraph found at the current cursor position."); + return; +} +``` + +`?.` ऑपरेटर `NullReferenceException` को रोकता है यदि दस्तावेज़ खाली हो। यह उन **edge cases** में से एक है जो शुरुआती लोगों को फँसाते हैं। + +--- + +## चरण 5: पैराग्राफ को पुनर्लिखने के लिए LLM को प्रॉम्प्ट करें + +अब हम वास्तव में **prompt large language model** करते हैं। प्रॉम्प्ट साधारण अंग्रेज़ी में है; रैपर इसे JSON के रूप में स्थानीय एंडपॉइंट पर भेजेगा। + +```csharp +// Build a friendly instruction for the model +string prompt = $"Rewrite the following sentence in a more formal tone:\n{originalParagraph}"; + +// Await the model's response – this is an async call +string rewrittenParagraph = await localLlm.PromptAsync(prompt); +``` + +इस तरह अनुरोध को क्यों लिखते हैं? LLM स्पष्ट, एक‑कार्य निर्देशों पर सबसे अच्छा जवाब देते हैं। कॉलन के बाद नई लाइन जोड़ने से निर्देश और सामग्री अलग हो जाती है, जिससे मॉडल के प्रॉम्प्ट को दोहराने की संभावना कम हो जाती है। + +**Expected output** – यदि `originalParagraph` `"Hey, what's up?"` था, तो LLM इस तरह उत्तर दे सकता है: + +> “Good day, how may I assist you?” + +आप परिणाम को प्रिंट करके सत्यापित कर सकते हैं: + +```csharp +Console.WriteLine("Original: " + originalParagraph); +Console.WriteLine("Rewritten: " + rewrittenParagraph); +``` + +--- + +## चरण 6: पुनर्लिखित टेक्स्ट को दस्तावेज़ में वापस डालें + +नया टेक्स्ट हाथ में होने पर, हम पुराने पैराग्राफ को बदलते हैं। `DocumentBuilder.Writeln` नई लाइन लिखता है और कर्सर को आगे ले जाता है, जो जोड़ने के लिए उपयुक्त है। यदि आपको बिल्कुल वही पैराग्राफ *बदलना* है, तो लिखने से पहले `docBuilder.CurrentParagraph.RemoveAllChildren()` उपयोग कर सकते हैं। + +```csharp +// Option A – Append a new paragraph (keeps the original) +docBuilder.Writeln(rewrittenParagraph); + +// Option B – Replace the existing paragraph (uncomment to use) +// docBuilder.CurrentParagraph.RemoveAllChildren(); +// docBuilder.CurrentParagraph.AppendChild(new Run(docBuilder.Document, rewrittenParagraph)); +``` + +दोनों तरीकों को दिखाया गया है ताकि आप अपने वर्कफ़्लो के अनुसार चुन सकें। + +--- + +## चरण 7: अपडेटेड दस्तावेज़ को सेव करें + +अंत में, हम बदलावों को नई फ़ाइल में सहेजते हैं। Aspose.Words फ़ाइल एक्सटेंशन के आधार पर फ़ॉर्मेट स्वचालित रूप से चुन लेता है। + +```csharp +string outputPath = Path.Combine(Environment.CurrentDirectory, "output.docx"); +sourceDocument.Save(outputPath); + +Console.WriteLine($"Document saved to {outputPath}"); +``` + +`output.docx` को Word में खोलें, और आप देखेंगे कि पैराग्राफ अब औपचारिक स्वर में पढ़ा जाता है। + +--- + +## पूर्ण कार्यशील उदाहरण + +नीचे **पूर्ण, स्व-निहित प्रोग्राम** है। इसे कॉपी‑पेस्ट करके एक कंसोल प्रोजेक्ट में रखें, NuGet पैकेज रिस्टोर करें, और चलाएँ—स्थानीय LLM चल रहा हो तो अतिरिक्त कॉन्फ़िगरेशन की ज़रूरत नहीं। + +```csharp +using Aspose.Words.AI; +using Aspose.Words; +using System; +using System.IO; +using System.Threading.Tasks; + +class Program +{ + static async Task Main() + { + // ------------------------------------------------- + // Step 1: Connect to the locally hosted LLM endpoint + // ------------------------------------------------- + var localLlm = new LocalLargeLanguageModel("http://localhost:11434/api/generate"); + + // ------------------------------------------------- + // Step 2: Load the source Word document + // ------------------------------------------------- + string inputPath = Path.Combine(Environment.CurrentDirectory, "input.docx"); + Document sourceDocument = new Document(inputPath); + + // ------------------------------------------------- + // Step 3: Retrieve the text of the current paragraph + // ------------------------------------------------- + DocumentBuilder docBuilder = new DocumentBuilder(sourceDocument); + string originalParagraph = docBuilder.CurrentParagraph?.GetText() ?? string.Empty; + + if (string.IsNullOrWhiteSpace(originalParagraph)) + { + Console.WriteLine("No paragraph found at the current cursor position."); + return; + } + + // ------------------------------------------------- + // Step 4: Ask the LLM to rewrite the paragraph in a formal tone + // ------------------------------------------------- + string prompt = $"Rewrite the following sentence in a more formal tone:\n{originalParagraph}"; + string rewrittenParagraph = await localLlm.PromptAsync(prompt); + + // ------------------------------------------------- + // Step 5: Insert the rewritten text back into the document + // ------------------------------------------------- + docBuilder.Writeln(rewrittenParagraph); + + // ------------------------------------------------- + // Step 6: Save the updated document + // ------------------------------------------------- + string outputPath = Path.Combine(Environment.CurrentDirectory, "output.docx"); + sourceDocument.Save(outputPath); + + Console.WriteLine("Original paragraph:"); + Console.WriteLine(originalParagraph); + Console.WriteLine("\nRewritten paragraph:"); + Console.WriteLine(rewrittenParagraph); + Console.WriteLine($"\nDocument saved to {outputPath}"); + } +} +``` + +### चलाने पर क्या उम्मीद रखें + +1. कंसोल मूल और पुनर्लिखित पैराग्राफ प्रिंट करता है। +2. `output.docx` `input.docx` के बगल में दिखाई देता है। +3. फ़ाइल खोलने पर नया औपचारिक पैराग्राफ मूल के बाद (या यदि आप वैकल्पिक कोड पर स्विच किए हैं तो बदला हुआ) दिखता है। + +--- + +## सामान्य Edge Cases को संभालना + +| Situation | Solution | +|-----------|----------| +| **खाली या केवल व्हाइटस्पेस वाला पैराग्राफ** | `Prompt` करने से पहले `string.IsNullOrWhiteSpace` जांचें (Step 3 देखें)। | +| **LLM त्रुटि या खाली स्ट्रिंग लौटाता है** | `PromptAsync` को `try/catch` में रैप करें और मूल टेक्स्ट पर वापस जाएँ। | +| **कई पैराग्राफ को पुनर्लिखने की आवश्यकता** | `sourceDocument.GetChildNodes(NodeType.Paragraph, true)` पर लूप करें और वही प्रॉम्प्ट लॉजिक लागू करें। | +| **बड़े दस्तावेज़ लेटेंसी पैदा करते हैं** | पैराग्राफ को बैच करें और एक ही अनुरोध में भेजें (प्रॉम्प्ट प्रति कॉल अधिकतम 4 KB)। | +| **Non‑ASCII अक्षर गड़बड़ हो जाते हैं** | सुनिश्चित करें कि LLM एंडपॉइंट UTF‑8 उपयोग करता है (अधिकांश आधुनिक मॉडल ऐसा करते हैं)। | + +--- + +## अगले कदम और संबंधित विषय + +- **Prompt large language model** को अधिक विस्तृत निर्देशों के साथ उपयोग करें (जैसे, स्टाइल गाइड, लंबाई सीमा)। +- **call local llm** को वेब API में उपयोग करके दस्तावेज़‑ऑटोमेशन को सर्विस के रूप में एक्सपोज़ करें। +- **load word document** को समानांतर स्ट्रीम में एक्सप्लोर करें उच्च‑थ्रूपुट परिदृश्यों के लिए। +- इस दृष्टिकोण को **rewrite text automatically** के साथ मिलाकर बल्क ईमेल जेनरेशन या रिपोर्ट मानकीकरण के लिए उपयोग करें। + +यदि आप और गहराई में जाना चाहते हैं, तो Aspose की **document merging** दस्तावेज़ीकरण और कस्टम सैंपलिंग पैरामीटर के लिए Ollama API रेफ़रेंस देखें। + +--- + +## निष्कर्ष + +हमने अभी दिखाया कि कैसे **connect to local llm** को C# से, **prompt large language model**, **load word document**, **call local llm**, और **rewrite text automatically**—एक ही चलाने योग्य कंसोल ऐप में किया जाए। यह पैटर्न स्केलेबल है: प्रॉम्प्ट बदलें, पैराग्राफ पर इटरेट करें, या लॉजिक को ASP.NET एंडपॉइंट के माध्यम से एक्सपोज़ करें। मुख्य बात यह है कि स्थानीय AI मॉडल क्लासिक दस्तावेज़‑प्रोसेसिंग लाइब्रेरी के साथ कसकर इंटीग्रेट किए जा सकते हैं, जिससे आप भरोसेमंद ऑन‑प्रेम वातावरण से बाहर निकले बिना शक्तिशाली ऑटोमेशन प्राप्त कर सकते हैं। + +थ्रेडिंग के बारे में प्रश्न हैं, + +{{< /blocks/products/pf/tutorial-page-section >}} +{{< /blocks/products/pf/main-container >}} +{{< /blocks/products/pf/main-wrap-class >}} +{{< blocks/products/products-backtop-button >}} \ No newline at end of file diff --git a/words/hindi/net/programming-with-markdownsaveoptions/_index.md b/words/hindi/net/programming-with-markdownsaveoptions/_index.md index e783bae3f5..9157378553 100644 --- a/words/hindi/net/programming-with-markdownsaveoptions/_index.md +++ b/words/hindi/net/programming-with-markdownsaveoptions/_index.md @@ -31,24 +31,24 @@ MarkdownSaveOptions के साथ वर्ड प्रोसेसिंग | [DOCX को मार्कडाउन में सहेजें – LaTeX समीकरणों के साथ पूर्ण C# गाइड](./save-docx-as-markdown-complete-c-guide-with-latex-equations/) | C# में LaTeX समीकरणों के साथ DOCX को Markdown में बदलने की पूरी गाइड। | | [DOCX को मार्कडाउन के रूप में सहेजें – इमेज एक्सट्रैक्शन के साथ पूर्ण C# गाइड](./save-docx-as-markdown-full-c-guide-with-image-extraction/) | इमेज एक्सट्रैक्शन के साथ DOCX को Markdown में बदलने की पूरी गाइड। C# में चरण‑दर‑चरण प्रक्रिया। | | [DOCX को मार्कडाउन में सहेजें – LaTeX समीकरणों के साथ पूर्ण C# गाइड](./save-docx-as-markdown-complete-c-guide-with-latex-equations/) | C# में LaTeX समीकरणों के साथ DOCX को मार्कडाउन में बदलने की पूरी गाइड। | -| [DOCX को मार्कडाउन के रूप में सहेजें – इमेज एक्सट्रैक्शन के साथ पूर्ण C# गाइड](./save-docx-as-markdown-full-c-guide-with-image-extraction/) | इमेज एक्सट्रैक्शन के साथ DOCX को मार्कडाउन में बदलने की पूरी गाइड। C# में चरण‑दर‑चरण प्रक्रिया। | +| [DOCX को मार्कडाउन के रूप में सहेजें – इमेज एक्सट्रैक्शन के साथ पूर्ण C# गाइड](./save-docx-as-markdown-full-c-guide-with-image-extraction/) | इमेज एक्सट्रैक्शन के साथ DOCX को Markdown में बदलने की पूरी गाइड। C# में चरण‑दर‑चरण प्रक्रिया। | | [Word को Markdown में बदलें – इमेज एक्सट्रैक्शन के साथ पूर्ण C# गाइड](./convert-word-to-markdown-in-c-full-guide-with-image-extracti/) | इमेज एक्सट्रैक्शन के साथ Word को Markdown में बदलने की पूरी गाइड। C# में चरण‑दर‑चरण प्रक्रिया। | | [Word से Markdown निर्यात कैसे करें – पूर्ण C# गाइड](./how-to-export-markdown-from-word-complete-c-guide/) | Word दस्तावेज़ को Markdown में निर्यात करने की पूरी C# गाइड | | [DOCX को मार्कडाउन में सहेजें – LaTeX समीकरणों के साथ पूर्ण C# गाइड](./save-docx-as-markdown-complete-c-guide-with-latex-equations/) | C# में LaTeX समीकरणों के साथ DOCX को मार्कडाउन में बदलने की पूरी गाइड। | -| [DOCX को मार्कडाउन में सहेजें – इमेज एक्सट्रैक्शन के साथ पूर्ण C# गाइड](./save-docx-as-markdown-full-c-guide-with-image-extraction/) | इमेज एक्सट्रैक्शन के साथ DOCX को मार्कडाउन में बदलने की पूरी गाइड। C# में चरण‑दर‑चरण प्रक्रिया। | +| [DOCX को मार्कडाउन में सहेजें – इमेज एक्सट्रैक्शन के साथ पूर्ण C# गाइड](./save-docx-as-markdown-full-c-guide-with-image-extraction/) | इमेज एक्सट्रैक्शन के साथ DOCX को Markdown में बदलने की पूरी गाइड। C# में चरण‑दर‑चरण प्रक्रिया। | | [Word से Markdown निर्यात कैसे करें – पूर्ण C# गाइड](./how-to-export-markdown-from-word-complete-c-guide/) | Word दस्तावेज़ को Markdown में निर्यात करने की पूरी C# गाइड | | [DOCX को मार्कडाउन में बदलें – चरण‑दर‑चरण गाइड](./how-to-save-markdown-from-docx-step-by-step-guide/) | DOCX फ़ाइल को मार्कडाउन में बदलने की चरण‑दर‑चरण प्रक्रिया सीखें। | | [DOCX से Markdown निर्यात कैसे करें – पूरी गाइड](./how-to-export-markdown-from-docx-complete-guide/) | DOCX फ़ाइल को Markdown में निर्यात करने की विस्तृत चरण‑दर‑चरण गाइड। | | [Word से LaTeX निर्यात कैसे करें: Aspose के साथ DOCX को Markdown में बदलें](./how-to-export-latex-from-word-convert-docx-to-markdown-with/) | Aspose.Words का उपयोग करके Word दस्तावेज़ से LaTeX निर्यात करके DOCX को Markdown में परिवर्तित करने की चरण‑दर‑चरण गाइड। | | [Word को Markdown में बदलें – छवियों को Base64 के रूप में एम्बेड करें](./convert-word-to-markdown-embed-images-as-base64/) | .NET के लिए Aspose.Words का उपयोग करके Word दस्तावेज़ों को Markdown में बदलें और छवियों को Base64 एन्कोडेड रूप में एम्बेड करें। | -| [मार्कडाउन का उपयोग कैसे करें: LaTeX समीकरणों के साथ DOCX को मार्कडाउन में परिवर्तित करें](./how-to-use-markdown-convert-docx-to-markdown-with-latex-equa/) | DOCX फ़ाइलों को LaTeX समीकरणों के साथ मार्कडाउन में बदलने की चरण-दर-स्तर मार्गदर्शिका। | +| [मार्कडाउन का उपयोग कैसे करें: LaTeX समीकरणों के साथ DOCX को मार्कडाउन में परिवर्तित करें](./how-to-use-markdown-convert-docx-to-markdown-with-latex-equa/) | DOCX फ़ाइलों को LaTeX समीकरणों के साथ मार्कडाउन में बदलने की चरण‑दर‑स्तर मार्गदर्शिका। | | [DOCX को मार्कडाउन में बदलें – चरण‑दर‑चरण C# गाइड](./convert-docx-to-markdown-step-by-step-c-guide/) | DOCX फ़ाइलों को C# में चरण‑दर‑चरण मार्कडाउन में बदलने की गाइड। | | [Word से Markdown कैसे सहेजें – पूर्ण C# गाइड](./how-to-save-markdown-from-word-complete-c-guide/) | .NET के लिए Aspose.Words का उपयोग करके Word को Markdown में सहेजने की पूरी गाइड | | [Word से Markdown कैसे सहेजें – पूर्ण गाइड](./how-to-save-markdown-from-word-complete-guide/) | .NET के लिए Aspose.Words का उपयोग करके Word को Markdown में सहेजने की पूरी गाइड | | [LaTeX निर्यात कैसे करें: DOCX को Markdown और TXT में बदलें](./how-to-export-latex-convert-docx-to-markdown-txt/) | DOCX फ़ाइल को LaTeX के साथ Markdown और TXT फ़ॉर्मेट में निर्यात करने की चरण‑दर‑चरण गाइड। | | [DOCX को Markdown में बदलते समय छवियों का नाम कैसे बदलें](./how-to-rename-images-when-converting-docx-to-markdown/) | DOCX को Markdown में बदलते समय छवियों के नाम बदलने की प्रक्रिया और कोड उदाहरण। | | [Aspose.Words के साथ docx को markdown में सहेजें – पूर्ण C# गाइड](./save-docx-as-markdown-with-aspose-words-full-c-guide/) | Aspose.Words का उपयोग करके C# में docx को markdown में बदलने की पूरी गाइड | - +| [markdown इमेज रिलेटिव पाथ – वर्ड को मार्कडाउन में बदलें](./markdown-image-relative-path-convert-word-to-markdown/) | Word दस्तावेज़ को Markdown में बदलते समय इमेज के रिलेटिव पाथ को सेट करने का तरीका सीखें। | {{< /blocks/products/pf/tutorial-page-section >}} diff --git a/words/hindi/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/_index.md b/words/hindi/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/_index.md new file mode 100644 index 0000000000..9b8387c109 --- /dev/null +++ b/words/hindi/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/_index.md @@ -0,0 +1,206 @@ +--- +category: general +date: 2026-04-28 +description: जब आप Word को markdown में बदलते हैं, तो markdown इमेज का रिलेटिव पाथ + कैसे सेट करें, Word से इमेज निकालें, और एक्सपोर्ट की गई इमेज के लिए रिसोर्सेज फ़ोल्डर + बनाएं, यह सीखें। +draft: false +keywords: +- markdown image relative path +- convert word to markdown +- extract images from word +- create resources folder +- export images from docx +language: hi +og_description: Word को markdown में बदलते समय markdown इमेज का रिलेटिव पाथ सेट करें, + Word से इमेज निकालें, और एक्सपोर्ट की गई इमेज के लिए रिसोर्सेज फ़ोल्डर बनाएं। +og_title: मार्कडाउन छवि सापेक्ष पथ – वर्ड को मार्कडाउन में बदलें +tags: +- Aspose.Words +- C# +- Markdown +- Image Export +title: मार्कडाउन छवि सापेक्ष पथ – वर्ड को मार्कडाउन में बदलें +url: /hi/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/ +--- + +{{< blocks/products/pf/main-wrap-class >}} +{{< blocks/products/pf/main-container >}} +{{< blocks/products/pf/tutorial-page-section >}} + +# markdown image relative path – Word को Markdown में बदलें + +क्या आपको **markdown image relative path** की ज़रूरत कभी पड़ी है जब आप **Word को markdown में बदलते** हैं? आप अकेले नहीं हैं। अधिकांश डेवलपर्स को समस्या आती है जब उत्पन्न किया गया Markdown छवियों को एक फ्लैट फ़ोल्डर में इंगित करता है, जिससे स्थिर साइट या GitHub रेपो में आप जो सापेक्ष लिंक संरचना अपेक्षित करते हैं, वह टूट जाती है। + +इस ट्यूटोरियल में हम एक पूर्ण, एंड‑टू‑एंड समाधान के माध्यम से चलेंगे जो **Word से छवियों को निकालता है**, **एक resources फ़ोल्डर बनाता है**, और छवि रेफ़रेंसेज़ को इस तरह पुनः लिखता है कि वे एक साफ़ *markdown image relative path* का उपयोग करें। अंत तक आपके पास एक तैयार‑से‑प्रकाशित `.md` फ़ाइल और एक सुव्यवस्थित `Resources` डायरेक्टरी होगी जिसमें मूल `.docx` से निकाली गई हर तस्वीर होगी। + +> **आपको क्या मिलेगा:** एक एकल C# प्रोग्राम (कोई बाहरी स्क्रिप्ट नहीं), प्रत्येक भाग के *क्यों* महत्वपूर्ण होने की स्पष्ट व्याख्या, और कुछ व्यावहारिक टिप्स जिन्हें आप अपने प्रोजेक्ट्स में कॉपी‑पेस्ट कर सकते हैं। + +--- + +## Prerequisites + +कोड में डुबने से पहले सुनिश्चित करें कि आपके पास है: + +- **.NET 6.0** या बाद का संस्करण स्थापित हो (आप .NET Framework 4.7+ को भी टार्गेट कर सकते हैं, लेकिन नए प्रोजेक्ट्स के लिए .NET 6 सबसे उपयुक्त है)। +- **Aspose.Words for .NET** (लेखन के समय उपलब्ध नवीनतम NuGet पैकेज, संस्करण 23.12)। इसे इस प्रकार स्थापित करें: + ```bash + dotnet add package Aspose.Words + ``` +- एक Word दस्तावेज़ जिसमें वास्तव में छवियां हों—इसे हम `WithImages.docx` कहेंगे। +- एक फ़ोल्डर जहाँ आप आउटपुट markdown और छवियों को रखना चाहते हैं, उदाहरण के लिए `C:\Projects\MarkdownExport`। + +कोई अतिरिक्त लाइब्रेरी आवश्यक नहीं है; बाकी सब Aspose.Words द्वारा संभाला जाता है। + +--- + +## Step 1: Load the source Word document (the starting point for convert word to markdown) + +```csharp +using System; +using System.IO; +using Aspose.Words; +using Aspose.Words.Saving; + +class Program +{ + static void Main() + { + // Adjust the path to point at your own .docx file. + string sourcePath = @"C:\Projects\MarkdownExport\WithImages.docx"; + + // Load the document – this is where Aspose.Words parses the Word file. + Document doc = new Document(sourcePath); + + // The rest of the workflow follows… + } +} +``` + +*Why this matters:* दस्तावेज़ को लोड करने से हमें आंतरिक नोड ट्री तक पहुंच मिलती है, जिसमें वे इमेज पार्ट्स शामिल होते हैं जिन्हें बाद में **export images from docx** करने की आवश्यकता होती है। यदि लोड विफल हो जाता है, तो बाद के कोई भी चरण नहीं चलेंगे, इसलिए पथ और फ़ाइल अनुमतियों की दोबारा जाँच करें। + +--- + +## Step 2: Configure `MarkdownSaveOptions` with a custom callback (the heart of create resources folder) + +`ResourceSavingCallback` हमें प्रत्येक बार जब Aspose.Words कोई इमेज फ़ाइल लिखना चाहता है, हस्तक्षेप करने की अनुमति देता है। कॉलबैक के भीतर हम **Resources सब‑फ़ोल्डर** बनाएंगे और रेफ़रेंस को इस तरह समायोजित करेंगे कि उत्पन्न markdown एक *markdown image relative path* का उपयोग करे। + +```csharp +// Inside Main(), after loading the document: +string outputFolder = @"C:\Projects\MarkdownExport"; +string resourcesFolder = Path.Combine(outputFolder, "Resources"); + +// Make sure the folder exists before we start saving anything. +Directory.CreateDirectory(resourcesFolder); + +// Set up the Markdown save options. +MarkdownSaveOptions mdOptions = new MarkdownSaveOptions +{ + // Hook that runs for every image resource. + ResourceSavingCallback = new MyMarkdownResourceCallback(resourcesFolder) +}; + +// Save the document as Markdown. +string markdownPath = Path.Combine(outputFolder, "Doc.md"); +doc.Save(markdownPath, mdOptions); +``` + +ध्यान दें कि हमने `resourcesFolder` को कॉलबैक के कन्स्ट्रक्टर में पास किया है—यह फ़ोल्डर पथ को लचीला रखता है और कोड में स्ट्रिंग्स को हार्ड‑कोड करने से बचाता है। + +--- + +## Step 3: Implement the callback that **creates resources folder** and rewrites the path + +```csharp +/// +/// Handles image extraction and path rewriting for markdown export. +/// +class MyMarkdownResourceCallback : IResourceSavingCallback +{ + private readonly string _resourcesFolder; + + public MyMarkdownResourceCallback(string resourcesFolder) + { + _resourcesFolder = resourcesFolder; + } + + public void ResourceSaving(ResourceSavingArgs args) + { + // 1️⃣ Build the full file system path where the image will be stored. + string targetPath = Path.Combine(_resourcesFolder, args.ResourceFileName); + + // 2️⃣ Ensure the directory exists (in case Aspose creates sub‑folders). + Directory.CreateDirectory(Path.GetDirectoryName(targetPath)); + + // 3️⃣ Write the image stream to disk. + using (FileStream fileStream = File.Create(targetPath)) + { + args.Stream.CopyTo(fileStream); + } + + // 4️⃣ Update the markdown reference to use a relative path. + // This is the crucial line that gives us the markdown image relative path. + args.ResourceFileName = Path.Combine("Resources", args.ResourceFileName); + } +} +``` + +*Why this works:* `args.Stream` में कच्चे इमेज बाइट्स होते हैं। इसे हमारे `Resources` फ़ोल्डर के अंदर किसी फ़ाइल में कॉपी करके हम **export images from docx** सुरक्षित रूप से कर लेते हैं। फिर हम `args.ResourceFileName` को एक सापेक्ष URL (`Resources/image.png`) से बदल देते हैं। जब Aspose.Words बाद में markdown लिखता है, तो वह ठीक वही स्ट्रिंग डालता है, जिससे हमें वांछित *markdown image relative path* मिल जाता है। + +--- + +## Step 4: Verify the generated Markdown (what the final output looks like) + +`Doc.md` को किसी भी टेक्स्ट एडिटर में खोलें। आपको कुछ इस तरह दिखना चाहिए: + +```markdown +# Sample Heading + +Here is an inline picture: + +![Image 0](Resources/Image_0.png) + +And a picture inside a table: + +![Image 1](Resources/Image_1.jpg) +``` + +महत्वपूर्ण बात यह है कि प्रत्येक इमेज रेफ़रेंस `Resources/...` की ओर इशारा करता है – यही वह **markdown image relative path** है जिसकी हमें तलाश थी। + +![markdown image relative path example](example.png "markdown image relative path example") + +*Tip:* यदि आप markdown को ऐसे व्यूअर में खोलते हैं जो सापेक्ष लिंक को सपोर्ट करता है (VS Code प्रीव्यू, GitHub, या कोई स्थिर साइट जेनरेटर), तो तस्वीरें बिना किसी अतिरिक्त कॉन्फ़िगरेशन के सही ढंग से रेंडर होंगी। + +--- + +## Step 5: Common pitfalls and pro‑tips + +| Issue | Why it happens | How to fix it | +|-------|----------------|---------------| +| Images end up in the root folder instead of `Resources` | कॉलबैक संलग्न नहीं था या `args.ResourceFileName` को ओवरराइट नहीं किया गया। | सुनिश्चित करें कि `ResourceSavingCallback` को `doc.Save` कॉल करने **से पहले** सेट किया गया हो। | +| Filenames contain illegal characters | Word कभी‑कभी छवियों को स्पेस या यूनिकोड प्रतीकों के साथ नाम देता है। | कॉलबैक के भीतर `args.ResourceFileName` को साफ़ करने के लिए `Path.GetInvalidFileNameChars()` का उपयोग करें। | +| Large documents take a long time to process | प्रत्येक इमेज सिंक्रोनस रूप से लिखी जाती है। | यदि आप .NET 6+ पर हैं और प्रदर्शन की आवश्यकता है, तो असिंक्रोनस I/O (`await args.Stream.CopyToAsync(fileStream)`) पर स्विच करें। | +| Relative paths break when the markdown is moved | पथ markdown फ़ाइल के स्थान के सापेक्ष होता है। | `Doc.md` और `Resources` फ़ोल्डर को साथ रखें, या कॉलबैक को इस प्रकार समायोजित करें कि अलग सापेक्ष प्रीफ़िक्स (जैसे `../assets`) उपयोग हो। | + +--- + +## Step 6: Extending the solution (what if you need more control?) + +- **Multiple output formats:** `MarkdownSaveOptions` को `HtmlSaveOptions` या `PdfSaveOptions` से बदलें जबकि वही कॉलबैक रखें—Aspose.Words फ़ॉर्मेट चाहे जो भी हो, प्रत्येक इमेज के लिए इसे कॉल करेगा। +- **Custom image naming:** यदि आप छवियों का नाम बदलना चाहते हैं (उदा., `figure-01.png`), तो फ़ाइल लिखने से पहले `args.ResourceFileName` को संशोधित करें। +- **Embedding images as Base64:** `args.ResourceFileName` को एक डेटा URI (`data:image/png;base64,...`) सेट करें और फ़ाइल लिखना छोड़ दें। यह एकल‑फ़ाइल markdown निर्यात के लिए उपयोगी है। + +--- + +## Conclusion + +अब आपके पास एक पूरी तरह कार्यात्मक C# प्रोग्राम है जो **Word को markdown में बदलता है**, **word से छवियों को निकालता है**, **एक resources फ़ोल्डर बनाता है**, और हर तस्वीर के लिए साफ़ **markdown image relative path** सुनिश्चित करता है। कोड स्वयं‑समाहित है, नवीनतम Aspose.Words संस्करण के साथ काम करता है, और किसी भी .NET प्रोजेक्ट में न्यूनतम प्रयास से डाला जा सकता है। + +अगले कदम? उत्पन्न markdown को Hugo या Jekyll जैसे स्थिर साइट जेनरेटर में फीड करें, या कॉलबैक को इस प्रकार प्रयोग करें कि छवियों को सीधे Base64 स्ट्रिंग्स के रूप में एम्बेड किया जाए। यदि आप किनारे के मामलों—जैसे SVG छवियां या अत्यधिक बड़ी फ़ाइलें—से मिलते हैं, तो “Common pitfalls” तालिका को देखें; एक छोटा सा बदलाव आमतौर पर समस्या हल कर देता है। + +Happy coding, and may your markdown always point to the right folder! + +{{< /blocks/products/pf/tutorial-page-section >}} +{{< /blocks/products/pf/main-container >}} +{{< /blocks/products/pf/main-wrap-class >}} +{{< blocks/products/products-backtop-button >}} \ No newline at end of file diff --git a/words/hindi/net/programming-with-officemath/_index.md b/words/hindi/net/programming-with-officemath/_index.md index 0cfb7da032..a9338022c5 100644 --- a/words/hindi/net/programming-with-officemath/_index.md +++ b/words/hindi/net/programming-with-officemath/_index.md @@ -23,7 +23,7 @@ OfficeMath के साथ वर्ड प्रोसेसिंग पर A | [गणित समीकरण](./math-equations/) | .NET के लिए Aspose.Words का उपयोग करके Word दस्तावेज़ों में गणितीय समीकरणों को कॉन्फ़िगर करना सीखें। उदाहरणों, अक्सर पूछे जाने वाले प्रश्नों और अधिक के साथ चरण-दर-चरण मार्गदर्शिका। | | [docx को txt के रूप में सहेजें – C# के साथ Word Math को LaTeX में निर्यात करें](./save-docx-as-txt-export-word-math-to-latex-with-c/) | C# का उपयोग करके docx फ़ाइल को txt में सहेजें और Word Math को LaTeX फ़ॉर्मेट में निर्यात करना सीखें। | | [डॉक्यूमेंट को Txt के रूप में सहेजें – C# में Word Math को LaTeX में निर्यात करें](./save-document-as-txt-export-word-math-to-latex-in-c/) | C# में Aspose.Words का उपयोग करके Word दस्तावेज़ को TXT के रूप में सहेजें और गणितीय समीकरणों को LaTeX में निर्यात करना सीखें। | - +| [DOCX को TXT में बदलें – C# में Word समीकरणों को LaTeX में निर्यात करें](./convert-docx-to-txt-export-word-equations-to-latex-in-c/) | C# का उपयोग करके DOCX फ़ाइल को TXT में बदलें और Word समीकरणों को LaTeX फ़ॉर्मेट में निर्यात करना सीखें। | {{< /blocks/products/pf/tutorial-page-section >}} diff --git a/words/hindi/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/_index.md b/words/hindi/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/_index.md new file mode 100644 index 0000000000..efce17b6b5 --- /dev/null +++ b/words/hindi/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/_index.md @@ -0,0 +1,212 @@ +--- +category: general +date: 2026-04-28 +description: Aspose.Words का उपयोग करके DOCX को TXT में बदलें और Word समीकरणों को + LaTeX में निर्यात करें। कुछ चरणों में Word को TXT के रूप में सहेजना और गणितीय वस्तुओं + को संभालना सीखें। +draft: false +keywords: +- convert docx to txt +- convert word equations to latex +- convert word to plain text +- save word as txt +- export equations as latex +language: hi +og_description: एक सरल C# स्निपेट के साथ DOCX को TXT में बदलें और Word समीकरणों को + LaTeX में निर्यात करें। पूर्ण गाइड, कोड, और टिप्स। +og_title: DOCX को TXT में बदलें – वर्ड समीकरणों को LaTeX में निर्यात करें +tags: +- C# +- Aspose.Words +- Document Conversion +title: DOCX को TXT में बदलें – C# में Word समीकरणों को LaTeX में निर्यात करें +url: /hi/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/ +--- + +{{< blocks/products/pf/main-wrap-class >}} +{{< blocks/products/pf/main-container >}} +{{< blocks/products/pf/tutorial-page-section >}} + +# DOCX को TXT में बदलें – Word समीकरणों को LaTeX में निर्यात करें + +क्या आपको कभी **docx को txt में बदलने** की ज़रूरत पड़ी है लेकिन इस बात की चिंता थी कि आपके Word फ़ाइल में मौजूद गणितीय समीकरण गड़बड़ हो जाएंगे? आप अकेले नहीं हैं। कई इंजीनियरिंग या शैक्षणिक प्रोजेक्ट्स में स्रोत दस्तावेज़ .docx में रहता है, जबकि डाउनस्ट्रीम टूल्स केवल plain‑text या LaTeX को समझते हैं। अच्छी खबर? कुछ ही पंक्तियों के C# और Aspose.Words के साथ आप **docx को txt में बदल सकते** हैं *और* हर समीकरण को साफ़ LaTeX कोड के रूप में रख सकते हैं। + +इस ट्यूटोरियल में हम पूरी प्रक्रिया को चरण‑दर‑चरण देखेंगे: .docx को लोड करना, सेव विकल्पों को इस तरह कॉन्फ़िगर करना कि Office Math ऑब्जेक्ट्स LaTeX बन जाएँ, और अंत में परिणाम को .txt फ़ाइल में लिखना। अंत तक आप जानेंगे कि **word को txt के रूप में सेव करें**, **word को plain text में बदलें**, और **समीकरणों को latex में निर्यात करें** बिना API दस्तावेज़ों में घुसे। + +## आप क्या सीखेंगे + +- वह सटीक API कॉल्स जो **docx को txt में बदलते** हुए समीकरणों को संरक्षित रखते हैं। +- क्यों `OfficeMathExportMode.LaTeX` चुनना **word समीकरणों को latex में बदलने** का अनुशंसित तरीका है। +- सामान्य किनारी मामलों को कैसे संभालें जैसे कि गायब फ़ॉन्ट्स या असमर्थित समीकरण सुविधाएँ। +- एक पूर्ण, तैयार‑to‑run C# प्रोग्राम जिसे आप किसी भी .NET प्रोजेक्ट में डाल सकते हैं। + +### आवश्यकताएँ + +- .NET 6.0 या बाद का (कोड .NET Framework 4.7+ पर भी काम करता है)। +- Aspose.Words for .NET का लाइसेंस (मुफ़्त ट्रायल मूल्यांकन के लिए पर्याप्त है)। +- एक Word दस्तावेज़ (`input.docx`) जिसमें कम से कम एक Office Math ऑब्जेक्ट हो। + +यदि आपके पास ये सब है, तो चलिए शुरू करते हैं। + +## चरण 1: Aspose.Words स्थापित करें + +कोड चलाने से पहले आपको लाइब्रेरी चाहिए। अपने प्रोजेक्ट फ़ोल्डर में टर्मिनल खोलें और चलाएँ: + +```bash +dotnet add package Aspose.Words +``` + +यह नवीनतम स्थिर संस्करण (2026‑04‑28 v24.12) को डाउनलोड करता है। अतिरिक्त DLLs की आवश्यकता नहीं है। + +## चरण 2: स्रोत दस्तावेज़ लोड करें + +सबसे पहले हम .docx फ़ाइल को एक `Document` ऑब्जेक्ट में पढ़ते हैं। यह ऑब्जेक्ट फ़ाइल की पूरी संरचना, जैसे टेक्स्ट रन, इमेज और गणितीय ऑब्जेक्ट्स, तक पहुंच प्रदान करता है। + +```csharp +using Aspose.Words; +using Aspose.Words.Saving; + +class Program +{ + static void Main() + { + // Step 2: Load the source document + Document doc = new Document(@"YOUR_DIRECTORY\input.docx"); +``` + +> **यह क्यों महत्वपूर्ण है:** दस्तावेज़ को मेमोरी में लोड करने से बाद में हम प्रत्येक तत्व को कैसे लिखना है, इसे समायोजित कर सकते हैं। यदि फ़ाइल नहीं मिली, तो Aspose `FileNotFoundException` फेंकेगा, जिसे आप प्रोडक्शन कोड में पकड़ना चाहेंगे। + +## चरण 3: LaTeX गणित के लिए TXT सेव विकल्प कॉन्फ़िगर करें + +डिफ़ॉल्ट रूप से, `Document.Save` plain text लिखता है और **Office Math** को हटा देता है। उन समीकरणों को रखने के लिए हम `OfficeMathExportMode` को `LaTeX` सेट करते हैं। यह निर्यातक को प्रत्येक समीकरण को उसके LaTeX समकक्ष में बदलने को कहता है। + +```csharp + // Step 3: Configure TXT save options to export Office Math as LaTeX + TxtSaveOptions txtOptions = new TxtSaveOptions + { + OfficeMathExportMode = OfficeMathExportMode.LaTeX, + // Optional: preserve line breaks as they appear in the original Word file + PreserveTableLayout = true + }; +``` + +> **प्रो टिप:** यदि आपको केवल समीकरण के कच्चे Unicode अक्षर चाहिए (जैसे त्वरित प्रीव्यू के लिए), तो आप `OfficeMathExportMode.Text` उपयोग कर सकते हैं। लेकिन अधिकांश वैज्ञानिक पाइपलाइन में, `LaTeX` स्वर्ण मानक है क्योंकि यह सभी LaTeX प्रोसेसर द्वारा सार्वभौमिक रूप से समझा जाता है। + +## चरण 4: दस्तावेज़ को Plain‑Text के रूप में सेव करें + +अब हम परिवर्तित सामग्री को `.txt` फ़ाइल में लिखते हैं। फ़ाइल में सामान्य पैराग्राफ, बुलेट पॉइंट, और—पिछले चरण के धन्यवाद—हर समीकरण के लिए LaTeX स्निपेट्स होंगे। + +```csharp + // Step 4: Save the document as plain‑text using the configured options + doc.Save(@"YOUR_DIRECTORY\Math.txt", txtOptions); + } +} +``` + +जब आप `Math.txt` खोलेंगे तो आपको कुछ इस तरह दिखेगा: + +``` +In this report we derive the quadratic formula: +\[ +x = \frac{-b \pm \sqrt{b^{2} - 4ac}}{2a} +\] + +The end. +``` + +ध्यान दें `\[` … `\]` डिलिमिटर? ये वह LaTeX गणित ब्लॉक हैं जो स्वचालित रूप से उत्पन्न होते हैं। + +## चरण 5: आउटपुट की जाँच करें (वैकल्पिक लेकिन अनुशंसित) + +समीकरणों में कस्टम प्रतीकों के कारण सूक्ष्म रूपांतरण समस्याएँ अक्सर छूट जाती हैं। एक त्वरित सत्यापन यह है कि उत्पन्न `.txt` को LaTeX कंपाइलर (जैसे `pdflatex`) में फीड करें और देखें कि क्या बिना त्रुटियों के कंपाइल होता है। + +```bash +pdflatex -interaction=nonstopmode Math.txt +``` + +यदि कंपाइल सफल हो जाता है, तो आपने प्रभावी रूप से **word समीकरणों को latex में बदल दिया** और **docx को txt में बदल दिया** एक ही बार में। यदि त्रुटियाँ आती हैं, तो अनिर्धारित कमांड्स के बारे में संदेश देखें—ये आमतौर पर उन समीकरण सुविधाओं को दर्शाते हैं जिन्हें Aspose.Words अनुवाद नहीं कर सकता (जैसे कुछ मैट्रिक्स नोटेशन)। ऐसे मामलों में आप `OfficeMathExportMode.MathML` पर वापस जा सकते हैं और फिर किसी अन्य टूल से MathML को LaTeX में परिवर्तित कर सकते हैं। + +## सामान्य समस्याएँ और समाधान + +| समस्या | क्यों होता है | समाधान | +|-------|----------------|-----| +| फ़ॉन्ट्स गायब | Aspose.Words को प्रतीकों को सही ढंग से रेंडर करने के लिए फ़ॉन्ट चाहिए। | मशीन पर गायब फ़ॉन्ट इंस्टॉल करें या उसे .docx में एम्बेड करें। | +| जटिल समीकरण निर्यात नहीं होते | कुछ नए Office Math फीचर अभी तक LaTeX में मैप नहीं हुए हैं। | `OfficeMathExportMode.MathML` उपयोग करें फिर MathML‑to‑LaTeX लाइब्रेरी से बदलें। | +| अतिरिक्त खाली लाइनें | Plain‑text सेवकर्ता पैराग्राफ ब्रेक को संरक्षित रखता है, जिससे व्हाइटस्पेस बढ़ सकता है। | `txtOptions.AddBidiMarks = false` सेट करें या सरल स्क्रिप्ट से फ़ाइल को पोस्ट‑प्रोसेस करें। | + +## पूर्ण कार्यशील उदाहरण (कॉपी‑पेस्ट तैयार) + +नीचे पूरा प्रोग्राम दिया गया है, जिसे आप सीधे कंपाइल कर सकते हैं। `YOUR_DIRECTORY` को उस फ़ोल्डर से बदलें जहाँ आपका `input.docx` स्थित है। + +```csharp +using System; +using Aspose.Words; +using Aspose.Words.Saving; + +namespace DocxToTxtWithLatex +{ + class Program + { + static void Main() + { + try + { + // Load the source document + Document doc = new Document(@"C:\Docs\input.docx"); + + // Configure save options: export equations as LaTeX + TxtSaveOptions txtOptions = new TxtSaveOptions + { + OfficeMathExportMode = OfficeMathExportMode.LaTeX, + PreserveTableLayout = true, + AddBidiMarks = false + }; + + // Save as plain‑text + string outputPath = @"C:\Docs\Math.txt"; + doc.Save(outputPath, txtOptions); + + Console.WriteLine($"Successfully converted DOCX to TXT. Output at: {outputPath}"); + } + catch (Exception ex) + { + Console.Error.WriteLine($"Conversion failed: {ex.Message}"); + } + } + } +} +``` + +इस प्रोग्राम को चलाने से **word को txt के रूप में सेव** होगा और हर Office Math ब्लॉक LaTeX में बदल जाएगा, जिससे आपको एक साफ़, खोज योग्य plain‑text फ़ाइल मिलेगी। + +## अगले कदम और संबंधित विषय + +- **बैच रूपांतरण:** ऊपर दिए गए लॉजिक को `foreach` लूप में रखें ताकि पूरे फ़ोल्डर की .docx फ़ाइलें प्रोसेस हो सकें। +- **PDF जनरेशन के साथ संयोजन:** LaTeX स्निपेट्स मिलने के बाद उन्हें PDF पाइपलाइन (जैसे `PdfSharp` + `MiKTeX`) में फीड करके PDF रिपोर्ट बनाएं। +- **अन्य फ़ॉर्मेट्स के लिए समीकरण निर्यात:** Aspose.Words `SaveFormat.Markdown` को भी सपोर्ट करता है, जो स्वचालित रूप से LaTeX एम्बेड कर सकता है। +- **परफ़ॉर्मेंस ट्यूनिंग:** बड़े दस्तावेज़ों के लिए वही `TxtSaveOptions` इंस्टेंस पुन: उपयोग करें और `AddBidiMarks` जैसी अनावश्यक सुविधाओं को बंद करें। + +--- + +### छवि उदाहरण (वैकल्पिक) + +यदि आप दृश्य संकेत पसंद करते हैं, तो यहाँ Notepad++ में आउटपुट फ़ाइल का स्क्रीनशॉट है। + +![convert docx to txt आउटपुट जिसमें LaTeX समीकरण दिखाए गए हैं](convert-docx-to-txt-output.png) + +*(Alt text: “convert docx to txt आउटपुट जिसमें LaTeX समीकरण दिखाए गए हैं” – मुख्य कीवर्ड आवश्यकता को पूरा करता है।)* + +--- + +## निष्कर्ष + +हमने अभी एक भरोसेमंद तरीका दिखाया है जिससे **docx को txt में बदला** जा सकता है जबकि हर समीकरण को साफ़ LaTeX के रूप में संरक्षित रखा जाता है। मुख्य बात `OfficeMathExportMode.LaTeX` फ़्लैग है, जो Word के स्वामित्व वाले गणित फ़ॉर्मेट को किसी भी LaTeX इंजन द्वारा समझे जाने योग्य रूप में बदल देता है। ऊपर दिया गया पूर्ण कोड नमूना आपको **word को txt के रूप में सेव**, **word को plain text में बदल**, और **समीकरणों को latex में निर्यात** करने में मदद करेगा, वह भी एक ही स्व-निहित रन में। + +इसे आज़माएँ—आउटपुट एक्सटेंशन को `.md` बदलें ताकि Markdown बन जाए, या इस स्निपेट को बड़े दस्तावेज़‑प्रोसेसिंग पाइपलाइन में एकीकृत करें। यदि कोई अजीब बात मिले, तो नीचे टिप्पणी छोड़ें; मैं मदद करने के लिए तैयार हूँ। + +कोडिंग का आनंद लें! + +{{< /blocks/products/pf/tutorial-page-section >}} +{{< /blocks/products/pf/main-container >}} +{{< /blocks/products/pf/main-wrap-class >}} +{{< blocks/products/products-backtop-button >}} \ No newline at end of file diff --git a/words/hongkong/net/ai-powered-document-processing/_index.md b/words/hongkong/net/ai-powered-document-processing/_index.md index 735f28b914..ec444c7e73 100644 --- a/words/hongkong/net/ai-powered-document-processing/_index.md +++ b/words/hongkong/net/ai-powered-document-processing/_index.md @@ -42,6 +42,7 @@ | [使用 Google AI 模型](./working-with-google-ai-model/) |使用 Aspose.Words for .NET 和 Google AI 提升您的文件處理能力,輕鬆建立簡潔的摘要。 | | [使用開放的人工智慧模型](./working-with-open-ai-model/) |使用 Aspose.Words for .NET 和 OpenAI 強大的模型實現高效的文檔摘要。立即深入了解這份綜合指南。 | | [使用匯總選項](./working-with-summarize-options/) |透過我們關於整合 AI 模型以獲得快速洞察的逐步指南,學習使用 Aspose.Words for .NET 有效地總結 Word 文件。 | +| [在 C# 中連接本地 LLM – 完整程式指南](./connect-to-local-llm-in-c-complete-programming-guide/) |學習如何在 C# 應用程式中連接本地大型語言模型,提供完整步驟與範例代碼,提升離線 AI 能力。 | {{< /blocks/products/pf/tutorial-page-section >}} diff --git a/words/hongkong/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/_index.md b/words/hongkong/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/_index.md new file mode 100644 index 0000000000..878e09c583 --- /dev/null +++ b/words/hongkong/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/_index.md @@ -0,0 +1,280 @@ +--- +category: general +date: 2026-04-28 +description: 從 C# 連接本機 LLM,提示大型語言模型載入 Word 文件,呼叫本機 LLM 自動改寫文字,並附有逐步程式碼示例。 +draft: false +keywords: +- connect to local llm +- prompt large language model +- load word document +- call local llm +- rewrite text automatically +language: zh-hant +og_description: 從 C# 連接本地 LLM,了解如何提示大型語言模型、載入 Word 文件、呼叫本地 LLM,並在數分鐘內自動重寫文字。 +og_title: 在 C# 中連接本地 LLM – 完整程式設計指南 +tags: +- Aspose.Words +- C# +- LLM +- AI Automation +title: 在 C# 中連接本地 LLM – 完整程式設計指南 +url: /zh-hant/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/ +--- + +{{< blocks/products/pf/main-wrap-class >}} +{{< blocks/products/pf/main-container >}} +{{< blocks/products/pf/tutorial-page-section >}} + +# 在 C# 中連接本地 LLM – 完整程式開發指南 + +是否曾經需要 **連接本地 llm** 從 .NET 應用程式,卻不知如何讓它與 Word 檔案互動?你並不孤單。在本指南中,我們將完整說明整個流程——連接本地 llm、**提示大型語言模型**、載入 Word 文件、**呼叫本地 llm**,最後 **自動改寫文字**。完成後,你將擁有一個可執行的範例,能將任意段落轉換為正式語氣,且不需要任何外部 API 金鑰。 + +## 本教學涵蓋內容 + +我們會先安裝必要的 NuGet 套件,接著啟動一個簡易的本地 LLM 端點(例如 Ollama 在 11434 埠)。之後,我們會使用 Aspose.Words 載入 `.docx` 檔案,將段落送給 LLM,取得改寫後的版本,並寫回同一份文件。你還會看到如何處理常見的陷阱——空段落、非同步釋放、編碼問題——讓程式碼在正式環境中也能穩定運作,而不只是示範用。 + +### 前置條件 + +- .NET 6.0 SDK 或更新版本(如果喜歡也可以使用 .NET 8) +- Visual Studio 2022 或搭配 C# 擴充功能的 VS Code +- **Aspose.Words for .NET**(免費試用版即可) +- 一個支援 `/api/generate` 合約的本地 LLM(例如 Ollama、LMStudio) +- 基本的 C# async/await 使用經驗 + +> **專業小技巧:** 若尚未安裝 Ollama,可執行 `ollama serve`,並使用 `ollama pull llama3` 下載模型。預設的 HTTP 端點為 `http://localhost:11434/api/generate`。 + +--- + +## 步驟 1:安裝必要套件 + +首先,將 Aspose.Words 與 Aspose.Words.AI NuGet 套件加入專案。 + +```bash +dotnet add package Aspose.Words +dotnet add package Aspose.Words.AI +``` + +這兩個函式庫提供 **載入 Word 文件** 的功能,並且有一層薄薄的包裝,讓你 **呼叫本地 llm** 時不必自行手寫 HTTP 請求。 + +--- + +## 步驟 2:連接本地 LLM 端點 + +連接本機模型只需要實例化 `LocalLargeLanguageModel`。建構子接受完整的產生端點 URL。 + +```csharp +using Aspose.Words.AI; +using Aspose.Words; +using System.Threading.Tasks; + +// Create a client that talks to the LLM running on localhost +var localLlm = new LocalLargeLanguageModel("http://localhost:11434/api/generate"); +``` + +為什麼要把端點包在類別裡?`LocalLargeLanguageModel` 會幫你處理 JSON 序列化、重試機制與串流回應——讓你可以專注於提示邏輯,而不必與 `HttpClient` 纏鬥。 + +--- + +## 步驟 3:載入來源 Word 文件 + +接下來,我們把文件載入記憶體。Aspose.Words 支援幾乎所有的 Word 格式,因此 `Document` 能在不安裝 Office 的情況下解析 `input.docx`。 + +```csharp +// Path to the source file – adjust as needed +string inputPath = Path.Combine(Environment.CurrentDirectory, "input.docx"); + +// Load the document; throws if the file is missing or corrupted +Document sourceDocument = new Document(inputPath); +``` + +如果需要使用串流(例如 ASP.NET 上傳的檔案),只要把檔案路徑換成 `MemoryStream`,再傳給 `Document` 建構子即可。 + +--- + +## 步驟 4:取得目前段落文字 + +我們會使用 `DocumentBuilder` 在文件中導航。此範例改寫 **第一個段落**,但你也可以遍歷 `sourceDocument.GetChildNodes(NodeType.Paragraph, true)` 來處理多個段落。 + +```csharp +// Builder gives us a cursor inside the document +DocumentBuilder docBuilder = new DocumentBuilder(sourceDocument); + +// Grab the text of the paragraph where the builder is currently positioned +string originalParagraph = docBuilder.CurrentParagraph?.GetText() ?? string.Empty; + +// Safety check – avoid sending empty strings to the LLM +if (string.IsNullOrWhiteSpace(originalParagraph)) +{ + Console.WriteLine("No paragraph found at the current cursor position."); + return; +} +``` + +`?.` 運算子可防止在文件為空時拋出 `NullReferenceException`。這是新手常碰到的 **邊緣案例**。 + +--- + +## 步驟 5:提示 LLM 改寫段落 + +現在正式 **提示大型語言模型**。提示內容為純英文;包裝器會把它以 JSON 送到本地端點。 + +```csharp +// Build a friendly instruction for the model +string prompt = $"Rewrite the following sentence in a more formal tone:\n{originalParagraph}"; + +// Await the model's response – this is an async call +string rewrittenParagraph = await localLlm.PromptAsync(prompt); +``` + +為什麼要這樣寫請求?LLM 最擅長回應清晰、單一任務的指示。冒號後加一個換行,可把指示與內容分開,降低模型直接回傳提示本身的機率。 + +**預期輸出** – 若 `originalParagraph` 為 `"Hey, what's up?"`,LLM 可能回傳: + +> “Good day, how may I assist you?” + +你可以透過列印結果來驗證: + +```csharp +Console.WriteLine("Original: " + originalParagraph); +Console.WriteLine("Rewritten: " + rewrittenParagraph); +``` + +--- + +## 步驟 6:將改寫後的文字寫回文件 + +取得新文字後,我們替換舊段落。`DocumentBuilder.Writeln` 會寫入新行並將游標往前移,適合用來追加。如果想 **直接取代** 同一段落,可在寫入前呼叫 `docBuilder.CurrentParagraph.RemoveAllChildren()`。 + +```csharp +// Option A – Append a new paragraph (keeps the original) +docBuilder.Writeln(rewrittenParagraph); + +// Option B – Replace the existing paragraph (uncomment to use) +// docBuilder.CurrentParagraph.RemoveAllChildren(); +// docBuilder.CurrentParagraph.AppendChild(new Run(docBuilder.Document, rewrittenParagraph)); +``` + +兩種寫法都有示範,讓你依需求選擇最合適的方式。 + +--- + +## 步驟 7:儲存更新後的文件 + +最後,我們把變更寫入新檔。Aspose.Words 會根據副檔名自動決定格式。 + +```csharp +string outputPath = Path.Combine(Environment.CurrentDirectory, "output.docx"); +sourceDocument.Save(outputPath); + +Console.WriteLine($"Document saved to {outputPath}"); +``` + +開啟 `output.docx`,你會看到段落已變成正式語氣。 + +--- + +## 完整可執行範例 + +以下是 **完整、獨立** 的程式。直接貼到 Console 專案、還原 NuGet 套件、執行即可——只要本地 LLM 正在執行,無需額外設定。 + +```csharp +using Aspose.Words.AI; +using Aspose.Words; +using System; +using System.IO; +using System.Threading.Tasks; + +class Program +{ + static async Task Main() + { + // ------------------------------------------------- + // Step 1: Connect to the locally hosted LLM endpoint + // ------------------------------------------------- + var localLlm = new LocalLargeLanguageModel("http://localhost:11434/api/generate"); + + // ------------------------------------------------- + // Step 2: Load the source Word document + // ------------------------------------------------- + string inputPath = Path.Combine(Environment.CurrentDirectory, "input.docx"); + Document sourceDocument = new Document(inputPath); + + // ------------------------------------------------- + // Step 3: Retrieve the text of the current paragraph + // ------------------------------------------------- + DocumentBuilder docBuilder = new DocumentBuilder(sourceDocument); + string originalParagraph = docBuilder.CurrentParagraph?.GetText() ?? string.Empty; + + if (string.IsNullOrWhiteSpace(originalParagraph)) + { + Console.WriteLine("No paragraph found at the current cursor position."); + return; + } + + // ------------------------------------------------- + // Step 4: Ask the LLM to rewrite the paragraph in a formal tone + // ------------------------------------------------- + string prompt = $"Rewrite the following sentence in a more formal tone:\n{originalParagraph}"; + string rewrittenParagraph = await localLlm.PromptAsync(prompt); + + // ------------------------------------------------- + // Step 5: Insert the rewritten text back into the document + // ------------------------------------------------- + docBuilder.Writeln(rewrittenParagraph); + + // ------------------------------------------------- + // Step 6: Save the updated document + // ------------------------------------------------- + string outputPath = Path.Combine(Environment.CurrentDirectory, "output.docx"); + sourceDocument.Save(outputPath); + + Console.WriteLine("Original paragraph:"); + Console.WriteLine(originalParagraph); + Console.WriteLine("\nRewritten paragraph:"); + Console.WriteLine(rewrittenParagraph); + Console.WriteLine($"\nDocument saved to {outputPath}"); + } +} +``` + +### 執行時的預期結果 + +1. 主控台會印出原始段落與改寫後的段落。 +2. `output.docx` 會與 `input.docx` 同目錄出現。 +3. 開啟檔案後,可看到新正式段落已插入(或若使用替換程式碼則已取代)。 + +--- + +## 常見邊緣案例處理 + +| 情境 | 解決方案 | +|-----------|----------| +| **段落為空或僅有空白** | 在提示前使用 `string.IsNullOrWhiteSpace` 檢查(見 Step 3)。 | +| **LLM 回傳錯誤或空字串** | 將 `PromptAsync` 包在 `try/catch`,若失敗則回退使用原始文字。 | +| **需要改寫多個段落** | 迭代 `sourceDocument.GetChildNodes(NodeType.Paragraph, true)`,對每個段落套用相同的提示邏輯。 | +| **大型文件造成延遲** | 將段落批次處理,單次請求最多傳送約 4 KB(視模型限制而定)。 | +| **非 ASCII 字元出現亂碼** | 確認 LLM 端點使用 UTF-8(大多數現代模型皆如此)。 | + +--- + +## 後續步驟與相關主題 + +- 使用更豐富的指示 **提示大型語言模型**(例如風格指南、長度限制)。 +- 在 Web API 中 **呼叫本地 llm**,將文件自動化服務化。 +- 探索在平行串流中 **載入 Word 文件**,以因應高吞吐量情境。 +- 結合 **自動改寫文字**,用於大量電子郵件產生或報告標準化。 + +想更深入了解,可參考 Aspose 文件中的 **文件合併** 章節,以及 Ollama API 參考文件中的自訂抽樣參數說明。 + +--- + +## 結論 + +我們已示範如何在 C# 中 **連接本地 llm**、**提示大型語言模型**、**載入 Word 文件**、**呼叫本地 llm**,以及 **自動改寫文字**——全部於一個可執行的 Console 應用程式中完成。此模式具備可擴充性:只要更換提示、遍歷段落,或將邏輯封裝成 ASP.NET 端點,即可靈活運用。關鍵在於,本地 AI 模型可以與傳統文件處理函式庫緊密結合,讓你在可信的本地環境中實現強大自動化,而不必依賴外部服務。 + +如有執行緒相關問題,歡迎提出。 + +{{< /blocks/products/pf/tutorial-page-section >}} +{{< /blocks/products/pf/main-container >}} +{{< /blocks/products/pf/main-wrap-class >}} +{{< blocks/products/products-backtop-button >}} \ No newline at end of file diff --git a/words/hongkong/net/programming-with-markdownsaveoptions/_index.md b/words/hongkong/net/programming-with-markdownsaveoptions/_index.md index faadf329d8..9320d423d0 100644 --- a/words/hongkong/net/programming-with-markdownsaveoptions/_index.md +++ b/words/hongkong/net/programming-with-markdownsaveoptions/_index.md @@ -41,6 +41,7 @@ | [使用 Aspose.Words 將 docx 儲存為 markdown – 完整 C# 指南](./save-docx-as-markdown-with-aspose-words-full-c-guide/) |示範如何使用 Aspose.Words for .NET 在 C# 中完整將 DOCX 轉換為 Markdown,提供完整步驟與範例程式碼。 | | [儲存 Word 圖像 – 使用 Aspose 將 Word 轉換為 Markdown](./save-word-images-convert-word-to-markdown-with-aspose/) |示範如何使用 Aspose.Words for .NET 從 Word 文件中提取圖像並將其匯出為 Markdown 格式。 | | [將 Word 轉換為 markdown – 完整 C# 指南(含圖像提取)](./convert-word-to-markdown-in-c-full-guide-with-image-extracti/) |示範如何使用 Aspose.Words for .NET 在 C# 中將 Word 轉換為 Markdown,並提取圖像。 | +| [Markdown 圖像相對路徑 – 將 Word 轉換為 Markdown](./markdown-image-relative-path-convert-word-to-markdown/) |示範如何在將 Word 轉換為 Markdown 時,使用相對路徑引用圖像。 | {{< /blocks/products/pf/tutorial-page-section >}} diff --git a/words/hongkong/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/_index.md b/words/hongkong/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/_index.md new file mode 100644 index 0000000000..fdca05a722 --- /dev/null +++ b/words/hongkong/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/_index.md @@ -0,0 +1,183 @@ +--- +category: general +date: 2026-04-28 +description: 學習如何在將 Word 轉換為 Markdown 時設定圖片的相對路徑、從 Word 中提取圖片,並為匯出的圖片建立資源資料夾。 +draft: false +keywords: +- markdown image relative path +- convert word to markdown +- extract images from word +- create resources folder +- export images from docx +language: zh-hant +og_description: 在將 Word 轉換為 Markdown 時設定圖片的相對路徑,提取 Word 中的圖片,並為匯出的圖片建立資源資料夾。 +og_title: Markdown 圖片相對路徑 – 將 Word 轉換為 Markdown +tags: +- Aspose.Words +- C# +- Markdown +- Image Export +title: Markdown 圖片相對路徑 – 將 Word 轉換成 Markdown +url: /zh-hant/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/ +--- + +{{< blocks/products/pf/main-wrap-class >}} +{{< blocks/products/pf/main-container >}} +{{< blocks/products/pf/tutorial-page-section >}} + +# markdown image 相對路徑 – 將 Word 轉換為 Markdown + +有沒有曾經在 **convert Word to markdown** 時需要 **markdown image relative path**?你並不孤單。大多數開發者在生成的 Markdown 指向平面資料夾中的圖片時會卡住,導致在靜態網站或 GitHub 倉庫中預期的相對連結結構被破壞。 + +在本教學中,我們將逐步說明一個完整、端到端的解決方案,該方案 **extracts images from Word**、**creates a resources folder**,並重新寫入圖片引用,使其使用乾淨的 *markdown image relative path*。完成後,你將擁有一個可直接發布的 `.md` 檔案,以及一個整齊排列的 `Resources` 目錄,內含從原始 `.docx` 中提取的所有圖片。 + +> **你將獲得:** 一個單一的 C# 程式(無需外部腳本)、對每個部分 *why* 重要性的清晰說明,以及一些實用技巧,讓你可以直接複製貼上到自己的專案中。 + +## 前置條件 + +- **.NET 6.0** 或更新版本已安裝(你也可以目標 .NET Framework 4.7+,但 .NET 6 是新專案的最佳選擇)。 +- **Aspose.Words for .NET**(撰寫本文時的最新 NuGet 套件,版本 23.12)。使用以下方式安裝: + ```bash + dotnet add package Aspose.Words + ``` +- 一個實際包含圖片的 Word 文件,假設名稱為 `WithImages.docx`。 +- 一個用來存放輸出 markdown 及圖片的資料夾,例如 `C:\Projects\MarkdownExport`。 + +不需要額外的函式庫;其餘皆由 Aspose.Words 處理。 + +## 步驟 1:載入來源 Word 文件(convert word to markdown 的起點) + +```csharp +using System; +using System.IO; +using Aspose.Words; +using Aspose.Words.Saving; + +class Program +{ + static void Main() + { + // Adjust the path to point at your own .docx file. + string sourcePath = @"C:\Projects\MarkdownExport\WithImages.docx"; + + // Load the document – this is where Aspose.Words parses the Word file. + Document doc = new Document(sourcePath); + + // The rest of the workflow follows… + } +} +``` + +*為什麼這很重要:* 載入文件讓我們可以存取內部節點樹,其中包含稍後需要 **export images from docx** 的圖片部分。如果載入失敗,後續步驟都不會執行,請再次確認路徑與檔案權限。 + +## 步驟 2:使用自訂回呼設定 `MarkdownSaveOptions`(create resources folder 的核心) + +```csharp +// Inside Main(), after loading the document: +string outputFolder = @"C:\Projects\MarkdownExport"; +string resourcesFolder = Path.Combine(outputFolder, "Resources"); + +// Make sure the folder exists before we start saving anything. +Directory.CreateDirectory(resourcesFolder); + +// Set up the Markdown save options. +MarkdownSaveOptions mdOptions = new MarkdownSaveOptions +{ + // Hook that runs for every image resource. + ResourceSavingCallback = new MyMarkdownResourceCallback(resourcesFolder) +}; + +// Save the document as Markdown. +string markdownPath = Path.Combine(outputFolder, "Doc.md"); +doc.Save(markdownPath, mdOptions); +``` + +請注意我們將 `resourcesFolder` 傳入回呼的建構子——這樣可以讓資料夾路徑保持彈性,避免在程式碼中硬編碼字串。 + +## 步驟 3:實作回呼以 **creates resources folder** 並重新寫入路徑 + +```csharp +/// +/// Handles image extraction and path rewriting for markdown export. +/// +class MyMarkdownResourceCallback : IResourceSavingCallback +{ + private readonly string _resourcesFolder; + + public MyMarkdownResourceCallback(string resourcesFolder) + { + _resourcesFolder = resourcesFolder; + } + + public void ResourceSaving(ResourceSavingArgs args) + { + // 1️⃣ Build the full file system path where the image will be stored. + string targetPath = Path.Combine(_resourcesFolder, args.ResourceFileName); + + // 2️⃣ Ensure the directory exists (in case Aspose creates sub‑folders). + Directory.CreateDirectory(Path.GetDirectoryName(targetPath)); + + // 3️⃣ Write the image stream to disk. + using (FileStream fileStream = File.Create(targetPath)) + { + args.Stream.CopyTo(fileStream); + } + + // 4️⃣ Update the markdown reference to use a relative path. + // This is the crucial line that gives us the markdown image relative path. + args.ResourceFileName = Path.Combine("Resources", args.ResourceFileName); + } +} +``` + +*為什麼這有效:* `args.Stream` 包含原始的圖片位元組。將其複製到我們的 `Resources` 資料夾內的檔案時,我們安全地 **export images from docx**。接著我們將 `args.ResourceFileName` 替換為相對 URL(`Resources/image.png`)。當 Aspose.Words 稍後寫入 markdown 時,會注入這個字串,從而得到期望的 *markdown image relative path*。 + +## 步驟 4:驗證產生的 Markdown(最終輸出樣式) + +在任意文字編輯器中開啟 `Doc.md`。你應該會看到類似以下內容: + +```markdown +# Sample Heading + +Here is an inline picture: + +![Image 0](Resources/Image_0.png) + +And a picture inside a table: + +![Image 1](Resources/Image_1.jpg) +``` + +重要的是每個圖片引用都指向 `Resources/...` —— 這就是我們想要的 **markdown image relative path**。 + +![markdown image relative path example](example.png "markdown image relative path example") + +*提示:* 若在支援相對連結的檢視器(如 VS Code 預覽、GitHub 或靜態網站產生器)中開啟 markdown,圖片會正確顯示,且不需額外設定。 + +## 步驟 5:常見陷阱與專業提示 + +| Issue | Why it happens | How to fix it | +|-------|----------------|---------------| +| Images end up in the root folder instead of `Resources` | The callback wasn’t attached or `args.ResourceFileName` wasn’t overwritten. | Double‑check that `ResourceSavingCallback` is set **before** calling `doc.Save`. | +| Filenames contain illegal characters | Word sometimes names images with spaces or Unicode symbols. | Use `Path.GetInvalidFileNameChars()` to sanitize `args.ResourceFileName` inside the callback. | +| Large documents take a long time to process | Each image is written synchronously. | Switch to asynchronous I/O (`await args.Stream.CopyToAsync(fileStream)`) if you’re on .NET 6+ and need performance. | +| Relative paths break when the markdown is moved | The path is relative to the markdown file location. | Keep `Doc.md` and the `Resources` folder together, or adjust the callback to use a different relative prefix (e.g., `../assets`). | + +## 步驟 6:擴充解決方案(如果需要更多控制) + +- **Multiple output formats:** 將 `MarkdownSaveOptions` 替換為 `HtmlSaveOptions` 或 `PdfSaveOptions`,同時保留相同的回呼——Aspose.Words 會對每張圖片呼叫它,無論輸出格式為何。 +- **Custom image naming:** 若想重新命名圖片(例如 `figure-01.png`),請在寫入檔案前於回呼中修改 `args.ResourceFileName`。 +- **Embedding images as Base64:** 將 `args.ResourceFileName` 設為資料 URI(`data:image/png;base64,...`),並跳過檔案寫入。這對單一檔案的 markdown 匯出非常方便。 + +## 結論 + +現在你已擁有一個完整功能的 C# 程式,能 **converts Word to markdown**、**extracts images from word**、**creates a resources folder**,並為每張圖片保證乾淨的 **markdown image relative path**。此程式碼自包含、相容最新的 Aspose.Words 版本,且可輕鬆嵌入任何 .NET 專案。 + +接下來的步驟?試著將產生的 markdown 輸入 Hugo 或 Jekyll 等靜態網站產生器,或是實驗回呼以直接將圖片嵌入為 Base64 字串。如果遇到特殊情況——例如 SVG 圖片或異常大的檔案——請回顧「常見陷阱」表格;通常只要微調即可解決。 + +祝程式開發愉快,願你的 markdown 永遠指向正確的資料夾! + +{{< /blocks/products/pf/tutorial-page-section >}} +{{< /blocks/products/pf/main-container >}} +{{< /blocks/products/pf/main-wrap-class >}} +{{< blocks/products/products-backtop-button >}} \ No newline at end of file diff --git a/words/hongkong/net/programming-with-officemath/_index.md b/words/hongkong/net/programming-with-officemath/_index.md index e7a64f0352..10982e617f 100644 --- a/words/hongkong/net/programming-with-officemath/_index.md +++ b/words/hongkong/net/programming-with-officemath/_index.md @@ -23,7 +23,7 @@ Aspose.Words for .NET 使用 OfficeMath 進行文字處理的教學課程將指 | [數學方程式](./math-equations/) |了解如何使用 Aspose.Words for .NET 在 Word 文件中配置數學方程式。包含範例、常見問題等內容的逐步指南。 | | [將 docx 儲存為 txt – 使用 C# 匯出 Word 數學為 LaTeX](./save-docx-as-txt-export-word-math-to-latex-with-c/) |示範如何使用 C# 將 docx 檔案轉換為 txt,並將其中的 Word 數學方程式匯出為 LaTeX 格式。 | | [將文件另存為 Txt – 使用 C# 匯出 Word 數學為 LaTeX](./save-document-as-txt-export-word-math-to-latex-in-c/) |示範如何將 Word 文檔另存為 TXT,並將其中的數學公式匯出為 LaTeX 格式(C#)。 | - +| [將 DOCX 轉換為 TXT – 匯出 Word 方程式為 LaTeX(C#)](./convert-docx-to-txt-export-word-equations-to-latex-in-c/) |示範如何使用 C# 將 DOCX 檔案轉換為 TXT,並將其中的 Word 方程式匯出為 LaTeX 格式。 | {{< /blocks/products/pf/tutorial-page-section >}} diff --git a/words/hongkong/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/_index.md b/words/hongkong/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/_index.md new file mode 100644 index 0000000000..e6a9380684 --- /dev/null +++ b/words/hongkong/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/_index.md @@ -0,0 +1,208 @@ +--- +category: general +date: 2026-04-28 +description: 使用 Aspose.Words 將 DOCX 轉換為 TXT,並匯出 Word 方程式為 LaTeX。了解如何將 Word 儲存為 TXT + 以及在幾個步驟中處理數學物件。 +draft: false +keywords: +- convert docx to txt +- convert word equations to latex +- convert word to plain text +- save word as txt +- export equations as latex +language: zh-hant +og_description: 將 DOCX 轉換為 TXT,並以簡易 C# 程式碼片段將 Word 方程式匯出為 LaTeX。完整指南、程式碼與技巧。 +og_title: 將 DOCX 轉換為 TXT – 匯出 Word 方程式為 LaTeX +tags: +- C# +- Aspose.Words +- Document Conversion +title: 將 DOCX 轉換為 TXT – 在 C# 中匯出 Word 方程式為 LaTeX +url: /zh-hant/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/ +--- + +{{< blocks/products/pf/main-wrap-class >}} +{{< blocks/products/pf/main-container >}} +{{< blocks/products/pf/tutorial-page-section >}} + +# 將 DOCX 轉換為 TXT – 匯出 Word 方程式為 LaTeX + +有沒有曾經需要 **convert docx to txt**,但又擔心 Word 檔案中的數學公式會變成亂碼?你並不孤單。在許多工程或學術專案中,原始文件是 .docx,但下游工具只能理解純文字或 LaTeX。好消息是,只要幾行 C# 程式碼搭配 Aspose.Words,就能 **convert docx to txt** *並* 保留每個公式為乾淨的 LaTeX 代碼。 + +在本教學中,我們將逐步說明整個流程:載入 .docx、設定儲存選項讓 Office Math 物件轉為 LaTeX,最後將結果寫入 .txt 檔案。完成後,你將知道如何 **save word as txt**、**convert word to plain text**,以及 **export equations as latex**,而不必在 API 文件中搜尋。 + +## 你將學到 + +- 需要的精確 API 呼叫,以 **convert docx to txt** 並保留公式。 +- 為何選擇 `OfficeMathExportMode.LaTeX` 是 **convert word equations to latex** 的推薦方式。 +- 如何處理常見的例外情況,例如缺少字型或不支援的公式功能。 +- 一個完整、可直接執行的 C# 程式,可放入任何 .NET 專案中使用。 + +### 前置條件 + +- .NET 6.0 或更新版本(此程式碼亦可於 .NET Framework 4.7+ 上執行)。 +- Aspose.Words for .NET 的授權(免費試用版可用於評估)。 +- 一個包含至少一個 Office Math 物件的 Word 文件(`input.docx`)。 + +如果你已具備上述條件,讓我們開始吧。 + +## 步驟 1:安裝 Aspose.Words + +在執行任何程式碼之前,你需要先安裝此函式庫。於專案資料夾開啟終端機並執行: + +```bash +dotnet add package Aspose.Words +``` + +這會下載最新的穩定版(截至 2026‑04‑28 為 v24.12)。不需要額外的 DLL。 + +## 步驟 2:載入來源文件 + +我們首先將 .docx 檔案讀入 `Document` 物件。此物件讓我們完整存取檔案結構,包括文字串、影像與數學物件。 + +```csharp +using Aspose.Words; +using Aspose.Words.Saving; + +class Program +{ + static void Main() + { + // Step 2: Load the source document + Document doc = new Document(@"YOUR_DIRECTORY\input.docx"); +``` + +> **為何重要:** 載入文件會在記憶體中建立表示,之後我們才能調整各元素的輸出方式。如果找不到檔案,Aspose 會拋出 `FileNotFoundException`,在正式環境中可能需要捕捉此例外。 + +## 步驟 3:設定 TXT 儲存選項以支援 LaTeX 數學 + +預設情況下,`Document.Save` 只會寫入純文字,並 **捨棄** 任何 Office Math。若要保留公式,我們將 `OfficeMathExportMode` 設為 `LaTeX`。這會指示匯出器將每個公式轉換為相對應的 LaTeX 代碼。 + +```csharp + // Step 3: Configure TXT save options to export Office Math as LaTeX + TxtSaveOptions txtOptions = new TxtSaveOptions + { + OfficeMathExportMode = OfficeMathExportMode.LaTeX, + // Optional: preserve line breaks as they appear in the original Word file + PreserveTableLayout = true + }; +``` + +> **小技巧:** 若只需要公式的原始 Unicode 字元(例如快速預覽),可以使用 `OfficeMathExportMode.Text`。但對於大多數科學工作流程而言,`LaTeX` 是最佳選擇,因為所有 LaTeX 處理器皆能理解。 + +## 步驟 4:將文件儲存為純文字 + +現在將轉換後的內容寫入 `.txt` 檔案。檔案會包含一般段落、項目符號,且—感謝前一步—每個公式皆以 LaTeX 片段呈現。 + +```csharp + // Step 4: Save the document as plain‑text using the configured options + doc.Save(@"YOUR_DIRECTORY\Math.txt", txtOptions); + } +} +``` + +開啟 `Math.txt` 時,你會看到類似以下內容: + +``` +In this report we derive the quadratic formula: +\[ +x = \frac{-b \pm \sqrt{b^{2} - 4ac}}{2a} +\] + +The end. +``` + +注意到 `\[` … `\]` 的分界符嗎?那是自動產生的 LaTeX 數學區塊。 + +## 步驟 5:驗證輸出(可選但建議) + +有時會忽略細微的轉換問題,特別是公式包含自訂符號時。快速的驗證方式是將產生的 `.txt` 交給 LaTeX 編譯器(例如 `pdflatex`)編譯,確認是否無錯誤。 + +```bash +pdflatex -interaction=nonstopmode Math.txt +``` + +若編譯成功,即表示已成功 **convert word equations to latex** 並 **convert docx to txt**。若出現錯誤,請留意未定義指令的訊息——通常代表 Aspose.Words 無法轉換的公式功能(例如特定矩陣表示法)。此時可改用 `OfficeMathExportMode.MathML`,再使用其他工具將 MathML 轉為 LaTeX。 + +## 常見陷阱與避免方法 + +| 問題 | 發生原因 | 解決方法 | +|-------|----------------|-----| +| 缺少字型 | Aspose.Words 需要字型才能正確呈現符號。 | 在機器上安裝缺少的字型或將其嵌入 .docx 中。 | +| 複雜公式未匯出 | 某些較新的 Office Math 功能尚未對應到 LaTeX。 | 使用 `OfficeMathExportMode.MathML`,然後使用 MathML‑to‑LaTeX 函式庫進行轉換。 | +| 多餘的空白行 | 純文字儲存器會保留段落換行,可能導致額外空白。 | 設定 `txtOptions.AddBidiMarks = false`,或使用簡單腳本後處理檔案。 | + +## 完整可執行範例(可直接複製貼上) + +以下為完整程式碼,可直接編譯。將 `YOUR_DIRECTORY` 替換為存放 `input.docx` 的資料夾路徑。 + +```csharp +using System; +using Aspose.Words; +using Aspose.Words.Saving; + +namespace DocxToTxtWithLatex +{ + class Program + { + static void Main() + { + try + { + // Load the source document + Document doc = new Document(@"C:\Docs\input.docx"); + + // Configure save options: export equations as LaTeX + TxtSaveOptions txtOptions = new TxtSaveOptions + { + OfficeMathExportMode = OfficeMathExportMode.LaTeX, + PreserveTableLayout = true, + AddBidiMarks = false + }; + + // Save as plain‑text + string outputPath = @"C:\Docs\Math.txt"; + doc.Save(outputPath, txtOptions); + + Console.WriteLine($"Successfully converted DOCX to TXT. Output at: {outputPath}"); + } + catch (Exception ex) + { + Console.Error.WriteLine($"Conversion failed: {ex.Message}"); + } + } + } +} +``` + +執行此程式將 **save word as txt**,同時將每個 Office Math 區塊轉換為 LaTeX,產生乾淨且可搜尋的純文字檔案。 + +## 往後步驟與相關主題 + +- **Batch conversion:** 將上述邏輯包在 `foreach` 迴圈中,以處理整個資料夾的 .docx 檔案。 +- **Combine with PDF generation:** 取得 LaTeX 片段後,將其送入 PDF 工作流程(例如 `PdfSharp` + `MiKTeX`)以產生 PDF 報告。 +- **Export equations as latex** for other formats: Aspose.Words 亦支援 `SaveFormat.Markdown`,可自動嵌入 LaTeX。 +- **Performance tuning:** 對於大型文件,重複使用同一個 `TxtSaveOptions` 實例,並停用不必要的功能,如 `AddBidiMarks`。 + +--- + +### 圖片範例(可選) + +如果你喜歡視覺化的提示,以下是 Notepad++ 中輸出檔案的螢幕截圖。 + +![convert docx to txt 輸出顯示 LaTeX 方程式](convert-docx-to-txt-output.png) + +(Alt text: “convert docx to txt 輸出顯示 LaTeX 方程式” – 滿足主要關鍵字需求。) + +## 結論 + +我們剛剛示範了一種可靠的方式,能 **convert docx to txt** 並保留每個公式為乾淨的 LaTeX。關鍵在於 `OfficeMathExportMode.LaTeX` 旗標,將 Word 專有的數學格式轉換為任何 LaTeX 引擎都能理解的形式。透過上述完整程式碼範例,你可以在一次執行中 **save word as txt**、**convert word to plain text**,以及 **export equations as latex**。 + +歡迎自行嘗試——將輸出副檔名改為 `.md` 以產生 Markdown,或將此片段整合到更大的文件處理流程中。若遇到任何問題,請在下方留言,我很樂意協助排除。 + +祝編程愉快! + +{{< /blocks/products/pf/tutorial-page-section >}} +{{< /blocks/products/pf/main-container >}} +{{< /blocks/products/pf/main-wrap-class >}} +{{< blocks/products/products-backtop-button >}} \ No newline at end of file diff --git a/words/hungarian/net/ai-powered-document-processing/_index.md b/words/hungarian/net/ai-powered-document-processing/_index.md index 00aeba09ae..5952c1bf80 100644 --- a/words/hungarian/net/ai-powered-document-processing/_index.md +++ b/words/hungarian/net/ai-powered-document-processing/_index.md @@ -38,10 +38,11 @@ Végül ne felejtsd el megnézni a miénket [Összefoglaló beállítások haszn ## Mesterséges intelligencia által vezérelt dokumentumfeldolgozási oktatóanyagok | Cím | Leírás | | --- | --- | -| [AI-modell használata](./working-with-ai-model/) | Tanulja meg, hogyan használhatja az Aspose.Words for .NET-et dokumentumok mesterséges intelligencia segítségével történő összefoglalására. Egyszerű lépések a dokumentumkezelés fejlesztéséhez. +| [AI-modell használata](./working-with-ai-model/) | Tanulja meg, hogyan használhatja az Aspose.Words for .NET-et dokumentumok mesterséges intelligencia segítségével történő összefoglalására. Egyszerű lépések a dokumentumkezelés fejlesztéséhez. | | [A Google AI modelljével való munka](./working-with-google-ai-model/) Emeld magasabb szintre a dokumentumfeldolgozást az Aspose.Words for .NET és a Google AI segítségével, hogy könnyedén készíthess tömör összefoglalókat. | | [Nyílt mesterséges intelligencia modellel való munka](./working-with-open-ai-model/) | Az Aspose.Words for .NET segítségével hatékony dokumentum-összefoglalókat készíthet az OpenAI hatékony modelljeivel. Merüljön el ebben az átfogó útmutatóban most. | | [Összefoglaló beállítások használata](./working-with-summarize-options/) | Tanulja meg, hogyan foglalhatja össze hatékonyan a Word-dokumentumokat az Aspose.Words for .NET segítségével lépésről lépésre bemutatott útmutatónkkal, amely bemutatja a mesterséges intelligencia modelljeinek integrálását a gyors elemzések érdekében. | +| [Csatlakozás helyi LLM-hez C#-ban – Teljes programozási útmutató](./connect-to-local-llm-in-c-complete-programming-guide/) | Ismerje meg, hogyan csatlakoztathatja a helyi nagy nyelvi modellt C# alkalmazásához az Aspose.Words segítségével. | {{< /blocks/products/pf/tutorial-page-section >}} diff --git a/words/hungarian/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/_index.md b/words/hungarian/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/_index.md new file mode 100644 index 0000000000..04a107430d --- /dev/null +++ b/words/hungarian/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/_index.md @@ -0,0 +1,284 @@ +--- +category: general +date: 2026-04-28 +description: C#‑ból csatlakozz a helyi LLM‑hez, és kérd meg a nagy nyelvi modellt, + hogy töltse be a Word‑dokumentumot, hívja meg a helyi LLM‑et, és automatikusan írja + át a szöveget. Lépésről‑lépésre kód is mellékelve. +draft: false +keywords: +- connect to local llm +- prompt large language model +- load word document +- call local llm +- rewrite text automatically +language: hu +og_description: Csatlakozz C#-ból a helyi LLM-hez, és ismerd meg, hogyan kérdezd le + a nagy nyelvi modellt, tölts be Word-dokumentumot, hívd meg a helyi LLM-et, és automatikusan + írd át a szöveget percek alatt. +og_title: Csatlakozás helyi LLM-hez C#-ban – Teljes programozási útmutató +tags: +- Aspose.Words +- C# +- LLM +- AI Automation +title: Kapcsolódás helyi LLM-hez C#-ban – Teljes programozási útmutató +url: /hu/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/ +--- + +{{< blocks/products/pf/main-wrap-class >}} +{{< blocks/products/pf/main-container >}} +{{< blocks/products/pf/tutorial-page-section >}} + +# Kapcsolódás helyi LLM-hez C#-ban – Teljes programozási útmutató + +Valaha is szükséged volt **helyi llm-hez kapcsolódni** egy .NET alkalmazásból, és azon tűnődtél, hogyan tudná egy Word‑fájlhoz beszélni? Nem vagy egyedül. Ebben az útmutatóban végigvezetünk a teljes folyamaton – helyi llm-hez kapcsolódás, **prompt large language model**, Word‑dokumentum betöltése, **call local llm**, és végül **rewrite text automatically**. A végére egy futtatható példát kapsz, amely bármely bekezdést formális hangnemre alakít át, külső API‑kulcsok nélkül. + +## Amit ez a tutorial lefed + +Először telepítjük a szükséges NuGet csomagokat, majd elindítunk egy egyszerű helyi LLM végpontot (gondolj az Ollama‑ra a 11434‑es porton). Ezután betöltünk egy `.docx` fájlt az Aspose.Words segítségével, elküldünk egy bekezdést az LLM‑nek, megkapjuk az átdolgozott változatot, és visszaírjuk ugyanabba a dokumentumba. Megmutatjuk, hogyan kezeljünk gyakori buktatókat – üres bekezdések, aszinkron felszabadítás, kódolási sajátosságok – hogy a kód ne csak demóként, hanem éles környezetben is működjön. + +### Előfeltételek + +- .NET 6.0 SDK vagy újabb (használhatod a .NET 8‑at is) +- Visual Studio 2022 vagy VS Code C# kiegészítővel +- **Aspose.Words for .NET** (az ingyenes próba is megfelelő) +- Helyileg futtatott LLM, amely támogatja a `/api/generate` szerződést (pl. Ollama, LMStudio) +- Alapvető ismeretek az async/await használatáról C#‑ban + +> **Pro tipp:** Ha még nem telepítetted az Ollama‑t, futtasd a `ollama serve` parancsot, és húzz le egy modellt a `ollama pull llama3` segítségével. Az alapértelmezett HTTP végpont: `http://localhost:11434/api/generate`. + +--- + +## 1. lépés: Szükséges csomagok telepítése + +Először add hozzá az Aspose.Words és az Aspose.Words.AI NuGet csomagokat a projektedhez. + +```bash +dotnet add package Aspose.Words +dotnet add package Aspose.Words.AI +``` + +Ezek a könyvtárak biztosítják a **load word document** képességet, valamint egy könnyű réteget a **call local llm** híváshoz anélkül, hogy saját HTTP kéréseket kellene megírnod. + +--- + +## 2. lépés: Kapcsolódás a helyi LLM végponthoz + +Egy helyileg futtatott modellhez való csatlakozás olyan egyszerű, mint a `LocalLargeLanguageModel` példányosítása. A konstruktor a generálási végpont teljes URL‑jét várja. + +```csharp +using Aspose.Words.AI; +using Aspose.Words; +using System.Threading.Tasks; + +// Create a client that talks to the LLM running on localhost +var localLlm = new LocalLargeLanguageModel("http://localhost:11434/api/generate"); +``` + +Miért csomagoljuk be a végpontot egy osztályba? A `LocalLargeLanguageModel` kezeli a JSON sorosítást, az újrapróbálkozásokat és a streaming válaszokat – így a prompt logikára koncentrálhatsz, a `HttpClient`-el való bajlódás helyett. + +--- + +## 3. lépés: A forrás Word‑dokumentum betöltése + +Ezután a dokumentumot memóriába hozzuk. Az Aspose.Words gyakorlatilag minden Word‑formátumot támogat, így a `Document` a `input.docx`‑et Office telepítése nélkül is be tudja olvasni. + +```csharp +// Path to the source file – adjust as needed +string inputPath = Path.Combine(Environment.CurrentDirectory, "input.docx"); + +// Load the document; throws if the file is missing or corrupted +Document sourceDocument = new Document(inputPath); +``` + +Ha stream‑mel kell dolgoznod (pl. egy ASP.NET‑en keresztül feltöltött fájl), egyszerűen cseréld le a fájlútvonalat egy `MemoryStream`‑re, és add át a `Document` konstruktorának. + +--- + +## 4. lépés: Az aktuális bekezdés szövegének kinyerése + +A `DocumentBuilder`‑rel navigálunk a dokumentumban. Ebben a példában az **első bekezdést** írjuk át, de a `sourceDocument.GetChildNodes(NodeType.Paragraph, true)` segítségével több bekezdést is feldolgozhatsz. + +```csharp +// Builder gives us a cursor inside the document +DocumentBuilder docBuilder = new DocumentBuilder(sourceDocument); + +// Grab the text of the paragraph where the builder is currently positioned +string originalParagraph = docBuilder.CurrentParagraph?.GetText() ?? string.Empty; + +// Safety check – avoid sending empty strings to the LLM +if (string.IsNullOrWhiteSpace(originalParagraph)) +{ + Console.WriteLine("No paragraph found at the current cursor position."); + return; +} +``` + +A `?.` operátor megakadályozza a `NullReferenceException`‑t, ha a dokumentum véletlenül üres. Ez egy tipikus **edge case**, amely a kezdőket gyakran meglepi. + +--- + +## 5. lépés: Prompt a LLM‑nek a bekezdés átírásához + +Most ténylegesen **prompt large language model**. A prompt egyszerű angol szöveg; a wrapper JSON‑ként küldi el a helyi végpontra. + +```csharp +// Build a friendly instruction for the model +string prompt = $"Rewrite the following sentence in a more formal tone:\n{originalParagraph}"; + +// Await the model's response – this is an async call +string rewrittenParagraph = await localLlm.PromptAsync(prompt); +``` + +Miért így fogalmazzuk meg a kérést? Az LLM‑ek a világos, egyfeladatos utasításokra reagálnak a legjobban. A kettőspont után egy sortörés elválasztja az instrukciót a tartalomtól, csökkentve annak esélyét, hogy a modell visszhangozza a promptot. + +**Várható kimenet** – Ha az `originalParagraph` értéke `"Hey, what's up?"`, a LLM így válaszolhat: + +> “Good day, how may I assist you?” + +A végeredményt ellenőrizheted a kiíratással: + +```csharp +Console.WriteLine("Original: " + originalParagraph); +Console.WriteLine("Rewritten: " + rewrittenParagraph); +``` + +--- + +## 6. lépés: Az átírt szöveg visszaillesztése a dokumentumba + +Miután megvan az új szöveg, lecseréljük a régi bekezdést. A `DocumentBuilder.Writeln` új sort ír és előre mozgatja a kurzort, ami tökéletes a hozzáfűzéshez. Ha **ugyanazt a bekezdést** szeretnéd felülírni, használhatod a `docBuilder.CurrentParagraph.RemoveAllChildren()`‑t a írás előtt. + +```csharp +// Option A – Append a new paragraph (keeps the original) +docBuilder.Writeln(rewrittenParagraph); + +// Option B – Replace the existing paragraph (uncomment to use) +// docBuilder.CurrentParagraph.RemoveAllChildren(); +// docBuilder.CurrentParagraph.AppendChild(new Run(docBuilder.Document, rewrittenParagraph)); +``` + +Mindkét megközelítés látható, hogy a munkafolyamatodhoz leginkább illeszkedőt választhasd. + +--- + +## 7. lépés: A módosított dokumentum mentése + +Végül a változtatásokat egy új fájlba mentjük. Az Aspose.Words automatikusan a fájlkiterjesztés alapján választja ki a formátumot. + +```csharp +string outputPath = Path.Combine(Environment.CurrentDirectory, "output.docx"); +sourceDocument.Save(outputPath); + +Console.WriteLine($"Document saved to {outputPath}"); +``` + +Nyisd meg az `output.docx`‑et Word‑ben, és láthatod, hogy a bekezdés most formális hangnemben jelenik meg. + +--- + +## Teljes működő példa + +Alább a **komplett, önálló program** található. Másold be egy konzolprojektbe, állítsd vissza a NuGet csomagokat, és futtasd – nincs szükség extra konfigurációra a futó helyi LLM‑en kívül. + +```csharp +using Aspose.Words.AI; +using Aspose.Words; +using System; +using System.IO; +using System.Threading.Tasks; + +class Program +{ + static async Task Main() + { + // ------------------------------------------------- + // Step 1: Connect to the locally hosted LLM endpoint + // ------------------------------------------------- + var localLlm = new LocalLargeLanguageModel("http://localhost:11434/api/generate"); + + // ------------------------------------------------- + // Step 2: Load the source Word document + // ------------------------------------------------- + string inputPath = Path.Combine(Environment.CurrentDirectory, "input.docx"); + Document sourceDocument = new Document(inputPath); + + // ------------------------------------------------- + // Step 3: Retrieve the text of the current paragraph + // ------------------------------------------------- + DocumentBuilder docBuilder = new DocumentBuilder(sourceDocument); + string originalParagraph = docBuilder.CurrentParagraph?.GetText() ?? string.Empty; + + if (string.IsNullOrWhiteSpace(originalParagraph)) + { + Console.WriteLine("No paragraph found at the current cursor position."); + return; + } + + // ------------------------------------------------- + // Step 4: Ask the LLM to rewrite the paragraph in a formal tone + // ------------------------------------------------- + string prompt = $"Rewrite the following sentence in a more formal tone:\n{originalParagraph}"; + string rewrittenParagraph = await localLlm.PromptAsync(prompt); + + // ------------------------------------------------- + // Step 5: Insert the rewritten text back into the document + // ------------------------------------------------- + docBuilder.Writeln(rewrittenParagraph); + + // ------------------------------------------------- + // Step 6: Save the updated document + // ------------------------------------------------- + string outputPath = Path.Combine(Environment.CurrentDirectory, "output.docx"); + sourceDocument.Save(outputPath); + + Console.WriteLine("Original paragraph:"); + Console.WriteLine(originalParagraph); + Console.WriteLine("\nRewritten paragraph:"); + Console.WriteLine(rewrittenParagraph); + Console.WriteLine($"\nDocument saved to {outputPath}"); + } +} +``` + +### Mit várhatsz a futtatás során + +1. A konzol kiírja az eredeti és az átírt bekezdést. +2. Az `output.docx` megjelenik az `input.docx` mellett. +3. A fájl megnyitásakor az új formális bekezdés a régi után (vagy a helyettesítő kód használata esetén felülírva) látható. + +--- + +## Gyakori edge case‑ek kezelése + +| Helyzet | Megoldás | +|-----------|----------| +| **Üres vagy csak szóközöket tartalmazó bekezdés** | A promptolás előtt ellenőrizd a `string.IsNullOrWhiteSpace` értéket (lásd 3. lépés). | +| **Az LLM hibát vagy üres stringet ad vissza** | A `PromptAsync`‑t tedd `try/catch`‑be, és ha hiba történik, használd az eredeti szöveget. | +| **Több bekezdést kell átírni** | Iterálj a `sourceDocument.GetChildNodes(NodeType.Paragraph, true)` elemein, és alkalmazd ugyanazt a prompt logikát. | +| **Nagy dokumentumok késleltetést okoznak** | Csoportosíts bekezdéseket, és egyetlen kérésben küldd el őket (max. 4 KB prompt egy híváshoz). | +| **Nem‑ASCII karakterek eltorzulnak** | Győződj meg róla, hogy a LLM végpont UTF‑8‑at használ (a legtöbb modern modell így működik). | + +--- + +## Következő lépések és kapcsolódó témák + +- **Prompt large language model** részletesebb instrukciókkal (pl. stílus útmutató, hosszkorlát). +- **Call local llm** használata web‑API‑ban, hogy a dokumentum‑automatizálást szolgáltatásként tedd elérhetővé. +- **Load word document** párhuzamos stream‑ekkel nagy áteresztőképességű forgatókönyvekhez. +- Kombináld ezt a megközelítést **rewrite text automatically**‑val tömeges e‑mail generáláshoz vagy jelentés‑standardizáláshoz. + +Ha mélyebben szeretnél elmerülni, nézd meg az Aspose dokumentációját a **document merging** témakörben, valamint az Ollama API referenciát a saját mintavételi paraméterekhez. + +--- + +## Összegzés + +Megmutattuk, hogyan **kapcsolódj helyi llm-hez** C#‑ból, **prompt large language model**, **load word document**, **call local llm**, és **rewrite text automatically** – mindezt egyetlen futtatható konzolalkalmazásban. A minta skálázható: cseréld a promptot, iterálj bekezdéseken, vagy tedd elérhetővé egy ASP.NET végponton keresztül. A legfontosabb tanulság, hogy a helyi AI modellek szorosan integrálhatók a hagyományos dokumentum‑feldolgozó könyvtárakkal, így erőteljes automatizálást érhetsz el anélkül, hogy elhagynád a megbízható on‑prem környezetet. + +Kérdések a szálkezeléssel kapcsolatban, + +{{< /blocks/products/pf/tutorial-page-section >}} +{{< /blocks/products/pf/main-container >}} +{{< /blocks/products/pf/main-wrap-class >}} +{{< blocks/products/products-backtop-button >}} \ No newline at end of file diff --git a/words/hungarian/net/programming-with-markdownsaveoptions/_index.md b/words/hungarian/net/programming-with-markdownsaveoptions/_index.md index 27001bd549..a6c6355664 100644 --- a/words/hungarian/net/programming-with-markdownsaveoptions/_index.md +++ b/words/hungarian/net/programming-with-markdownsaveoptions/_index.md @@ -25,6 +25,8 @@ Szövegszerkesztés MarkdownSaveOptions segítségével egy részletes útmutat | [Képek mappa beállítása](./set-images-folder/) Konvertálja a Word dokumentumokat Markdown formátumba megfelelő táblázatigazítással az Aspose.Words for .NET segítségével. Kövesse részletes útmutatónkat a tökéletes eredmény érdekében. | | [Word mentése PDF-be és sérült Word helyreállítása – Word konvertálása Markdownba C#-ban](./save-word-as-pdf-and-recover-corrupted-word-convert-word-to/) | Ismerje meg, hogyan menthet Word dokumentumokat PDF-be, helyreállíthatja a sérült fájlokat, és konvertálhatja őket Markdown formátumba C#-ban az Aspose.Words for .NET segítségével. | | [Hogyan használjuk a Markdown-et: DOCX konvertálása Markdownba LaTeX egyenletekkel](./how-to-use-markdown-convert-docx-to-markdown-with-latex-equa/) | Ismerje meg, hogyan konvertálhat DOCX fájlokat Markdownba LaTeX egyenletekkel az Aspose.Words for .NET segítségével. | +### [Markdown kép relatív útvonal – Word konvertálása Markdownba](./markdown-image-relative-path-convert-word-to-markdown/) +Ismerje meg, hogyan használhatja a relatív képutakat a Word dokumentumok Markdownba konvertálásakor az Aspose.Words for .NET segítségével. ### [Hozzon létre akadálymentes PDF-et és konvertálja a Word-et Markdownba – Teljes C# útmutató](./create-accessible-pdf-and-convert-word-to-markdown-full-c-gu/) Ismerje meg, hogyan hozhat létre akadálymentes PDF-et, és konvertálhat Word dokumentumokat Markdown formátumba C#-ban az Aspose.Words for .NET segítségével. ### [DOCX konvertálása Markdownba – Lépésről‑lépésre C# útmutató](./convert-docx-to-markdown-step-by-step-c-guide/) diff --git a/words/hungarian/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/_index.md b/words/hungarian/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/_index.md new file mode 100644 index 0000000000..0004579463 --- /dev/null +++ b/words/hungarian/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/_index.md @@ -0,0 +1,207 @@ +--- +category: general +date: 2026-04-28 +description: Tanulja meg, hogyan állíthat be relatív útvonalat a markdown képekhez + a Word markdownra konvertálásakor, hogyan vonja ki a képeket a Wordből, és hogyan + hozza létre az erőforrások mappáját az exportált képek számára. +draft: false +keywords: +- markdown image relative path +- convert word to markdown +- extract images from word +- create resources folder +- export images from docx +language: hu +og_description: Állíts be egy relatív útvonalat a markdown képekhez, miközben Word-et + markdownra konvertálsz, kinyered a képeket a Wordből, és létrehozod az erőforrások + mappáját az exportált képeknek. +og_title: markdown kép relatív útvonala – Word konvertálása Markdownba +tags: +- Aspose.Words +- C# +- Markdown +- Image Export +title: Markdown kép relatív útvonal – Word konvertálása Markdownra +url: /hu/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/ +--- + +{{< blocks/products/pf/main-wrap-class >}} +{{< blocks/products/pf/main-container >}} +{{< blocks/products/pf/tutorial-page-section >}} + +# markdown kép relatív útvonal – Word konvertálása markdownra + +Valaha szükséged volt **markdown kép relatív útvonalra**, miközben **Word-ot konvertálsz markdownra**? Nem vagy egyedül. A legtöbb fejlesztő problémába ütközik, amikor a generált Markdown egy lapos mappára mutató képeket tartalmaz, ami megtöri a relatív hivatkozási struktúrát, amit egy statikus weboldalon vagy egy GitHub repóban elvársz. + +Ebben a tutorialban egy teljes, vég‑től‑végig megoldáson keresztül vezetünk, amely **kivonja a képeket a Word‑ből**, **létrehozza a resources mappát**, és átírja a kép hivatkozásokat, hogy egy tiszta *markdown kép relatív útvonalat* használjanak. A végére egy közzétételre kész `.md` fájlt és egy rendezett `Resources` könyvtárat kapsz, amely az eredeti `.docx`-ből kinyert minden képet tartalmazza. + +> **Amit kapsz:** egyetlen C# program (külső szkriptek nélkül), egy világos magyarázat arra, *miért* fontos minden részlet, és néhány gyakorlati tipp, amelyet egyszerűen beilleszthetsz a saját projektjeidbe. + +--- + +## Prerequisites + +Mielőtt a kódba merülnénk, győződj meg róla, hogy: + +- **.NET 6.0** vagy újabb telepítve van (célozhatsz .NET Framework 4.7+ verziót is, de a .NET 6 a legoptimálisabb új projektekhez). +- **Aspose.Words for .NET** (a cikk írásakor elérhető legújabb NuGet csomag, 23.12 verzió). Telepítsd a következővel: + ```bash + dotnet add package Aspose.Words + ``` +- Egy olyan Word dokumentum, amely valóban tartalmaz képeket – nevezzük `WithImages.docx`‑nek. +- Egy mappa, ahová a kimeneti markdown és a képek kerülnek, például `C:\Projects\MarkdownExport`. + +Nem szükséges további könyvtár; minden mást az Aspose.Words kezel. + +--- + +## Step 1: Load the source Word document (the starting point for convert word to markdown) + +```csharp +using System; +using System.IO; +using Aspose.Words; +using Aspose.Words.Saving; + +class Program +{ + static void Main() + { + // Adjust the path to point at your own .docx file. + string sourcePath = @"C:\Projects\MarkdownExport\WithImages.docx"; + + // Load the document – this is where Aspose.Words parses the Word file. + Document doc = new Document(sourcePath); + + // The rest of the workflow follows… + } +} +``` + +*Miért fontos:* A dokumentum betöltése hozzáférést biztosít a belső csomópontfához, amely tartalmazza a később **export images from docx**‑hez szükséges kép részeket. Ha a betöltés sikertelen, a későbbi lépések egyike sem fut le, ezért ellenőrizd a fájl útvonalát és a jogosultságokat. + +--- + +## Step 2: Configure `MarkdownSaveOptions` with a custom callback (the heart of create resources folder) + +A `ResourceSavingCallback` lehetővé teszi, hogy minden alkalommal beavatkozzunk, amikor az Aspose.Words képfájlt akar írni. A callbackben **létrehozzuk a Resources almappát** és módosítjuk a hivatkozást, hogy a generált markdown egy *markdown image relative path*-t használjon. + +```csharp +// Inside Main(), after loading the document: +string outputFolder = @"C:\Projects\MarkdownExport"; +string resourcesFolder = Path.Combine(outputFolder, "Resources"); + +// Make sure the folder exists before we start saving anything. +Directory.CreateDirectory(resourcesFolder); + +// Set up the Markdown save options. +MarkdownSaveOptions mdOptions = new MarkdownSaveOptions +{ + // Hook that runs for every image resource. + ResourceSavingCallback = new MyMarkdownResourceCallback(resourcesFolder) +}; + +// Save the document as Markdown. +string markdownPath = Path.Combine(outputFolder, "Doc.md"); +doc.Save(markdownPath, mdOptions); +``` + +Vedd észre, hogy a `resourcesFolder`‑t átadtuk a callback konstruktorának – ez rugalmasan tartja a mappa útvonalát, és elkerüli a stringek kemény kódolását a kódban. + +--- + +## Step 3: Implement the callback that **creates resources folder** and rewrites the path + +```csharp +/// +/// Handles image extraction and path rewriting for markdown export. +/// +class MyMarkdownResourceCallback : IResourceSavingCallback +{ + private readonly string _resourcesFolder; + + public MyMarkdownResourceCallback(string resourcesFolder) + { + _resourcesFolder = resourcesFolder; + } + + public void ResourceSaving(ResourceSavingArgs args) + { + // 1️⃣ Build the full file system path where the image will be stored. + string targetPath = Path.Combine(_resourcesFolder, args.ResourceFileName); + + // 2️⃣ Ensure the directory exists (in case Aspose creates sub‑folders). + Directory.CreateDirectory(Path.GetDirectoryName(targetPath)); + + // 3️⃣ Write the image stream to disk. + using (FileStream fileStream = File.Create(targetPath)) + { + args.Stream.CopyTo(fileStream); + } + + // 4️⃣ Update the markdown reference to use a relative path. + // This is the crucial line that gives us the markdown image relative path. + args.ResourceFileName = Path.Combine("Resources", args.ResourceFileName); + } +} +``` + +*Miért működik:* Az `args.Stream` a nyers kép bájtokat tartalmazza. A `Resources` mappánkba egy fájlba másolva **export images from docx**‑t biztonságosan elvégezzük. Ezután a `args.ResourceFileName`‑t egy relatív URL‑re (`Resources/image.png`) cseréljük. Amikor az Aspose.Words később írja a markdown‑t, pontosan ezt a karakterláncot illeszti be, így megkapjuk a kívánt *markdown image relative path*-t. + +--- + +## Step 4: Verify the generated Markdown (what the final output looks like) + +Nyisd meg a `Doc.md`‑t bármely szövegszerkesztőben. Valami hasonlót kell látnod: + +```markdown +# Sample Heading + +Here is an inline picture: + +![Image 0](Resources/Image_0.png) + +And a picture inside a table: + +![Image 1](Resources/Image_1.jpg) +``` + +A lényeg, hogy minden kép hivatkozás a `Resources/...`‑ra mutasson – ez a **markdown image relative path**, amit kerestünk. + +![markdown kép relatív útvonal példa](example.png "markdown kép relatív útvonal példa") + +*Tippek:* Ha a markdown‑t olyan nézőben nyitod meg, amely tiszteletben tartja a relatív hivatkozásokat (VS Code előnézet, GitHub vagy egy statikus weboldalgenerátor), a képek helyesen fognak megjelenni további konfiguráció nélkül. + +--- + +## Step 5: Common pitfalls and pro‑tips + +| Probléma | Miért fordul elő | Hogyan javítsuk | +|----------|------------------|-----------------| +| A képek a gyökérmappába kerülnek a `Resources` helyett | A callback nem lett csatolva, vagy az `args.ResourceFileName` nem lett felülírva. | Ellenőrizd, hogy a `ResourceSavingCallback` **a** `doc.Save` hívása **előtt** legyen beállítva. | +| A fájlnevek illegális karaktereket tartalmaznak | A Word néha szóközökkel vagy Unicode szimbólumokkal nevezi a képeket. | Használd a `Path.GetInvalidFileNameChars()`‑t az `args.ResourceFileName` tisztításához a callbackben. | +| Nagy dokumentumok lassan futnak | Minden kép szinkron módon íródik. | Válts aszinkron I/O‑ra (`await args.Stream.CopyToAsync(fileStream)`) .NET 6+ környezetben, ha teljesítményre van szükség. | +| Relatív utak elromlanak, ha a markdownot áthelyezik | Az útvonal a markdown fájl helyéhez relatív. | Tartsd együtt a `Doc.md`‑t és a `Resources` mappát, vagy módosítsd a callbacket, hogy más relatív előtagot használjon (pl. `../assets`). | + +--- + +## Step 6: Extending the solution (what if you need more control?) + +- **Multiple output formats:** Cseréld a `MarkdownSaveOptions`‑t `HtmlSaveOptions`‑ra vagy `PdfSaveOptions`‑ra, miközben ugyanazt a callbacket használod – az Aspose.Words minden formátum esetén meghívja a képekhez. +- **Custom image naming:** Ha át szeretnéd nevezni a képeket (pl. `figure-01.png`), módosítsd az `args.ResourceFileName`‑t a callbackben a fájl írása előtt. +- **Embedding images as Base64:** Állítsd be az `args.ResourceFileName`‑t egy data URI‑ra (`data:image/png;base64,...`) és hagyd ki a fájl írását. Ez hasznos egyetlen fájlból álló markdown exportokhoz. + +--- + +## Conclusion + +Most már van egy teljesen működő C# programod, amely **Word‑ot konvertál markdownra**, **kivonja a képeket a word‑ből**, **létrehozza a resources mappát**, és minden képhez tiszta **markdown kép relatív útvonalat** biztosít. A kód önálló, a legújabb Aspose.Words verzióval kompatibilis, és bármely .NET projektbe minimális erőfeszítéssel beilleszthető. + +Mi a következő lépés? Próbáld meg a generált markdown‑t egy statikus weboldalgenerátorral, például Hugo‑val vagy Jekyll‑lel feldolgozni, vagy kísérletezz a callbacktel, hogy a képeket közvetlenül Base64‑ként ágyazd be. Ha edge‑case‑ekkel találkozol – például SVG képekkel vagy szokatlanul nagy fájlokkal – nézd meg újra a „Common pitfalls” táblázatot; egy apró módosítás általában megoldja a problémát. + +Boldog kódolást, és legyen a markdownod mindig a megfelelő mappára mutató! + +{{< /blocks/products/pf/tutorial-page-section >}} +{{< /blocks/products/pf/main-container >}} +{{< /blocks/products/pf/main-wrap-class >}} +{{< blocks/products/products-backtop-button >}} \ No newline at end of file diff --git a/words/hungarian/net/programming-with-officemath/_index.md b/words/hungarian/net/programming-with-officemath/_index.md index cf86996aca..a8ebec6652 100644 --- a/words/hungarian/net/programming-with-officemath/_index.md +++ b/words/hungarian/net/programming-with-officemath/_index.md @@ -22,8 +22,8 @@ Az Aspose.Words for .NET oktatóanyagok az OfficeMath szövegszerkesztésről v | --- | --- | | [Matematikai egyenletek](./math-equations/) | Ismerje meg, hogyan konfigurálhat matematikai egyenleteket Word dokumentumokban az Aspose.Words for .NET használatával. Lépésről lépésre útmutató példákkal, GYIK-kel és egyebekkel. | | [Docx mentése txt‑ként – Word Math exportálása LaTeX‑be C#‑val](./save-docx-as-txt-export-word-math-to-latex-with-c/) | Tanulja meg, hogyan menthet DOCX fájlt TXT‑ként, és exportálja a Word Math egyenleteket LaTeX‑be C#‑al. | - -| [Dokumentum mentése TXT-ként – Word Math exportálása LaTeX-be C#-ban](./save-document-as-txt-export-word-math-to-latex-in-c/) | Mutatja, hogyan menthet Word-dokumentumot TXT formátumba, és exportálhatja a matematikai képleteket LaTeX-be C#-ban. | +| [Dokumentum mentése TXT-ként – Word Math exportálása LaTeX-be C#-ban](./save-document-as-txt-export-word-math-to-latex-in-c/) | Mutatja, hogyan menthet Word-dokumentumot TXT formátumba, és exportálhatja a matematikai képleteket LaTeX‑be C#‑ban. | +| [DOCX konvertálása TXT‑ként – Word egyenletek exportálása LaTeX‑be C#‑val](./convert-docx-to-txt-export-word-equations-to-latex-in-c/) | Ismerje meg, hogyan konvertálhat DOCX fájlt TXT‑re, és exportálhatja a Word egyenleteket LaTeX‑be C#‑val. | {{< /blocks/products/pf/tutorial-page-section >}} diff --git a/words/hungarian/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/_index.md b/words/hungarian/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/_index.md new file mode 100644 index 0000000000..5007f2a350 --- /dev/null +++ b/words/hungarian/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/_index.md @@ -0,0 +1,212 @@ +--- +category: general +date: 2026-04-28 +description: Konvertálja a DOCX-et TXT-re, és exportálja a Word egyenleteket LaTeX-be + az Aspose.Words segítségével. Tanulja meg, hogyan mentse a Word dokumentumot TXT + formátumban, és kezelje a matematikai objektumokat néhány lépésben. +draft: false +keywords: +- convert docx to txt +- convert word equations to latex +- convert word to plain text +- save word as txt +- export equations as latex +language: hu +og_description: Konvertálja a DOCX-et TXT-be, és exportálja a Word egyenleteket LaTeX-be + egy egyszerű C# kódrészlettel. Teljes útmutató, kód és tippek. +og_title: DOCX konvertálása TXT-re – Word egyenletek exportálása LaTeX-be +tags: +- C# +- Aspose.Words +- Document Conversion +title: DOCX konvertálása TXT-re – Word egyenletek exportálása LaTeX-be C#-ban +url: /hu/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/ +--- + +{{< blocks/products/pf/main-wrap-class >}} +{{< blocks/products/pf/main-container >}} +{{< blocks/products/pf/tutorial-page-section >}} + +# Convert DOCX to TXT – Export Word Equations to LaTeX + +Valaha is szükséged volt **docx to txt** konvertálásra, de attól tartottál, hogy a Word‑ben lévő matematikai képletek összekuszálódnak? Nem vagy egyedül. Sok mérnöki vagy tudományos projektben a forrásdokumentum .docx formátumban van, míg a downstream eszközök csak egyszerű szöveget vagy LaTeX‑et értenek. A jó hír? Néhány C# sor és az Aspose.Words segítségével **docx to txt** konvertálhatsz *és* minden egyenletet tiszta LaTeX kódként megőrizhetsz. + +Ebben az útmutatóban végigvezetünk a teljes folyamaton: egy .docx betöltése, a mentési beállítások konfigurálása úgy, hogy az Office Math objektumok LaTeX‑be kerüljenek, majd a végeredmény .txt fájlba írása. A végére tudni fogod, hogyan **save word as txt**, **convert word to plain text**, és **export equations as latex** anélkül, hogy az API dokumentációját kutatnád. + +## What You’ll Learn + +- A pontos API hívások, amelyekkel **docx to txt** konvertálás közben megőrizheted a képleteket. +- Miért ajánlott a `OfficeMathExportMode.LaTeX` használata a **convert word equations to latex** feladathoz. +- Hogyan kezeld a gyakori edge case‑eket, például hiányzó betűtípusok vagy nem támogatott képletfunkciók. +- Egy komplett, azonnal futtatható C# program, amely bármely .NET projektbe beilleszthető. + +### Prerequisites + +- .NET 6.0 vagy újabb (a kód .NET Framework 4.7+‑on is működik). +- Licenc az Aspose.Words for .NET‑hez (az ingyenes próba verzió elegendő a kiértékeléshez). +- Egy Word dokumentum (`input.docx`), amely legalább egy Office Math objektumot tartalmaz. + +Ha ezek megvannak, vágjunk bele. + +## Step 1: Install Aspose.Words + +Mielőtt bármilyen kód futna, szükség van a könyvtárra. Nyiss egy terminált a projekt mappájában és futtasd: + +```bash +dotnet add package Aspose.Words +``` + +Ez letölti a legújabb stabil verziót (2026‑04‑28‑i v24.12). További DLL‑ekre nincs szükség. + +## Step 2: Load the Source Document + +Az első lépés a .docx fájl beolvasása egy `Document` objektumba. Ez az objektum teljes hozzáférést biztosít a fájl szerkezetéhez, beleértve a szövegrészeket, képeket és matematikai objektumokat. + +```csharp +using Aspose.Words; +using Aspose.Words.Saving; + +class Program +{ + static void Main() + { + // Step 2: Load the source document + Document doc = new Document(@"YOUR_DIRECTORY\input.docx"); +``` + +> **Why this matters:** A dokumentum betöltése egy memóriában lévő reprezentációt hoz létre, így később finomhangolhatjuk, hogyan kerülnek kiírásra az egyes elemek. Ha a fájl nem található, az Aspose `FileNotFoundException`‑t dob, amit éles környezetben érdemes elkapni. + +## Step 3: Configure TXT Save Options for LaTeX Math + +Alapértelmezés szerint a `Document.Save` egyszerű szöveget ír, és **eldobja** az Office Math objektumokat. Ahhoz, hogy megmaradjanak, beállítjuk az `OfficeMathExportMode`‑t `LaTeX`‑re. Ez azt mondja az exportálónak, hogy minden egyenletet a LaTeX megfelelőjére fordítson. + +```csharp + // Step 3: Configure TXT save options to export Office Math as LaTeX + TxtSaveOptions txtOptions = new TxtSaveOptions + { + OfficeMathExportMode = OfficeMathExportMode.LaTeX, + // Optional: preserve line breaks as they appear in the original Word file + PreserveTableLayout = true + }; +``` + +> **Pro tip:** Ha csak a képlet nyers Unicode karaktereire van szükséged (például gyors előnézethez), használhatod az `OfficeMathExportMode.Text`‑et. De a legtöbb tudományos pipeline‑ban a `LaTeX` a legjobb választás, mivel univerzálisan értelmezhető a LaTeX processzorok által. + +## Step 4: Save the Document as Plain‑Text + +Most a transzformált tartalmat egy `.txt` fájlba írjuk. A fájl tartalmazni fogja a szokásos bekezdéseket, felsorolásokat, és—köszönhetően az előző lépésnek—LaTeX kódrészleteket minden egyenlethez. + +```csharp + // Step 4: Save the document as plain‑text using the configured options + doc.Save(@"YOUR_DIRECTORY\Math.txt", txtOptions); + } +} +``` + +Amikor megnyitod a `Math.txt`‑t, valami ilyesmit látsz majd: + +``` +In this report we derive the quadratic formula: +\[ +x = \frac{-b \pm \sqrt{b^{2} - 4ac}}{2a} +\] + +The end. +``` + +Észrevetted a `\[` … `\]` határolókat? Ezek a LaTeX matematikai blokkok, amelyeket a rendszer automatikusan generál. + +## Step 5: Verify the Output (Optional but Recommended) + +Könnyű elkerülni egy finom konverziós hibát, különösen ha a képletek egyedi szimbólumokat tartalmaznak. Egy gyors ellenőrzésként futtasd a generált `.txt`‑t egy LaTeX fordítóval (pl. `pdflatex`), és nézd meg, hogy hibamentesen lefordul-e. + +```bash +pdflatex -interaction=nonstopmode Math.txt +``` + +Ha a fordítás sikeres, akkor hatékonyan **convert word equations to latex** és **convert docx to txt** egy lépésben. Ha hibákba ütközöl, keresd a „undefined command” üzeneteket—ezek általában olyan képletfunkcióra utalnak, amelyet az Aspose.Words nem tud lefordítani (például bizonyos mátrix jelölések). Ilyenkor visszatérhetsz az `OfficeMathExportMode.MathML`‑re, és a MathML‑t egy másik eszközzel LaTeX‑re konvertálhatod. + +## Common Pitfalls & How to Avoid Them + +| Issue | Why it Happens | Fix | +|-------|----------------|-----| +| Missing fonts | Aspose.Wordsnek szüksége van a betűtípusra a szimbólumok helyes megjelenítéséhez. | Telepítsd a hiányzó betűtípust a gépre, vagy ágyazd be a .docx‑be. | +| Complex equations not exported | Néhány újabb Office Math funkció még nincs leképezve LaTeX‑re. | Használd az `OfficeMathExportMode.MathML`‑t, majd konvertáld MathML‑ről LaTeX‑re egy megfelelő könyvtárral. | +| Extra blank lines | A plain‑text mentő megőrzi a bekezdéseltöréseket, ami felesleges üres sorokhoz vezethet. | Állítsd be `txtOptions.AddBidiMarks = false`‑t, vagy utólag egy egyszerű script‑tel tisztítsd a fájlt. | + +## Full Working Example (Copy‑Paste Ready) + +Az alábbiakban a teljes program látható, amely azonnal lefordítható. Cseréld le a `YOUR_DIRECTORY`‑t arra a mappára, ahol az `input.docx` található. + +```csharp +using System; +using Aspose.Words; +using Aspose.Words.Saving; + +namespace DocxToTxtWithLatex +{ + class Program + { + static void Main() + { + try + { + // Load the source document + Document doc = new Document(@"C:\Docs\input.docx"); + + // Configure save options: export equations as LaTeX + TxtSaveOptions txtOptions = new TxtSaveOptions + { + OfficeMathExportMode = OfficeMathExportMode.LaTeX, + PreserveTableLayout = true, + AddBidiMarks = false + }; + + // Save as plain‑text + string outputPath = @"C:\Docs\Math.txt"; + doc.Save(outputPath, txtOptions); + + Console.WriteLine($"Successfully converted DOCX to TXT. Output at: {outputPath}"); + } + catch (Exception ex) + { + Console.Error.WriteLine($"Conversion failed: {ex.Message}"); + } + } + } +} +``` + +A program futtatásával **save word as txt** lesz, miközben minden Office Math blokk LaTeX‑re konvertálódik, így egy tiszta, kereshető egyszerű szövegfájlt kapsz. + +## Next Steps & Related Topics + +- **Batch conversion:** Csomagold be a fenti logikát egy `foreach` ciklusba, hogy egy egész mappát dolgozz fel .docx fájlokból. +- **Combine with PDF generation:** Miután megvannak a LaTeX snippetek, add át őket egy PDF pipeline‑nak (pl. `PdfSharp` + `MiKTeX`) a PDF jelentések előállításához. +- **Export equations as latex** for other formats: Az Aspose.Words támogatja a `SaveFormat.Markdown`‑ot is, amely automatikusan beágyaz LaTeX‑et. +- **Performance tuning:** Nagy dokumentumok esetén használd újra ugyanazt a `TxtSaveOptions` példányt, és tiltsd le a felesleges funkciókat, például az `AddBidiMarks`‑t. + +--- + +### Image Example (Optional) + +Ha vizuális segítségre van szükséged, itt egy képernyőkép a kimeneti fájlról a Notepad++‑ban. + +![convert docx to txt output showing LaTeX equations](convert-docx-to-txt-output.png) + +*(Alt text: “convert docx to txt output showing LaTeX equations” – satisfies the primary keyword requirement.)* + +--- + +## Conclusion + +Most már bemutattuk, hogyan **convert docx to txt** úgy, hogy minden egyenlet tiszta LaTeX formában megmarad. A kulcs az `OfficeMathExportMode.LaTeX` kapcsoló, amely a Word saját matematikai formátumát egy olyan formátummá alakítja, amelyet bármely LaTeX motor megért. A fenti teljes kódrészlettel **save word as txt**, **convert word to plain text**, és **export equations as latex** egyetlen, önálló futtatásban valósítható meg. + +Nyugodtan kísérletezz—cseréld le a kimeneti kiterjesztést `.md`‑re Markdownhoz, vagy integráld a snippetet egy nagyobb dokumentum‑feldolgozó pipeline‑ba. Ha bármilyen furcsaságot észlelsz, írj egy megjegyzést alul; szívesen segítek a hibaelhárításban. + +Happy coding! + +{{< /blocks/products/pf/tutorial-page-section >}} +{{< /blocks/products/pf/main-container >}} +{{< /blocks/products/pf/main-wrap-class >}} +{{< blocks/products/products-backtop-button >}} \ No newline at end of file diff --git a/words/indonesian/net/ai-powered-document-processing/_index.md b/words/indonesian/net/ai-powered-document-processing/_index.md index 25f2e8c2a3..7db00ac5e2 100644 --- a/words/indonesian/net/ai-powered-document-processing/_index.md +++ b/words/indonesian/net/ai-powered-document-processing/_index.md @@ -42,6 +42,7 @@ Bahasa Indonesia: --- | --- Bahasa Indonesia: | [Bekerja Dengan Model AI Google](./working-with-google-ai-model/) Tingkatkan pemrosesan dokumen Anda dengan Aspose.Words untuk .NET dan Google AI untuk membuat ringkasan ringkas dengan mudah. Bahasa Indonesia: | [Bekerja Dengan Model AI Terbuka](./working-with-open-ai-model/) | Dapatkan ringkasan dokumen yang efisien menggunakan Aspose.Words untuk .NET dengan model-model canggih OpenAI. Pelajari panduan lengkap ini sekarang. Bahasa Indonesia: | [Bekerja Dengan Opsi Ringkasan](./working-with-summarize-options/) | Pelajari cara meringkas dokumen Word secara efektif menggunakan Aspose.Words untuk .NET dengan panduan langkah demi langkah kami tentang mengintegrasikan model AI untuk wawasan cepat. | +| [Terhubung ke LLM Lokal di C# – Panduan Pemrograman Lengkap](./connect-to-local-llm-in-c-complete-programming-guide/) | Panduan lengkap untuk menghubungkan aplikasi C# Anda ke model LLM lokal menggunakan Aspose.Words. | {{< /blocks/products/pf/tutorial-page-section >}} diff --git a/words/indonesian/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/_index.md b/words/indonesian/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/_index.md new file mode 100644 index 0000000000..c25014d642 --- /dev/null +++ b/words/indonesian/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/_index.md @@ -0,0 +1,284 @@ +--- +category: general +date: 2026-04-28 +description: Hubungkan ke LLM lokal dari C# dan minta model bahasa besar untuk memuat + dokumen Word, panggil LLM lokal, serta menulis ulang teks secara otomatis. Kode + langkah demi langkah disertakan. +draft: false +keywords: +- connect to local llm +- prompt large language model +- load word document +- call local llm +- rewrite text automatically +language: id +og_description: Hubungkan ke LLM lokal dari C# dan lihat cara memberi prompt pada + model bahasa besar, memuat dokumen Word, memanggil LLM lokal, serta menulis ulang + teks secara otomatis dalam hitungan menit. +og_title: Terhubung ke LLM Lokal di C# – Panduan Pemrograman Lengkap +tags: +- Aspose.Words +- C# +- LLM +- AI Automation +title: Terhubung ke LLM Lokal di C# – Panduan Pemrograman Lengkap +url: /id/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/ +--- + +{{< blocks/products/pf/main-wrap-class >}} +{{< blocks/products/pf/main-container >}} +{{< blocks/products/pf/tutorial-page-section >}} + +# Menghubungkan ke LLM Lokal di C# – Panduan Pemrograman Lengkap + +Pernahkah Anda perlu **menghubungkan ke LLM lokal** dari aplikasi .NET dan bertanya-tanya bagaimana membuatnya berinteraksi dengan file Word? Anda tidak sendirian. Dalam panduan ini kami akan membahas seluruh proses—menghubungkan ke LLM lokal, **prompt large language model**, memuat dokumen Word, **call local llm**, dan akhirnya **rewrite text automatically**. Pada akhir tutorial Anda akan memiliki contoh yang dapat dijalankan yang mengubah setiap paragraf menjadi nada formal tanpa kunci API eksternal. + +## Apa yang Dibahas dalam Tutorial Ini + +Kami akan memulai dengan menginstal paket NuGet yang diperlukan, lalu menjalankan endpoint LLM lokal sederhana (bayangkan Ollama pada port 11434). Setelah itu kami akan memuat file `.docx` menggunakan Aspose.Words, mengirim sebuah paragraf ke LLM, menerima versi yang telah ditulis ulang, dan menuliskannya kembali ke dokumen yang sama. Anda juga akan melihat cara menangani jebakan umum—paragraf null, async disposal, dan keanehan encoding—sehingga kode berfungsi di produksi, bukan hanya demo. + +### Prasyarat + +- .NET 6.0 SDK atau yang lebih baru (Anda juga dapat menggunakan .NET 8 jika suka) +- Visual Studio 2022 atau VS Code dengan ekstensi C# +- **Aspose.Words for .NET** (versi trial gratis sudah cukup) +- LLM yang dihosting secara lokal dan mendukung kontrak `/api/generate` (misalnya Ollama, LMStudio) +- Familiaritas dasar dengan async/await di C# + +> **Pro tip:** Jika Anda belum menginstal Ollama, jalankan `ollama serve` dan tarik model dengan `ollama pull llama3`. Endpoint HTTP default akan menjadi `http://localhost:11434/api/generate`. + +--- + +## Langkah 1: Instal Paket yang Diperlukan + +Pertama, tambahkan paket NuGet Aspose.Words dan Aspose.Words.AI ke proyek Anda. + +```bash +dotnet add package Aspose.Words +dotnet add package Aspose.Words.AI +``` + +Perpustakaan ini memberi kita kemampuan **load word document** dan wrapper tipis untuk **call local llm** tanpa harus membuat permintaan HTTP secara manual. + +--- + +## Langkah 2: Hubungkan ke Endpoint LLM Lokal + +Menghubungkan ke model yang dihosting secara lokal sesederhana menginstansiasi `LocalLargeLanguageModel`. Konstruktor mengharapkan URL lengkap dari endpoint generasi. + +```csharp +using Aspose.Words.AI; +using Aspose.Words; +using System.Threading.Tasks; + +// Create a client that talks to the LLM running on localhost +var localLlm = new LocalLargeLanguageModel("http://localhost:11434/api/generate"); +``` + +Mengapa kita membungkus endpoint dalam sebuah kelas? `LocalLargeLanguageModel` menangani serialisasi JSON, retry, dan streaming response untuk Anda—sehingga Anda dapat fokus pada logika prompt alih-alih mengutak‑atik `HttpClient`. + +--- + +## Langkah 3: Muat Dokumen Word Sumber + +Selanjutnya, kita membawa dokumen ke memori. Aspose.Words mendukung hampir semua format Word, jadi `Document` akan mem‑parse `input.docx` tanpa perlu Office terinstal. + +```csharp +// Path to the source file – adjust as needed +string inputPath = Path.Combine(Environment.CurrentDirectory, "input.docx"); + +// Load the document; throws if the file is missing or corrupted +Document sourceDocument = new Document(inputPath); +``` + +Jika Anda perlu bekerja dengan stream (misalnya file yang di‑upload lewat ASP.NET), cukup ganti path file dengan `MemoryStream` dan berikan ke konstruktor `Document`. + +--- + +## Langkah 4: Ekstrak Teks Paragraf Saat Ini + +Kita akan menggunakan `DocumentBuilder` untuk menavigasi dokumen. Pada contoh ini kami menulis ulang **paragraf pertama**, tetapi Anda dapat mengiterasi `sourceDocument.GetChildNodes(NodeType.Paragraph, true)` untuk memproses banyak paragraf. + +```csharp +// Builder gives us a cursor inside the document +DocumentBuilder docBuilder = new DocumentBuilder(sourceDocument); + +// Grab the text of the paragraph where the builder is currently positioned +string originalParagraph = docBuilder.CurrentParagraph?.GetText() ?? string.Empty; + +// Safety check – avoid sending empty strings to the LLM +if (string.IsNullOrWhiteSpace(originalParagraph)) +{ + Console.WriteLine("No paragraph found at the current cursor position."); + return; +} +``` + +Operator `?.` mencegah `NullReferenceException` jika dokumen ternyata kosong. Ini adalah salah satu **edge cases** yang sering menjebak pemula. + +--- + +## Langkah 5: Prompt LLM untuk Menulis Ulang Paragraf + +Sekarang kita benar‑benar **prompt large language model**. Prompt ditulis dalam bahasa Inggris biasa; wrapper akan mengirimnya sebagai JSON ke endpoint lokal. + +```csharp +// Build a friendly instruction for the model +string prompt = $"Rewrite the following sentence in a more formal tone:\n{originalParagraph}"; + +// Await the model's response – this is an async call +string rewrittenParagraph = await localLlm.PromptAsync(prompt); +``` + +Mengapa permintaan diformulasikan seperti ini? LLM merespons paling baik pada instruksi yang jelas dan satu‑tugas. Menambahkan baris baru setelah titik dua memisahkan instruksi dari konten, mengurangi kemungkinan model meng‑echo prompt kembali. + +**Output yang diharapkan** – Jika `originalParagraph` berisi `"Hey, what's up?"`, LLM mungkin mengembalikan: + +> “Good day, how may I assist you?” + +Anda dapat memverifikasi hasilnya dengan mencetaknya: + +```csharp +Console.WriteLine("Original: " + originalParagraph); +Console.WriteLine("Rewritten: " + rewrittenParagraph); +``` + +--- + +## Langkah 6: Sisipkan Teks yang Telah Ditulis Ulang ke Dokumen + +Dengan teks baru di tangan, kita mengganti paragraf lama. `DocumentBuilder.Writeln` menulis baris baru dan memindahkan kursor ke depan, cocok untuk menambahkan. Jika Anda perlu *mengganti* paragraf yang sama persis, Anda dapat menggunakan `docBuilder.CurrentParagraph.RemoveAllChildren()` sebelum menulis. + +```csharp +// Option A – Append a new paragraph (keeps the original) +docBuilder.Writeln(rewrittenParagraph); + +// Option B – Replace the existing paragraph (uncomment to use) +// docBuilder.CurrentParagraph.RemoveAllChildren(); +// docBuilder.CurrentParagraph.AppendChild(new Run(docBuilder.Document, rewrittenParagraph)); +``` + +Kedua pendekatan ditampilkan sehingga Anda dapat memilih yang paling sesuai dengan alur kerja Anda. + +--- + +## Langkah 7: Simpan Dokumen yang Telah Diperbarui + +Akhirnya, kita menyimpan perubahan ke file baru. Aspose.Words secara otomatis memilih format berdasarkan ekstensi file. + +```csharp +string outputPath = Path.Combine(Environment.CurrentDirectory, "output.docx"); +sourceDocument.Save(outputPath); + +Console.WriteLine($"Document saved to {outputPath}"); +``` + +Buka `output.docx` di Word, dan Anda akan melihat paragraf kini ditulis dengan nada formal. + +--- + +## Contoh Program Lengkap yang Berfungsi + +Berikut adalah **program lengkap yang berdiri sendiri**. Salin‑tempel ke proyek console, restore paket NuGet, dan jalankan—tidak ada konfigurasi tambahan yang diperlukan selain LLM lokal yang sedang berjalan. + +```csharp +using Aspose.Words.AI; +using Aspose.Words; +using System; +using System.IO; +using System.Threading.Tasks; + +class Program +{ + static async Task Main() + { + // ------------------------------------------------- + // Step 1: Connect to the locally hosted LLM endpoint + // ------------------------------------------------- + var localLlm = new LocalLargeLanguageModel("http://localhost:11434/api/generate"); + + // ------------------------------------------------- + // Step 2: Load the source Word document + // ------------------------------------------------- + string inputPath = Path.Combine(Environment.CurrentDirectory, "input.docx"); + Document sourceDocument = new Document(inputPath); + + // ------------------------------------------------- + // Step 3: Retrieve the text of the current paragraph + // ------------------------------------------------- + DocumentBuilder docBuilder = new DocumentBuilder(sourceDocument); + string originalParagraph = docBuilder.CurrentParagraph?.GetText() ?? string.Empty; + + if (string.IsNullOrWhiteSpace(originalParagraph)) + { + Console.WriteLine("No paragraph found at the current cursor position."); + return; + } + + // ------------------------------------------------- + // Step 4: Ask the LLM to rewrite the paragraph in a formal tone + // ------------------------------------------------- + string prompt = $"Rewrite the following sentence in a more formal tone:\n{originalParagraph}"; + string rewrittenParagraph = await localLlm.PromptAsync(prompt); + + // ------------------------------------------------- + // Step 5: Insert the rewritten text back into the document + // ------------------------------------------------- + docBuilder.Writeln(rewrittenParagraph); + + // ------------------------------------------------- + // Step 6: Save the updated document + // ------------------------------------------------- + string outputPath = Path.Combine(Environment.CurrentDirectory, "output.docx"); + sourceDocument.Save(outputPath); + + Console.WriteLine("Original paragraph:"); + Console.WriteLine(originalParagraph); + Console.WriteLine("\nRewritten paragraph:"); + Console.WriteLine(rewrittenParagraph); + Console.WriteLine($"\nDocument saved to {outputPath}"); + } +} +``` + +### Apa yang Diharapkan Saat Menjalankannya + +1. Konsol mencetak paragraf asli dan paragraf yang telah ditulis ulang. +2. `output.docx` muncul di samping `input.docx`. +3. Membuka file menunjukkan paragraf formal baru disisipkan setelah yang asli (atau diganti, jika Anda menggunakan kode alternatif). + +--- + +## Menangani Edge Cases yang Umum + +| Situasi | Solusi | +|-----------|----------| +| **Paragraf kosong atau hanya spasi** | Periksa `string.IsNullOrWhiteSpace` sebelum melakukan prompt (lihat Langkah 3). | +| **LLM mengembalikan error atau string kosong** | Bungkus `PromptAsync` dalam `try/catch` dan gunakan teks asli sebagai fallback. | +| **Beberapa paragraf perlu ditulis ulang** | Loop melalui `sourceDocument.GetChildNodes(NodeType.Paragraph, true)` dan terapkan logika prompt yang sama. | +| **Dokumen besar menyebabkan latensi** | Batch paragraf dan kirim dalam satu permintaan (prompt hingga 4 KB per panggilan). | +| **Karakter non‑ASCII menjadi rusak** | Pastikan endpoint LLM menggunakan UTF‑8 (sebagian besar model modern sudah melakukannya). | + +--- + +## Langkah Selanjutnya & Topik Terkait + +- **Prompt large language model** dengan instruksi yang lebih kaya (misalnya panduan gaya, batas panjang). +- Gunakan **call local llm** dalam sebuah web API untuk mengekspos otomatisasi dokumen sebagai layanan. +- Jelajahi **load word document** dalam aliran paralel untuk skenario throughput tinggi. +- Gabungkan pendekatan ini dengan **rewrite text automatically** untuk pembuatan email massal atau standarisasi laporan. + +Jika Anda ingin menggali lebih dalam, lihat dokumentasi Aspose tentang **document merging** dan referensi API Ollama untuk parameter sampling khusus. + +--- + +## Kesimpulan + +Kami baru saja menunjukkan cara **menghubungkan ke LLM lokal** dari C#, **prompt large language model**, **load word document**, **call local llm**, dan **rewrite text automatically**—semuanya dalam satu aplikasi console yang dapat dijalankan. Pola ini dapat diskalakan: ganti prompt, iterasi paragraf, atau ekspos logika melalui endpoint ASP.NET. Inti utama adalah model AI lokal dapat diintegrasikan secara erat dengan perpustakaan pemrosesan dokumen klasik, memberi Anda otomasi kuat tanpa harus meninggalkan lingkungan on‑prem yang terpercaya. + +Ada pertanyaan tentang threading, + +{{< /blocks/products/pf/tutorial-page-section >}} +{{< /blocks/products/pf/main-container >}} +{{< /blocks/products/pf/main-wrap-class >}} +{{< blocks/products/products-backtop-button >}} \ No newline at end of file diff --git a/words/indonesian/net/programming-with-markdownsaveoptions/_index.md b/words/indonesian/net/programming-with-markdownsaveoptions/_index.md index 469155f3a2..551af70378 100644 --- a/words/indonesian/net/programming-with-markdownsaveoptions/_index.md +++ b/words/indonesian/net/programming-with-markdownsaveoptions/_index.md @@ -59,6 +59,8 @@ Pelajari cara mengonversi file DOCX menjadi Markdown menggunakan Aspose.Words de Pelajari cara mengekspor gambar dari dokumen Word saat mengonversinya ke format Markdown menggunakan Aspose.Words. ### [Konversi Word ke Markdown dalam C# – Panduan Lengkap dengan Ekstraksi Gambar](./convert-word-to-markdown-in-c-full-guide-with-image-extracti/) Pelajari cara mengonversi dokumen Word ke Markdown dengan ekstraksi gambar lengkap menggunakan C#. +### [Jalur Relatif Gambar Markdown – Konversi Word ke Markdown](./markdown-image-relative-path-convert-word-to-markdown/) +Pelajari cara mengatur jalur relatif gambar saat mengonversi dokumen Word ke format Markdown menggunakan Aspose.Words untuk .NET. {{< /blocks/products/pf/tutorial-page-section >}} diff --git a/words/indonesian/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/_index.md b/words/indonesian/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/_index.md new file mode 100644 index 0000000000..e4cbc073d7 --- /dev/null +++ b/words/indonesian/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/_index.md @@ -0,0 +1,206 @@ +--- +category: general +date: 2026-04-28 +description: Pelajari cara mengatur jalur relatif gambar markdown saat Anda mengonversi + Word ke markdown, mengekstrak gambar dari Word, dan membuat folder sumber daya untuk + gambar yang diekspor. +draft: false +keywords: +- markdown image relative path +- convert word to markdown +- extract images from word +- create resources folder +- export images from docx +language: id +og_description: Atur jalur relatif gambar markdown saat Anda mengonversi Word ke markdown, + mengekstrak gambar dari Word, dan membuat folder sumber daya untuk gambar yang diekspor. +og_title: Jalur Relatif Gambar Markdown – Konversi Word ke Markdown +tags: +- Aspose.Words +- C# +- Markdown +- Image Export +title: Jalur Relatif Gambar Markdown – Konversi Word ke Markdown +url: /id/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/ +--- + +{{< blocks/products/pf/main-wrap-class >}} +{{< blocks/products/pf/main-container >}} +{{< blocks/products/pf/tutorial-page-section >}} + +# jalur relatif gambar markdown – Mengonversi Word ke Markdown + +Pernah membutuhkan **jalur relatif gambar markdown** saat Anda **mengonversi Word ke markdown**? Anda tidak sendirian. Kebanyakan pengembang mengalami masalah ketika Markdown yang dihasilkan menunjuk ke gambar di folder datar, memutuskan struktur tautan relatif yang Anda harapkan di situs statis atau repositori GitHub. + +Dalam tutorial ini kita akan membahas solusi lengkap, end‑to‑end yang **mengekstrak gambar dari Word**, **membuat folder resources**, dan menulis ulang referensi gambar sehingga menggunakan *jalur relatif gambar markdown* yang bersih. Pada akhir tutorial Anda akan memiliki file `.md` siap‑terbit dan direktori `Resources` yang terorganisir rapi berisi setiap gambar yang diekstrak dari `.docx` asli. + +> **Apa yang akan Anda dapatkan:** satu program C# (tanpa skrip eksternal), penjelasan jelas tentang *mengapa* setiap bagian penting, dan beberapa tips praktis yang dapat Anda salin‑tempel ke proyek Anda sendiri. + +--- + +## Prasyarat + +Sebelum kita masuk ke kode, pastikan Anda memiliki: + +- **.NET 6.0** atau yang lebih baru terpasang (Anda juga dapat menargetkan .NET Framework 4.7+, tetapi .NET 6 adalah pilihan tepat untuk proyek baru). +- **Aspose.Words for .NET** (paket NuGet terbaru pada saat penulisan, versi 23.12). Instal dengan: + ```bash + dotnet add package Aspose.Words + ``` +- Dokumen Word yang memang berisi gambar—misalnya `WithImages.docx`. +- Folder tempat Anda ingin menyimpan markdown hasil dan gambar, misalnya `C:\Projects\MarkdownExport`. + +Tidak ada pustaka tambahan yang diperlukan; semua hal lain ditangani oleh Aspose.Words. + +--- + +## Langkah 1: Muat dokumen Word sumber (titik awal untuk mengonversi word ke markdown) + +```csharp +using System; +using System.IO; +using Aspose.Words; +using Aspose.Words.Saving; + +class Program +{ + static void Main() + { + // Adjust the path to point at your own .docx file. + string sourcePath = @"C:\Projects\MarkdownExport\WithImages.docx"; + + // Load the document – this is where Aspose.Words parses the Word file. + Document doc = new Document(sourcePath); + + // The rest of the workflow follows… + } +} +``` + +*Mengapa ini penting:* Memuat dokumen memberi kita akses ke pohon node internal, yang mencakup bagian gambar yang nanti akan **mengekspor gambar dari docx**. Jika pemuatan gagal, langkah‑langkah selanjutnya tidak akan berjalan, jadi periksa kembali jalur dan izin file. + +--- + +## Langkah 2: Konfigurasikan `MarkdownSaveOptions` dengan callback khusus (inti dari membuat folder resources) + +`ResourceSavingCallback` memungkinkan kita menyela setiap kali Aspose.Words ingin menulis file gambar. Di dalam callback kita akan **membuat sub‑folder Resources** dan menyesuaikan referensi sehingga markdown yang dihasilkan menggunakan *jalur relatif gambar markdown*. + +```csharp +// Inside Main(), after loading the document: +string outputFolder = @"C:\Projects\MarkdownExport"; +string resourcesFolder = Path.Combine(outputFolder, "Resources"); + +// Make sure the folder exists before we start saving anything. +Directory.CreateDirectory(resourcesFolder); + +// Set up the Markdown save options. +MarkdownSaveOptions mdOptions = new MarkdownSaveOptions +{ + // Hook that runs for every image resource. + ResourceSavingCallback = new MyMarkdownResourceCallback(resourcesFolder) +}; + +// Save the document as Markdown. +string markdownPath = Path.Combine(outputFolder, "Doc.md"); +doc.Save(markdownPath, mdOptions); +``` + +Perhatikan kami mengoper `resourcesFolder` ke konstruktor callback—ini menjaga jalur folder tetap fleksibel dan menghindari hard‑coding string di seluruh kode. + +--- + +## Langkah 3: Implementasikan callback yang **membuat folder resources** dan menulis ulang jalur + +```csharp +/// +/// Handles image extraction and path rewriting for markdown export. +/// +class MyMarkdownResourceCallback : IResourceSavingCallback +{ + private readonly string _resourcesFolder; + + public MyMarkdownResourceCallback(string resourcesFolder) + { + _resourcesFolder = resourcesFolder; + } + + public void ResourceSaving(ResourceSavingArgs args) + { + // 1️⃣ Build the full file system path where the image will be stored. + string targetPath = Path.Combine(_resourcesFolder, args.ResourceFileName); + + // 2️⃣ Ensure the directory exists (in case Aspose creates sub‑folders). + Directory.CreateDirectory(Path.GetDirectoryName(targetPath)); + + // 3️⃣ Write the image stream to disk. + using (FileStream fileStream = File.Create(targetPath)) + { + args.Stream.CopyTo(fileStream); + } + + // 4️⃣ Update the markdown reference to use a relative path. + // This is the crucial line that gives us the markdown image relative path. + args.ResourceFileName = Path.Combine("Resources", args.ResourceFileName); + } +} +``` + +*Mengapa ini berhasil:* `args.Stream` berisi byte gambar mentah. Dengan menyalinnya ke file di dalam folder `Resources` kita **mengekspor gambar dari docx** dengan aman. Kemudian kita mengganti `args.ResourceFileName` dengan URL relatif (`Resources/image.png`). Saat Aspose.Words kemudian menulis markdown, ia menyuntikkan string tersebut persis, memberi kita *jalur relatif gambar markdown* yang diinginkan. + +--- + +## Langkah 4: Verifikasi Markdown yang dihasilkan (seperti apa output akhir) + +Buka `Doc.md` di editor teks apa pun. Anda akan melihat sesuatu yang mirip dengan: + +```markdown +# Sample Heading + +Here is an inline picture: + +![Image 0](Resources/Image_0.png) + +And a picture inside a table: + +![Image 1](Resources/Image_1.jpg) +``` + +Bagian pentingnya adalah setiap referensi gambar menunjuk ke `Resources/...` – itulah **jalur relatif gambar markdown** yang kita cari. + +![contoh jalur relatif gambar markdown](example.png "contoh jalur relatif gambar markdown") + +*Tip:* Jika Anda membuka markdown di penampil yang menghormati tautan relatif (pratinjau VS Code, GitHub, atau generator situs statis), gambar akan ditampilkan dengan benar tanpa konfigurasi tambahan. + +--- + +## Langkah 5: Kesalahan umum dan pro‑tips + +| Masalah | Mengapa terjadi | Cara memperbaikinya | +|---------|----------------|---------------------| +| Gambar berakhir di folder root alih‑alih `Resources` | Callback tidak terpasang atau `args.ResourceFileName` tidak ditimpa. | Pastikan `ResourceSavingCallback` diset **sebelum** memanggil `doc.Save`. | +| Nama file mengandung karakter ilegal | Word kadang memberi nama gambar dengan spasi atau simbol Unicode. | Gunakan `Path.GetInvalidFileNameChars()` untuk membersihkan `args.ResourceFileName` di dalam callback. | +| Dokumen besar memakan waktu lama untuk diproses | Setiap gambar ditulis secara sinkron. | Beralih ke I/O asynchronous (`await args.Stream.CopyToAsync(fileStream)`) jika Anda berada di .NET 6+ dan membutuhkan performa. | +| Jalur relatif rusak saat markdown dipindahkan | Jalur relatif terhadap lokasi file markdown. | Simpan `Doc.md` dan folder `Resources` bersama, atau sesuaikan callback untuk menggunakan prefiks relatif lain (misalnya `../assets`). | + +--- + +## Langkah 6: Memperluas solusi (bagaimana jika Anda membutuhkan kontrol lebih?) + +- **Beberapa format output:** Ganti `MarkdownSaveOptions` dengan `HtmlSaveOptions` atau `PdfSaveOptions` sambil tetap menggunakan callback yang sama—Aspose.Words akan memanggilnya untuk setiap gambar terlepas dari format. +- **Penamaan gambar khusus:** Jika Anda ingin mengganti nama gambar (misalnya `figure-01.png`), ubah `args.ResourceFileName` di dalam callback sebelum menulis file. +- **Menyematkan gambar sebagai Base64:** Set `args.ResourceFileName` ke data URI (`data:image/png;base64,...`) dan lewati penulisan file. Ini berguna untuk ekspor markdown satu‑file. + +--- + +## Kesimpulan + +Anda kini memiliki program C# yang berfungsi penuh untuk **mengonversi Word ke markdown**, **mengekstrak gambar dari word**, **membuat folder resources**, dan menjamin **jalur relatif gambar markdown** yang bersih untuk setiap gambar. Kode ini berdiri sendiri, bekerja dengan versi Aspose.Words terbaru, dan dapat dimasukkan ke proyek .NET mana pun dengan usaha minimal. + +Langkah selanjutnya? Coba masukkan markdown yang dihasilkan ke generator situs statis seperti Hugo atau Jekyll, atau bereksperimen dengan callback untuk menyematkan gambar langsung sebagai string Base64. Jika Anda menemukan kasus tepi—misalnya gambar SVG atau file sangat besar—kembali ke tabel “Kesalahan umum”; penyesuaian kecil biasanya menyelesaikannya. + +Selamat coding, semoga markdown Anda selalu menunjuk ke folder yang tepat! + +{{< /blocks/products/pf/tutorial-page-section >}} +{{< /blocks/products/pf/main-container >}} +{{< /blocks/products/pf/main-wrap-class >}} +{{< blocks/products/products-backtop-button >}} \ No newline at end of file diff --git a/words/indonesian/net/programming-with-officemath/_index.md b/words/indonesian/net/programming-with-officemath/_index.md index 4fcb049d31..34e04746da 100644 --- a/words/indonesian/net/programming-with-officemath/_index.md +++ b/words/indonesian/net/programming-with-officemath/_index.md @@ -23,6 +23,7 @@ Bahasa Indonesia: --- | --- Bahasa Indonesia: | [Persamaan Matematika](./math-equations/) | Pelajari cara mengonfigurasi persamaan matematika dalam dokumen Word menggunakan Aspose.Words untuk .NET. Panduan langkah demi langkah dengan contoh, Tanya Jawab Umum, dan banyak lagi. | | [Simpan docx sebagai txt – Ekspor Word Math ke LaTeX dengan C#](./save-docx-as-txt-export-word-math-to-latex-with-c/) | Pelajari cara menyimpan dokumen .docx sebagai file .txt dan mengekspor persamaan Word Math ke format LaTeX menggunakan C#. | | [Simpan Dokumen sebagai Txt – Ekspor Matematika Word ke LaTeX dalam C#](./save-document-as-txt-export-word-math-to-latex-in-c/) | Pelajari cara menyimpan dokumen sebagai file txt dan mengekspor persamaan Word ke format LaTeX menggunakan C#. | +| [Konversi DOCX ke TXT – Ekspor Persamaan Word ke LaTeX dalam C#](./convert-docx-to-txt-export-word-equations-to-latex-in-c/) | Pelajari cara mengonversi file DOCX menjadi TXT dan mengekspor persamaan Word ke format LaTeX menggunakan C#. | {{< /blocks/products/pf/tutorial-page-section >}} diff --git a/words/indonesian/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/_index.md b/words/indonesian/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/_index.md new file mode 100644 index 0000000000..a4d48253d3 --- /dev/null +++ b/words/indonesian/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/_index.md @@ -0,0 +1,212 @@ +--- +category: general +date: 2026-04-28 +description: Ubah DOCX ke TXT dan ekspor persamaan Word ke LaTeX menggunakan Aspose.Words. + Pelajari cara menyimpan Word sebagai TXT dan menangani objek matematika dalam beberapa + langkah. +draft: false +keywords: +- convert docx to txt +- convert word equations to latex +- convert word to plain text +- save word as txt +- export equations as latex +language: id +og_description: Konversi DOCX ke TXT dan ekspor persamaan Word ke LaTeX dengan cuplikan + C# sederhana. Panduan lengkap, kode, dan tips. +og_title: Konversi DOCX ke TXT – Ekspor Persamaan Word ke LaTeX +tags: +- C# +- Aspose.Words +- Document Conversion +title: Ubah DOCX ke TXT – Ekspor Persamaan Word ke LaTeX dalam C# +url: /id/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/ +--- + +{{< blocks/products/pf/main-wrap-class >}} +{{< blocks/products/pf/main-container >}} +{{< blocks/products/pf/tutorial-page-section >}} + +# Convert DOCX ke TXT – Ekspor Persamaan Word ke LaTeX + +Pernah perlu **convert docx to txt** tetapi khawatir matematika di file Word Anda akan menjadi berantakan? Anda tidak sendirian. Dalam banyak proyek teknik atau akademik, dokumen sumber berada dalam .docx, namun alat hilir hanya memahami plain‑text atau LaTeX. Kabar baik? Dengan beberapa baris C# dan Aspose.Words Anda dapat **convert docx to txt** *dan* mempertahankan setiap persamaan sebagai kode LaTeX yang bersih. + +Dalam tutorial ini kami akan membahas seluruh proses: memuat .docx, mengonfigurasi opsi penyimpanan sehingga objek Office Math menjadi LaTeX, dan akhirnya menulis hasilnya ke file .txt. Pada akhir tutorial Anda akan tahu cara **save word as txt**, **convert word to plain text**, dan **export equations as latex** tanpa harus mencari‑cari di dokumentasi API. + +## Apa yang Akan Anda Pelajari + +- Panggilan API yang tepat untuk **convert docx to txt** sambil mempertahankan persamaan. +- Mengapa memilih `OfficeMathExportMode.LaTeX` adalah cara yang direkomendasikan untuk **convert word equations to latex**. +- Cara menangani kasus tepi umum seperti font yang hilang atau fitur persamaan yang tidak didukung. +- Program C# lengkap, siap‑jalankan yang dapat Anda masukkan ke proyek .NET mana pun. + +### Prasyarat + +- .NET 6.0 atau lebih baru (kode ini juga bekerja pada .NET Framework 4.7+). +- Lisensi untuk Aspose.Words for .NET (versi trial gratis dapat dipakai untuk evaluasi). +- Dokumen Word (`input.docx`) yang berisi setidaknya satu objek Office Math. + +Jika Anda sudah memiliki semua itu, mari kita mulai. + +## Langkah 1: Instal Aspose.Words + +Sebelum kode apa pun dijalankan, Anda perlu pustaka tersebut. Buka terminal di folder proyek Anda dan jalankan: + +```bash +dotnet add package Aspose.Words +``` + +Perintah ini akan mengunduh versi stabil terbaru (per 2026‑04‑28 v24.12). Tidak ada DLL tambahan yang diperlukan. + +## Langkah 2: Muat Dokumen Sumber + +Hal pertama yang kami lakukan adalah membaca file .docx ke dalam objek `Document`. Objek ini memberi kami akses penuh ke struktur file, termasuk rangkaian teks, gambar, dan objek matematika. + +```csharp +using Aspose.Words; +using Aspose.Words.Saving; + +class Program +{ + static void Main() + { + // Step 2: Load the source document + Document doc = new Document(@"YOUR_DIRECTORY\input.docx"); +``` + +> **Mengapa ini penting:** Memuat dokumen membuat representasi dalam memori, sehingga nanti kami dapat menyesuaikan cara setiap elemen ditulis. Jika file tidak ditemukan, Aspose akan melempar `FileNotFoundException`, yang mungkin ingin Anda tangani dalam kode produksi. + +## Langkah 3: Konfigurasikan Opsi Penyimpanan TXT untuk LaTeX Math + +Secara default, `Document.Save` menulis teks biasa dan **mengabaikan** semua Office Math. Untuk mempertahankan persamaan tersebut, kami mengatur `OfficeMathExportMode` ke `LaTeX`. Ini memberi tahu exporter untuk menerjemahkan setiap persamaan ke ekivalen LaTeX‑nya. + +```csharp + // Step 3: Configure TXT save options to export Office Math as LaTeX + TxtSaveOptions txtOptions = new TxtSaveOptions + { + OfficeMathExportMode = OfficeMathExportMode.LaTeX, + // Optional: preserve line breaks as they appear in the original Word file + PreserveTableLayout = true + }; +``` + +> **Tip profesional:** Jika Anda hanya membutuhkan karakter Unicode mentah dari persamaan (misalnya, untuk pratinjau cepat), Anda dapat menggunakan `OfficeMathExportMode.Text`. Namun untuk kebanyakan alur kerja ilmiah, `LaTeX` adalah standar emas karena dipahami secara universal oleh prosesor LaTeX. + +## Langkah 4: Simpan Dokumen sebagai Plain‑Text + +Sekarang kami menulis konten yang telah diubah ke file `.txt`. File tersebut akan berisi paragraf biasa, poin bullet, dan—berkat langkah sebelumnya—potongan LaTeX untuk setiap persamaan. + +```csharp + // Step 4: Save the document as plain‑text using the configured options + doc.Save(@"YOUR_DIRECTORY\Math.txt", txtOptions); + } +} +``` + +Saat Anda membuka `Math.txt` Anda akan melihat sesuatu seperti: + +``` +In this report we derive the quadratic formula: +\[ +x = \frac{-b \pm \sqrt{b^{2} - 4ac}}{2a} +\] + +The end. +``` + +Perhatikan delimiter `\[` … `\]`? Itu adalah blok matematika LaTeX yang dihasilkan secara otomatis. + +## Langkah 5: Verifikasi Output (Opsional tapi Disarankan) + +Mudah untuk melewatkan masalah konversi yang halus, terutama ketika persamaan mengandung simbol khusus. Pemeriksaan cepat adalah dengan memberi file `.txt` yang dihasilkan ke kompiler LaTeX (misalnya, `pdflatex`) dan melihat apakah ia dapat dikompilasi tanpa error. + +```bash +pdflatex -interaction=nonstopmode Math.txt +``` + +Jika kompilasi berhasil, Anda telah berhasil **convert word equations to latex** dan **convert docx to txt** dalam satu langkah. Jika muncul error, cari pesan tentang perintah yang tidak terdefinisi—biasanya menandakan fitur persamaan yang tidak dapat diterjemahkan oleh Aspose.Words (misalnya, notasi matriks tertentu). Dalam kasus seperti itu, Anda dapat beralih ke `OfficeMathExportMode.MathML` dan memproses MathML menjadi LaTeX dengan alat lain. + +## Kesalahan Umum & Cara Menghindarinya + +| Masalah | Mengapa Terjadi | Solusi | +|-------|----------------|-----| +| Font yang hilang | Aspose.Words memerlukan font untuk merender simbol dengan benar. | Instal font yang hilang di mesin atau sematkan dalam .docx. | +| Persamaan kompleks tidak diekspor | Beberapa fitur Office Math terbaru belum dipetakan ke LaTeX. | Gunakan `OfficeMathExportMode.MathML` lalu konversi dengan pustaka MathML‑to‑LaTeX. | +| Baris kosong berlebih | Penyimpan plain‑text mempertahankan jeda paragraf, yang dapat menambah whitespace. | Atur `txtOptions.AddBidiMarks = false` atau proses file dengan skrip sederhana. | + +## Contoh Lengkap yang Siap Pakai (Copy‑Paste) + +Berikut seluruh program, siap untuk dikompilasi. Ganti `YOUR_DIRECTORY` dengan folder yang berisi `input.docx` Anda. + +```csharp +using System; +using Aspose.Words; +using Aspose.Words.Saving; + +namespace DocxToTxtWithLatex +{ + class Program + { + static void Main() + { + try + { + // Load the source document + Document doc = new Document(@"C:\Docs\input.docx"); + + // Configure save options: export equations as LaTeX + TxtSaveOptions txtOptions = new TxtSaveOptions + { + OfficeMathExportMode = OfficeMathExportMode.LaTeX, + PreserveTableLayout = true, + AddBidiMarks = false + }; + + // Save as plain‑text + string outputPath = @"C:\Docs\Math.txt"; + doc.Save(outputPath, txtOptions); + + Console.WriteLine($"Successfully converted DOCX to TXT. Output at: {outputPath}"); + } + catch (Exception ex) + { + Console.Error.WriteLine($"Conversion failed: {ex.Message}"); + } + } + } +} +``` + +Menjalankan program ini akan **save word as txt** sambil mengubah setiap blok Office Math menjadi LaTeX, menghasilkan file teks polos yang bersih dan dapat dicari. + +## Langkah Selanjutnya & Topik Terkait + +- **Konversi batch:** Bungkus logika di atas dalam loop `foreach` untuk memproses seluruh folder berisi file .docx. +- **Kombinasikan dengan pembuatan PDF:** Setelah Anda memiliki potongan LaTeX, alirkan ke pipeline PDF (misalnya, `PdfSharp` + `MiKTeX`) untuk menghasilkan laporan PDF. +- **Export equations as latex** untuk format lain: Aspose.Words juga mendukung `SaveFormat.Markdown`, yang dapat menyematkan LaTeX secara otomatis. +- **Optimasi performa:** Untuk dokumen besar, gunakan kembali instance `TxtSaveOptions` yang sama dan nonaktifkan fitur yang tidak diperlukan seperti `AddBidiMarks`. + +--- + +### Contoh Gambar (Opsional) + +Jika Anda lebih suka petunjuk visual, berikut tangkapan layar file output di Notepad++. + +![convert docx to txt output showing LaTeX equations](convert-docx-to-txt-output.png) + +*(Teks alternatif: “output convert docx to txt menampilkan persamaan LaTeX” – memenuhi persyaratan kata kunci utama.)* + +--- + +## Kesimpulan + +Kami baru saja menunjukkan cara andal untuk **convert docx to txt** sambil mempertahankan setiap persamaan sebagai LaTeX yang bersih. Kuncinya adalah flag `OfficeMathExportMode.LaTeX`, yang mengubah format matematika proprietari Word menjadi sesuatu yang dapat dipahami oleh mesin LaTeX mana pun. Dengan contoh kode lengkap di atas, Anda dapat **save word as txt**, **convert word to plain text**, dan **export equations as latex** dalam satu proses yang mandiri. + +Silakan bereksperimen—ganti ekstensi output menjadi `.md` untuk Markdown, atau integrasikan potongan kode ke pipeline pemrosesan dokumen yang lebih besar. Jika Anda menemukan hal aneh, tinggalkan komentar di bawah; saya siap membantu memecahkan masalah. + +Selamat coding! + +{{< /blocks/products/pf/tutorial-page-section >}} +{{< /blocks/products/pf/main-container >}} +{{< /blocks/products/pf/main-wrap-class >}} +{{< blocks/products/products-backtop-button >}} \ No newline at end of file diff --git a/words/italian/net/ai-powered-document-processing/_index.md b/words/italian/net/ai-powered-document-processing/_index.md index 1a644a2079..5679deeeaa 100644 --- a/words/italian/net/ai-powered-document-processing/_index.md +++ b/words/italian/net/ai-powered-document-processing/_index.md @@ -42,6 +42,7 @@ Infine, non dimenticare di dare un'occhiata al nostro [Lavorare con le opzioni d | [Lavorare con il modello AI di Google](./working-with-google-ai-model/) Migliora l'elaborazione dei tuoi documenti con Aspose.Words per .NET e Google AI per creare riepiloghi concisi senza sforzo. | | [Lavorare con il modello di intelligenza artificiale aperta](./working-with-open-ai-model/) | Ottieni un riepilogo efficiente dei documenti utilizzando Aspose.Words per .NET con i potenti modelli di OpenAI. Scopri subito questa guida completa. | | [Lavorare con le opzioni di riepilogo](./working-with-summarize-options/) | Impara a riassumere in modo efficace i documenti Word utilizzando Aspose.Words per .NET con la nostra guida dettagliata sull'integrazione dei modelli di intelligenza artificiale per ottenere informazioni rapide. | +| [Connettersi a LLM locale in C# – Guida completa di programmazione](./connect-to-local-llm-in-c-complete-programming-guide/) | Scopri come collegare un modello linguistico locale in C# usando Aspose.Words per .NET, con esempi passo‑passo e codice completo. | {{< /blocks/products/pf/tutorial-page-section >}} diff --git a/words/italian/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/_index.md b/words/italian/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/_index.md new file mode 100644 index 0000000000..d515401fc3 --- /dev/null +++ b/words/italian/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/_index.md @@ -0,0 +1,284 @@ +--- +category: general +date: 2026-04-28 +description: Connetti al LLM locale da C# e chiedi al modello di linguaggio di caricare + un documento Word, chiama l'LLM locale e riscrivi il testo automaticamente. Codice + passo‑passo incluso. +draft: false +keywords: +- connect to local llm +- prompt large language model +- load word document +- call local llm +- rewrite text automatically +language: it +og_description: Connettiti a un LLM locale da C#, scopri come interagire con un modello + di linguaggio di grandi dimensioni, caricare un documento Word, chiamare l'LLM locale + e riscrivere il testo automaticamente in pochi minuti. +og_title: Connetti al LLM locale in C# – Guida completa di programmazione +tags: +- Aspose.Words +- C# +- LLM +- AI Automation +title: Connettersi a LLM locale in C# – Guida completa alla programmazione +url: /it/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/ +--- + +{{< blocks/products/pf/main-wrap-class >}} +{{< blocks/products/pf/main-container >}} +{{< blocks/products/pf/tutorial-page-section >}} + +# Connettersi a LLM locale in C# – Guida completa di programmazione + +Hai mai avuto bisogno di **connect to local llm** da un'app .NET e ti sei chiesto come farla parlare con un file Word? Non sei solo. In questa guida percorreremo l'intero processo—connect to local llm, **prompt large language model**, caricare un documento Word, **call local llm**, e infine **rewrite text automatically**. Alla fine avrai un esempio eseguibile che trasforma qualsiasi paragrafo in un tono formale senza chiavi API esterne. + +## Cosa copre questo tutorial + +Inizieremo installando i pacchetti NuGet necessari, poi avvieremo un semplice endpoint LLM locale (pensa a Ollama sulla porta 11434). Successivamente caricheremo un file `.docx` usando Aspose.Words, invieremo un paragrafo al LLM, riceveremo una versione riscritta e la scriveremo nuovamente nello stesso documento. Vedrai anche come gestire le problematiche comuni—paragrafi null, disposal asincrono e stranezze di codifica—così il codice funziona in produzione, non solo in una demo. + +### Prerequisiti + +- .NET 6.0 SDK o versioni successive (puoi anche usare .NET 8 se preferisci) +- Visual Studio 2022 o VS Code con estensione C# +- **Aspose.Words for .NET** (la versione di prova gratuita funziona bene) +- Un LLM ospitato localmente che supporta il contratto `/api/generate` (ad es., Ollama, LMStudio) +- Familiarità di base con async/await in C# + +> **Consiglio professionale:** Se non hai ancora installato Ollama, esegui `ollama serve` e scarica un modello con `ollama pull llama3`. L'endpoint HTTP predefinito sarà `http://localhost:11434/api/generate`. + +--- + +## Passo 1: Installa i pacchetti richiesti + +Per prima cosa, aggiungi i pacchetti NuGet Aspose.Words e Aspose.Words.AI al tuo progetto. + +```bash +dotnet add package Aspose.Words +dotnet add package Aspose.Words.AI +``` + +Queste librerie ci forniscono la funzionalità di **load word document** e un wrapper leggero per **call local llm** senza dover creare manualmente richieste HTTP. + +--- + +## Passo 2: Connettersi all'endpoint LLM locale + +Connettersi a un modello ospitato localmente è semplice come istanziare `LocalLargeLanguageModel`. Il costruttore si aspetta l'URL completo dell'endpoint di generazione. + +```csharp +using Aspose.Words.AI; +using Aspose.Words; +using System.Threading.Tasks; + +// Create a client that talks to the LLM running on localhost +var localLlm = new LocalLargeLanguageModel("http://localhost:11434/api/generate"); +``` + +Perché avvolgiamo l'endpoint in una classe? `LocalLargeLanguageModel` gestisce la serializzazione JSON, i retry e le risposte in streaming per te—così puoi concentrarti sulla logica del prompt invece di armeggiare con `HttpClient`. + +--- + +## Passo 3: Caricare il documento Word sorgente + +Successivamente, carichiamo il documento in memoria. Aspose.Words supporta praticamente tutti i formati Word, quindi `Document` analizzerà `input.docx` senza necessità di avere Office installato. + +```csharp +// Path to the source file – adjust as needed +string inputPath = Path.Combine(Environment.CurrentDirectory, "input.docx"); + +// Load the document; throws if the file is missing or corrupted +Document sourceDocument = new Document(inputPath); +``` + +Se devi lavorare con uno stream (ad es., un file caricato tramite ASP.NET), basta sostituire il percorso del file con un `MemoryStream` e passarlo al costruttore `Document`. + +--- + +## Passo 4: Estrarre il testo del paragrafo corrente + +Useremo `DocumentBuilder` per navigare nel documento. In questo esempio riscriviamo **il primo paragrafo**, ma puoi iterare su `sourceDocument.GetChildNodes(NodeType.Paragraph, true)` per elaborarne molti. + +```csharp +// Builder gives us a cursor inside the document +DocumentBuilder docBuilder = new DocumentBuilder(sourceDocument); + +// Grab the text of the paragraph where the builder is currently positioned +string originalParagraph = docBuilder.CurrentParagraph?.GetText() ?? string.Empty; + +// Safety check – avoid sending empty strings to the LLM +if (string.IsNullOrWhiteSpace(originalParagraph)) +{ + Console.WriteLine("No paragraph found at the current cursor position."); + return; +} +``` + +L'operatore `?.` previene una `NullReferenceException` se il documento dovesse essere vuoto. Questo è uno di quegli **edge cases** che confondono i principianti. + +--- + +## Passo 5: Promptare il LLM per riscrivere il paragrafo + +Ora effettivamente **prompt large language model**. Il prompt è in inglese semplice; il wrapper lo invierà come JSON all'endpoint locale. + +```csharp +// Build a friendly instruction for the model +string prompt = $"Rewrite the following sentence in a more formal tone:\n{originalParagraph}"; + +// Await the model's response – this is an async call +string rewrittenParagraph = await localLlm.PromptAsync(prompt); +``` + +Perché formulare la richiesta in questo modo? I LLM rispondono meglio a istruzioni chiare e a compito unico. Aggiungere una nuova riga dopo i due punti separa l'istruzione dal contenuto, riducendo la probabilità che il modello ripeta il prompt. + +**Output atteso** – Se `originalParagraph` era `"Hey, what's up?"`, il LLM potrebbe restituire: + +> “Good day, how may I assist you?” + +Puoi verificare il risultato stampandolo: + +```csharp +Console.WriteLine("Original: " + originalParagraph); +Console.WriteLine("Rewritten: " + rewrittenParagraph); +``` + +--- + +## Passo 6: Inserire il testo riscritto nuovamente nel documento + +Con il nuovo testo a disposizione, sostituiamo il vecchio paragrafo. `DocumentBuilder.Writeln` scrive una nuova riga e sposta il cursore in avanti, perfetto per aggiungere. Se devi *sostituire* esattamente lo stesso paragrafo, puoi usare `docBuilder.CurrentParagraph.RemoveAllChildren()` prima di scrivere. + +```csharp +// Option A – Append a new paragraph (keeps the original) +docBuilder.Writeln(rewrittenParagraph); + +// Option B – Replace the existing paragraph (uncomment to use) +// docBuilder.CurrentParagraph.RemoveAllChildren(); +// docBuilder.CurrentParagraph.AppendChild(new Run(docBuilder.Document, rewrittenParagraph)); +``` + +Entrambi gli approcci sono mostrati così puoi scegliere quello che si adatta al tuo flusso di lavoro. + +--- + +## Passo 7: Salvare il documento aggiornato + +Infine, salviamo le modifiche in un nuovo file. Aspose.Words sceglie automaticamente il formato in base all'estensione del file. + +```csharp +string outputPath = Path.Combine(Environment.CurrentDirectory, "output.docx"); +sourceDocument.Save(outputPath); + +Console.WriteLine($"Document saved to {outputPath}"); +``` + +Apri `output.docx` in Word e vedrai che il paragrafo ora è scritto in tono formale. + +--- + +## Esempio completo funzionante + +Di seguito trovi il **programma completo e autonomo**. Copialo e incollalo in un progetto console, ripristina i pacchetti NuGet ed eseguilo—non è necessaria alcuna configurazione aggiuntiva oltre a un LLM locale in esecuzione. + +```csharp +using Aspose.Words.AI; +using Aspose.Words; +using System; +using System.IO; +using System.Threading.Tasks; + +class Program +{ + static async Task Main() + { + // ------------------------------------------------- + // Step 1: Connect to the locally hosted LLM endpoint + // ------------------------------------------------- + var localLlm = new LocalLargeLanguageModel("http://localhost:11434/api/generate"); + + // ------------------------------------------------- + // Step 2: Load the source Word document + // ------------------------------------------------- + string inputPath = Path.Combine(Environment.CurrentDirectory, "input.docx"); + Document sourceDocument = new Document(inputPath); + + // ------------------------------------------------- + // Step 3: Retrieve the text of the current paragraph + // ------------------------------------------------- + DocumentBuilder docBuilder = new DocumentBuilder(sourceDocument); + string originalParagraph = docBuilder.CurrentParagraph?.GetText() ?? string.Empty; + + if (string.IsNullOrWhiteSpace(originalParagraph)) + { + Console.WriteLine("No paragraph found at the current cursor position."); + return; + } + + // ------------------------------------------------- + // Step 4: Ask the LLM to rewrite the paragraph in a formal tone + // ------------------------------------------------- + string prompt = $"Rewrite the following sentence in a more formal tone:\n{originalParagraph}"; + string rewrittenParagraph = await localLlm.PromptAsync(prompt); + + // ------------------------------------------------- + // Step 5: Insert the rewritten text back into the document + // ------------------------------------------------- + docBuilder.Writeln(rewrittenParagraph); + + // ------------------------------------------------- + // Step 6: Save the updated document + // ------------------------------------------------- + string outputPath = Path.Combine(Environment.CurrentDirectory, "output.docx"); + sourceDocument.Save(outputPath); + + Console.WriteLine("Original paragraph:"); + Console.WriteLine(originalParagraph); + Console.WriteLine("\nRewritten paragraph:"); + Console.WriteLine(rewrittenParagraph); + Console.WriteLine($"\nDocument saved to {outputPath}"); + } +} +``` + +### Cosa aspettarsi quando lo esegui + +1. La console stampa i paragrafi originali e riscritti. +2. `output.docx` appare accanto a `input.docx`. +3. Aprendo il file si vede il nuovo paragrafo formale inserito dopo l'originale (o sostituito, se hai usato il codice alternativo). + +--- + +## Gestire i casi limite comuni + +| Situation | Solution | +|-----------|----------| +| **Paragrafo vuoto o composto solo da spazi** | Verifica `string.IsNullOrWhiteSpace` prima di fare il prompt (vedi Passo 3). | +| **Il LLM restituisce un errore o una stringa vuota** | Avvolgi `PromptAsync` in un `try/catch` e ricorri al testo originale. | +| **Più paragrafi necessitano di riscrittura** | Itera su `sourceDocument.GetChildNodes(NodeType.Paragraph, true)` e applica la stessa logica di prompt. | +| **Documenti grandi causano latenza** | Raggruppa i paragrafi e inviali in una singola richiesta (prompt fino a 4 KB per chiamata). | +| **Caratteri non ASCII vengono corrotti** | Assicurati che l'endpoint LLM utilizzi UTF-8 (la maggior parte dei modelli moderni lo fa). | + +--- + +## Prossimi passi e argomenti correlati + +- **Prompt large language model** con istruzioni più ricche (ad es., guide di stile, limiti di lunghezza). +- Usa **call local llm** in una web API per esporre l'automazione dei documenti come servizio. +- Esplora **load word document** in stream paralleli per scenari ad alta velocità. +- Combina questo approccio con **rewrite text automatically** per generare email di massa o standardizzare report. + +Se vuoi approfondire, consulta la documentazione di Aspose su **document merging** e il riferimento API di Ollama per parametri di campionamento personalizzati. + +--- + +## Conclusione + +Ti abbiamo appena mostrato come **connect to local llm** da C#, **prompt large language model**, **load word document**, **call local llm**, e **rewrite text automatically**—tutto in una singola app console eseguibile. Il modello è scalabile: cambia il prompt, itera sui paragrafi o espone la logica tramite un endpoint ASP.NET. Il punto chiave è che i modelli AI locali possono essere strettamente integrati con le librerie classiche di elaborazione documenti, offrendoti un'automazione potente senza mai uscire dal tuo ambiente on‑prem di fiducia. + +Hai domande sul threading, + +{{< /blocks/products/pf/tutorial-page-section >}} +{{< /blocks/products/pf/main-container >}} +{{< /blocks/products/pf/main-wrap-class >}} +{{< blocks/products/products-backtop-button >}} \ No newline at end of file diff --git a/words/italian/net/programming-with-markdownsaveoptions/_index.md b/words/italian/net/programming-with-markdownsaveoptions/_index.md index b439838a1a..6e023ff534 100644 --- a/words/italian/net/programming-with-markdownsaveoptions/_index.md +++ b/words/italian/net/programming-with-markdownsaveoptions/_index.md @@ -35,13 +35,13 @@ | [Come salvare Markdown da DOCX – Guida passo‑passo](./how-to-save-markdown-from-docx-step-by-step-guide/) | Scopri come salvare documenti DOCX in Markdown con una guida passo‑passo completa. | | [Come salvare Markdown da Word – Guida completa C#](./how-to-save-markdown-from-word-complete-c-guide/) | Scopri come salvare documenti Word in formato Markdown con una guida completa in C#. | | [Come salvare Markdown da Word – Guida completa](./how-to-save-markdown-from-word-complete-guide/) | Scopri come salvare documenti Word in formato Markdown con una guida completa. | - | [Come esportare LaTeX da Word: Converti DOCX in Markdown con Aspose](./how-to-export-latex-from-word-convert-docx-to-markdown-with/) | Scopri come esportare equazioni LaTeX da documenti Word convertendoli in Markdown con Aspose.Words per .NET. | | [Converti Word in Markdown – Incorpora immagini come Base64](./convert-word-to-markdown-embed-images-as-base64/) | Converti documenti Word in Markdown incorporando le immagini direttamente come stringhe Base64 per un output autonomo. | | [Come esportare LaTeX: Converti DOCX in Markdown e TXT](./how-to-export-latex-convert-docx-to-markdown-txt/) | Scopri come convertire file DOCX in Markdown e TXT mantenendo le equazioni LaTeX con Aspose.Words per .NET. | | [Come rinominare le immagini durante la conversione da DOCX a Markdown](./how-to-rename-images-when-converting-docx-to-markdown/) | Scopri come rinominare le immagini durante la conversione da DOCX a Markdown con Aspose.Words per .NET. | | [Salva docx come markdown con Aspose.Words – Guida completa C#](./save-docx-as-markdown-with-aspose-words-full-c-guide/) | Impara a convertire file DOCX in Markdown con Aspose.Words usando C# con una guida completa. | | [Salva immagini Word – Converti Word in Markdown con Aspose](./save-word-images-convert-word-to-markdown-with-aspose/) | Salva le immagini da documenti Word e convertili in Markdown usando Aspose.Words per .NET. | +| [Percorso relativo immagine markdown – Converti Word in Markdown](./markdown-image-relative-path-convert-word-to-markdown/) | Converti documenti Word in Markdown gestendo percorsi relativi delle immagini per un output corretto. | {{< /blocks/products/pf/tutorial-page-section >}} diff --git a/words/italian/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/_index.md b/words/italian/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/_index.md new file mode 100644 index 0000000000..ecff4c759f --- /dev/null +++ b/words/italian/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/_index.md @@ -0,0 +1,207 @@ +--- +category: general +date: 2026-04-28 +description: Scopri come impostare un percorso relativo per le immagini markdown quando + converti Word in markdown, estrai le immagini da Word e crea una cartella risorse + per le immagini esportate. +draft: false +keywords: +- markdown image relative path +- convert word to markdown +- extract images from word +- create resources folder +- export images from docx +language: it +og_description: Imposta un percorso relativo per le immagini markdown durante la conversione + da Word a markdown, estrai le immagini da Word e crea una cartella risorse per le + immagini esportate. +og_title: Percorso relativo dell'immagine markdown – Converti Word in Markdown +tags: +- Aspose.Words +- C# +- Markdown +- Image Export +title: Percorso relativo dell'immagine markdown – Converti Word in Markdown +url: /it/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/ +--- + +{{< blocks/products/pf/main-wrap-class >}} +{{< blocks/products/pf/main-container >}} +{{< blocks/products/pf/tutorial-page-section >}} + +# markdown image relative path – Convert Word to Markdown + +Ti è mai capitato di aver bisogno di un **percorso relativo immagine markdown** mentre **converti Word in markdown**? Non sei l’unico. La maggior parte degli sviluppatori si imbatte in un problema quando il Markdown generato punta a immagini in una cartella piatta, rompendo la struttura di link relativi che ti aspetti in un sito statico o in un repository GitHub. + +In questo tutorial percorreremo una soluzione completa, end‑to‑end che **estrae le immagini da Word**, **crea una cartella resources**, e riscrive i riferimenti alle immagini in modo che usino un pulito *percorso relativo immagine markdown*. Alla fine avrai un file `.md` pronto per la pubblicazione e una directory `Resources` ordinatamente organizzata contenente tutte le immagini estratte dal `.docx` originale. + +> **What you’ll get:** un singolo programma C# (senza script esterni), una spiegazione chiara del *perché* di ogni parte, e una serie di consigli pratici da copiare‑incollare nei tuoi progetti. + +--- + +## Prerequisiti + +Prima di immergerci nel codice, assicurati di avere: + +- **.NET 6.0** o successivo installato (puoi anche puntare a .NET Framework 4.7+, ma .NET 6 è l’opzione ideale per nuovi progetti). +- **Aspose.Words for .NET** (l’ultimo pacchetto NuGet al momento della stesura, versione 23.12). Installalo con: + ```bash + dotnet add package Aspose.Words + ``` +- Un documento Word che contenga effettivamente immagini—lo chiameremo `WithImages.docx`. +- Una cartella dove vuoi che vivano il markdown di output e le immagini, ad esempio `C:\Projects\MarkdownExport`. + +Non sono richieste librerie aggiuntive; tutto il resto è gestito da Aspose.Words. + +--- + +## Step 1: Load the source Word document (the starting point for convert word to markdown) + +```csharp +using System; +using System.IO; +using Aspose.Words; +using Aspose.Words.Saving; + +class Program +{ + static void Main() + { + // Adjust the path to point at your own .docx file. + string sourcePath = @"C:\Projects\MarkdownExport\WithImages.docx"; + + // Load the document – this is where Aspose.Words parses the Word file. + Document doc = new Document(sourcePath); + + // The rest of the workflow follows… + } +} +``` + +*Why this matters:* Caricare il documento ci dà accesso all’albero interno dei nodi, che include le parti immagine di cui avremo bisogno più tardi per **export images from docx**. Se il caricamento fallisce, nessuno dei passaggi successivi verrà eseguito, quindi verifica il percorso e i permessi del file. + +--- + +## Step 2: Configure `MarkdownSaveOptions` with a custom callback (the heart of create resources folder) + +Il `ResourceSavingCallback` ci permette di intervenire ogni volta che Aspose.Words vuole scrivere un file immagine. All’interno del callback **creeremo una sottocartella Resources** e regoleremo il riferimento in modo che il markdown generato utilizzi un *percorso relativo immagine markdown*. + +```csharp +// Inside Main(), after loading the document: +string outputFolder = @"C:\Projects\MarkdownExport"; +string resourcesFolder = Path.Combine(outputFolder, "Resources"); + +// Make sure the folder exists before we start saving anything. +Directory.CreateDirectory(resourcesFolder); + +// Set up the Markdown save options. +MarkdownSaveOptions mdOptions = new MarkdownSaveOptions +{ + // Hook that runs for every image resource. + ResourceSavingCallback = new MyMarkdownResourceCallback(resourcesFolder) +}; + +// Save the document as Markdown. +string markdownPath = Path.Combine(outputFolder, "Doc.md"); +doc.Save(markdownPath, mdOptions); +``` + +Nota che abbiamo passato `resourcesFolder` nel costruttore del callback—questo mantiene il percorso della cartella flessibile ed evita di hard‑codare stringhe nel codice. + +--- + +## Step 3: Implement the callback that **creates resources folder** and rewrites the path + +```csharp +/// +/// Handles image extraction and path rewriting for markdown export. +/// +class MyMarkdownResourceCallback : IResourceSavingCallback +{ + private readonly string _resourcesFolder; + + public MyMarkdownResourceCallback(string resourcesFolder) + { + _resourcesFolder = resourcesFolder; + } + + public void ResourceSaving(ResourceSavingArgs args) + { + // 1️⃣ Build the full file system path where the image will be stored. + string targetPath = Path.Combine(_resourcesFolder, args.ResourceFileName); + + // 2️⃣ Ensure the directory exists (in case Aspose creates sub‑folders). + Directory.CreateDirectory(Path.GetDirectoryName(targetPath)); + + // 3️⃣ Write the image stream to disk. + using (FileStream fileStream = File.Create(targetPath)) + { + args.Stream.CopyTo(fileStream); + } + + // 4️⃣ Update the markdown reference to use a relative path. + // This is the crucial line that gives us the markdown image relative path. + args.ResourceFileName = Path.Combine("Resources", args.ResourceFileName); + } +} +``` + +*Why this works:* `args.Stream` contiene i byte grezzi dell’immagine. Copiandoli in un file all’interno della nostra cartella `Resources` **export images from docx** in modo sicuro. Poi sostituiamo `args.ResourceFileName` con un URL relativo (`Resources/image.png`). Quando Aspose.Words scriverà successivamente il markdown, inserirà esattamente quella stringa, fornendoci il desiderato *percorso relativo immagine markdown*. + +--- + +## Step 4: Verify the generated Markdown (what the final output looks like) + +Apri `Doc.md` in qualsiasi editor di testo. Dovresti vedere qualcosa di simile a: + +```markdown +# Sample Heading + +Here is an inline picture: + +![Image 0](Resources/Image_0.png) + +And a picture inside a table: + +![Image 1](Resources/Image_1.jpg) +``` + +La parte importante è che ogni riferimento immagine punti a `Resources/...` – questo è il **percorso relativo immagine markdown** che stavamo cercando. + +![percorso relativo immagine markdown esempio](example.png "percorso relativo immagine markdown esempio") + +*Tip:* Se apri il markdown in un visualizzatore che rispetta i link relativi (anteprima di VS Code, GitHub, o un generatore di siti statici), le immagini verranno renderizzate correttamente senza alcuna configurazione aggiuntiva. + +--- + +## Step 5: Common pitfalls and pro‑tips + +| Issue | Why it happens | How to fix it | +|-------|----------------|---------------| +| Images end up in the root folder instead of `Resources` | The callback wasn’t attached or `args.ResourceFileName` wasn’t overwritten. | Double‑check that `ResourceSavingCallback` is set **before** calling `doc.Save`. | +| Filenames contain illegal characters | Word sometimes names images with spaces or Unicode symbols. | Use `Path.GetInvalidFileNameChars()` to sanitize `args.ResourceFileName` inside the callback. | +| Large documents take a long time to process | Each image is written synchronously. | Switch to asynchronous I/O (`await args.Stream.CopyToAsync(fileStream)`) if you’re on .NET 6+ and need performance. | +| Relative paths break when the markdown is moved | The path is relative to the markdown file location. | Keep `Doc.md` and the `Resources` folder together, or adjust the callback to use a different relative prefix (e.g., `../assets`). | + +--- + +## Step 6: Extending the solution (what if you need more control?) + +- **Multiple output formats:** Replace `MarkdownSaveOptions` with `HtmlSaveOptions` o `PdfSaveOptions` mantenendo lo stesso callback—Aspose.Words lo invocherà per ogni immagine indipendentemente dal formato. +- **Custom image naming:** Se vuoi rinominare le immagini (es. `figure-01.png`), modifica `args.ResourceFileName` all’interno del callback prima di scrivere il file. +- **Embedding images as Base64:** Imposta `args.ResourceFileName` a un data URI (`data:image/png;base64,...`) e salta la scrittura su file. Questo è utile per esportazioni markdown monofile. + +--- + +## Conclusione + +Ora disponi di un programma C# completamente funzionale che **converte Word in markdown**, **estrae le immagini da word**, **crea una cartella resources**, e garantisce un pulito **percorso relativo immagine markdown** per ogni immagine. Il codice è autonomo, funziona con l’ultima versione di Aspose.Words, e può essere inserito in qualsiasi progetto .NET con il minimo sforzo. + +Prossimi passi? Prova a far passare il markdown generato a un generatore di siti statici come Hugo o Jekyll, o sperimenta con il callback per incorporare le immagini direttamente come stringhe Base64. Se incontri casi particolari—ad esempio immagini SVG o file eccezionalmente grandi—riferisciti alla tabella “Common pitfalls”; una piccola modifica di solito risolve il problema. + +Buon coding, e che il tuo markdown punti sempre alla cartella giusta! + +{{< /blocks/products/pf/tutorial-page-section >}} +{{< /blocks/products/pf/main-container >}} +{{< /blocks/products/pf/main-wrap-class >}} +{{< blocks/products/products-backtop-button >}} \ No newline at end of file diff --git a/words/italian/net/programming-with-officemath/_index.md b/words/italian/net/programming-with-officemath/_index.md index 4721a364f2..d46c179c9a 100644 --- a/words/italian/net/programming-with-officemath/_index.md +++ b/words/italian/net/programming-with-officemath/_index.md @@ -23,6 +23,7 @@ tutorial di Aspose.Words per .NET sull'elaborazione testi con OfficeMath vi guid | [Equazioni matematiche](./math-equations/) | Scopri come configurare equazioni matematiche nei documenti Word utilizzando Aspose.Words per .NET. Guida dettagliata con esempi, FAQ e altro ancora. | | [Salva docx come txt – Esporta Word Math in LaTeX con C#](./save-docx-as-txt-export-word-math-to-latex-with-c/) | Scopri come salvare documenti docx in formato txt ed esportare equazioni Word Math in LaTeX usando C#. | | [Salva documento come Txt – Esporta Word Math in LaTeX in C#](./save-document-as-txt-export-word-math-to-latex-in-c/) | Impara a salvare un documento Word come file Txt ed esportare le equazioni matematiche in LaTeX usando C# con Aspose.Words. | +| [Converti docx in txt – Esporta Equazioni Word in LaTeX in C#](./convert-docx-to-txt-export-word-equations-to-latex-in-c/) | Scopri come convertire file DOCX in TXT ed esportare le equazioni Word in LaTeX usando C# con Aspose.Words. | {{< /blocks/products/pf/tutorial-page-section >}} diff --git a/words/italian/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/_index.md b/words/italian/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/_index.md new file mode 100644 index 0000000000..7004968611 --- /dev/null +++ b/words/italian/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/_index.md @@ -0,0 +1,212 @@ +--- +category: general +date: 2026-04-28 +description: Converti DOCX in TXT ed esporta le equazioni di Word in LaTeX usando + Aspose.Words. Scopri come salvare Word come TXT e gestire gli oggetti matematici + in pochi passaggi. +draft: false +keywords: +- convert docx to txt +- convert word equations to latex +- convert word to plain text +- save word as txt +- export equations as latex +language: it +og_description: Converti DOCX in TXT ed esporta le equazioni Word in LaTeX con un + semplice snippet C#. Guida completa, codice e consigli. +og_title: Converti DOCX in TXT – Esporta le equazioni Word in LaTeX +tags: +- C# +- Aspose.Words +- Document Conversion +title: Converti DOCX in TXT – Esporta le equazioni di Word in LaTeX in C# +url: /it/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/ +--- + +{{< blocks/products/pf/main-wrap-class >}} +{{< blocks/products/pf/main-container >}} +{{< blocks/products/pf/tutorial-page-section >}} + +# Converti DOCX in TXT – Esporta le equazioni Word in LaTeX + +Hai mai avuto bisogno di **convertire docx in txt** ma temuto che la matematica nel tuo file Word si trasformasse in un pasticcio incomprensibile? Non sei solo. In molti progetti di ingegneria o accademici, il documento sorgente è in .docx, ma gli strumenti a valle comprendono solo plain‑text o LaTeX. La buona notizia? Con poche righe di C# e Aspose.Words puoi **convertire docx in txt** *e* mantenere ogni equazione come codice LaTeX pulito. + +In questo tutorial percorreremo l’intero processo: caricare un .docx, configurare le opzioni di salvataggio in modo che gli oggetti Office Math diventino LaTeX, e infine scrivere il risultato in un file .txt. Alla fine saprai come **save word as txt**, **convert word to plain text**, e **export equations as latex** senza dover setacciare la documentazione dell’API. + +## Cosa imparerai + +- Le chiamate API esatte necessarie per **convertire docx in txt** mantenendo le equazioni. +- Perché scegliere `OfficeMathExportMode.LaTeX` è il metodo consigliato per **convertire word equations to latex**. +- Come gestire casi limite comuni, come font mancanti o funzionalità di equazione non supportate. +- Un programma C# completo, pronto‑da‑eseguire, che puoi inserire in qualsiasi progetto .NET. + +### Prerequisiti + +- .NET 6.0 o successivo (il codice funziona anche su .NET Framework 4.7+). +- Una licenza per Aspose.Words per .NET (la versione di prova gratuita è valida per la valutazione). +- Un documento Word (`input.docx`) che contiene almeno un oggetto Office Math. + +Se li hai, iniziamo. + +## Passo 1: Installa Aspose.Words + +Prima che venga eseguito qualsiasi codice, è necessaria la libreria. Apri un terminale nella cartella del tuo progetto ed esegui: + +```bash +dotnet add package Aspose.Words +``` + +Questo scarica l’ultima versione stabile (al 2026‑04‑28 v24.12). Non sono richiesti DLL aggiuntivi. + +## Passo 2: Carica il documento sorgente + +La prima cosa che facciamo è leggere il file .docx in un oggetto `Document`. Questo oggetto ci dà pieno accesso alla struttura del file, includendo sequenze di testo, immagini e oggetti matematici. + +```csharp +using Aspose.Words; +using Aspose.Words.Saving; + +class Program +{ + static void Main() + { + // Step 2: Load the source document + Document doc = new Document(@"YOUR_DIRECTORY\input.docx"); +``` + +> **Perché è importante:** Caricare il documento crea una rappresentazione in memoria, così in seguito possiamo modificare come ogni elemento viene scritto. Se il file non viene trovato, Aspose lancia una `FileNotFoundException`, che potresti voler gestire nel codice di produzione. + +## Passo 3: Configura le opzioni di salvataggio TXT per la matematica LaTeX + +Per impostazione predefinita, `Document.Save` scrive plain text e **scarta** qualsiasi Office Math. Per mantenere quelle equazioni, impostiamo `OfficeMathExportMode` su `LaTeX`. Questo indica all’esportatore di tradurre ogni equazione nella sua equivalente LaTeX. + +```csharp + // Step 3: Configure TXT save options to export Office Math as LaTeX + TxtSaveOptions txtOptions = new TxtSaveOptions + { + OfficeMathExportMode = OfficeMathExportMode.LaTeX, + // Optional: preserve line breaks as they appear in the original Word file + PreserveTableLayout = true + }; +``` + +> **Suggerimento:** Se ti servono solo i caratteri Unicode grezzi dell’equazione (ad esempio per un’anteprima veloce), puoi usare `OfficeMathExportMode.Text`. Ma per la maggior parte dei flussi scientifici, `LaTeX` è lo standard di riferimento perché è universalmente compreso dai processori LaTeX. + +## Passo 4: Salva il documento come plain‑text + +Ora scriviamo il contenuto trasformato in un file `.txt`. Il file conterrà paragrafi regolari, elenchi puntati e—grazie al passo precedente—snippet LaTeX per ogni equazione. + +```csharp + // Step 4: Save the document as plain‑text using the configured options + doc.Save(@"YOUR_DIRECTORY\Math.txt", txtOptions); + } +} +``` + +Quando apri `Math.txt` vedrai qualcosa del genere: + +``` +In this report we derive the quadratic formula: +\[ +x = \frac{-b \pm \sqrt{b^{2} - 4ac}}{2a} +\] + +The end. +``` + +Nota i delimitatori `\[` … `\]`? Sono i blocchi matematici LaTeX generati automaticamente. + +## Passo 5: Verifica l’output (Opzionale ma consigliato) + +È facile perdere un problema di conversione sottile, soprattutto quando le equazioni contengono simboli personalizzati. Un rapido controllo di coerenza è alimentare il `.txt` generato a un compilatore LaTeX (ad esempio `pdflatex`) e verificare se compila senza errori. + +```bash +pdflatex -interaction=nonstopmode Math.txt +``` + +Se la compilazione ha successo, hai effettivamente **convert word equations to latex** e **convert docx to txt** in un unico passo. Se incontri errori, cerca messaggi su comandi non definiti—di solito indicano una funzionalità dell’equazione che Aspose.Words non può tradurre (ad esempio certe notazioni di matrici). In tali casi, puoi tornare a `OfficeMathExportMode.MathML` e post‑processare il MathML in LaTeX con un altro strumento. + +## Problemi comuni e come evitarli + +| Problema | Perché accade | Soluzione | +|----------|----------------|----------| +| Font mancanti | Aspose.Words ha bisogno del font per renderizzare correttamente i simboli. | Installa il font mancante sulla macchina o incorporalo nel .docx. | +| Equazioni complesse non esportate | Alcune funzionalità più recenti di Office Math non sono ancora mappate su LaTeX. | Usa `OfficeMathExportMode.MathML` poi converti con una libreria MathML‑to‑LaTeX. | +| Righe vuote extra | Il salvataggio in plain‑text preserva le interruzioni di paragrafo, aggiungendo spazi bianchi. | Imposta `txtOptions.AddBidiMarks = false` o post‑processa il file con uno script semplice. | + +## Esempio completo funzionante (pronto per copia‑incolla) + +Di seguito trovi l’intero programma, pronto per la compilazione. Sostituisci `YOUR_DIRECTORY` con la cartella che contiene il tuo `input.docx`. + +```csharp +using System; +using Aspose.Words; +using Aspose.Words.Saving; + +namespace DocxToTxtWithLatex +{ + class Program + { + static void Main() + { + try + { + // Load the source document + Document doc = new Document(@"C:\Docs\input.docx"); + + // Configure save options: export equations as LaTeX + TxtSaveOptions txtOptions = new TxtSaveOptions + { + OfficeMathExportMode = OfficeMathExportMode.LaTeX, + PreserveTableLayout = true, + AddBidiMarks = false + }; + + // Save as plain‑text + string outputPath = @"C:\Docs\Math.txt"; + doc.Save(outputPath, txtOptions); + + Console.WriteLine($"Successfully converted DOCX to TXT. Output at: {outputPath}"); + } + catch (Exception ex) + { + Console.Error.WriteLine($"Conversion failed: {ex.Message}"); + } + } + } +} +``` + +Eseguendo questo programma **save word as txt** trasformando ogni blocco Office Math in LaTeX, otterrai un file plain‑text pulito e ricercabile. + +## Prossimi passi e argomenti correlati + +- **Conversione batch:** Avvolgi la logica sopra in un ciclo `foreach` per elaborare un’intera cartella di file .docx. +- **Combina con la generazione PDF:** Dopo aver ottenuto gli snippet LaTeX, inseriscili in una pipeline PDF (ad esempio `PdfSharp` + `MiKTeX`) per produrre report PDF. +- **Export equations as latex** per altri formati: Aspose.Words supporta anche `SaveFormat.Markdown`, che può incorporare LaTeX automaticamente. +- **Ottimizzazione delle prestazioni:** Per documenti di grandi dimensioni, riutilizza la stessa istanza di `TxtSaveOptions` e disabilita funzionalità non necessarie come `AddBidiMarks`. + +--- + +### Esempio di immagine (Opzionale) + +Se preferisci un’indicazione visiva, ecco uno screenshot del file di output in Notepad++. + +![output della conversione da docx a txt che mostra le equazioni LaTeX](convert-docx-to-txt-output.png) + +*(Testo alternativo: “output della conversione da docx a txt che mostra le equazioni LaTeX” – soddisfa il requisito della parola chiave principale.)* + +--- + +## Conclusione + +Abbiamo appena mostrato un metodo affidabile per **convertire docx in txt** mantenendo ogni equazione come LaTeX pulito. La chiave è il flag `OfficeMathExportMode.LaTeX`, che trasforma il formato matematico proprietario di Word in qualcosa che qualsiasi motore LaTeX può comprendere. Con il codice completo sopra puoi **save word as txt**, **convert word to plain text**, e **export equations as latex** in un’unica esecuzione autonoma. + +Sentiti libero di sperimentare—cambia l’estensione di output in `.md` per Markdown, o integra lo snippet in una pipeline di elaborazione documenti più ampia. Se incontri problemi, lascia un commento qui sotto; sarò felice di aiutarti a risolverli. + +Buon coding! + +{{< /blocks/products/pf/tutorial-page-section >}} +{{< /blocks/products/pf/main-container >}} +{{< /blocks/products/pf/main-wrap-class >}} +{{< blocks/products/products-backtop-button >}} \ No newline at end of file diff --git a/words/japanese/net/ai-powered-document-processing/_index.md b/words/japanese/net/ai-powered-document-processing/_index.md index f996a6e25b..6eb08d38d7 100644 --- a/words/japanese/net/ai-powered-document-processing/_index.md +++ b/words/japanese/net/ai-powered-document-processing/_index.md @@ -38,10 +38,11 @@ ## AIを活用したドキュメント処理チュートリアル タイトル | 説明 | | --- | --- | -| [AIモデルの操作](./working-with-ai-model/) Aspose.Words for .NET を使ってAIを活用したドキュメント要約を作成する方法を学びましょう。ドキュメント管理を強化するための簡単な手順をご紹介します。 | -| [Google AI モデルの操作](./working-with-google-ai-model/) Aspose.Words for .NET と Google AI を使用してドキュメント処理を強化し、簡潔な要約を簡単に作成します。 | -| [オープンAIモデルの使用](./working-with-open-ai-model/) Aspose.Words for .NETとOpenAIの強力なモデルを活用し、効率的なドキュメント要約を実現しましょう。この包括的なガイドを今すぐご覧ください。 | -| [要約オプションの操作](./working-with-summarize-options/) 迅速な分析情報を得るために AI モデルを統合するステップバイステップ ガイドを使用して、Aspose.Words for .NET を使用して Word 文書を効果的に要約する方法を学びます。 | +| [AIモデルの操作](./working-with-ai-model/) | Aspose.Words for .NET を使ってAIを活用したドキュメント要約を作成する方法を学びましょう。ドキュメント管理を強化するための簡単な手順をご紹介します。 | +| [Google AI モデルの操作](./working-with-google-ai-model/) | Aspose.Words for .NET と Google AI を使用してドキュメント処理を強化し、簡潔な要約を簡単に作成します。 | +| [オープンAIモデルの使用](./working-with-open-ai-model/) | Aspose.Words for .NETとOpenAIの強力なモデルを活用し、効率的なドキュメント要約を実現しましょう。この包括的なガイドを今すぐご覧ください。 | +| [要約オプションの操作](./working-with-summarize-options/) | 迅速な分析情報を得るために AI モデルを統合するステップバイステップ ガイドを使用して、Aspose.Words for .NET を使用して Word 文書を効果的に要約する方法を学びます。 | +| [C# でローカル LLM に接続 – 完全プログラミングガイド](./connect-to-local-llm-in-c-complete-programming-guide/) | Aspose.Words for .NET とローカル LLM を統合し、C# で高度なテキスト生成と要約を実装する方法をステップバイステップで解説します。 | {{< /blocks/products/pf/tutorial-page-section >}} diff --git a/words/japanese/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/_index.md b/words/japanese/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/_index.md new file mode 100644 index 0000000000..80d481f6f6 --- /dev/null +++ b/words/japanese/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/_index.md @@ -0,0 +1,258 @@ +--- +category: general +date: 2026-04-28 +description: C# からローカル LLM に接続し、大規模言語モデルに Word 文書の読み込みを指示し、ローカル LLM を呼び出してテキストを自動的に書き換える。ステップバイステップのコードが含まれています。 +draft: false +keywords: +- connect to local llm +- prompt large language model +- load word document +- call local llm +- rewrite text automatically +language: ja +og_description: C#からローカルLLMに接続し、大規模言語モデルへのプロンプト方法、Word文書の読み込み、ローカルLLMの呼び出し、そして数分でテキストを自動的に書き換える方法を確認できます。 +og_title: C#でローカルLLMに接続する – 完全プログラミングガイド +tags: +- Aspose.Words +- C# +- LLM +- AI Automation +title: C#でローカルLLMに接続する – 完全プログラミングガイド +url: /ja/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/ +--- + +{{< blocks/products/pf/main-wrap-class >}} +{{< blocks/products/pf/main-container >}} +{{< blocks/products/pf/tutorial-page-section >}} + +# C#でローカルLLMに接続 – 完全プログラミングガイド + +.NET アプリから **connect to local llm** して、Word ファイルとやり取りさせたいことはありませんか? あなたは一人ではありません。このガイドでは、ローカルLLM に接続し、**prompt large language model**、Word 文書をロードし、**call local llm**、そして最終的に **rewrite text automatically** する全プロセスを解説します。最後まで実行可能なサンプルができ、任意の段落をフォーマルな口調に変換でき、外部 API キーは一切不要です。 + +## このチュートリアルでカバーする内容 + +必要な NuGet パッケージをインストールし、シンプルなローカル LLM エンドポイント(例: ポート 11434 の Ollama)を起動します。その後、Aspose.Words を使って `.docx` ファイルを読み込み、段落を LLM に送信し、書き換えたバージョンを受け取って同じ文書に書き戻します。さらに、null 段落、非同期破棄、エンコーディングの問題といった一般的な落とし穴への対処方法も紹介するので、コードはデモだけでなく本番環境でも動作します。 + +### 前提条件 + +- .NET 6.0 SDK 以上(.NET 8 でも可) +- Visual Studio 2022 または C# 拡張機能付き VS Code +- **Aspose.Words for .NET**(無料トライアルで問題ありません) +- `/api/generate` 契約に対応したローカルホストの LLM(例: Ollama、LMStudio) +- C# の async/await の基本的な知識 + +> **Pro tip:** まだ Ollama をインストールしていない場合は `ollama serve` を実行し、`ollama pull llama3` でモデルを取得してください。デフォルトの HTTP エンドポイントは `http://localhost:11434/api/generate` です。 + +## ステップ1: 必要なパッケージをインストール + +まず、プロジェクトに Aspose.Words と Aspose.Words.AI の NuGet パッケージを追加します。 + +```bash +dotnet add package Aspose.Words +dotnet add package Aspose.Words.AI +``` + +これらのライブラリは **load word document** 機能と、HTTP リクエストを手作業で作成せずに **call local llm** できる薄いラッパーを提供します。 + +## ステップ2: ローカルLLMエンドポイントに接続 + +ローカルでホストされたモデルへの接続は、`LocalLargeLanguageModel` をインスタンス化するだけで完了します。コンストラクタには生成エンドポイントの完全な URL を渡します。 + +```csharp +using Aspose.Words.AI; +using Aspose.Words; +using System.Threading.Tasks; + +// Create a client that talks to the LLM running on localhost +var localLlm = new LocalLargeLanguageModel("http://localhost:11434/api/generate"); +``` + +なぜエンドポイントをクラスでラップするのか? `LocalLargeLanguageModel` は JSON シリアライズ、リトライ、ストリーミング応答を自動で処理してくれるため、`HttpClient` をいじる代わりにプロンプトロジックに集中できます。 + +## ステップ3: ソースWord文書をロード + +次に、文書をメモリに取り込みます。Aspose.Words は事実上すべての Word フォーマットに対応しているので、`Document` は Office がインストールされていなくても `input.docx` を解析できます。 + +```csharp +// Path to the source file – adjust as needed +string inputPath = Path.Combine(Environment.CurrentDirectory, "input.docx"); + +// Load the document; throws if the file is missing or corrupted +Document sourceDocument = new Document(inputPath); +``` + +ストリーム(例: ASP.NET でアップロードされたファイル)で処理したい場合は、ファイルパスを `MemoryStream` に置き換えて `Document` コンストラクタに渡すだけです。 + +## ステップ4: 現在の段落テキストを抽出 + +`DocumentBuilder` を使って文書を操作します。この例では **the first paragraph** を書き換えますが、`sourceDocument.GetChildNodes(NodeType.Paragraph, true)` をループすれば多数の段落を処理できます。 + +```csharp +// Builder gives us a cursor inside the document +DocumentBuilder docBuilder = new DocumentBuilder(sourceDocument); + +// Grab the text of the paragraph where the builder is currently positioned +string originalParagraph = docBuilder.CurrentParagraph?.GetText() ?? string.Empty; + +// Safety check – avoid sending empty strings to the LLM +if (string.IsNullOrWhiteSpace(originalParagraph)) +{ + Console.WriteLine("No paragraph found at the current cursor position."); + return; +} +``` + +`?.` 演算子は、文書が空の場合に `NullReferenceException` が発生するのを防ぎます。これは初心者がつまずきやすい **edge cases** の一つです。 + +## ステップ5: LLMに段落を書き換えるようプロンプト + +いよいよ **prompt large language model** を実行します。プロンプトはプレーンな英語で、ラッパーが JSON に変換してローカルエンドポイントに送ります。 + +```csharp +// Build a friendly instruction for the model +string prompt = $"Rewrite the following sentence in a more formal tone:\n{originalParagraph}"; + +// Await the model's response – this is an async call +string rewrittenParagraph = await localLlm.PromptAsync(prompt); +``` + +なぜこのようにリクエストを構成するのか? LLM は明確で単一タスクの指示に最もよく応答します。コロンの後に改行を入れることで指示とコンテンツを分離し、モデルがプロンプトをそのままエコーしてしまう可能性を減らします。 + +**期待される出力** – `originalParagraph` が `"Hey, what's up?"` の場合、LLM は次のように返すかもしれません: + +> “Good day, how may I assist you?” + +結果は次のように出力して確認できます: + +```csharp +Console.WriteLine("Original: " + originalParagraph); +Console.WriteLine("Rewritten: " + rewrittenParagraph); +``` + +## ステップ6: 書き換えたテキストを文書に挿入 + +新しいテキストが手に入ったら、古い段落を置き換えます。`DocumentBuilder.Writeln` は改行を書き込みカーソルを前進させるので、追記に最適です。*replace* したい場合は、書き込む前に `docBuilder.CurrentParagraph.RemoveAllChildren()` を使用してください。 + +```csharp +// Option A – Append a new paragraph (keeps the original) +docBuilder.Writeln(rewrittenParagraph); + +// Option B – Replace the existing paragraph (uncomment to use) +// docBuilder.CurrentParagraph.RemoveAllChildren(); +// docBuilder.CurrentParagraph.AppendChild(new Run(docBuilder.Document, rewrittenParagraph)); +``` + +両方のアプローチを示しているので、ワークフローに合う方を選んでください。 + +## ステップ7: 更新された文書を保存 + +最後に、変更を新しいファイルに永続化します。Aspose.Words は拡張子に基づいて自動的にフォーマットを選択します。 + +```csharp +string outputPath = Path.Combine(Environment.CurrentDirectory, "output.docx"); +sourceDocument.Save(outputPath); + +Console.WriteLine($"Document saved to {outputPath}"); +``` + +Word で `output.docx` を開くと、段落がフォーマルな口調に変わっていることが確認できます。 + +## 完全な動作例 + +以下は **complete, self‑contained program** です。コンソールプロジェクトにコピペし、NuGet パッケージを復元して実行してください。ローカル LLM が起動している以外は追加設定は不要です。 + +```csharp +using Aspose.Words.AI; +using Aspose.Words; +using System; +using System.IO; +using System.Threading.Tasks; + +class Program +{ + static async Task Main() + { + // ------------------------------------------------- + // Step 1: Connect to the locally hosted LLM endpoint + // ------------------------------------------------- + var localLlm = new LocalLargeLanguageModel("http://localhost:11434/api/generate"); + + // ------------------------------------------------- + // Step 2: Load the source Word document + // ------------------------------------------------- + string inputPath = Path.Combine(Environment.CurrentDirectory, "input.docx"); + Document sourceDocument = new Document(inputPath); + + // ------------------------------------------------- + // Step 3: Retrieve the text of the current paragraph + // ------------------------------------------------- + DocumentBuilder docBuilder = new DocumentBuilder(sourceDocument); + string originalParagraph = docBuilder.CurrentParagraph?.GetText() ?? string.Empty; + + if (string.IsNullOrWhiteSpace(originalParagraph)) + { + Console.WriteLine("No paragraph found at the current cursor position."); + return; + } + + // ------------------------------------------------- + // Step 4: Ask the LLM to rewrite the paragraph in a formal tone + // ------------------------------------------------- + string prompt = $"Rewrite the following sentence in a more formal tone:\n{originalParagraph}"; + string rewrittenParagraph = await localLlm.PromptAsync(prompt); + + // ------------------------------------------------- + // Step 5: Insert the rewritten text back into the document + // ------------------------------------------------- + docBuilder.Writeln(rewrittenParagraph); + + // ------------------------------------------------- + // Step 6: Save the updated document + // ------------------------------------------------- + string outputPath = Path.Combine(Environment.CurrentDirectory, "output.docx"); + sourceDocument.Save(outputPath); + + Console.WriteLine("Original paragraph:"); + Console.WriteLine(originalParagraph); + Console.WriteLine("\nRewritten paragraph:"); + Console.WriteLine(rewrittenParagraph); + Console.WriteLine($"\nDocument saved to {outputPath}"); + } +} +``` + +### 実行時の期待結果 + +1. コンソールに元の段落と書き換え後の段落が表示されます。 +2. `output.docx` が `input.docx` の隣に生成されます。 +3. ファイルを開くと、新しいフォーマルな段落が元の段落の後に挿入(または代替コードに切り替えた場合は置き換え)されていることが確認できます。 + +## 一般的なエッジケースの対処 + +| Situation | Solution | +|-----------|----------| +| **Empty or whitespace‑only paragraph** | `string.IsNullOrWhiteSpace` をチェックしてからプロンプトを送信します(Step 3 参照)。 | +| **LLM returns an error or empty string** | `PromptAsync` を `try/catch` でラップし、失敗時は元のテキストにフォールバックします。 | +| **Multiple paragraphs need rewriting** | `sourceDocument.GetChildNodes(NodeType.Paragraph, true)` をループし、同じプロンプトロジックを適用します。 | +| **Large documents cause latency** | 段落をバッチ化して 1 回のリクエストで送信します(1 回の呼び出しで最大 4 KB のプロンプトが目安)。 | +| **Non‑ASCII characters get garbled** | LLM エンドポイントが UTF‑8 を使用していることを確認します(ほとんどの最新モデルは対応)。 | + +## 次のステップと関連トピック + +- **Prompt large language model** にスタイルガイドや長さ制限など、よりリッチな指示を付与する。 +- **call local llm** を Web API で利用し、文書自動化をサービスとして公開する。 +- 高スループットシナリオ向けに **load word document** を並列ストリームで処理する方法を探る。 +- この手法と **rewrite text automatically** を組み合わせて、メール大量生成やレポート標準化に活用する。 + +さらに深掘りしたい場合は、Aspose の **document merging** に関するドキュメントと、カスタムサンプリングパラメータ用の Ollama API リファレンスをご覧ください。 + +## 結論 + +今回、C# から **connect to local llm** し、**prompt large language model**、**load word document**、**call local llm**、そして **rewrite text automatically** する方法を、単一の実行可能コンソールアプリで示しました。このパターンはスケール可能で、プロンプトを差し替えたり段落をループ処理したり、ASP.NET エンドポイントとして公開したりと応用が利きます。重要なポイントは、ローカル AI モデルを従来の文書処理ライブラリと緊密に統合でき、信頼できるオンプレミス環境を離れることなく強力な自動化が実現できるということです。 + +スレッドに関する質問があれば、 + +{{< /blocks/products/pf/tutorial-page-section >}} +{{< /blocks/products/pf/main-container >}} +{{< /blocks/products/pf/main-wrap-class >}} +{{< blocks/products/products-backtop-button >}} \ No newline at end of file diff --git a/words/japanese/net/programming-with-markdownsaveoptions/_index.md b/words/japanese/net/programming-with-markdownsaveoptions/_index.md index 353fd34e79..880ddba830 100644 --- a/words/japanese/net/programming-with-markdownsaveoptions/_index.md +++ b/words/japanese/net/programming-with-markdownsaveoptions/_index.md @@ -41,6 +41,7 @@ MarkdownSaveOptions を使用した Words Processing は、Aspose.Words for .NET | [DOCX を Markdown に変換する際の画像リネーム方法](./how-to-rename-images-when-converting-docx-to-markdown/) Aspose.Words for .NET を使用して、DOCX から Markdown へ変換する際に画像ファイル名を自動的に変更する手順を解説します。 | | [Aspose.Words を使用して docx を Markdown に保存 – 完全 C# ガイド](./save-docx-as-markdown-with-aspose-words-full-c-guide/) Aspose.Words for .NET を使い、docx ファイルを Markdown 形式に変換して保存する完全な C# 手順を解説します。 | | [Word 画像を保存 – Aspose を使用して Word を Markdown に変換](./save-word-images-convert-word-to-markdown-with-aspose/) Aspose.Words for .NET を使用して、Word 文書の画像を抽出し、Markdown 形式に変換する方法をステップバイステップで解説します。 | +| [Markdown 画像の相対パス – Word を Markdown に変換](./markdown-image-relative-path-convert-word-to-markdown/) Aspose.Words for .NET を使用して、画像の相対パスを保持しながら Word 文書を Markdown に変換する方法を解説します。 | {{< /blocks/products/pf/tutorial-page-section >}} diff --git a/words/japanese/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/_index.md b/words/japanese/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/_index.md new file mode 100644 index 0000000000..c3a048013f --- /dev/null +++ b/words/japanese/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/_index.md @@ -0,0 +1,202 @@ +--- +category: general +date: 2026-04-28 +description: Word を Markdown に変換する際に画像の相対パスを設定する方法、Word から画像を抽出する方法、エクスポートされた画像用のリソースフォルダーを作成する方法を学びましょう。 +draft: false +keywords: +- markdown image relative path +- convert word to markdown +- extract images from word +- create resources folder +- export images from docx +language: ja +og_description: Word を Markdown に変換する際に、画像の相対パスを設定し、Word から画像を抽出して、エクスポートされた画像用の resources + フォルダーを作成します。 +og_title: Markdown画像の相対パス – WordをMarkdownに変換 +tags: +- Aspose.Words +- C# +- Markdown +- Image Export +title: Markdown画像の相対パス – WordをMarkdownに変換 +url: /ja/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/ +--- + +{{< blocks/products/pf/main-wrap-class >}} +{{< blocks/products/pf/main-container >}} +{{< blocks/products/pf/tutorial-page-section >}} + +# markdown image relative path – Word を Markdown に変換 + +Word を markdown に変換する際に **markdown image relative path** が必要になったことはありませんか? あなただけではありません。生成された Markdown がフラットなフォルダー内の画像を指してしまい、静的サイトや GitHub リポジトリで期待する相対リンク構造が壊れるという問題に多くの開発者が直面しています。 + +このチュートリアルでは、**Word から画像を抽出し**、**リソースフォルダーを作成**し、画像参照を書き換えてクリーンな *markdown image relative path* を使用する、完全なエンドツーエンドのソリューションを順を追って解説します。最後まで読むと、公開準備が整った `.md` ファイルと、元の `.docx` から抽出したすべての画像が整理された `Resources` ディレクトリが手に入ります。 + +> **得られるもの:** 単一の C# プログラム(外部スクリプト不要)、各パーツが重要な理由の明確な説明、そして自分のプロジェクトにコピーペーストできる実用的なヒント集です。 + +--- + +## 前提条件 + +- **.NET 6.0** 以降がインストールされていること(.NET Framework 4.7+ をターゲットにすることも可能ですが、新規プロジェクトには .NET 6 が最適です)。 +- **Aspose.Words for .NET**(執筆時点での最新 NuGet パッケージ、バージョン 23.12)。以下でインストールします: + ```bash + dotnet add package Aspose.Words + ``` +- 画像を含む Word ドキュメント(例: `WithImages.docx`)を用意します。 +- 出力する markdown と画像を保存したいフォルダー、例: `C:\Projects\MarkdownExport`。 + +追加のライブラリは不要です。その他はすべて Aspose.Words が処理します。 + +--- + +## Step 1: ソース Word ドキュメントをロードする(Word を markdown に変換する出発点) + +```csharp +using System; +using System.IO; +using Aspose.Words; +using Aspose.Words.Saving; + +class Program +{ + static void Main() + { + // Adjust the path to point at your own .docx file. + string sourcePath = @"C:\Projects\MarkdownExport\WithImages.docx"; + + // Load the document – this is where Aspose.Words parses the Word file. + Document doc = new Document(sourcePath); + + // The rest of the workflow follows… + } +} +``` + +*Why this matters:* ドキュメントをロードすると内部ノードツリーにアクセスでき、後で **export images from docx** が必要になる画像パーツが含まれています。ロードに失敗すると以降の手順がすべて実行されないため、パスとファイル権限を再確認してください。 + +--- + +## Step 2: カスタムコールバックで `MarkdownSaveOptions` を設定する(リソースフォルダー作成の核心) + +`ResourceSavingCallback` を使用すると、Aspose.Words が画像ファイルを書き込むたびに介入できます。コールバック内で **Resources サブフォルダーを作成**し、参照を調整して生成された markdown が *markdown image relative path* を使用するようにします。 + +```csharp +// Inside Main(), after loading the document: +string outputFolder = @"C:\Projects\MarkdownExport"; +string resourcesFolder = Path.Combine(outputFolder, "Resources"); + +// Make sure the folder exists before we start saving anything. +Directory.CreateDirectory(resourcesFolder); + +// Set up the Markdown save options. +MarkdownSaveOptions mdOptions = new MarkdownSaveOptions +{ + // Hook that runs for every image resource. + ResourceSavingCallback = new MyMarkdownResourceCallback(resourcesFolder) +}; + +// Save the document as Markdown. +string markdownPath = Path.Combine(outputFolder, "Doc.md"); +doc.Save(markdownPath, mdOptions); +``` + +`resourcesFolder` をコールバックのコンストラクタに渡していることに注目してください。これによりフォルダー パスが柔軟になり、コード全体で文字列をハードコーディングすることを防げます。 + +--- + +## Step 3: **リソースフォルダーを作成**しパスを書き換えるコールバックを実装する + +```csharp +/// +/// Handles image extraction and path rewriting for markdown export. +/// +class MyMarkdownResourceCallback : IResourceSavingCallback +{ + private readonly string _resourcesFolder; + + public MyMarkdownResourceCallback(string resourcesFolder) + { + _resourcesFolder = resourcesFolder; + } + + public void ResourceSaving(ResourceSavingArgs args) + { + // 1️⃣ Build the full file system path where the image will be stored. + string targetPath = Path.Combine(_resourcesFolder, args.ResourceFileName); + + // 2️⃣ Ensure the directory exists (in case Aspose creates sub‑folders). + Directory.CreateDirectory(Path.GetDirectoryName(targetPath)); + + // 3️⃣ Write the image stream to disk. + using (FileStream fileStream = File.Create(targetPath)) + { + args.Stream.CopyTo(fileStream); + } + + // 4️⃣ Update the markdown reference to use a relative path. + // This is the crucial line that gives us the markdown image relative path. + args.ResourceFileName = Path.Combine("Resources", args.ResourceFileName); + } +} +``` + +*Why this works:* `args.Stream` には生の画像バイトが含まれています。これを `Resources` フォルダー内のファイルにコピーすることで **export images from docx** を安全に行います。その後、`args.ResourceFileName` を相対 URL(`Resources/image.png`)に置き換えます。後で Aspose.Words が markdown を書き出す際にその文字列がそのまま挿入され、目的の *markdown image relative path* が得られます。 + +--- + +## Step 4: 生成された Markdown を確認する(最終出力のイメージ) + +`Doc.md` を任意のテキストエディタで開きます。以下のような内容が表示されるはずです: + +```markdown +# Sample Heading + +Here is an inline picture: + +![Image 0](Resources/Image_0.png) + +And a picture inside a table: + +![Image 1](Resources/Image_1.jpg) +``` + +重要なのは、各画像参照が `Resources/...` を指していることです。これが求めていた **markdown image relative path** です。 + +![markdown image relative path example](example.png "markdown image relative path example") + +*Tip:* 相対リンクを解釈するビューア(VS Code のプレビュー、GitHub、または静的サイトジェネレータ)で markdown を開くと、追加設定なしで画像が正しく表示されます。 + +--- + +## Step 5: よくある落とし穴とプロのコツ + +| Issue | Why it happens | How to fix it | +|-------|----------------|---------------| +| 画像が `Resources` ではなくルートフォルダーに保存される | コールバックが設定されていない、または `args.ResourceFileName` が上書きされていないためです。 | `ResourceSavingCallback` が `doc.Save` を呼び出す **前に** 設定されているか再確認してください。 | +| ファイル名に不正な文字が含まれる | Word は画像にスペースや Unicode 記号を含む名前を付けることがあります。 | コールバック内で `Path.GetInvalidFileNameChars()` を使用して `args.ResourceFileName` をサニタイズしてください。 | +| 大きなドキュメントの処理に時間がかかる | 各画像が同期的に書き込まれているためです。 | .NET 6 以降でパフォーマンスが必要な場合は、非同期 I/O(`await args.Stream.CopyToAsync(fileStream)`)に切り替えてください。 | +| markdown を移動すると相対パスが壊れる | パスが markdown ファイルの位置に対して相対的であるためです。 | `Doc.md` と `Resources` フォルダーを同じ場所に保つか、コールバックで別の相対プレフィックス(例: `../assets`)を使用するよう調整してください。 | + +--- + +## Step 6: ソリューションの拡張(さらに制御が必要な場合) + +- **Multiple output formats:** `MarkdownSaveOptions` を `HtmlSaveOptions` や `PdfSaveOptions` に置き換えても同じコールバックを使用できます。Aspose.Words はフォーマットに関係なくすべての画像でコールバックを呼び出します。 +- **Custom image naming:** 画像をリネームしたい場合(例: `figure-01.png`)、ファイルを書き込む前にコールバック内で `args.ResourceFileName` を変更してください。 +- **Embedding images as Base64:** `args.ResourceFileName` をデータ URI(`data:image/png;base64,...`)に設定し、ファイル書き込みを省略します。単一ファイルの markdown エクスポートに便利です。 + +--- + +## 結論 + +これで、**Word を markdown に変換**し、**word から画像を抽出**し、**リソースフォルダーを作成**し、すべての画像に対してクリーンな **markdown image relative path** を保証する完全に機能する C# プログラムが手に入りました。コードは自己完結型で、最新の Aspose.Words バージョンで動作し、最小限の手間で任意の .NET プロジェクトに組み込むことができます。 + +次のステップは? 生成された markdown を Hugo や Jekyll などの静的サイトジェネレータに流し込んでみるか、コールバックを試して画像を直接 Base64 文字列として埋め込んでみてください。SVG 画像や異常に大きなファイルなどのエッジケースに直面した場合は、上記の「よくある落とし穴」表を参照してください。小さな調整で問題は解決できることが多いです。 + +コーディングを楽しんで、markdown が常に正しいフォルダーを指すように! + +{{< /blocks/products/pf/tutorial-page-section >}} +{{< /blocks/products/pf/main-container >}} +{{< /blocks/products/pf/main-wrap-class >}} +{{< blocks/products/products-backtop-button >}} \ No newline at end of file diff --git a/words/japanese/net/programming-with-officemath/_index.md b/words/japanese/net/programming-with-officemath/_index.md index 711192f896..f46170005c 100644 --- a/words/japanese/net/programming-with-officemath/_index.md +++ b/words/japanese/net/programming-with-officemath/_index.md @@ -23,6 +23,7 @@ Aspose.Words for .NET の OfficeMath を使った Words Processing チュート | [数式](./math-equations/) Aspose.Words for .NET を使用して Word 文書に数式を設定する方法を学びます。例、FAQ などを交えたステップバイステップのガイドです。 | | [docx を txt に保存 – C# で Word Math を LaTeX にエクスポート](./save-docx-as-txt-export-word-math-to-latex-with-c/) C# を使用して、docx ファイルをテキスト形式に変換し、Word の数式を LaTeX 形式でエクスポートする方法を解説します。 | | [ドキュメントをTXTとして保存 – C#でWord MathをLaTeXにエクスポート](./save-document-as-txt-export-word-math-to-latex-in-c/) C# を使用して Word 文書を TXT 形式で保存し、数式を LaTeX にエクスポートする方法を解説します。 | +| [DOCX を TXT に変換 – C# で Word の数式を LaTeX にエクスポート](./convert-docx-to-txt-export-word-equations-to-latex-in-c/) C# を使用して DOCX を TXT に変換し、Word の数式を LaTeX 形式でエクスポートする方法を解説します。 | {{< /blocks/products/pf/tutorial-page-section >}} diff --git a/words/japanese/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/_index.md b/words/japanese/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/_index.md new file mode 100644 index 0000000000..82b9140e5d --- /dev/null +++ b/words/japanese/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/_index.md @@ -0,0 +1,206 @@ +--- +category: general +date: 2026-04-28 +description: Aspose.Words を使用して DOCX を TXT に変換し、Word の数式を LaTeX にエクスポートします。数ステップで + Word を TXT として保存し、数式オブジェクトを処理する方法を学びましょう。 +draft: false +keywords: +- convert docx to txt +- convert word equations to latex +- convert word to plain text +- save word as txt +- export equations as latex +language: ja +og_description: シンプルなC#スニペットでDOCXをTXTに変換し、Wordの数式をLaTeXにエクスポート。完全ガイド、コード、ヒントをご紹介。 +og_title: DOCX を TXT に変換 – Word の数式を LaTeX にエクスポート +tags: +- C# +- Aspose.Words +- Document Conversion +title: DOCX を TXT に変換 – C# で Word の数式を LaTeX にエクスポート +url: /ja/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/ +--- + +{{< blocks/products/pf/main-wrap-class >}} +{{< blocks/products/pf/main-container >}} +{{< blocks/products/pf/tutorial-page-section >}} + +# DOCX を TXT に変換 – Word の数式を LaTeX にエクスポート + +Word ファイル内の数式が文字化けしてしまうのではないかと心配しながら、**convert docx to txt** が必要になったことはありませんか? あなたは一人ではありません。多くのエンジニアリングや学術プロジェクトでは、ソース文書は .docx 形式で保存されている一方、下流のツールはプレーンテキストまたは LaTeX のみを理解します。良いニュースは、C# と Aspose.Words の数行で **convert docx to txt** ができ、すべての数式をきれいな LaTeX コードとして保持できることです。 + +このチュートリアルでは、プロセス全体を順に解説します:.docx の読み込み、Office Math オブジェクトを LaTeX に変換する保存オプションの設定、そして最終的に結果を .txt ファイルに書き出す方法です。最後まで読むと、**save word as txt**、**convert word to plain text**、**export equations as latex** を API ドキュメントを探さずに実行できるようになります。 + +## 学習できること + +- 数式を保持しながら **convert docx to txt** を実行するために必要な正確な API 呼び出し。 +- `OfficeMathExportMode.LaTeX` を選択することが **convert word equations to latex** の推奨方法である理由。 +- フォントが欠如している、または数式機能がサポートされていないといった一般的なエッジケースの対処方法。 +- 任意の .NET プロジェクトに組み込める、完全な実行可能 C# プログラム。 + +### 前提条件 + +- .NET 6.0 以降(コードは .NET Framework 4.7+ でも動作します)。 +- Aspose.Words for .NET のライセンス(評価用に無料トライアルが利用可能)。 +- 少なくとも 1 つの Office Math オブジェクトを含む Word 文書(`input.docx`)。 + +これらが揃ったら、さっそく始めましょう。 + +## 手順 1: Aspose.Words のインストール + +コードを実行する前にライブラリが必要です。プロジェクトフォルダーでターミナルを開き、以下を実行してください。 + +```bash +dotnet add package Aspose.Words +``` + +これにより最新の安定版(2026‑04‑28 時点の v24.12)が取得されます。追加の DLL は必要ありません。 + +## 手順 2: ソース文書の読み込み + +最初に行うのは、.docx ファイルを `Document` オブジェクトに読み込むことです。このオブジェクトにより、テキストラン、画像、数式オブジェクトなど、ファイルの構造全体にフルアクセスできます。 + +```csharp +using Aspose.Words; +using Aspose.Words.Saving; + +class Program +{ + static void Main() + { + // Step 2: Load the source document + Document doc = new Document(@"YOUR_DIRECTORY\input.docx"); +``` + +> **重要な理由:** 文書を読み込むことでメモリ上に表現が作成され、後で各要素の書き出し方法を調整できます。ファイルが見つからない場合、Aspose は `FileNotFoundException` をスローします。実運用コードではこれを捕捉した方が良いでしょう。 + +## 手順 3: LaTeX 数式用の TXT 保存オプションを設定 + +既定では、`Document.Save` はプレーンテキストを書き出し、Office Math を **除去** します。数式を保持するために、`OfficeMathExportMode` を `LaTeX` に設定します。これにより、エクスポーターは各数式を LaTeX 形式に変換します。 + +```csharp + // Step 3: Configure TXT save options to export Office Math as LaTeX + TxtSaveOptions txtOptions = new TxtSaveOptions + { + OfficeMathExportMode = OfficeMathExportMode.LaTeX, + // Optional: preserve line breaks as they appear in the original Word file + PreserveTableLayout = true + }; +``` + +> **プロのコツ:** 数式の生の Unicode 文字だけが必要な場合(例えば簡易プレビュー用)には、`OfficeMathExportMode.Text` を使用できます。ただし、ほとんどの科学的パイプラインでは、`LaTeX` が標準です。LaTeX プロセッサが普遍的に理解できるからです。 + +## 手順 4: 文書をプレーンテキストとして保存 + +これで変換された内容を `.txt` ファイルに書き出します。このファイルには通常の段落、箇条書き、そして前の手順のおかげで各数式の LaTeX スニペットが含まれます。 + +```csharp + // Step 4: Save the document as plain‑text using the configured options + doc.Save(@"YOUR_DIRECTORY\Math.txt", txtOptions); + } +} +``` + +`Math.txt` を開くと、以下のような内容が表示されます。 + +``` +In this report we derive the quadratic formula: +\[ +x = \frac{-b \pm \sqrt{b^{2} - 4ac}}{2a} +\] + +The end. +``` + +`\[` … `\]` のデリミタに注目してください。これらは自動生成された LaTeX 数式ブロックです。 + +## 手順 5: 出力の検証(任意だが推奨) + +特に数式にカスタム記号が含まれる場合、微妙な変換問題を見逃しやすいです。簡単なチェックとして、生成された `.txt` を LaTeX コンパイラ(例: `pdflatex`)に渡し、エラーなくコンパイルできるか確認してください。 + +```bash +pdflatex -interaction=nonstopmode Math.txt +``` + +コンパイルが成功すれば、**convert word equations to latex** と **convert docx to txt** を一度に実現したことになります。エラーが出た場合は、未定義コマンドに関するメッセージを探してください。これは通常、Aspose.Words が変換できない数式機能(例: 特定の行列表記)を示しています。そのような場合は `OfficeMathExportMode.MathML` にフォールバックし、別ツールで MathML を LaTeX に変換することができます。 + +## よくある落とし穴と回避策 + +| Issue | Why it Happens | Fix | +|-------|----------------|-----| +| フォントが欠如している | Aspose.Words はシンボルを正しく表示するためにフォントが必要です。 | 不足しているフォントをマシンにインストールするか、.docx に埋め込んでください。 | +| 複雑な数式がエクスポートされない | 一部の新しい Office Math 機能はまだ LaTeX にマッピングされていません。 | `OfficeMathExportMode.MathML` を使用し、MathML‑to‑LaTeX ライブラリで変換してください。 | +| 余分な空白行 | プレーンテキスト保存は段落区切りを保持するため、余分な空白が生じることがあります。 | `txtOptions.AddBidiMarks = false` を設定するか、シンプルなスクリプトでファイルを後処理してください。 | + +## 完全動作サンプル(コピー&ペースト可能) + +以下はコンパイル可能な完全なプログラムです。`YOUR_DIRECTORY` を `input.docx` が格納されているフォルダーに置き換えてください。 + +```csharp +using System; +using Aspose.Words; +using Aspose.Words.Saving; + +namespace DocxToTxtWithLatex +{ + class Program + { + static void Main() + { + try + { + // Load the source document + Document doc = new Document(@"C:\Docs\input.docx"); + + // Configure save options: export equations as LaTeX + TxtSaveOptions txtOptions = new TxtSaveOptions + { + OfficeMathExportMode = OfficeMathExportMode.LaTeX, + PreserveTableLayout = true, + AddBidiMarks = false + }; + + // Save as plain‑text + string outputPath = @"C:\Docs\Math.txt"; + doc.Save(outputPath, txtOptions); + + Console.WriteLine($"Successfully converted DOCX to TXT. Output at: {outputPath}"); + } + catch (Exception ex) + { + Console.Error.WriteLine($"Conversion failed: {ex.Message}"); + } + } + } +} +``` + +このプログラムを実行すると、すべての Office Math ブロックが LaTeX に変換され、**save word as txt** が実現され、検索可能なクリーンなプレーンテキストファイルが得られます。 + +## 次のステップと関連トピック + +- **バッチ変換:** 上記ロジックを `foreach` ループでラップし、フォルダー内のすべての .docx ファイルを処理します。 +- **PDF 生成との組み合わせ:** LaTeX スニペットを取得したら、PDF パイプライン(例: `PdfSharp` + `MiKTeX`)に渡して PDF レポートを作成します。 +- **他形式への数式エクスポート(latex):** Aspose.Words は `SaveFormat.Markdown` もサポートしており、LaTeX を自動的に埋め込むことができます。 +- **パフォーマンスチューニング:** 大規模文書では同じ `TxtSaveOptions` インスタンスを再利用し、`AddBidiMarks` など不要な機能を無効にします。 + +### 画像例(オプション) + +視覚的な手がかりが欲しい場合は、Notepad++ で表示した出力ファイルのスクリーンショットをご覧ください。 + +![LaTeX 数式を示す convert docx to txt の出力](convert-docx-to-txt-output.png) + +(Alt テキスト: “convert docx to txt output showing LaTeX equations” – 主要キーワード要件を満たしています。) + +## 結論 + +ここでは、**convert docx to txt** を実現し、すべての数式をきれいな LaTeX として保持する信頼できる方法を示しました。ポイントは `OfficeMathExportMode.LaTeX` フラグで、Word の独自数式形式を任意の LaTeX エンジンが理解できる形に変換します。上記の完全なコードサンプルを使用すれば、**save word as txt**、**convert word to plain text**、**export equations as latex** を単一の自己完結型実行で行えます。 + +自由に試してみてください。出力拡張子を `.md` に変更して Markdown にしたり、スニペットをより大規模な文書処理パイプラインに組み込んだりできます。問題が発生したら下にコメントを残してください。喜んでトラブルシューティングをお手伝いします。 + +コーディングを楽しんで! + +{{< /blocks/products/pf/tutorial-page-section >}} +{{< /blocks/products/pf/main-container >}} +{{< /blocks/products/pf/main-wrap-class >}} +{{< blocks/products/products-backtop-button >}} \ No newline at end of file diff --git a/words/korean/net/ai-powered-document-processing/_index.md b/words/korean/net/ai-powered-document-processing/_index.md index 8e5be14ea6..dcf2e13fc5 100644 --- a/words/korean/net/ai-powered-document-processing/_index.md +++ b/words/korean/net/ai-powered-document-processing/_index.md @@ -42,6 +42,7 @@ | [Google AI 모델 작업](./working-with-google-ai-model/) Aspose.Words for .NET과 Google AI를 사용하여 문서 처리 능력을 향상시키고 간결한 요약을 손쉽게 작성하세요. | | [오픈 AI 모델 작업](./working-with-open-ai-model/) | OpenAI의 강력한 모델을 기반으로 Aspose.Words for .NET을 사용하여 효율적인 문서 요약을 구현해 보세요. 지금 바로 이 종합 가이드를 살펴보세요. | | [요약 옵션 사용](./working-with-summarize-options/) | Aspose.Words for .NET을 사용하여 Word 문서를 효과적으로 요약하는 방법을 알아보고, AI 모델을 통합하여 빠른 통찰력을 얻는 방법에 대한 단계별 가이드를 살펴보세요. | +| [C#에서 로컬 LLM에 연결 – 완전 프로그래밍 가이드](./connect-to-local-llm-in-c-complete-programming-guide/) | C#을 사용해 로컬 LLM에 연결하고 활용하는 방법을 단계별로 안내합니다. | {{< /blocks/products/pf/tutorial-page-section >}} diff --git a/words/korean/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/_index.md b/words/korean/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/_index.md new file mode 100644 index 0000000000..80c03f83d5 --- /dev/null +++ b/words/korean/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/_index.md @@ -0,0 +1,262 @@ +--- +category: general +date: 2026-04-28 +description: C#에서 로컬 LLM에 연결하고 대형 언어 모델에 워드 문서를 로드하도록 프롬프트한 뒤, 로컬 LLM을 호출해 텍스트를 자동으로 + 재작성합니다. 단계별 코드가 포함되어 있습니다. +draft: false +keywords: +- connect to local llm +- prompt large language model +- load word document +- call local llm +- rewrite text automatically +language: ko +og_description: C#에서 로컬 LLM에 연결하고 대형 언어 모델에 프롬프트를 주는 방법, 워드 문서를 로드하고 로컬 LLM을 호출해 텍스트를 + 자동으로 몇 분 안에 재작성하는 과정을 확인하세요. +og_title: C#에서 로컬 LLM 연결하기 – 완전 프로그래밍 가이드 +tags: +- Aspose.Words +- C# +- LLM +- AI Automation +title: C#에서 로컬 LLM에 연결하기 – 완전한 프로그래밍 가이드 +url: /ko/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/ +--- + +{{< blocks/products/pf/main-wrap-class >}} +{{< blocks/products/pf/main-container >}} +{{< blocks/products/pf/tutorial-page-section >}} + +# C#에서 로컬 LLM에 연결하기 – 완전 프로그래밍 가이드 + +.NET 앱에서 **connect to local llm**을(를) 연결하고 Word 파일과 대화하게 하는 방법이 궁금했던 적이 있나요? 혼자가 아닙니다. 이 가이드에서는 전체 과정을 단계별로 살펴봅니다—connect to local llm, **prompt large language model**, Word 문서 로드, **call local llm**, 그리고 마지막으로 **rewrite text automatically**. 끝까지 진행하면 외부 API 키 없이도 모든 문단을 격식 있는 어조로 변환하는 실행 가능한 샘플을 얻을 수 있습니다. + +## 이 튜토리얼에서 다루는 내용 + +필요한 NuGet 패키지를 설치하고, 간단한 로컬 LLM 엔드포인트를 실행합니다(예: 포트 11434의 Ollama). 그 다음 Aspose.Words를 사용해 `.docx` 파일을 로드하고, 문단을 LLM에 전송하여 재작성된 버전을 받아 동일한 문서에 다시 씁니다. 또한 일반적인 함정—null 문단, async 해제, 인코딩 문제—을 처리하는 방법을 보여드리므로 코드가 데모가 아니라 실제 운영 환경에서도 동작합니다. + +### 필수 조건 + +- .NET 6.0 SDK 또는 그 이상(.NET 8도 사용 가능) +- Visual Studio 2022 또는 C# 확장 기능이 포함된 VS Code +- **Aspose.Words for .NET** (무료 체험으로 충분합니다) +- `/api/generate` 계약을 지원하는 로컬 호스팅 LLM (예: Ollama, LMStudio) +- C#에서 async/await에 대한 기본 지식 + +> **Pro tip:** Ollama를 아직 설치하지 않았다면 `ollama serve`를 실행하고 `ollama pull llama3`로 모델을 가져오세요. 기본 HTTP 엔드포인트는 `http://localhost:11434/api/generate`가 됩니다. + +--- + +## Step 1: 필수 패키지 설치 + +먼저, 프로젝트에 Aspose.Words와 Aspose.Words.AI NuGet 패키지를 추가합니다. + +```bash +dotnet add package Aspose.Words +dotnet add package Aspose.Words.AI +``` + +이 라이브러리들은 **load word document** 기능과 HTTP 요청을 직접 작성하지 않아도 되는 **call local llm**용 얇은 래퍼를 제공합니다. + +## Step 2: 로컬 LLM 엔드포인트에 연결하기 + +로컬에 호스팅된 모델에 연결하는 것은 `LocalLargeLanguageModel`을 인스턴스화하는 것만큼 간단합니다. 생성자는 생성 엔드포인트의 전체 URL을 기대합니다. + +```csharp +using Aspose.Words.AI; +using Aspose.Words; +using System.Threading.Tasks; + +// Create a client that talks to the LLM running on localhost +var localLlm = new LocalLargeLanguageModel("http://localhost:11434/api/generate"); +``` + +왜 엔드포인트를 클래스로 감싸는 걸까요? `LocalLargeLanguageModel`은 JSON 직렬화, 재시도, 스트리밍 응답을 처리해 주므로 `HttpClient`를 직접 다루는 대신 프롬프트 로직에 집중할 수 있습니다. + +## Step 3: 원본 Word 문서 로드하기 + +다음으로 문서를 메모리로 가져옵니다. Aspose.Words는 사실상 모든 Word 형식을 지원하므로 `Document`는 Office가 설치되지 않아도 `input.docx`를 파싱합니다. + +```csharp +// Path to the source file – adjust as needed +string inputPath = Path.Combine(Environment.CurrentDirectory, "input.docx"); + +// Load the document; throws if the file is missing or corrupted +Document sourceDocument = new Document(inputPath); +``` + +스트림으로 작업해야 하는 경우(예: ASP.NET을 통해 업로드된 파일) 파일 경로를 `MemoryStream`으로 교체하고 `Document` 생성자에 전달하면 됩니다. + +## Step 4: 현재 문단 텍스트 추출하기 + +`DocumentBuilder`를 사용해 문서를 탐색합니다. 이 예제에서는 **the first paragraph**을 재작성하지만, `sourceDocument.GetChildNodes(NodeType.Paragraph, true)`를 반복하여 여러 문단을 처리할 수 있습니다. + +```csharp +// Builder gives us a cursor inside the document +DocumentBuilder docBuilder = new DocumentBuilder(sourceDocument); + +// Grab the text of the paragraph where the builder is currently positioned +string originalParagraph = docBuilder.CurrentParagraph?.GetText() ?? string.Empty; + +// Safety check – avoid sending empty strings to the LLM +if (string.IsNullOrWhiteSpace(originalParagraph)) +{ + Console.WriteLine("No paragraph found at the current cursor position."); + return; +} +``` + +`?.` 연산자는 문서가 비어 있을 경우 `NullReferenceException`을 방지합니다. 이는 초보자들이 흔히 겪는 **edge cases** 중 하나입니다. + +## Step 5: LLM에 문단 재작성 요청하기 + +이제 실제로 **prompt large language model**을 수행합니다. 프롬프트는 일반 영어이며, 래퍼가 이를 JSON으로 로컬 엔드포인트에 전송합니다. + +```csharp +// Build a friendly instruction for the model +string prompt = $"Rewrite the following sentence in a more formal tone:\n{originalParagraph}"; + +// Await the model's response – this is an async call +string rewrittenParagraph = await localLlm.PromptAsync(prompt); +``` + +왜 이렇게 요청을 구성할까요? LLM은 명확하고 단일 작업 지시문에 가장 잘 반응합니다. 콜론 뒤에 줄바꿈을 추가하면 지시와 내용이 구분되어 모델이 프롬프트를 그대로 반환할 가능성이 줄어듭니다. + +**Expected output** – `originalParagraph`가 `"Hey, what's up?"`였을 경우, LLM은 다음과 같이 반환할 수 있습니다: + +> “Good day, how may I assist you?” + +결과를 확인하려면 출력해 보면 됩니다: + +```csharp +Console.WriteLine("Original: " + originalParagraph); +Console.WriteLine("Rewritten: " + rewrittenParagraph); +``` + +## Step 6: 재작성된 텍스트를 문서에 삽입하기 + +새 텍스트를 얻었으면 기존 문단을 교체합니다. `DocumentBuilder.Writeln`은 새 줄을 쓰고 커서를 앞으로 이동시켜 추가에 적합합니다. 정확히 같은 문단을 *replace*해야 한다면, 쓰기 전에 `docBuilder.CurrentParagraph.RemoveAllChildren()`를 사용할 수 있습니다. + +```csharp +// Option A – Append a new paragraph (keeps the original) +docBuilder.Writeln(rewrittenParagraph); + +// Option B – Replace the existing paragraph (uncomment to use) +// docBuilder.CurrentParagraph.RemoveAllChildren(); +// docBuilder.CurrentParagraph.AppendChild(new Run(docBuilder.Document, rewrittenParagraph)); +``` + +두 가지 접근 방식을 모두 보여주므로 워크플로에 맞는 방식을 선택하면 됩니다. + +## Step 7: 업데이트된 문서 저장하기 + +마지막으로 변경 사항을 새 파일에 저장합니다. Aspose.Words는 파일 확장자를 기반으로 형식을 자동으로 선택합니다. + +```csharp +string outputPath = Path.Combine(Environment.CurrentDirectory, "output.docx"); +sourceDocument.Save(outputPath); + +Console.WriteLine($"Document saved to {outputPath}"); +``` + +`output.docx`를 Word에서 열면 문단이 이제 격식 있는 어조로 표시되는 것을 확인할 수 있습니다. + +## 전체 작동 예제 + +아래는 **complete, self‑contained program**입니다. 콘솔 프로젝트에 복사·붙여넣기하고, NuGet 패키지를 복원한 뒤 실행하면 됩니다—실행 중인 로컬 LLM 외에 추가 설정이 필요 없습니다. + +```csharp +using Aspose.Words.AI; +using Aspose.Words; +using System; +using System.IO; +using System.Threading.Tasks; + +class Program +{ + static async Task Main() + { + // ------------------------------------------------- + // Step 1: Connect to the locally hosted LLM endpoint + // ------------------------------------------------- + var localLlm = new LocalLargeLanguageModel("http://localhost:11434/api/generate"); + + // ------------------------------------------------- + // Step 2: Load the source Word document + // ------------------------------------------------- + string inputPath = Path.Combine(Environment.CurrentDirectory, "input.docx"); + Document sourceDocument = new Document(inputPath); + + // ------------------------------------------------- + // Step 3: Retrieve the text of the current paragraph + // ------------------------------------------------- + DocumentBuilder docBuilder = new DocumentBuilder(sourceDocument); + string originalParagraph = docBuilder.CurrentParagraph?.GetText() ?? string.Empty; + + if (string.IsNullOrWhiteSpace(originalParagraph)) + { + Console.WriteLine("No paragraph found at the current cursor position."); + return; + } + + // ------------------------------------------------- + // Step 4: Ask the LLM to rewrite the paragraph in a formal tone + // ------------------------------------------------- + string prompt = $"Rewrite the following sentence in a more formal tone:\n{originalParagraph}"; + string rewrittenParagraph = await localLlm.PromptAsync(prompt); + + // ------------------------------------------------- + // Step 5: Insert the rewritten text back into the document + // ------------------------------------------------- + docBuilder.Writeln(rewrittenParagraph); + + // ------------------------------------------------- + // Step 6: Save the updated document + // ------------------------------------------------- + string outputPath = Path.Combine(Environment.CurrentDirectory, "output.docx"); + sourceDocument.Save(outputPath); + + Console.WriteLine("Original paragraph:"); + Console.WriteLine(originalParagraph); + Console.WriteLine("\nRewritten paragraph:"); + Console.WriteLine(rewrittenParagraph); + Console.WriteLine($"\nDocument saved to {outputPath}"); + } +} +``` + +### 실행 시 기대되는 결과 + +1. 콘솔에 원본 및 재작성된 문단이 출력됩니다. +2. `output.docx`가 `input.docx` 옆에 생성됩니다. +3. 파일을 열면 새 격식 있는 문단이 원본 뒤에 삽입된(또는 대체된) 것을 확인할 수 있습니다. + +## 일반적인 Edge Cases 처리하기 + +| Situation | Solution | +|-----------|----------| +| **빈 문자열 또는 공백만 있는 문단** | 프롬프트를 보내기 전에 `string.IsNullOrWhiteSpace`를 확인하세요 (Step 3 참고). | +| **LLM이 오류 또는 빈 문자열을 반환** | `PromptAsync`를 `try/catch`로 감싸고 원본 텍스트를 사용하도록 폴백합니다. | +| **여러 문단을 재작성해야 함** | `sourceDocument.GetChildNodes(NodeType.Paragraph, true)`를 반복하고 동일한 프롬프트 로직을 적용합니다. | +| **대용량 문서가 지연을 초래** | 문단을 배치하여 한 번에 전송합니다(호출당 프롬프트 최대 4 KB). | +| **Non‑ASCII 문자 깨짐** | LLM 엔드포인트가 UTF‑8을 사용하도록 확인합니다(대부분 최신 모델이 지원). | + +## 다음 단계 및 관련 주제 + +- **Prompt large language model**에 더 풍부한 지시사항(예: 스타일 가이드, 길이 제한)을 제공합니다. +- 웹 API에서 **call local llm**을 사용해 문서 자동화를 서비스로 노출합니다. +- 고처리량 시나리오를 위해 병렬 스트림에서 **load word document**를 탐색합니다. +- 대량 이메일 생성 또는 보고서 표준화를 위해 이 방식을 **rewrite text automatically**와 결합합니다. + +더 깊이 파고들고 싶다면 Aspose의 **document merging** 문서와 Ollama API 레퍼런스에서 맞춤 샘플링 파라미터를 확인하세요. + +## 결론 + +우리는 이제 C#에서 **connect to local llm**을 수행하고, **prompt large language model**, **load word document**, **call local llm**, 그리고 **rewrite text automatically**를 단일 실행 가능한 콘솔 앱에서 구현하는 방법을 보여주었습니다. 이 패턴은 확장성이 있어 프롬프트를 교체하거나, 문단을 반복하거나, ASP.NET 엔드포인트를 통해 로직을 노출할 수 있습니다. 핵심은 로컬 AI 모델을 기존 문서 처리 라이브러리와 긴밀히 통합함으로써 신뢰할 수 있는 온프레미스 환경을 떠나지 않고도 강력한 자동화를 구현할 수 있다는 점입니다. + +스레딩에 대한 질문이 있으면, + +{{< /blocks/products/pf/tutorial-page-section >}} +{{< /blocks/products/pf/main-container >}} +{{< /blocks/products/pf/main-wrap-class >}} +{{< blocks/products/products-backtop-button >}} \ No newline at end of file diff --git a/words/korean/net/programming-with-markdownsaveoptions/_index.md b/words/korean/net/programming-with-markdownsaveoptions/_index.md index f1dbf065f8..95ca7297a3 100644 --- a/words/korean/net/programming-with-markdownsaveoptions/_index.md +++ b/words/korean/net/programming-with-markdownsaveoptions/_index.md @@ -41,6 +41,7 @@ | [DOCX를 Markdown으로 변환할 때 이미지 이름 바꾸기](./how-to-rename-images-when-converting-docx-to-markdown/) | Aspose.Words for .NET을 사용하여 DOCX를 Markdown으로 변환하면서 이미지 파일 이름을 원하는 대로 바꾸는 방법을 단계별로 안내합니다. | | [Aspose.Words를 사용하여 docx를 markdown으로 저장 – 전체 C# 가이드](./save-docx-as-markdown-with-aspose-words-full-c-guide/) | Aspose.Words for .NET을 사용하여 docx 파일을 markdown 형식으로 저장하는 전체 C# 가이드를 제공합니다. | | [Word 이미지 저장 – Aspose로 Word를 Markdown으로 변환](./save-word-images-convert-word-to-markdown-with-aspose/) | Aspose.Words for .NET을 사용하여 Word 문서의 이미지를 추출하고 Markdown으로 변환하는 방법을 단계별로 안내합니다. | +| [마크다운 이미지 상대 경로 – Word를 Markdown으로 변환](./markdown-image-relative-path-convert-word-to-markdown/) | Aspose.Words for .NET을 사용하여 Word 문서의 이미지를 상대 경로로 지정해 Markdown으로 변환하는 방법을 단계별로 안내합니다. | {{< /blocks/products/pf/tutorial-page-section >}} diff --git a/words/korean/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/_index.md b/words/korean/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/_index.md new file mode 100644 index 0000000000..cc2b92281a --- /dev/null +++ b/words/korean/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/_index.md @@ -0,0 +1,203 @@ +--- +category: general +date: 2026-04-28 +description: Word를 markdown으로 변환할 때 마크다운 이미지 상대 경로를 설정하는 방법, Word에서 이미지를 추출하는 방법, + 그리고 내보낸 이미지용 resources 폴더를 만드는 방법을 배워보세요. +draft: false +keywords: +- markdown image relative path +- convert word to markdown +- extract images from word +- create resources folder +- export images from docx +language: ko +og_description: Word를 markdown으로 변환하면서 마크다운 이미지 상대 경로를 설정하고, Word에서 이미지를 추출하며, 내보낸 + 이미지를 위한 resources 폴더를 생성합니다. +og_title: 마크다운 이미지 상대 경로 – Word를 마크다운으로 변환 +tags: +- Aspose.Words +- C# +- Markdown +- Image Export +title: 마크다운 이미지 상대 경로 – 워드에서 마크다운으로 변환 +url: /ko/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/ +--- + +{{< blocks/products/pf/main-wrap-class >}} +{{< blocks/products/pf/main-container >}} +{{< blocks/products/pf/tutorial-page-section >}} + +# markdown 이미지 상대 경로 – Word를 Markdown으로 변환 + +Word를 markdown으로 변환할 때 **markdown 이미지 상대 경로**가 필요했던 적이 있나요? 당신만 그런 것이 아닙니다. 대부분의 개발자는 생성된 Markdown이 평면 폴더에 이미지를 가리키게 되어 정적 사이트나 GitHub 저장소에서 기대하는 상대 링크 구조가 깨지는 문제에 부딪힙니다. + +이 튜토리얼에서는 **Word에서 이미지 추출**, **리소스 폴더 생성**, 그리고 이미지 참조를 깔끔한 *markdown 이미지 상대 경로*로 재작성하는 완전한 엔드‑투‑엔드 솔루션을 단계별로 살펴봅니다. 최종적으로는 바로 게시할 수 있는 `.md` 파일과 원본 `.docx`에서 추출된 모든 그림이 정리된 `Resources` 디렉터리를 얻게 됩니다. + +> **얻을 수 있는 것:** 외부 스크립트 없이 단일 C# 프로그램, 각 부분이 왜 중요한지에 대한 명확한 설명, 그리고 여러분의 프로젝트에 바로 복사‑붙여넣기 할 수 있는 실용적인 팁 몇 가지. + +--- + +## 사전 요구 사항 + +- **.NET 6.0** 이상이 설치되어 있어야 합니다(.NET Framework 4.7+도 대상이 될 수 있지만, 새 프로젝트에는 .NET 6이 가장 적합합니다). +- **Aspose.Words for .NET** (작성 시점 최신 NuGet 패키지, 버전 23.12). 다음과 같이 설치합니다: + ```bash + dotnet add package Aspose.Words + ``` +- 실제로 이미지가 포함된 Word 문서—예를 들어 `WithImages.docx`. +- 출력 markdown과 이미지를 저장할 폴더, 예시: `C:\Projects\MarkdownExport`. + +추가 라이브러리는 필요하지 않으며, 나머지는 모두 Aspose.Words가 처리합니다. + +--- + +## Step 1: 원본 Word 문서 로드 (Word를 markdown으로 변환하기 위한 시작점) + +```csharp +using System; +using System.IO; +using Aspose.Words; +using Aspose.Words.Saving; + +class Program +{ + static void Main() + { + // Adjust the path to point at your own .docx file. + string sourcePath = @"C:\Projects\MarkdownExport\WithImages.docx"; + + // Load the document – this is where Aspose.Words parses the Word file. + Document doc = new Document(sourcePath); + + // The rest of the workflow follows… + } +} +``` + +*Why this matters:* 문서를 로드하면 내부 노드 트리에 접근할 수 있게 되며, 여기에는 나중에 **export images from docx**가 필요한 이미지 파트가 포함됩니다. 로드에 실패하면 이후 단계가 전혀 실행되지 않으므로 경로와 파일 권한을 반드시 확인하세요. + +--- + +## Step 2: `MarkdownSaveOptions`를 사용자 콜백으로 구성 (리소스 폴더 생성의 핵심) + +`ResourceSavingCallback`을 사용하면 Aspose.Words가 이미지 파일을 쓰려 할 때마다 개입할 수 있습니다. 콜백 내부에서 **Resources 하위 폴더**를 만들고, 생성된 markdown이 *markdown 이미지 상대 경로*를 사용하도록 참조를 조정합니다. + +```csharp +// Inside Main(), after loading the document: +string outputFolder = @"C:\Projects\MarkdownExport"; +string resourcesFolder = Path.Combine(outputFolder, "Resources"); + +// Make sure the folder exists before we start saving anything. +Directory.CreateDirectory(resourcesFolder); + +// Set up the Markdown save options. +MarkdownSaveOptions mdOptions = new MarkdownSaveOptions +{ + // Hook that runs for every image resource. + ResourceSavingCallback = new MyMarkdownResourceCallback(resourcesFolder) +}; + +// Save the document as Markdown. +string markdownPath = Path.Combine(outputFolder, "Doc.md"); +doc.Save(markdownPath, mdOptions); +``` + +`resourcesFolder`를 콜백 생성자의 매개변수로 전달했으므로 폴더 경로를 유연하게 유지할 수 있고, 코드 전반에 문자열을 하드코딩하는 일을 피할 수 있습니다. + +--- + +## Step 3: **리소스 폴더 생성** 및 경로 재작성 콜백 구현 + +```csharp +/// +/// Handles image extraction and path rewriting for markdown export. +/// +class MyMarkdownResourceCallback : IResourceSavingCallback +{ + private readonly string _resourcesFolder; + + public MyMarkdownResourceCallback(string resourcesFolder) + { + _resourcesFolder = resourcesFolder; + } + + public void ResourceSaving(ResourceSavingArgs args) + { + // 1️⃣ Build the full file system path where the image will be stored. + string targetPath = Path.Combine(_resourcesFolder, args.ResourceFileName); + + // 2️⃣ Ensure the directory exists (in case Aspose creates sub‑folders). + Directory.CreateDirectory(Path.GetDirectoryName(targetPath)); + + // 3️⃣ Write the image stream to disk. + using (FileStream fileStream = File.Create(targetPath)) + { + args.Stream.CopyTo(fileStream); + } + + // 4️⃣ Update the markdown reference to use a relative path. + // This is the crucial line that gives us the markdown image relative path. + args.ResourceFileName = Path.Combine("Resources", args.ResourceFileName); + } +} +``` + +*Why this works:* `args.Stream`에는 원시 이미지 바이트가 들어 있습니다. 이를 `Resources` 폴더 안의 파일에 복사함으로써 **export images from docx**를 안전하게 수행합니다. 그런 다음 `args.ResourceFileName`을 상대 URL(`Resources/image.png`)로 교체합니다. 이후 Aspose.Words가 markdown을 쓸 때 정확히 그 문자열을 삽입해 원하는 *markdown 이미지 상대 경로*를 얻을 수 있습니다. + +--- + +## Step 4: 생성된 Markdown 확인 (최종 출력 예시) + +텍스트 편집기에서 `Doc.md`를 열어보세요. 다음과 비슷한 내용이 표시됩니다: + +```markdown +# Sample Heading + +Here is an inline picture: + +![Image 0](Resources/Image_0.png) + +And a picture inside a table: + +![Image 1](Resources/Image_1.jpg) +``` + +중요한 점은 각 이미지 참조가 `Resources/...`를 가리키고 있다는 것입니다—우리가 원했던 **markdown 이미지 상대 경로**가 바로 그것입니다. + +![markdown 이미지 상대 경로 예시](example.png "markdown 이미지 상대 경로 예시") + +*Tip:* VS Code 미리보기, GitHub, 혹은 정적 사이트 생성기와 같이 상대 링크를 지원하는 뷰어에서 markdown을 열면 별도 설정 없이도 그림이 올바르게 표시됩니다. + +--- + +## Step 5: Common pitfalls and pro‑tips + +| 문제 | 발생 원인 | 해결 방법 | +|-------|----------------|---------------| +| 이미지가 `Resources`가 아닌 루트 폴더에 저장됨 | 콜백이 연결되지 않았거나 `args.ResourceFileName`이 덮어쓰기되지 않음 | `doc.Save`를 호출하기 **전** `ResourceSavingCallback`이 설정되어 있는지 확인 | +| 파일 이름에 금지 문자 포함 | Word가 이미지 이름에 공백이나 유니코드 기호를 사용할 때 발생 | 콜백 내부에서 `Path.GetInvalidFileNameChars()`를 사용해 `args.ResourceFileName`을 정리 | +| 대용량 문서 처리 시간이 오래 걸림 | 각 이미지를 동기식으로 기록하기 때문 | .NET 6 이상에서 비동기 I/O(`await args.Stream.CopyToAsync(fileStream)`)로 전환 | +| markdown을 이동하면 상대 경로가 깨짐 | 경로가 markdown 파일 위치를 기준으로 상대적이기 때문 | `Doc.md`와 `Resources` 폴더를 함께 두거나, 콜백에서 다른 상대 접두사(e.g., `../assets`)를 사용하도록 조정 | + +--- + +## Step 6: Extending the solution (what if you need more control?) + +- **다중 출력 포맷:** `MarkdownSaveOptions` 대신 `HtmlSaveOptions` 혹은 `PdfSaveOptions`를 사용해도 동일한 콜백이 적용됩니다—Aspose.Words는 포맷에 관계없이 모든 이미지에 대해 콜백을 호출합니다. +- **맞춤 이미지 명명:** 이미지 이름을 `figure-01.png`처럼 바꾸고 싶다면 파일을 쓰기 전에 `args.ResourceFileName`을 수정하면 됩니다. +- **이미지를 Base64로 임베드:** `args.ResourceFileName`을 데이터 URI(`data:image/png;base64,...`)로 설정하고 파일 쓰기를 건너뛰세요. 단일 파일 markdown을 내보낼 때 유용합니다. + +--- + +## Conclusion + +이제 **Word를 markdown으로 변환**, **word에서 이미지 추출**, **리소스 폴더 생성**, 그리고 모든 그림에 대해 깔끔한 **markdown 이미지 상대 경로**를 보장하는 완전한 C# 프로그램을 갖추었습니다. 코드는 자체 포함형이며 최신 Aspose.Words 버전과 호환되고, 최소한의 노력으로 어떤 .NET 프로젝트에도 바로 적용할 수 있습니다. + +다음 단계는? 생성된 markdown을 Hugo나 Jekyll 같은 정적 사이트 생성기에 넣어 보거나, 콜백을 수정해 이미지를 직접 Base64 문자열로 임베드해 보세요. SVG 이미지나 매우 큰 파일 같은 특수 케이스가 발생하면 “Common pitfalls” 표를 다시 참고하세요—작은 조정만으로도 대부분 해결됩니다. + +행복한 코딩 되시고, markdown이 언제나 올바른 폴더를 가리키길 바랍니다! + +{{< /blocks/products/pf/tutorial-page-section >}} +{{< /blocks/products/pf/main-container >}} +{{< /blocks/products/pf/main-wrap-class >}} +{{< blocks/products/products-backtop-button >}} \ No newline at end of file diff --git a/words/korean/net/programming-with-officemath/_index.md b/words/korean/net/programming-with-officemath/_index.md index ee849d70d6..7764a97132 100644 --- a/words/korean/net/programming-with-officemath/_index.md +++ b/words/korean/net/programming-with-officemath/_index.md @@ -24,6 +24,7 @@ Aspose.Words for .NET 튜토리얼에서는 OfficeMath를 활용한 단어 처 | [docx를 txt로 저장 – C#로 Word 수학을 LaTeX로 내보내기](./save-docx-as-txt-export-word-math-to-latex-with-c/) | C#를 사용해 docx 파일을 txt로 변환하고, Word 수학 방정식을 LaTeX 형식으로 내보내는 방법을 안내합니다. | | [문서를 Txt로 저장 – C#에서 Word 수학을 LaTeX로 내보내기](./save-document-as-txt-export-word-math-to-latex-in-c/) | C#을 사용하여 Word 문서의 수학 방정식을 LaTeX 형식의 텍스트 파일로 저장하는 방법을 단계별로 안내합니다. | +| [DOCX를 TXT로 변환 – C#에서 Word 방정식을 LaTeX로 내보내기](./convert-docx-to-txt-export-word-equations-to-latex-in-c/) | C#를 사용해 DOCX 파일을 TXT로 변환하고, Word 방정식을 LaTeX 형식으로 내보내는 방법을 안내합니다. | {{< /blocks/products/pf/tutorial-page-section >}} diff --git a/words/korean/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/_index.md b/words/korean/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/_index.md new file mode 100644 index 0000000000..2c262fe80a --- /dev/null +++ b/words/korean/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/_index.md @@ -0,0 +1,210 @@ +--- +category: general +date: 2026-04-28 +description: Aspose.Words를 사용하여 DOCX를 TXT로 변환하고 Word 수식을 LaTeX로 내보냅니다. 몇 단계만으로 Word를 + TXT로 저장하고 수학 객체를 처리하는 방법을 알아보세요. +draft: false +keywords: +- convert docx to txt +- convert word equations to latex +- convert word to plain text +- save word as txt +- export equations as latex +language: ko +og_description: 간단한 C# 스니펫으로 DOCX를 TXT로 변환하고 Word 수식을 LaTeX로 내보내세요. 전체 가이드, 코드 및 팁. +og_title: DOCX를 TXT로 변환 – Word 수식을 LaTeX로 내보내기 +tags: +- C# +- Aspose.Words +- Document Conversion +title: DOCX를 TXT로 변환 – C#에서 Word 수식을 LaTeX로 내보내기 +url: /ko/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/ +--- + +{{< blocks/products/pf/main-wrap-class >}} +{{< blocks/products/pf/main-container >}} +{{< blocks/products/pf/tutorial-page-section >}} + +# DOCX를 TXT로 변환 – Word 수식을 LaTeX로 내보내기 + +**docx를 txt로 변환**해야 하는데 Word 파일의 수식이 깨져 보일까 걱정되셨나요? 혼자가 아닙니다. 많은 엔지니어링·학술 프로젝트에서 원본 문서는 .docx 형태이지만, 이후 도구들은 일반 텍스트나 LaTeX만을 이해합니다. 좋은 소식은? 몇 줄의 C# 코드와 Aspose.Words만 있으면 **docx를 txt로 변환**하면서 모든 수식을 깔끔한 LaTeX 코드로 유지할 수 있다는 것입니다. + +이 튜토리얼에서는 전체 과정을 단계별로 살펴봅니다: .docx 로드, Office Math 객체를 LaTeX로 변환하도록 저장 옵션을 설정, 그리고 최종적으로 .txt 파일에 결과를 기록합니다. 끝까지 따라오시면 **word를 txt로 저장**, **word를 일반 텍스트로 변환**, **수식을 latex로 내보내기**를 API 문서를 뒤적이지 않고도 할 수 있게 됩니다. + +## 배울 내용 + +- 수식을 보존하면서 **docx를 txt로 변환**하기 위해 필요한 정확한 API 호출 +- `OfficeMathExportMode.LaTeX`를 선택하는 것이 **Word 수식을 LaTeX로 변환**하는 권장 방법인 이유 +- 누락된 폰트나 지원되지 않는 수식 기능과 같은 일반적인 엣지 케이스 처리 방법 +- 어떤 .NET 프로젝트에든 바로 넣어 사용할 수 있는 완전한 C# 예제 프로그램 + +### 사전 준비 + +- .NET 6.0 이상 (코드는 .NET Framework 4.7+에서도 동작) +- Aspose.Words for .NET 라이선스 (평가용 무료 체험 가능) +- 최소 하나의 Office Math 객체가 포함된 Word 문서(`input.docx`) + +위 조건을 갖췄다면 바로 시작해봅시다. + +## 1단계: Aspose.Words 설치 + +코드를 실행하기 전에 라이브러리를 먼저 받아야 합니다. 프로젝트 폴더에서 터미널을 열고 다음을 실행하세요: + +```bash +dotnet add package Aspose.Words +``` + +2026‑04‑28 현재 최신 안정 버전(v24.12)을 가져옵니다. 추가 DLL은 필요하지 않습니다. + +## 2단계: 원본 문서 로드 + +먼저 .docx 파일을 `Document` 객체로 읽어옵니다. 이 객체를 통해 텍스트 실행, 이미지, 수식 등 파일 구조 전체에 접근할 수 있습니다. + +```csharp +using Aspose.Words; +using Aspose.Words.Saving; + +class Program +{ + static void Main() + { + // Step 2: Load the source document + Document doc = new Document(@"YOUR_DIRECTORY\input.docx"); +``` + +> **왜 중요한가:** 문서를 메모리 상에 로드하면 이후 각 요소를 어떻게 기록할지 조정할 수 있습니다. 파일을 찾지 못하면 Aspose가 `FileNotFoundException`을 발생시키므로, 실제 서비스에서는 예외 처리를 고려하세요. + +## 3단계: LaTeX 수식을 위한 TXT 저장 옵션 설정 + +기본적으로 `Document.Save`는 일반 텍스트만 기록하고 Office Math를 **버립니다**. 수식을 유지하려면 `OfficeMathExportMode`를 `LaTeX`로 설정합니다. 이렇게 하면 각 수식이 LaTeX 형태로 변환됩니다. + +```csharp + // Step 3: Configure TXT save options to export Office Math as LaTeX + TxtSaveOptions txtOptions = new TxtSaveOptions + { + OfficeMathExportMode = OfficeMathExportMode.LaTeX, + // Optional: preserve line breaks as they appear in the original Word file + PreserveTableLayout = true + }; +``` + +> **프로 팁:** 수식의 원시 유니코드 문자만 필요하다면(`빠른 미리보기` 등) `OfficeMathExportMode.Text`를 사용할 수 있습니다. 하지만 대부분의 과학 파이프라인에서는 `LaTeX`가 표준이며, LaTeX 프로세서가 보편적으로 이해합니다. + +## 4단계: 문서를 일반 텍스트로 저장 + +이제 변환된 내용을 `.txt` 파일에 기록합니다. 파일에는 일반 문단, 글머리표, 그리고 이전 단계에서 만든 LaTeX 스니펫이 포함됩니다. + +```csharp + // Step 4: Save the document as plain‑text using the configured options + doc.Save(@"YOUR_DIRECTORY\Math.txt", txtOptions); + } +} +``` + +`Math.txt`를 열면 다음과 같은 내용이 보일 것입니다: + +``` +In this report we derive the quadratic formula: +\[ +x = \frac{-b \pm \sqrt{b^{2} - 4ac}}{2a} +\] + +The end. +``` + +`\[` … `\]` 구분자를 눈여겨 보세요. 이는 자동으로 생성된 LaTeX 수식 블록입니다. + +## 5단계: 출력 확인 (선택 사항이지만 권장) + +특히 사용자 정의 기호가 포함된 수식은 미묘한 변환 오류가 발생하기 쉽습니다. 간단히 생성된 `.txt`를 LaTeX 컴파일러(`pdflatex` 등)에 넘겨 오류 없이 컴파일되는지 확인해 보세요. + +```bash +pdflatex -interaction=nonstopmode Math.txt +``` + +컴파일이 성공하면 **Word 수식을 LaTeX로 변환**하고 **docx를 txt로 변환**을 한 번에 수행한 것입니다. 오류가 발생하면 “undefined command”와 같은 메시지를 찾아보세요. 이는 Aspose.Words가 현재 지원하지 않는 수식 기능(예: 특정 행렬 표기) 때문일 수 있습니다. 이 경우 `OfficeMathExportMode.MathML`로 내보낸 뒤 별도 도구로 MathML을 LaTeX로 변환하면 됩니다. + +## 흔히 겪는 문제와 해결 방법 + +| Issue | Why it Happens | Fix | +|-------|----------------|-----| +| Missing fonts | Aspose.Words가 기호를 올바르게 렌더링하려면 해당 폰트가 필요합니다. | 머신에 폰트를 설치하거나 .docx에 폰트를 포함시킵니다. | +| Complex equations not exported | 최신 Office Math 기능 중 일부는 아직 LaTeX 매핑이 제공되지 않습니다. | `OfficeMathExportMode.MathML`을 사용한 뒤 MathML‑to‑LaTeX 라이브러리로 변환합니다. | +| Extra blank lines | 일반 텍스트 저장 시 문단 구분이 그대로 보존돼 공백 라인이 늘어날 수 있습니다. | `txtOptions.AddBidiMarks = false` 로 설정하거나 간단한 스크립트로 후처리합니다. | + +## 전체 작업 예제 (복사‑붙여넣기 바로 사용) + +아래는 전체 프로그램 코드입니다. `YOUR_DIRECTORY`를 `input.docx`가 들어 있는 폴더 경로로 바꾸세요. + +```csharp +using System; +using Aspose.Words; +using Aspose.Words.Saving; + +namespace DocxToTxtWithLatex +{ + class Program + { + static void Main() + { + try + { + // Load the source document + Document doc = new Document(@"C:\Docs\input.docx"); + + // Configure save options: export equations as LaTeX + TxtSaveOptions txtOptions = new TxtSaveOptions + { + OfficeMathExportMode = OfficeMathExportMode.LaTeX, + PreserveTableLayout = true, + AddBidiMarks = false + }; + + // Save as plain‑text + string outputPath = @"C:\Docs\Math.txt"; + doc.Save(outputPath, txtOptions); + + Console.WriteLine($"Successfully converted DOCX to TXT. Output at: {outputPath}"); + } + catch (Exception ex) + { + Console.Error.WriteLine($"Conversion failed: {ex.Message}"); + } + } + } +} +``` + +이 프로그램을 실행하면 **word를 txt로 저장**하면서 모든 Office Math 블록을 LaTeX로 변환해, 깔끔하고 검색 가능한 일반 텍스트 파일을 얻을 수 있습니다. + +## 다음 단계 및 연관 주제 + +- **배치 변환:** 위 로직을 `foreach` 루프로 감싸서 폴더 내 모든 .docx 파일을 한 번에 처리합니다. +- **PDF 생성과 결합:** LaTeX 스니펫을 얻은 뒤 `PdfSharp` + `MiKTeX` 등으로 PDF 파이프라인에 연결해 보고서를 만들 수 있습니다. +- **다른 포맷으로 수식 내보내기:** Aspose.Words는 `SaveFormat.Markdown`도 지원해 LaTeX를 자동 삽입합니다. +- **성능 튜닝:** 대용량 문서에서는 동일 `TxtSaveOptions` 인스턴스를 재사용하고 `AddBidiMarks` 같은 불필요한 옵션을 비활성화합니다. + +--- + +### 이미지 예시 (선택) + +시각적인 예시가 필요하다면 Notepad++에서 출력 파일을 연 스크린샷을 확인하세요. + +![convert docx to txt output showing LaTeX equations](convert-docx-to-txt-output.png) + +*(Alt text: “convert docx to txt output showing LaTeX equations” – 주요 키워드 요구 사항을 만족합니다.)* + +--- + +## 결론 + +우리는 **docx를 txt로 변환**하면서 모든 수식을 깔끔한 LaTeX로 보존하는 신뢰할 수 있는 방법을 살펴보았습니다. 핵심은 `OfficeMathExportMode.LaTeX` 플래그이며, 이를 통해 Word 고유의 수식 포맷을 어떤 LaTeX 엔진에서도 이해할 수 있는 형태로 바꿀 수 있습니다. 위 전체 코드 샘플을 활용하면 **word를 txt로 저장**, **word를 일반 텍스트로 변환**, **수식을 latex로 내보내기**를 한 번에 수행할 수 있습니다. + +출력 확장자를 `.md`로 바꾸어 Markdown으로 저장하거나, 더 큰 문서 처리 파이프라인에 통합해 보세요. 궁금한 점이나 문제 발생 시 댓글로 알려 주세요. 기꺼이 도와드리겠습니다. + +Happy coding! + +{{< /blocks/products/pf/tutorial-page-section >}} +{{< /blocks/products/pf/main-container >}} +{{< /blocks/products/pf/main-wrap-class >}} +{{< blocks/products/products-backtop-button >}} \ No newline at end of file diff --git a/words/polish/net/ai-powered-document-processing/_index.md b/words/polish/net/ai-powered-document-processing/_index.md index 18902b04e6..55f3fdc66d 100644 --- a/words/polish/net/ai-powered-document-processing/_index.md +++ b/words/polish/net/ai-powered-document-processing/_index.md @@ -39,9 +39,10 @@ Na koniec nie zapomnij sprawdzić naszego [Praca z opcjami podsumowania](./worki | Tytuł | Opis | | --- | --- | | [Praca z modelem AI](./working-with-ai-model/) | Dowiedz się, jak używać Aspose.Words dla .NET do podsumowywania dokumentów za pomocą AI. Proste kroki w celu ulepszenia zarządzania dokumentami. | -| [Praca z modelem Google AI](./working-with-google-ai-model/) Ulepsz przetwarzanie dokumentów dzięki Aspose.Words for .NET i Google AI, aby bez wysiłku tworzyć zwięzłe podsumowania. | +| [Praca z modelem Google AI](./working-with-google-ai-model/) | Ulepsz przetwarzanie dokumentów dzięki Aspose.Words for .NET i Google AI, aby bez wysiłku tworzyć zwięzłe podsumowania. | | [Praca z otwartym modelem AI](./working-with-open-ai-model/) | Odblokuj wydajne podsumowanie dokumentów za pomocą Aspose.Words dla .NET z potężnymi modelami OpenAI. Zanurz się w tym kompleksowym przewodniku już teraz. | | [Praca z opcjami podsumowania](./working-with-summarize-options/) | Naucz się skutecznie podsumowywać dokumenty Word za pomocą Aspose.Words dla platformy .NET dzięki naszemu przewodnikowi krok po kroku dotyczącemu integrowania modeli AI w celu szybkiego uzyskiwania analiz. | +| [Połączenie z lokalnym modelem LLM w C# – Kompletny przewodnik programistyczny](./connect-to-local-llm-in-c-complete-programming-guide/) | Dowiedz się, jak w C# podłączyć lokalny model LLM i wykorzystać go w Aspose.Words do przetwarzania dokumentów. | {{< /blocks/products/pf/tutorial-page-section >}} diff --git a/words/polish/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/_index.md b/words/polish/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/_index.md new file mode 100644 index 0000000000..17293e8761 --- /dev/null +++ b/words/polish/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/_index.md @@ -0,0 +1,284 @@ +--- +category: general +date: 2026-04-28 +description: Połącz się z lokalnym LLM z poziomu C# i poproś duży model językowy o + załadowanie dokumentu Word, wywołaj lokalny LLM i automatycznie przepisz tekst. + Dołączony kod krok po kroku. +draft: false +keywords: +- connect to local llm +- prompt large language model +- load word document +- call local llm +- rewrite text automatically +language: pl +og_description: Połącz się z lokalnym LLM z poziomu C# i zobacz, jak zadawać polecenia + dużemu modelowi językowemu, wczytywać dokument Word, wywoływać lokalny LLM i automatycznie + przekształcać tekst w ciągu kilku minut. +og_title: Połącz się z lokalnym LLM w C# – Kompletny przewodnik programistyczny +tags: +- Aspose.Words +- C# +- LLM +- AI Automation +title: Połącz się z lokalnym LLM w C# – Kompletny przewodnik programistyczny +url: /pl/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/ +--- + +{{< blocks/products/pf/main-wrap-class >}} +{{< blocks/products/pf/main-container >}} +{{< blocks/products/pf/tutorial-page-section >}} + +# Połącz się z lokalnym LLM w C# – Kompletny przewodnik programistyczny + +Czy kiedykolwiek potrzebowałeś **połączyć się z lokalnym llm** z aplikacji .NET i zastanawiałeś się, jak sprawić, by rozmawiał z plikiem Word? Nie jesteś sam. W tym przewodniku przeprowadzimy Cię przez cały proces — połączenie z lokalnym llm, **prompt large language model**, załadowanie dokumentu Word, **call local llm**, a na końcu **rewrite text automatically**. Po zakończeniu będziesz mieć działający przykład, który przekształca dowolny akapit w formalny ton bez użycia zewnętrznych kluczy API. + +## Co obejmuje ten tutorial + +Zaczniemy od zainstalowania niezbędnych pakietów NuGet, a następnie uruchomimy prosty lokalny punkt końcowy LLM (np. Ollama na porcie 11434). Następnie załadujemy plik `.docx` przy użyciu Aspose.Words, wyślemy akapit do LLM, otrzymamy przepisana wersję i zapisujemy ją z powrotem do tego samego dokumentu. Zobaczysz także, jak radzić sobie z typowymi pułapkami — pustymi akapitami, asynchronicznym zwalnianiem zasobów i problemami z kodowaniem — aby kod działał w produkcji, a nie tylko w demonstracji. + +### Wymagania wstępne + +- .NET 6.0 SDK lub nowszy (możesz także używać .NET 8, jeśli chcesz) +- Visual Studio 2022 lub VS Code z rozszerzeniem C# +- **Aspose.Words for .NET** (bezpłatna wersja próbna działa) +- Lokalnie hostowany LLM obsługujący kontrakt `/api/generate` (np. Ollama, LMStudio) +- Podstawowa znajomość async/await w C# + +> **Wskazówka:** Jeśli nie zainstalowałeś jeszcze Ollama, uruchom `ollama serve` i pobierz model za pomocą `ollama pull llama3`. Domyślny punkt końcowy HTTP będzie `http://localhost:11434/api/generate`. + +--- + +## Krok 1: Zainstaluj wymagane pakiety + +Najpierw dodaj pakiety NuGet Aspose.Words i Aspose.Words.AI do swojego projektu. + +```bash +dotnet add package Aspose.Words +dotnet add package Aspose.Words.AI +``` + +Te biblioteki zapewniają nam możliwość **load word document** oraz cienką warstwę do **call local llm** bez ręcznego tworzenia żądań HTTP. + +--- + +## Krok 2: Połącz się z lokalnym punktem końcowym LLM + +Połączenie z lokalnie hostowanym modelem jest tak proste, jak stworzenie instancji `LocalLargeLanguageModel`. Konstruktor oczekuje pełnego URL punktu końcowego generacji. + +```csharp +using Aspose.Words.AI; +using Aspose.Words; +using System.Threading.Tasks; + +// Create a client that talks to the LLM running on localhost +var localLlm = new LocalLargeLanguageModel("http://localhost:11434/api/generate"); +``` + +Dlaczego opakowujemy punkt końcowy w klasie? `LocalLargeLanguageModel` zajmuje się serializacją JSON, ponawianiem prób i strumieniowaniem odpowiedzi — dzięki temu możesz skupić się na logice promptu, zamiast majstrować przy `HttpClient`. + +--- + +## Krok 3: Załaduj źródłowy dokument Word + +Następnie wczytujemy dokument do pamięci. Aspose.Words obsługuje praktycznie każdy format Word, więc `Document` sparsuje `input.docx` bez konieczności instalacji Office. + +```csharp +// Path to the source file – adjust as needed +string inputPath = Path.Combine(Environment.CurrentDirectory, "input.docx"); + +// Load the document; throws if the file is missing or corrupted +Document sourceDocument = new Document(inputPath); +``` + +Jeśli potrzebujesz pracować ze strumieniem (np. plik przesłany przez ASP.NET), po prostu zamień ścieżkę pliku na `MemoryStream` i przekaż go do konstruktora `Document`. + +--- + +## Krok 4: Wyodrębnij bieżący tekst akapitu + +Użyjemy `DocumentBuilder` do nawigacji po dokumencie. W tym przykładzie przepisujemy **pierwszy akapit**, ale możesz iterować po `sourceDocument.GetChildNodes(NodeType.Paragraph, true)`, aby przetworzyć wiele. + +```csharp +// Builder gives us a cursor inside the document +DocumentBuilder docBuilder = new DocumentBuilder(sourceDocument); + +// Grab the text of the paragraph where the builder is currently positioned +string originalParagraph = docBuilder.CurrentParagraph?.GetText() ?? string.Empty; + +// Safety check – avoid sending empty strings to the LLM +if (string.IsNullOrWhiteSpace(originalParagraph)) +{ + Console.WriteLine("No paragraph found at the current cursor position."); + return; +} +``` + +Operator `?.` zapobiega `NullReferenceException`, jeśli dokument okaże się pusty. To jeden z tych **edge cases**, które potykają początkujących. + +--- + +## Krok 5: Prompt the LLM to Rewrite the Paragraph + +Teraz faktycznie **prompt large language model**. Prompt jest w zwykłym angielskim; wrapper wyśle go jako JSON do lokalnego punktu końcowego. + +```csharp +// Build a friendly instruction for the model +string prompt = $"Rewrite the following sentence in a more formal tone:\n{originalParagraph}"; + +// Await the model's response – this is an async call +string rewrittenParagraph = await localLlm.PromptAsync(prompt); +``` + +Dlaczego formułujemy zapytanie w ten sposób? LLM najlepiej reagują na jasne, jednowątkowe instrukcje. Dodanie nowej linii po dwukropku oddziela instrukcję od treści, zmniejszając ryzyko, że model odzwierciedli sam prompt. + +**Oczekiwany wynik** – Jeśli `originalParagraph` było `"Hey, what's up?"`, LLM może zwrócić: + +> “Good day, how may I assist you?” + +Możesz zweryfikować wynik, wypisując go: + +```csharp +Console.WriteLine("Original: " + originalParagraph); +Console.WriteLine("Rewritten: " + rewrittenParagraph); +``` + +--- + +## Krok 6: Wstaw przepisany tekst z powrotem do dokumentu + +Mając nowy tekst, zastępujemy stary akapit. `DocumentBuilder.Writeln` zapisuje nową linię i przesuwa kursor do przodu, co jest idealne przy dopisywaniu. Jeśli potrzebujesz *zastąpić* dokładnie ten sam akapit, możesz użyć `docBuilder.CurrentParagraph.RemoveAllChildren()` przed zapisaniem. + +```csharp +// Option A – Append a new paragraph (keeps the original) +docBuilder.Writeln(rewrittenParagraph); + +// Option B – Replace the existing paragraph (uncomment to use) +// docBuilder.CurrentParagraph.RemoveAllChildren(); +// docBuilder.CurrentParagraph.AppendChild(new Run(docBuilder.Document, rewrittenParagraph)); +``` + +Oba podejścia są pokazane, abyś mógł wybrać to, które pasuje do Twojego przepływu pracy. + +--- + +## Krok 7: Zapisz zaktualizowany dokument + +Na koniec zapisujemy zmiany do nowego pliku. Aspose.Words automatycznie wybiera format na podstawie rozszerzenia pliku. + +```csharp +string outputPath = Path.Combine(Environment.CurrentDirectory, "output.docx"); +sourceDocument.Save(outputPath); + +Console.WriteLine($"Document saved to {outputPath}"); +``` + +Otwórz `output.docx` w Wordzie, a zobaczysz, że akapit jest teraz w formalnym tonie. + +--- + +## Pełny działający przykład + +Poniżej znajduje się **kompletny, samodzielny program**. Skopiuj i wklej go do projektu konsolowego, przywróć pakiety NuGet i uruchom — nie wymaga dodatkowej konfiguracji poza uruchomionym lokalnym LLM. + +```csharp +using Aspose.Words.AI; +using Aspose.Words; +using System; +using System.IO; +using System.Threading.Tasks; + +class Program +{ + static async Task Main() + { + // ------------------------------------------------- + // Step 1: Connect to the locally hosted LLM endpoint + // ------------------------------------------------- + var localLlm = new LocalLargeLanguageModel("http://localhost:11434/api/generate"); + + // ------------------------------------------------- + // Step 2: Load the source Word document + // ------------------------------------------------- + string inputPath = Path.Combine(Environment.CurrentDirectory, "input.docx"); + Document sourceDocument = new Document(inputPath); + + // ------------------------------------------------- + // Step 3: Retrieve the text of the current paragraph + // ------------------------------------------------- + DocumentBuilder docBuilder = new DocumentBuilder(sourceDocument); + string originalParagraph = docBuilder.CurrentParagraph?.GetText() ?? string.Empty; + + if (string.IsNullOrWhiteSpace(originalParagraph)) + { + Console.WriteLine("No paragraph found at the current cursor position."); + return; + } + + // ------------------------------------------------- + // Step 4: Ask the LLM to rewrite the paragraph in a formal tone + // ------------------------------------------------- + string prompt = $"Rewrite the following sentence in a more formal tone:\n{originalParagraph}"; + string rewrittenParagraph = await localLlm.PromptAsync(prompt); + + // ------------------------------------------------- + // Step 5: Insert the rewritten text back into the document + // ------------------------------------------------- + docBuilder.Writeln(rewrittenParagraph); + + // ------------------------------------------------- + // Step 6: Save the updated document + // ------------------------------------------------- + string outputPath = Path.Combine(Environment.CurrentDirectory, "output.docx"); + sourceDocument.Save(outputPath); + + Console.WriteLine("Original paragraph:"); + Console.WriteLine(originalParagraph); + Console.WriteLine("\nRewritten paragraph:"); + Console.WriteLine(rewrittenParagraph); + Console.WriteLine($"\nDocument saved to {outputPath}"); + } +} +``` + +### Czego się spodziewać po uruchomieniu + +1. Konsola wypisuje oryginalny i przepisany akapit. +2. `output.docx` pojawia się obok `input.docx`. +3. Otwierając plik, widzisz nowy formalny akapit wstawiony po oryginalnym (lub zastąpiony, jeśli użyłeś alternatywnego kodu). + +--- + +## Obsługa typowych przypadków brzegowych + +| Situation | Solution | +|-----------|----------| +| **Pusty lub zawierający tylko białe znaki akapit** | Sprawdź `string.IsNullOrWhiteSpace` przed wywołaniem promptu (zobacz Krok 3). | +| **LLM zwraca błąd lub pusty ciąg** | Opakuj `PromptAsync` w `try/catch` i w razie potrzeby użyj oryginalnego tekstu. | +| **Wiele akapitów wymaga przepisania** | Iteruj po `sourceDocument.GetChildNodes(NodeType.Paragraph, true)` i zastosuj tę samą logikę promptu. | +| **Duże dokumenty powodują opóźnienia** | Grupuj akapity i wyślij je w jednym żądaniu (prompt do 4 KB na wywołanie). | +| **Znaki nie‑ASCII są zniekształcone** | Upewnij się, że punkt końcowy LLM używa UTF‑8 (większość nowoczesnych modeli tak robi). | + +--- + +## Kolejne kroki i powiązane tematy + +- **Prompt large language model** z bardziej rozbudowanymi instrukcjami (np. przewodniki stylu, limity długości). +- Użyj **call local llm** w API webowym, aby udostępnić automatyzację dokumentów jako usługę. +- Zbadaj **load word document** w równoległych strumieniach dla scenariuszy wysokiej przepustowości. +- Połącz to podejście z **rewrite text automatically** dla masowej generacji e‑maili lub standaryzacji raportów. + +Jeśli chcesz zagłębić się bardziej, sprawdź dokumentację Aspose dotyczącą **document merging** oraz referencję API Ollama w celu dostosowania parametrów próbkowania. + +--- + +## Podsumowanie + +Właśnie pokazaliśmy, jak **connect to local llm** z C#, **prompt large language model**, **load word document**, **call local llm** i **rewrite text automatically** — wszystko w jednym, uruchamialnym programie konsolowym. Ten wzorzec skaluje się: zamień prompt, iteruj po akapitach lub udostępnij logikę przez endpoint ASP.NET. Najważniejsze wnioski to fakt, że lokalne modele AI mogą być ściśle zintegrowane z klasycznymi bibliotekami przetwarzania dokumentów, dając potężną automatyzację bez opuszczania zaufanego środowiska on‑prem. + +Masz pytania dotyczące wątków, + +{{< /blocks/products/pf/tutorial-page-section >}} +{{< /blocks/products/pf/main-container >}} +{{< /blocks/products/pf/main-wrap-class >}} +{{< blocks/products/products-backtop-button >}} \ No newline at end of file diff --git a/words/polish/net/programming-with-markdownsaveoptions/_index.md b/words/polish/net/programming-with-markdownsaveoptions/_index.md index e303dbabf2..379bad8697 100644 --- a/words/polish/net/programming-with-markdownsaveoptions/_index.md +++ b/words/polish/net/programming-with-markdownsaveoptions/_index.md @@ -67,6 +67,9 @@ Dowiedz się, jak automatycznie zmieniać nazwy obrazów przy konwersji dokument ### [Zapisz obrazy Word – konwertuj Word do Markdown przy użyciu Aspose](./save-word-images-convert-word-to-markdown-with-aspose/) Dowiedz się, jak wyodrębnić obrazy z dokumentu Word i zapisać je podczas konwersji do formatu Markdown przy użyciu Aspose.Words. +### [Ścieżka względna obrazu markdown – konwertuj Word do Markdown](./markdown-image-relative-path-convert-word-to-markdown/) +Dowiedz się, jak ustawić względne ścieżki obrazów przy konwersji dokumentu Word do formatu Markdown przy użyciu Aspose.Words. + {{< /blocks/products/pf/tutorial-page-section >}} diff --git a/words/polish/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/_index.md b/words/polish/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/_index.md new file mode 100644 index 0000000000..e8b8475fdb --- /dev/null +++ b/words/polish/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/_index.md @@ -0,0 +1,207 @@ +--- +category: general +date: 2026-04-28 +description: Dowiedz się, jak ustawić względną ścieżkę obrazu w markdown podczas konwertowania + pliku Word na markdown, wyodrębnić obrazy z Worda oraz utworzyć folder zasobów dla + wyeksportowanych obrazów. +draft: false +keywords: +- markdown image relative path +- convert word to markdown +- extract images from word +- create resources folder +- export images from docx +language: pl +og_description: Ustaw względną ścieżkę obrazu w markdown podczas konwersji Worda na + markdown, wyodrębnij obrazy z Worda i utwórz folder zasobów dla wyeksportowanych + obrazów. +og_title: Relatywna ścieżka obrazu w markdown – Konwertuj Word na Markdown +tags: +- Aspose.Words +- C# +- Markdown +- Image Export +title: relatywna ścieżka obrazu w markdown – konwersja Word do Markdown +url: /pl/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/ +--- + +{{< blocks/products/pf/main-wrap-class >}} +{{< blocks/products/pf/main-container >}} +{{< blocks/products/pf/tutorial-page-section >}} + +# markdown image relative path – Convert Word to Markdown + +Czy kiedykolwiek potrzebowałeś **markdown image relative path** podczas **konwersji Word do markdown**? Nie jesteś sam. Większość programistów napotyka problem, gdy wygenerowany Markdown odwołuje się do obrazów w płaskim folderze, łamiąc strukturę względnych linków, której oczekujesz w statycznej stronie lub repozytorium GitHub. + +W tym poradniku przeprowadzimy Cię przez kompletną, end‑to‑end rozwiązanie, które **wyodrębnia obrazy z Worda**, **tworzy folder zasobów** i przepisuje odwołania do obrazów, aby używały czystego *markdown image relative path*. Po zakończeniu będziesz mieć gotowy do publikacji plik `.md` oraz schludnie zorganizowany katalog `Resources` zawierający wszystkie obrazy wyodrębnione z oryginalnego pliku `.docx`. + +> **Co otrzymasz:** pojedynczy program w C# (bez zewnętrznych skryptów), jasne wyjaśnienie *dlaczego* każdy element ma znaczenie oraz kilka praktycznych wskazówek, które możesz skopiować i wkleić do własnych projektów. + +--- + +## Prerequisites + +Zanim przejdziemy do kodu, upewnij się, że masz: + +- **.NET 6.0** lub nowszy zainstalowany (możesz także celować w .NET Framework 4.7+, ale .NET 6 to optymalne rozwiązanie dla nowych projektów). +- **Aspose.Words for .NET** (najnowszy pakiet NuGet w momencie pisania, wersja 23.12). Zainstaluj go poleceniem: + ```bash + dotnet add package Aspose.Words + ``` +- Dokument Word, który rzeczywiście zawiera obrazy — nazwijmy go `WithImages.docx`. +- Folder, w którym mają się znaleźć wygenerowany markdown i obrazy, np. `C:\Projects\MarkdownExport`. + +Nie są wymagane dodatkowe biblioteki; wszystko inne obsługuje Aspose.Words. + +--- + +## Step 1: Load the source Word document (the starting point for convert word to markdown) + +```csharp +using System; +using System.IO; +using Aspose.Words; +using Aspose.Words.Saving; + +class Program +{ + static void Main() + { + // Adjust the path to point at your own .docx file. + string sourcePath = @"C:\Projects\MarkdownExport\WithImages.docx"; + + // Load the document – this is where Aspose.Words parses the Word file. + Document doc = new Document(sourcePath); + + // The rest of the workflow follows… + } +} +``` + +*Dlaczego to ważne:* Załadowanie dokumentu daje dostęp do wewnętrznego drzewa węzłów, które zawiera części obrazu, które później musimy **export images from docx**. Jeśli ładowanie się nie powiedzie, żaden z kolejnych kroków nie zostanie wykonany, więc sprawdź dokładnie ścieżkę i uprawnienia do pliku. + +--- + +## Step 2: Configure `MarkdownSaveOptions` with a custom callback (the heart of create resources folder) + +`ResourceSavingCallback` pozwala nam interweniować za każdym razem, gdy Aspose.Words chce zapisać plik obrazu. Wewnątrz callbacku **utworzymy podfolder Resources** i dostosujemy odwołanie, aby wygenerowany markdown używał *markdown image relative path*. + +```csharp +// Inside Main(), after loading the document: +string outputFolder = @"C:\Projects\MarkdownExport"; +string resourcesFolder = Path.Combine(outputFolder, "Resources"); + +// Make sure the folder exists before we start saving anything. +Directory.CreateDirectory(resourcesFolder); + +// Set up the Markdown save options. +MarkdownSaveOptions mdOptions = new MarkdownSaveOptions +{ + // Hook that runs for every image resource. + ResourceSavingCallback = new MyMarkdownResourceCallback(resourcesFolder) +}; + +// Save the document as Markdown. +string markdownPath = Path.Combine(outputFolder, "Doc.md"); +doc.Save(markdownPath, mdOptions); +``` + +Zauważ, że przekazaliśmy `resourcesFolder` do konstruktora callbacku — dzięki temu ścieżka folderu pozostaje elastyczna i nie musimy twardo kodować łańcuchów znaków w całym kodzie. + +--- + +## Step 3: Implement the callback that **creates resources folder** and rewrites the path + +```csharp +/// +/// Handles image extraction and path rewriting for markdown export. +/// +class MyMarkdownResourceCallback : IResourceSavingCallback +{ + private readonly string _resourcesFolder; + + public MyMarkdownResourceCallback(string resourcesFolder) + { + _resourcesFolder = resourcesFolder; + } + + public void ResourceSaving(ResourceSavingArgs args) + { + // 1️⃣ Build the full file system path where the image will be stored. + string targetPath = Path.Combine(_resourcesFolder, args.ResourceFileName); + + // 2️⃣ Ensure the directory exists (in case Aspose creates sub‑folders). + Directory.CreateDirectory(Path.GetDirectoryName(targetPath)); + + // 3️⃣ Write the image stream to disk. + using (FileStream fileStream = File.Create(targetPath)) + { + args.Stream.CopyTo(fileStream); + } + + // 4️⃣ Update the markdown reference to use a relative path. + // This is the crucial line that gives us the markdown image relative path. + args.ResourceFileName = Path.Combine("Resources", args.ResourceFileName); + } +} +``` + +*Dlaczego to działa:* `args.Stream` zawiera surowe bajty obrazu. Kopiując je do pliku w naszym folderze `Resources`, **export images from docx** w bezpieczny sposób. Następnie zamieniamy `args.ResourceFileName` na względny URL (`Resources/image.png`). Kiedy Aspose.Words później zapisze markdown, wstrzyknie dokładnie ten łańcuch, dając nam pożądany *markdown image relative path*. + +--- + +## Step 4: Verify the generated Markdown (what the final output looks like) + +Otwórz `Doc.md` w dowolnym edytorze tekstu. Powinieneś zobaczyć coś podobnego do: + +```markdown +# Sample Heading + +Here is an inline picture: + +![Image 0](Resources/Image_0.png) + +And a picture inside a table: + +![Image 1](Resources/Image_1.jpg) +``` + +Kluczowe jest to, że każde odwołanie do obrazu wskazuje na `Resources/...` — to jest **markdown image relative path**, którego szukaliśmy. + +![przykład markdown image relative path](example.png "przykład markdown image relative path") + +*Wskazówka:* Jeśli otworzysz markdown w przeglądarce, która respektuje względne linki (podgląd w VS Code, GitHub lub generator statycznych stron), obrazy zostaną poprawnie wyświetlone bez dodatkowej konfiguracji. + +--- + +## Step 5: Common pitfalls and pro‑tips + +| Problem | Dlaczego się pojawia | Jak naprawić | +|---------|----------------------|--------------| +| Obrazy trafiają do katalogu głównego zamiast do `Resources` | Callback nie został podłączony lub `args.ResourceFileName` nie został nadpisany. | Sprawdź, czy `ResourceSavingCallback` jest ustawiony **przed** wywołaniem `doc.Save`. | +| Nazwy plików zawierają niedozwolone znaki | Word czasami nazywa obrazy spacjami lub symbolami Unicode. | Użyj `Path.GetInvalidFileNameChars()` do sanitizacji `args.ResourceFileName` w callbacku. | +| Duże dokumenty przetwarzane są długo | Każdy obraz jest zapisywany synchronicznie. | Przejdź na asynchroniczny I/O (`await args.Stream.CopyToAsync(fileStream)`) w .NET 6+ jeśli potrzebujesz wydajności. | +| Względne ścieżki przestają działać po przeniesieniu markdowna | Ścieżka jest względna względem lokalizacji pliku markdown. | Trzymaj `Doc.md` i folder `Resources` razem lub zmodyfikuj callback, aby używał innego prefiksu względnego (np. `../assets`). | + +--- + +## Step 6: Extending the solution (what if you need more control?) + +- **Wiele formatów wyjściowych:** Zamień `MarkdownSaveOptions` na `HtmlSaveOptions` lub `PdfSaveOptions`, zachowując ten sam callback — Aspose.Words wywoła go dla każdego obrazu, niezależnie od formatu. +- **Niestandardowe nazewnictwo obrazów:** Jeśli chcesz zmienić nazwy obrazów (np. `figure-01.png`), zmodyfikuj `args.ResourceFileName` w callbacku przed zapisem pliku. +- **Osadzanie obrazów jako Base64:** Ustaw `args.ResourceFileName` na data URI (`data:image/png;base64,...`) i pomiń zapis do pliku. To przydatne przy jednoplikowych eksportach markdown. + +--- + +## Conclusion + +Masz teraz w pełni funkcjonalny program w C#, który **konwertuje Word do markdown**, **wyodrębnia obrazy z word**, **tworzy folder zasobów** i zapewnia czysty **markdown image relative path** dla każdego obrazu. Kod jest samodzielny, działa z najnowszą wersją Aspose.Words i może być wstawiony do dowolnego projektu .NET przy minimalnym nakładzie pracy. + +Co dalej? Spróbuj wprowadzić wygenerowany markdown do generatora statycznych stron, takiego jak Hugo lub Jekyll, albo poeksperymentuj z callbackiem, aby osadzać obrazy bezpośrednio jako ciągi Base64. Jeśli napotkasz nietypowe przypadki — np. obrazy SVG lub wyjątkowo duże pliki — odwołaj się do tabeli „Common pitfalls”; zazwyczaj mała poprawka rozwiązuje problem. + +Miłego kodowania i niech Twój markdown zawsze wskazuje na właściwy folder! + +{{< /blocks/products/pf/tutorial-page-section >}} +{{< /blocks/products/pf/main-container >}} +{{< /blocks/products/pf/main-wrap-class >}} +{{< blocks/products/products-backtop-button >}} \ No newline at end of file diff --git a/words/polish/net/programming-with-officemath/_index.md b/words/polish/net/programming-with-officemath/_index.md index 09e196a2b6..80b5d43aec 100644 --- a/words/polish/net/programming-with-officemath/_index.md +++ b/words/polish/net/programming-with-officemath/_index.md @@ -23,7 +23,7 @@ Samouczki Aspose.Words for .NET dotyczące przetwarzania słów za pomocą Offic | [Równania matematyczne](./math-equations/) | Dowiedz się, jak skonfigurować równania matematyczne w dokumentach Word za pomocą Aspose.Words dla .NET. Przewodnik krok po kroku z przykładami, często zadawanymi pytaniami i nie tylko. | | [Zapisz docx jako txt – Eksportuj matematyczne elementy Word do LaTeX w C#](./save-docx-as-txt-export-word-math-to-latex-with-c/) | Dowiedz się, jak zapisać dokument docx jako plik txt i wyeksportować równania Word do formatu LaTeX przy użyciu C#. | | [Zapisz dokument jako Txt – Eksportuj równania Word do LaTeX w C#](./save-document-as-txt-export-word-math-to-latex-in-c/) | Dowiedz się, jak zapisać dokument jako plik txt i wyeksportować równania Word do formatu LaTeX przy użyciu C#. | - +| [Konwertuj DOCX na TXT – Eksportuj równania Word do LaTeX w C#](./convert-docx-to-txt-export-word-equations-to-latex-in-c/) | Dowiedz się, jak konwertować plik DOCX na TXT i eksportować równania Word do formatu LaTeX przy użyciu C#. | {{< /blocks/products/pf/tutorial-page-section >}} diff --git a/words/polish/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/_index.md b/words/polish/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/_index.md new file mode 100644 index 0000000000..aeb8302e9e --- /dev/null +++ b/words/polish/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/_index.md @@ -0,0 +1,212 @@ +--- +category: general +date: 2026-04-28 +description: Konwertuj DOCX na TXT i eksportuj równania Worda do LaTeX przy użyciu + Aspose.Words. Dowiedz się, jak zapisać dokument Word jako TXT i obsłużyć obiekty + matematyczne w kilku krokach. +draft: false +keywords: +- convert docx to txt +- convert word equations to latex +- convert word to plain text +- save word as txt +- export equations as latex +language: pl +og_description: Konwertuj DOCX na TXT i eksportuj równania Worda do LaTeX przy użyciu + prostego fragmentu C#. Pełny przewodnik, kod i wskazówki. +og_title: Konwertuj DOCX na TXT – Eksportuj równania Worda do LaTeX +tags: +- C# +- Aspose.Words +- Document Conversion +title: Konwertuj DOCX na TXT – Eksportuj równania Worda do LaTeX w C# +url: /pl/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/ +--- + +{{< blocks/products/pf/main-wrap-class >}} +{{< blocks/products/pf/main-container >}} +{{< blocks/products/pf/tutorial-page-section >}} + +# Konwertuj DOCX na TXT – Eksportuj równania Worda do LaTeX + +Czy kiedykolwiek potrzebowałeś **convert docx to txt**, ale obawiałeś się, że matematyka w Twoim pliku Word zamieni się w nieczytelny bałagan? Nie jesteś sam. W wielu projektach inżynierskich lub akademickich źródłowy dokument jest w .docx, a narzędzia downstream rozumieją tylko zwykły tekst lub LaTeX. Dobra wiadomość? Kilka linii C# i Aspose.Words pozwoli Ci **convert docx to txt** *i* zachować każde równanie jako czysty kod LaTeX. + +W tym tutorialu przejdziemy przez cały proces: wczytanie .docx, skonfigurowanie opcji zapisu tak, aby obiekty Office Math stały się LaTeX, a na końcu zapis wyniku do pliku .txt. Po zakończeniu będziesz wiedział, jak **save word as txt**, **convert word to plain text** i **export equations as latex** bez przeszukiwania dokumentacji API. + +## Co się nauczysz + +- Dokładne wywołania API potrzebne do **convert docx to txt** przy zachowaniu równań. +- Dlaczego wybór `OfficeMathExportMode.LaTeX` jest zalecaną metodą **convert word equations to latex**. +- Jak radzić sobie z typowymi przypadkami brzegowymi, takimi jak brakujące czcionki czy nieobsługiwane funkcje równań. +- Kompletny, gotowy do uruchomienia program w C#, który możesz wkleić do dowolnego projektu .NET. + +### Wymagania wstępne + +- .NET 6.0 lub nowszy (kod działa także na .NET Framework 4.7+). +- Licencja na Aspose.Words for .NET (bezpłatna wersja próbna wystarczy do oceny). +- Dokument Word (`input.docx`) zawierający przynajmniej jeden obiekt Office Math. + +Jeśli masz to wszystko, zaczynamy. + +## Krok 1: Zainstaluj Aspose.Words + +Zanim jakikolwiek kod zostanie uruchomiony, potrzebujesz biblioteki. Otwórz terminal w folderze projektu i wykonaj: + +```bash +dotnet add package Aspose.Words +``` + +Pobiera najnowszą stabilną wersję (stan na 2026‑04‑28 v24.12). Nie są wymagane dodatkowe DLL‑y. + +## Krok 2: Wczytaj dokument źródłowy + +Pierwszą rzeczą, którą robimy, jest odczytanie pliku .docx do obiektu `Document`. Obiekt ten daje pełny dostęp do struktury pliku, w tym do fragmentów tekstu, obrazów i obiektów matematycznych. + +```csharp +using Aspose.Words; +using Aspose.Words.Saving; + +class Program +{ + static void Main() + { + // Step 2: Load the source document + Document doc = new Document(@"YOUR_DIRECTORY\input.docx"); +``` + +> **Dlaczego to ważne:** Ładowanie dokumentu tworzy reprezentację w pamięci, dzięki czemu później możemy dostosować sposób zapisu każdego elementu. Jeśli plik nie zostanie znaleziony, Aspose rzuca `FileNotFoundException`, który warto obsłużyć w kodzie produkcyjnym. + +## Krok 3: Skonfiguruj opcje zapisu TXT dla matematyki LaTeX + +Domyślnie `Document.Save` zapisuje zwykły tekst i **pomija** wszelkie obiekty Office Math. Aby zachować równania, ustawiamy `OfficeMathExportMode` na `LaTeX`. To polecenie eksportera przetłumaczyć każde równanie na jego odpowiednik w LaTeX. + +```csharp + // Step 3: Configure TXT save options to export Office Math as LaTeX + TxtSaveOptions txtOptions = new TxtSaveOptions + { + OfficeMathExportMode = OfficeMathExportMode.LaTeX, + // Optional: preserve line breaks as they appear in the original Word file + PreserveTableLayout = true + }; +``` + +> **Pro tip:** Jeśli potrzebujesz tylko surowych znaków Unicode równania (np. do szybkiego podglądu), możesz użyć `OfficeMathExportMode.Text`. Jednak w większości przepływów naukowych `LaTeX` jest standardem, ponieważ jest powszechnie rozumiany przez procesory LaTeX. + +## Krok 4: Zapisz dokument jako zwykły tekst + +Teraz zapisujemy przetworzoną zawartość do pliku `.txt`. Plik będzie zawierał zwykłe akapity, wypunktowania i — dzięki poprzedniemu krokowi — fragmenty LaTeX dla każdego równania. + +```csharp + // Step 4: Save the document as plain‑text using the configured options + doc.Save(@"YOUR_DIRECTORY\Math.txt", txtOptions); + } +} +``` + +Po otwarciu `Math.txt` zobaczysz coś w stylu: + +``` +In this report we derive the quadratic formula: +\[ +x = \frac{-b \pm \sqrt{b^{2} - 4ac}}{2a} +\] + +The end. +``` + +Zauważ delimitery `\[` … `\]`? To bloki matematyczne LaTeX generowane automatycznie. + +## Krok 5: Zweryfikuj wynik (opcjonalnie, ale zalecane) + +Łatwo przeoczyć subtelną nieprawidłowość konwersji, szczególnie gdy równania zawierają własne symbole. Szybka kontrola to przekazanie wygenerowanego `.txt` do kompilatora LaTeX (np. `pdflatex`) i sprawdzenie, czy kompiluje się bez błędów. + +```bash +pdflatex -interaction=nonstopmode Math.txt +``` + +Jeśli kompilacja się powiedzie, skutecznie **convert word equations to latex** i **convert docx to txt** w jednym kroku. Jeśli pojawią się błędy, szukaj komunikatów o nieznanych poleceniach — zwykle wskazują one na funkcję równania, której Aspose.Words nie potrafi przetłumaczyć (np. niektóre notacje macierzy). W takich przypadkach możesz przejść na `OfficeMathExportMode.MathML` i przetworzyć MathML na LaTeX przy pomocy innego narzędzia. + +## Typowe pułapki i jak ich unikać + +| Problem | Dlaczego się pojawia | Rozwiązanie | +|-------|----------------|-----| +| Brakujące czcionki | Aspose.Words potrzebuje czcionki, aby poprawnie renderować symbole. | Zainstaluj brakującą czcionkę na komputerze lub osadź ją w .docx. | +| Złożone równania nie eksportują się | Niektóre nowsze funkcje Office Math nie mają jeszcze mapowania do LaTeX. | Użyj `OfficeMathExportMode.MathML`, a potem skonwertuj MathML na LaTeX przy pomocy biblioteki. | +| Dodatkowe puste linie | Zapis w formacie tekstowym zachowuje podziały akapitów, co może dodać białych znaków. | Ustaw `txtOptions.AddBidiMarks = false` lub przetwórz plik prostym skryptem. | + +## Pełny działający przykład (Gotowy do kopiowania) + +Poniżej znajduje się cały program, gotowy do kompilacji. Zamień `YOUR_DIRECTORY` na folder, w którym znajduje się Twój `input.docx`. + +```csharp +using System; +using Aspose.Words; +using Aspose.Words.Saving; + +namespace DocxToTxtWithLatex +{ + class Program + { + static void Main() + { + try + { + // Load the source document + Document doc = new Document(@"C:\Docs\input.docx"); + + // Configure save options: export equations as LaTeX + TxtSaveOptions txtOptions = new TxtSaveOptions + { + OfficeMathExportMode = OfficeMathExportMode.LaTeX, + PreserveTableLayout = true, + AddBidiMarks = false + }; + + // Save as plain‑text + string outputPath = @"C:\Docs\Math.txt"; + doc.Save(outputPath, txtOptions); + + Console.WriteLine($"Successfully converted DOCX to TXT. Output at: {outputPath}"); + } + catch (Exception ex) + { + Console.Error.WriteLine($"Conversion failed: {ex.Message}"); + } + } + } +} +``` + +Uruchomienie tego programu **save word as txt** jednocześnie zamienia każdy blok Office Math na LaTeX, dając czysty, przeszukiwalny plik tekstowy. + +## Kolejne kroki i powiązane tematy + +- **Konwersja wsadowa:** Owiń powyższą logikę w pętlę `foreach`, aby przetworzyć cały folder plików .docx. +- **Połączenie z generowaniem PDF:** Po uzyskaniu fragmentów LaTeX, przekaż je do pipeline’u PDF (np. `PdfSharp` + `MiKTeX`), aby tworzyć raporty PDF. +- **Export equations as latex** dla innych formatów: Aspose.Words obsługuje także `SaveFormat.Markdown`, który może automatycznie osadzać LaTeX. +- **Optymalizacja wydajności:** W przypadku bardzo dużych dokumentów, ponownie używaj tej samej instancji `TxtSaveOptions` i wyłącz niepotrzebne funkcje, takie jak `AddBidiMarks`. + +--- + +### Przykład obrazu (Opcjonalnie) + +Jeśli wolisz wskazówkę wizualną, oto zrzut ekranu pliku wyjściowego w Notepad++. + +![convert docx to txt output showing LaTeX equations](convert-docx-to-txt-output.png) + +*(Alt text: “convert docx to txt output showing LaTeX equations” – spełnia wymóg głównego słowa kluczowego.)* + +--- + +## Zakończenie + +Pokazaliśmy niezawodny sposób na **convert docx to txt** przy zachowaniu każdego równania jako czystego LaTeX. Kluczem jest flaga `OfficeMathExportMode.LaTeX`, która zamienia własny format matematyczny Worda na coś, co rozumie każdy silnik LaTeX. Dzięki pełnemu przykładowi kodu możesz **save word as txt**, **convert word to plain text** i **export equations as latex** w jednym, samodzielnym uruchomieniu. + +Śmiało eksperymentuj — zmień rozszerzenie wyjścia na `.md`, aby uzyskać Markdown, albo włącz fragment do większego pipeline’u przetwarzania dokumentów. Jeśli napotkasz jakiekolwiek problemy, zostaw komentarz poniżej; chętnie pomogę w rozwiązaniu. + +Miłego kodowania! + +{{< /blocks/products/pf/tutorial-page-section >}} +{{< /blocks/products/pf/main-container >}} +{{< /blocks/products/pf/main-wrap-class >}} +{{< blocks/products/products-backtop-button >}} \ No newline at end of file diff --git a/words/portuguese/net/ai-powered-document-processing/_index.md b/words/portuguese/net/ai-powered-document-processing/_index.md index dc46a1820e..f729a75f3d 100644 --- a/words/portuguese/net/ai-powered-document-processing/_index.md +++ b/words/portuguese/net/ai-powered-document-processing/_index.md @@ -42,6 +42,7 @@ Por fim, não se esqueça de conferir nosso [Trabalhando com opções de resumo] | [Trabalhando com o modelo de IA do Google](./working-with-google-ai-model/) Melhore o processamento de seus documentos com o Aspose.Words para .NET e o Google AI para criar resumos concisos sem esforço. | | [Trabalhando com o modelo de IA aberta](./working-with-open-ai-model/) | Desbloqueie a sumarização eficiente de documentos usando o Aspose.Words para .NET com os poderosos modelos da OpenAI. Mergulhe neste guia completo agora mesmo. | | [Trabalhando com opções de resumo](./working-with-summarize-options/) | Aprenda a resumir documentos do Word de forma eficaz usando o Aspose.Words para .NET com nosso guia passo a passo sobre integração de modelos de IA para obter insights rápidos. | +| [Conectar a LLM Local em C# – Guia de Programação Completo](./connect-to-local-llm-in-c-complete-programming-guide/) | Aprenda a integrar um modelo de linguagem local em aplicações C# usando Aspose.Words, com passo a passo completo. | {{< /blocks/products/pf/tutorial-page-section >}} diff --git a/words/portuguese/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/_index.md b/words/portuguese/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/_index.md new file mode 100644 index 0000000000..0e7454164e --- /dev/null +++ b/words/portuguese/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/_index.md @@ -0,0 +1,284 @@ +--- +category: general +date: 2026-04-28 +description: Conecte-se ao LLM local a partir de C# e solicite ao modelo de linguagem + grande que carregue um documento Word, chame o LLM local e reescreva o texto automaticamente. + Código passo a passo incluído. +draft: false +keywords: +- connect to local llm +- prompt large language model +- load word document +- call local llm +- rewrite text automatically +language: pt +og_description: Conecte-se ao LLM local a partir do C# e veja como interagir com um + modelo de linguagem grande, carregar um documento Word, chamar o LLM local e reescrever + o texto automaticamente em minutos. +og_title: Conecte-se ao LLM Local em C# – Guia Completo de Programação +tags: +- Aspose.Words +- C# +- LLM +- AI Automation +title: Conecte-se ao LLM local em C# – Guia completo de programação +url: /pt/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/ +--- + +{{< blocks/products/pf/main-wrap-class >}} +{{< blocks/products/pf/main-container >}} +{{< blocks/products/pf/tutorial-page-section >}} + +# Conectar a um LLM Local em C# – Guia Completo de Programação + +Já precisou **conectar a um llm local** a partir de um aplicativo .NET e se perguntou como fazê‑lo conversar com um arquivo Word? Você não está sozinho. Neste guia percorreremos todo o processo — conectar ao llm local, **prompt large language model**, carregar um documento Word, **call local llm** e, por fim, **rewrite text automatically**. Ao final, você terá um exemplo executável que transforma qualquer parágrafo em um tom formal sem precisar de chaves de API externas. + +## O Que Este Tutorial Abrange + +Começaremos instalando os pacotes NuGet necessários, depois iniciaremos um endpoint simples de LLM local (pense no Ollama na porta 11434). Em seguida, carregaremos um arquivo `.docx` usando Aspose.Words, enviaremos um parágrafo ao LLM, receberemos a versão reescrita e a gravaremos de volta no mesmo documento. Você também verá como lidar com armadilhas comuns — parágrafos nulos, descarte assíncrono e peculiaridades de codificação — para que o código funcione em produção, não apenas em uma demonstração. + +### Pré‑requisitos + +- .NET 6.0 SDK ou posterior (você também pode usar .NET 8 se preferir) +- Visual Studio 2022 ou VS Code com extensão C# +- **Aspose.Words for .NET** (a versão de avaliação funciona) +- Um LLM hospedado localmente que siga o contrato `/api/generate` (ex.: Ollama, LMStudio) +- Familiaridade básica com async/await em C# + +> **Pro tip:** Se ainda não instalou o Ollama, execute `ollama serve` e faça o download de um modelo com `ollama pull llama3`. O endpoint HTTP padrão será `http://localhost:11434/api/generate`. + +--- + +## Etapa 1: Instalar os Pacotes Necessários + +Primeiro, adicione os pacotes NuGet Aspose.Words e Aspose.Words.AI ao seu projeto. + +```bash +dotnet add package Aspose.Words +dotnet add package Aspose.Words.AI +``` + +Essas bibliotecas nos dão a capacidade de **load word document** e um wrapper leve para **call local llm** sem precisar criar requisições HTTP manualmente. + +--- + +## Etapa 2: Conectar ao Endpoint do LLM Local + +Conectar a um modelo hospedado localmente é tão simples quanto instanciar `LocalLargeLanguageModel`. O construtor espera a URL completa do endpoint de geração. + +```csharp +using Aspose.Words.AI; +using Aspose.Words; +using System.Threading.Tasks; + +// Create a client that talks to the LLM running on localhost +var localLlm = new LocalLargeLanguageModel("http://localhost:11434/api/generate"); +``` + +Por que envolver o endpoint em uma classe? O `LocalLargeLanguageModel` cuida da serialização JSON, tentativas de nova conexão e respostas em streaming para você — assim você pode focar na lógica do prompt em vez de mexer com `HttpClient`. + +--- + +## Etapa 3: Carregar o Documento Word de Origem + +Em seguida, trazemos o documento para a memória. Aspose.Words suporta praticamente todos os formatos Word, então `Document` analisará `input.docx` sem precisar do Office instalado. + +```csharp +// Path to the source file – adjust as needed +string inputPath = Path.Combine(Environment.CurrentDirectory, "input.docx"); + +// Load the document; throws if the file is missing or corrupted +Document sourceDocument = new Document(inputPath); +``` + +Se precisar trabalhar com um stream (por exemplo, um arquivo enviado via ASP.NET), basta substituir o caminho do arquivo por um `MemoryStream` e passá‑lo ao construtor `Document`. + +--- + +## Etapa 4: Extrair o Texto do Parágrafo Atual + +Usaremos `DocumentBuilder` para navegar no documento. Neste exemplo reescrevemos **o primeiro parágrafo**, mas você pode iterar sobre `sourceDocument.GetChildNodes(NodeType.Paragraph, true)` para processar vários. + +```csharp +// Builder gives us a cursor inside the document +DocumentBuilder docBuilder = new DocumentBuilder(sourceDocument); + +// Grab the text of the paragraph where the builder is currently positioned +string originalParagraph = docBuilder.CurrentParagraph?.GetText() ?? string.Empty; + +// Safety check – avoid sending empty strings to the LLM +if (string.IsNullOrWhiteSpace(originalParagraph)) +{ + Console.WriteLine("No paragraph found at the current cursor position."); + return; +} +``` + +O operador `?.` evita um `NullReferenceException` caso o documento esteja vazio. Esse é um daqueles **edge cases** que pegam iniciantes. + +--- + +## Etapa 5: Prompt the LLM to Rewrite the Paragraph + +Agora realmente **prompt large language model**. O prompt está em inglês simples; o wrapper o enviará como JSON ao endpoint local. + +```csharp +// Build a friendly instruction for the model +string prompt = $"Rewrite the following sentence in a more formal tone:\n{originalParagraph}"; + +// Await the model's response – this is an async call +string rewrittenParagraph = await localLlm.PromptAsync(prompt); +``` + +Por que formular a solicitação dessa maneira? LLMs respondem melhor a instruções claras e de tarefa única. Inserir uma nova linha após os dois‑pontos separa a instrução do conteúdo, reduzindo a chance de o modelo ecoar o prompt de volta. + +**Saída esperada** – Se `originalParagraph` fosse `"Hey, what's up?"`, o LLM poderia retornar: + +> “Good day, how may I assist you?” + +Você pode verificar o resultado imprimindo‑o: + +```csharp +Console.WriteLine("Original: " + originalParagraph); +Console.WriteLine("Rewritten: " + rewrittenParagraph); +``` + +--- + +## Etapa 6: Inserir o Texto Reescrito de Volta no Documento + +Com o novo texto em mãos, substituímos o parágrafo antigo. `DocumentBuilder.Writeln` grava uma nova linha e avança o cursor, o que é perfeito para acrescentar. Se precisar *substituir* exatamente o mesmo parágrafo, use `docBuilder.CurrentParagraph.RemoveAllChildren()` antes de escrever. + +```csharp +// Option A – Append a new paragraph (keeps the original) +docBuilder.Writeln(rewrittenParagraph); + +// Option B – Replace the existing paragraph (uncomment to use) +// docBuilder.CurrentParagraph.RemoveAllChildren(); +// docBuilder.CurrentParagraph.AppendChild(new Run(docBuilder.Document, rewrittenParagraph)); +``` + +Ambas as abordagens são mostradas para que você escolha a que melhor se adapta ao seu fluxo de trabalho. + +--- + +## Etapa 7: Salvar o Documento Atualizado + +Por fim, persistimos as alterações em um novo arquivo. Aspose.Words escolhe automaticamente o formato com base na extensão do arquivo. + +```csharp +string outputPath = Path.Combine(Environment.CurrentDirectory, "output.docx"); +sourceDocument.Save(outputPath); + +Console.WriteLine($"Document saved to {outputPath}"); +``` + +Abra `output.docx` no Word e verá que o parágrafo agora está em tom formal. + +--- + +## Exemplo Completo Funcional + +Abaixo está o **programa completo e autocontido**. Copie‑e cole em um projeto de console, restaure os pacotes NuGet e execute — nenhuma configuração extra é necessária além de um LLM local em execução. + +```csharp +using Aspose.Words.AI; +using Aspose.Words; +using System; +using System.IO; +using System.Threading.Tasks; + +class Program +{ + static async Task Main() + { + // ------------------------------------------------- + // Step 1: Connect to the locally hosted LLM endpoint + // ------------------------------------------------- + var localLlm = new LocalLargeLanguageModel("http://localhost:11434/api/generate"); + + // ------------------------------------------------- + // Step 2: Load the source Word document + // ------------------------------------------------- + string inputPath = Path.Combine(Environment.CurrentDirectory, "input.docx"); + Document sourceDocument = new Document(inputPath); + + // ------------------------------------------------- + // Step 3: Retrieve the text of the current paragraph + // ------------------------------------------------- + DocumentBuilder docBuilder = new DocumentBuilder(sourceDocument); + string originalParagraph = docBuilder.CurrentParagraph?.GetText() ?? string.Empty; + + if (string.IsNullOrWhiteSpace(originalParagraph)) + { + Console.WriteLine("No paragraph found at the current cursor position."); + return; + } + + // ------------------------------------------------- + // Step 4: Ask the LLM to rewrite the paragraph in a formal tone + // ------------------------------------------------- + string prompt = $"Rewrite the following sentence in a more formal tone:\n{originalParagraph}"; + string rewrittenParagraph = await localLlm.PromptAsync(prompt); + + // ------------------------------------------------- + // Step 5: Insert the rewritten text back into the document + // ------------------------------------------------- + docBuilder.Writeln(rewrittenParagraph); + + // ------------------------------------------------- + // Step 6: Save the updated document + // ------------------------------------------------- + string outputPath = Path.Combine(Environment.CurrentDirectory, "output.docx"); + sourceDocument.Save(outputPath); + + Console.WriteLine("Original paragraph:"); + Console.WriteLine(originalParagraph); + Console.WriteLine("\nRewritten paragraph:"); + Console.WriteLine(rewrittenParagraph); + Console.WriteLine($"\nDocument saved to {outputPath}"); + } +} +``` + +### O Que Esperar ao Executar + +1. O console imprime os parágrafos original e reescrito. +2. `output.docx` aparece ao lado de `input.docx`. +3. Ao abrir o arquivo, o novo parágrafo formal está inserido após o original (ou substituído, se você usou o código alternativo). + +--- + +## Lidando com Casos de Borda Comuns + +| Situação | Solução | +|-----------|----------| +| **Parágrafo vazio ou contendo apenas espaços** | Verifique `string.IsNullOrWhiteSpace` antes de fazer o prompt (veja a Etapa 3). | +| **LLM devolve erro ou string vazia** | Envolva `PromptAsync` em um `try/catch` e retorne o texto original como fallback. | +| **Vários parágrafos precisam ser reescritos** | Percorra `sourceDocument.GetChildNodes(NodeType.Paragraph, true)` e aplique a mesma lógica de prompt. | +| **Documentos grandes causam latência** | Agrupe parágrafos e envie‑os em uma única requisição (prompt de até 4 KB por chamada). | +| **Caracteres não‑ASCII ficam corrompidos** | Garanta que o endpoint do LLM use UTF‑8 (a maioria dos modelos modernos já faz). | + +--- + +## Próximos Passos & Tópicos Relacionados + +- **Prompt large language model** com instruções mais ricas (ex.: guias de estilo, limites de tamanho). +- Use **call local llm** em uma Web API para expor a automação de documentos como serviço. +- Explore **load word document** em streams paralelos para cenários de alta taxa de transferência. +- Combine esta abordagem com **rewrite text automatically** para geração em massa de e‑mails ou padronização de relatórios. + +Se quiser aprofundar, consulte a documentação da Aspose sobre **document merging** e a referência da API do Ollama para parâmetros de amostragem personalizados. + +--- + +## Conclusão + +Acabamos de mostrar como **connect to local llm** a partir de C#, **prompt large language model**, **load word document**, **call local llm** e **rewrite text automatically** — tudo em um único aplicativo console executável. O padrão escala: troque o prompt, itere sobre parágrafos ou exponha a lógica via endpoint ASP.NET. O ponto principal é que modelos de IA locais podem ser integrados estreitamente com bibliotecas clássicas de processamento de documentos, proporcionando automação poderosa sem jamais sair do seu ambiente on‑prem confiável. + +Tem dúvidas sobre threading, + +{{< /blocks/products/pf/tutorial-page-section >}} +{{< /blocks/products/pf/main-container >}} +{{< /blocks/products/pf/main-wrap-class >}} +{{< blocks/products/products-backtop-button >}} \ No newline at end of file diff --git a/words/portuguese/net/programming-with-markdownsaveoptions/_index.md b/words/portuguese/net/programming-with-markdownsaveoptions/_index.md index 0d50695fe3..d1800f8951 100644 --- a/words/portuguese/net/programming-with-markdownsaveoptions/_index.md +++ b/words/portuguese/net/programming-with-markdownsaveoptions/_index.md @@ -75,6 +75,9 @@ Aprenda a extrair e salvar imagens de documentos Word ao convertê-los para Mark ### [Converter Word para Markdown em C# – Guia completo com extração de imagens](./convert-word-to-markdown-in-c-full-guide-with-image-extracti/) Aprenda a converter documentos Word para Markdown em C#, extraindo imagens e preservando a formatação usando Aspose.Words. +### [Caminho relativo da imagem markdown – Converter Word para Markdown](./markdown-image-relative-path-convert-word-to-markdown/) +Aprenda a usar caminhos relativos de imagens ao converter documentos Word para Markdown com Aspose.Words para .NET. + {{< /blocks/products/pf/tutorial-page-section >}} diff --git a/words/portuguese/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/_index.md b/words/portuguese/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/_index.md new file mode 100644 index 0000000000..e20c535252 --- /dev/null +++ b/words/portuguese/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/_index.md @@ -0,0 +1,205 @@ +--- +category: general +date: 2026-04-28 +description: Aprenda como definir um caminho relativo de imagem em markdown ao converter + Word para markdown, extrair imagens do Word e criar uma pasta de recursos para as + imagens exportadas. +draft: false +keywords: +- markdown image relative path +- convert word to markdown +- extract images from word +- create resources folder +- export images from docx +language: pt +og_description: Defina um caminho relativo de imagem em markdown ao converter Word + para markdown, extraia imagens do Word e crie uma pasta de recursos para as imagens + exportadas. +og_title: caminho relativo da imagem markdown – Converter Word para Markdown +tags: +- Aspose.Words +- C# +- Markdown +- Image Export +title: Caminho relativo da imagem em Markdown – Converter Word para Markdown +url: /pt/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/ +--- + +{{< blocks/products/pf/main-wrap-class >}} +{{< blocks/products/pf/main-container >}} +{{< blocks/products/pf/tutorial-page-section >}} + +# caminho relativo de imagem markdown – Converter Word para Markdown + +Já precisou de um **caminho relativo de imagem markdown** enquanto **converte Word para markdown**? Você não está sozinho. A maioria dos desenvolvedores encontra um obstáculo quando o Markdown gerado aponta para imagens em uma pasta plana, quebrando a estrutura de links relativos que você espera em um site estático ou em um repositório GitHub. + +Neste tutorial, percorreremos uma solução completa, de ponta a ponta, que **extrai imagens do Word**, **cria uma pasta de recursos** e reescreve as referências de imagem para que usem um *caminho relativo de imagem markdown* limpo. Ao final, você terá um arquivo `.md` pronto para publicação e um diretório `Resources` organizadamente estruturado contendo todas as imagens extraídas do `.docx` original. + +> **O que você receberá:** um único programa C# (sem scripts externos), uma explicação clara do *porquê* cada parte importa, e um conjunto de dicas práticas que você pode copiar‑colar em seus próprios projetos. + +--- + +## Pré-requisitos + +- **.NET 6.0** ou posterior instalado (você também pode direcionar o .NET Framework 4.7+, mas o .NET 6 é a escolha ideal para novos projetos). +- **Aspose.Words for .NET** (o pacote NuGet mais recente no momento da escrita, versão 23.12). Instale com: + ```bash + dotnet add package Aspose.Words + ``` +- Um documento Word que realmente contém imagens — vamos chamá‑lo de `WithImages.docx`. +- Uma pasta onde você deseja que o markdown de saída e as imagens fiquem, por exemplo `C:\Projects\MarkdownExport`. + +Nenhuma biblioteca adicional é necessária; todo o resto é tratado pelo Aspose.Words. + +--- + +## Etapa 1: Carregar o documento Word de origem (ponto de partida para converter Word para markdown) + +```csharp +using System; +using System.IO; +using Aspose.Words; +using Aspose.Words.Saving; + +class Program +{ + static void Main() + { + // Adjust the path to point at your own .docx file. + string sourcePath = @"C:\Projects\MarkdownExport\WithImages.docx"; + + // Load the document – this is where Aspose.Words parses the Word file. + Document doc = new Document(sourcePath); + + // The rest of the workflow follows… + } +} +``` + +*Por que isso importa:* Carregar o documento nos dá acesso à árvore interna de nós, que inclui as partes de imagem que mais tarde precisamos **exportar imagens do docx**. Se o carregamento falhar, nenhuma das etapas posteriores será executada, então verifique novamente o caminho e as permissões de arquivo. + +--- + +## Etapa 2: Configurar `MarkdownSaveOptions` com um callback personalizado (o coração da criação da pasta de recursos) + +O `ResourceSavingCallback` nos permite intervir toda vez que o Aspose.Words quiser gravar um arquivo de imagem. Dentro do callback, **criaremos uma sub‑pasta Resources** e ajustaremos a referência para que o markdown gerado use um *caminho relativo de imagem markdown*. + +```csharp +// Inside Main(), after loading the document: +string outputFolder = @"C:\Projects\MarkdownExport"; +string resourcesFolder = Path.Combine(outputFolder, "Resources"); + +// Make sure the folder exists before we start saving anything. +Directory.CreateDirectory(resourcesFolder); + +// Set up the Markdown save options. +MarkdownSaveOptions mdOptions = new MarkdownSaveOptions +{ + // Hook that runs for every image resource. + ResourceSavingCallback = new MyMarkdownResourceCallback(resourcesFolder) +}; + +// Save the document as Markdown. +string markdownPath = Path.Combine(outputFolder, "Doc.md"); +doc.Save(markdownPath, mdOptions); +``` + +Observe que passamos `resourcesFolder` para o construtor do callback — isso mantém o caminho da pasta flexível e evita codificar strings diretamente no código. + +--- + +## Etapa 3: Implementar o callback que **cria a pasta de recursos** e reescreve o caminho + +```csharp +/// +/// Handles image extraction and path rewriting for markdown export. +/// +class MyMarkdownResourceCallback : IResourceSavingCallback +{ + private readonly string _resourcesFolder; + + public MyMarkdownResourceCallback(string resourcesFolder) + { + _resourcesFolder = resourcesFolder; + } + + public void ResourceSaving(ResourceSavingArgs args) + { + // 1️⃣ Build the full file system path where the image will be stored. + string targetPath = Path.Combine(_resourcesFolder, args.ResourceFileName); + + // 2️⃣ Ensure the directory exists (in case Aspose creates sub‑folders). + Directory.CreateDirectory(Path.GetDirectoryName(targetPath)); + + // 3️⃣ Write the image stream to disk. + using (FileStream fileStream = File.Create(targetPath)) + { + args.Stream.CopyTo(fileStream); + } + + // 4️⃣ Update the markdown reference to use a relative path. + // This is the crucial line that gives us the markdown image relative path. + args.ResourceFileName = Path.Combine("Resources", args.ResourceFileName); + } +} +``` + +*Por que isso funciona:* `args.Stream` contém os bytes brutos da imagem. Ao copiá‑los para um arquivo dentro da nossa pasta `Resources`, **exportamos imagens do docx** com segurança. Em seguida, substituímos `args.ResourceFileName` por uma URL relativa (`Resources/image.png`). Quando o Aspose.Words posteriormente grava o markdown, ele injeta exatamente essa string, nos proporcionando o *caminho relativo de imagem markdown* desejado. + +--- + +## Etapa 4: Verificar o Markdown gerado (como a saída final se parece) + +Abra `Doc.md` em qualquer editor de texto. Você deverá ver algo semelhante a: + +```markdown +# Sample Heading + +Here is an inline picture: + +![Image 0](Resources/Image_0.png) + +And a picture inside a table: + +![Image 1](Resources/Image_1.jpg) +``` + +A parte importante é que cada referência de imagem aponta para `Resources/...` – esse é o **caminho relativo de imagem markdown** que buscávamos. + +![exemplo de caminho relativo de imagem markdown](example.png "exemplo de caminho relativo de imagem markdown") + +*Dica:* Se você abrir o markdown em um visualizador que respeita links relativos (visualização do VS Code, GitHub ou um gerador de site estático), as imagens serão renderizadas corretamente sem nenhuma configuração adicional. + +--- + +## Etapa 5: Armadilhas comuns e dicas avançadas + +| Problema | Por que acontece | Como corrigir | +|----------|------------------|---------------| +| Imagens acabam na pasta raiz em vez de `Resources` | O callback não foi anexado ou `args.ResourceFileName` não foi sobrescrito. | Verifique novamente se `ResourceSavingCallback` está definido **antes** de chamar `doc.Save`. | +| Nomes de arquivos contêm caracteres ilegais | O Word às vezes nomeia imagens com espaços ou símbolos Unicode. | Use `Path.GetInvalidFileNameChars()` para sanitizar `args.ResourceFileName` dentro do callback. | +| Documentos grandes demoram muito para processar | Cada imagem é gravada de forma síncrona. | Altere para I/O assíncrono (`await args.Stream.CopyToAsync(fileStream)`) se você estiver no .NET 6+ e precisar de desempenho. | +| Caminhos relativos quebram quando o markdown é movido | O caminho é relativo à localização do arquivo markdown. | Mantenha `Doc.md` e a pasta `Resources` juntos, ou ajuste o callback para usar um prefixo relativo diferente (por exemplo, `../assets`). | + +--- + +## Etapa 6: Expandindo a solução (e se você precisar de mais controle?) + +- **Múltiplos formatos de saída:** Substitua `MarkdownSaveOptions` por `HtmlSaveOptions` ou `PdfSaveOptions` mantendo o mesmo callback — o Aspose.Words o invocará para cada imagem, independentemente do formato. +- **Nomeação personalizada de imagens:** Se quiser renomear imagens (por exemplo, `figure-01.png`), modifique `args.ResourceFileName` dentro do callback antes de gravar o arquivo. +- **Incorporar imagens como Base64:** Defina `args.ResourceFileName` como um data URI (`data:image/png;base64,...`) e pule a gravação do arquivo. Isso é útil para exportações de markdown em um único arquivo. + +--- + +## Conclusão + +Agora você tem um programa C# totalmente funcional que **converte Word para markdown**, **extrai imagens do word**, **cria uma pasta de recursos**, e garante um **caminho relativo de imagem markdown** limpo para cada imagem. O código é autônomo, funciona com a versão mais recente do Aspose.Words e pode ser inserido em qualquer projeto .NET com esforço mínimo. + +Próximos passos? Experimente alimentar o markdown gerado em um gerador de site estático como Hugo ou Jekyll, ou experimente o callback para incorporar imagens diretamente como strings Base64. Se você encontrar casos extremos — por exemplo, imagens SVG ou arquivos incomumente grandes — consulte a tabela “Armadilhas comuns”; um pequeno ajuste geralmente resolve o problema. + +Feliz codificação, e que seu markdown sempre aponte para a pasta correta! + +{{< /blocks/products/pf/tutorial-page-section >}} +{{< /blocks/products/pf/main-container >}} +{{< /blocks/products/pf/main-wrap-class >}} +{{< blocks/products/products-backtop-button >}} \ No newline at end of file diff --git a/words/portuguese/net/programming-with-officemath/_index.md b/words/portuguese/net/programming-with-officemath/_index.md index e0218760bc..c37f462c50 100644 --- a/words/portuguese/net/programming-with-officemath/_index.md +++ b/words/portuguese/net/programming-with-officemath/_index.md @@ -23,6 +23,7 @@ Os tutoriais do Aspose.Words para .NET sobre Processamento de Texto com o Office | [Equações matemáticas](./math-equations/) | Aprenda a configurar equações matemáticas em documentos do Word usando o Aspose.Words para .NET. Guia passo a passo com exemplos, perguntas frequentes e muito mais. | | [Salvar docx como txt – Exportar Word Math para LaTeX com C#](./save-docx-as-txt-export-word-math-to-latex-with-c/) | Saiba como salvar documentos .docx como .txt e exportar equações Word Math para LaTeX usando C#. | | [Salvar documento como Txt – Exportar matemática do Word para LaTeX em C#](./save-document-as-txt-export-word-math-to-latex-in-c/) | Aprenda a salvar documentos como TXT e exportar equações do Word para LaTeX usando C#. | +| [Converter DOCX para TXT – Exportar Equações do Word para LaTeX em C#](./convert-docx-to-txt-export-word-equations-to-latex-in-c/) | Aprenda a converter arquivos DOCX em TXT e exportar equações do Word para LaTeX usando C#. | {{< /blocks/products/pf/tutorial-page-section >}} diff --git a/words/portuguese/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/_index.md b/words/portuguese/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/_index.md new file mode 100644 index 0000000000..3704d4d26f --- /dev/null +++ b/words/portuguese/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/_index.md @@ -0,0 +1,211 @@ +--- +category: general +date: 2026-04-28 +description: Converta DOCX para TXT e exporte equações do Word para LaTeX usando Aspose.Words. + Aprenda como salvar o Word como TXT e lidar com objetos matemáticos em poucos passos. +draft: false +keywords: +- convert docx to txt +- convert word equations to latex +- convert word to plain text +- save word as txt +- export equations as latex +language: pt +og_description: Converta DOCX para TXT e exporte equações do Word para LaTeX com um + simples trecho de C#. Guia completo, código e dicas. +og_title: Converter DOCX para TXT – Exportar Equações do Word para LaTeX +tags: +- C# +- Aspose.Words +- Document Conversion +title: Converter DOCX para TXT – Exportar Equações do Word para LaTeX em C# +url: /pt/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/ +--- + +{{< blocks/products/pf/main-wrap-class >}} +{{< blocks/products/pf/main-container >}} +{{< blocks/products/pf/tutorial-page-section >}} + +# Converter DOCX para TXT – Exportar Equações do Word para LaTeX + +Já precisou **converter docx para txt** mas temia que as fórmulas no seu arquivo Word se transformassem em uma bagunça ilegível? Você não está sozinho. Em muitos projetos de engenharia ou acadêmicos, o documento fonte está em .docx, porém as ferramentas posteriores só entendem plain‑text ou LaTeX. A boa notícia? Com algumas linhas de C# e Aspose.Words você pode **converter docx para txt** *e* manter cada equação como código LaTeX limpo. + +Neste tutorial vamos percorrer todo o processo: carregar um .docx, configurar as opções de salvamento para que os objetos Office Math se tornem LaTeX e, finalmente, gravar o resultado em um arquivo .txt. Ao final, você saberá como **save word as txt**, **convert word to plain text** e **export equations as latex** sem precisar vasculhar a documentação da API. + +## O que você aprenderá + +- As chamadas de API exatas necessárias para **converter docx para txt** preservando as equações. +- Por que escolher `OfficeMathExportMode.LaTeX` é a forma recomendada de **convert word equations to latex**. +- Como lidar com casos de borda comuns, como fontes ausentes ou recursos de equação não suportados. +- Um programa C# completo, pronto‑para‑executar, que você pode inserir em qualquer projeto .NET. + +### Pré‑requisitos + +- .NET 6.0 ou superior (o código também funciona no .NET Framework 4.7+). +- Uma licença para Aspose.Words for .NET (a avaliação gratuita funciona para testes). +- Um documento Word (`input.docx`) que contenha ao menos um objeto Office Math. + +Se você tem tudo isso, vamos começar. + +## Etapa 1: Instalar Aspose.Words + +Antes que qualquer código seja executado, você precisa da biblioteca. Abra um terminal na pasta do seu projeto e execute: + +```bash +dotnet add package Aspose.Words +``` + +Isso baixa a versão estável mais recente (em 2026‑04‑28 v24.12). Nenhum DLL extra é necessário. + +## Etapa 2: Carregar o Documento Fonte + +A primeira coisa que fazemos é ler o arquivo .docx em um objeto `Document`. Esse objeto nos dá acesso total à estrutura do arquivo, incluindo trechos de texto, imagens e objetos matemáticos. + +```csharp +using Aspose.Words; +using Aspose.Words.Saving; + +class Program +{ + static void Main() + { + // Step 2: Load the source document + Document doc = new Document(@"YOUR_DIRECTORY\input.docx"); +``` + +> **Por que isso importa:** Carregar o documento cria uma representação em memória, de modo que depois possamos ajustar como cada elemento será escrito. Se o arquivo não for encontrado, o Aspose lança uma `FileNotFoundException`, que você pode querer capturar em código de produção. + +## Etapa 3: Configurar Opções de Salvamento TXT para Matemática LaTeX + +Por padrão, `Document.Save` grava texto simples e **descarta** qualquer Office Math. Para manter essas equações, definimos `OfficeMathExportMode` como `LaTeX`. Isso indica ao exportador que traduza cada equação para seu equivalente LaTeX. + +```csharp + // Step 3: Configure TXT save options to export Office Math as LaTeX + TxtSaveOptions txtOptions = new TxtSaveOptions + { + OfficeMathExportMode = OfficeMathExportMode.LaTeX, + // Optional: preserve line breaks as they appear in the original Word file + PreserveTableLayout = true + }; +``` + +> **Dica profissional:** Se você precisar apenas dos caracteres Unicode brutos da equação (por exemplo, para uma pré‑visualização rápida), pode usar `OfficeMathExportMode.Text`. Mas para a maioria dos pipelines científicos, `LaTeX` é o padrão ouro porque é universalmente compreendido pelos processadores LaTeX. + +## Etapa 4: Salvar o Documento como Texto Simples + +Agora gravamos o conteúdo transformado em um arquivo `.txt`. O arquivo conterá parágrafos normais, marcadores e—graças à etapa anterior—trechos LaTeX para cada equação. + +```csharp + // Step 4: Save the document as plain‑text using the configured options + doc.Save(@"YOUR_DIRECTORY\Math.txt", txtOptions); + } +} +``` + +Ao abrir `Math.txt` você verá algo como: + +``` +In this report we derive the quadratic formula: +\[ +x = \frac{-b \pm \sqrt{b^{2} - 4ac}}{2a} +\] + +The end. +``` + +Observe os delimitadores `\[` … `\]`? Eles são os blocos de matemática LaTeX gerados automaticamente. + +## Etapa 5: Verificar a Saída (Opcional, mas Recomendado) + +É fácil perder um problema sutil de conversão, especialmente quando as equações contêm símbolos personalizados. Uma verificação rápida é alimentar o `.txt` gerado a um compilador LaTeX (por exemplo, `pdflatex`) e ver se ele compila sem erros. + +```bash +pdflatex -interaction=nonstopmode Math.txt +``` + +Se a compilação for bem‑sucedida, você efetivamente **convert word equations to latex** e **convert docx to txt** de uma só vez. Se ocorrerem erros, procure mensagens sobre comandos indefinidos—geralmente indicam um recurso de equação que o Aspose.Words não consegue traduzir (por exemplo, certas notações de matriz). Nesses casos, você pode recorrer a `OfficeMathExportMode.MathML` e pós‑processar o MathML para LaTeX com outra ferramenta. + +## Armadilhas Comuns & Como Evitá‑las + +| Problema | Por que acontece | Solução | +|----------|------------------|---------| +| Fontes ausentes | Aspose.Words precisa da fonte para renderizar símbolos corretamente. | Instale a fonte faltante na máquina ou incorpore‑a no .docx. | +| Equações complexas não exportadas | Alguns recursos mais novos do Office Math ainda não foram mapeados para LaTeX. | Use `OfficeMathExportMode.MathML` e depois converta com uma biblioteca MathML‑to‑LaTeX. | +| Linhas em branco extras | O salvador de texto simples preserva quebras de parágrafo, o que pode gerar espaços vazios. | Defina `txtOptions.AddBidiMarks = false` ou pós‑procese o arquivo com um script simples. | + +## Exemplo Completo (Pronto para Copiar‑Colar) + +Abaixo está o programa inteiro, pronto para compilar. Substitua `YOUR_DIRECTORY` pela pasta que contém seu `input.docx`. + +```csharp +using System; +using Aspose.Words; +using Aspose.Words.Saving; + +namespace DocxToTxtWithLatex +{ + class Program + { + static void Main() + { + try + { + // Load the source document + Document doc = new Document(@"C:\Docs\input.docx"); + + // Configure save options: export equations as LaTeX + TxtSaveOptions txtOptions = new TxtSaveOptions + { + OfficeMathExportMode = OfficeMathExportMode.LaTeX, + PreserveTableLayout = true, + AddBidiMarks = false + }; + + // Save as plain‑text + string outputPath = @"C:\Docs\Math.txt"; + doc.Save(outputPath, txtOptions); + + Console.WriteLine($"Successfully converted DOCX to TXT. Output at: {outputPath}"); + } + catch (Exception ex) + { + Console.Error.WriteLine($"Conversion failed: {ex.Message}"); + } + } + } +} +``` + +Executar este programa **save word as txt** enquanto converte cada bloco Office Math em LaTeX, fornecendo um arquivo de texto simples, pesquisável e limpo. + +## Próximos Passos & Tópicos Relacionados + +- **Conversão em lote:** Envolva a lógica acima em um loop `foreach` para processar uma pasta inteira de arquivos .docx. +- **Combinar com geração de PDF:** Depois de obter os trechos LaTeX, alimente‑os em um pipeline PDF (por exemplo, `PdfSharp` + `MiKTeX`) para produzir relatórios em PDF. +- **Exportar equações como latex** para outros formatos: Aspose.Words também suporta `SaveFormat.Markdown`, que pode incorporar LaTeX automaticamente. +- **Ajuste de desempenho:** Para documentos muito grandes, reutilize a mesma instância de `TxtSaveOptions` e desative recursos desnecessários como `AddBidiMarks`. + +--- + +### Exemplo de Imagem (Opcional) + +Se preferir um indicativo visual, aqui está uma captura de tela do arquivo de saída no Notepad++. + +![converter docx para txt exibindo equações LaTeX](convert-docx-to-txt-output.png) + +*(Texto alternativo: “saída de converter docx para txt exibindo equações LaTeX” – satisfaz o requisito de palavra‑chave principal.)* + +--- + +## Conclusão + +Acabamos de demonstrar uma forma confiável de **converter docx para txt** preservando cada equação como LaTeX limpo. A chave é a flag `OfficeMathExportMode.LaTeX`, que transforma o formato proprietário de matemática do Word em algo que qualquer motor LaTeX entende. Com o exemplo de código completo acima, você pode **save word as txt**, **convert word to plain text** e **export equations as latex** em uma única execução autônoma. + +Sinta‑se à vontade para experimentar—troque a extensão de saída para `.md` para Markdown, ou integre o trecho em um pipeline maior de processamento de documentos. Se encontrar alguma particularidade, deixe um comentário abaixo; ficarei feliz em ajudar a solucionar. + +Happy coding! + +{{< /blocks/products/pf/tutorial-page-section >}} +{{< /blocks/products/pf/main-container >}} +{{< /blocks/products/pf/main-wrap-class >}} +{{< blocks/products/products-backtop-button >}} \ No newline at end of file diff --git a/words/russian/net/ai-powered-document-processing/_index.md b/words/russian/net/ai-powered-document-processing/_index.md index adae326f0c..478b9cf0c1 100644 --- a/words/russian/net/ai-powered-document-processing/_index.md +++ b/words/russian/net/ai-powered-document-processing/_index.md @@ -42,6 +42,7 @@ | [Работа с моделью Google AI](./working-with-google-ai-model/) Повысьте уровень обработки документов с помощью Aspose.Words для .NET и Google AI, чтобы легко создавать краткие резюме. | | [Работа с открытой моделью ИИ](./working-with-open-ai-model/) | Разблокируйте эффективное реферирование документов с помощью Aspose.Words для .NET с мощными моделями OpenAI. Погрузитесь в это всеобъемлющее руководство прямо сейчас. | | [Работа с параметрами резюмирования](./working-with-summarize-options/) | Научитесь эффективно резюмировать документы Word с помощью Aspose.Words для .NET с помощью нашего пошагового руководства по интеграции моделей ИИ для быстрого получения информации. | +| [Подключение к локальному LLM на C# – Полное руководство по программированию](./connect-to-local-llm-in-c-complete-programming-guide/) | Узнайте, как интегрировать локальную модель LLM в C# с помощью Aspose.Words для .NET, пошаговое руководство. | {{< /blocks/products/pf/tutorial-page-section >}} diff --git a/words/russian/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/_index.md b/words/russian/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/_index.md new file mode 100644 index 0000000000..2f60d47235 --- /dev/null +++ b/words/russian/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/_index.md @@ -0,0 +1,284 @@ +--- +category: general +date: 2026-04-28 +description: Подключитесь к локальной LLM из C# и попросите большую языковую модель + загрузить документ Word, вызвать локальную LLM и автоматически переписать текст. + Пошаговый код включён. +draft: false +keywords: +- connect to local llm +- prompt large language model +- load word document +- call local llm +- rewrite text automatically +language: ru +og_description: Подключитесь к локальной LLM из C# и узнайте, как задавать запросы + большой языковой модели, загружать документ Word, вызывать локальную LLM и автоматически + переписывать текст за несколько минут. +og_title: Подключение к локальному LLM на C# — Полное руководство по программированию +tags: +- Aspose.Words +- C# +- LLM +- AI Automation +title: Подключение к локальному LLM в C# — Полное руководство по программированию +url: /ru/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/ +--- + +{{< blocks/products/pf/main-wrap-class >}} +{{< blocks/products/pf/main-container >}} +{{< blocks/products/pf/tutorial-page-section >}} + +# Подключение к локальному LLM в C# – Полное руководство по программированию + +Когда‑нибудь вам нужно было **connect to local llm** из .NET‑приложения и вы задавались вопросом, как заставить его работать с файлом Word? Вы не одиноки. В этом руководстве мы пройдем весь процесс — подключение к локальному llm, **prompt large language model**, загрузка документа Word, **call local llm**, и, наконец, **rewrite text automatically**. К концу у вас будет готовый пример, который преобразует любой абзац в формальный стиль без использования внешних API‑ключей. + +## Что покрывает этот учебник + +Мы начнём с установки необходимых пакетов NuGet, затем запустим простой локальный LLM‑endpoint (например, Ollama на порту 11434). После этого мы загрузим файл `.docx` с помощью Aspose.Words, отправим абзац в LLM, получим переписанную версию и запишем её обратно в тот же документ. Вы также увидите, как справляться с распространёнными подводными камнями — пустыми абзацами, асинхронным освобождением ресурсов и особенностями кодировки — чтобы код работал в продакшене, а не только в демонстрации. + +### Требования + +- .NET 6.0 SDK или новее (можно также использовать .NET 8, если хотите) +- Visual Studio 2022 или VS Code с расширением C# +- **Aspose.Words for .NET** (бесплатная пробная версия подходит) +- Локально развернутый LLM, поддерживающий контракт `/api/generate` (например, Ollama, LMStudio) +- Базовое знакомство с async/await в C# + +> **Pro tip:** Если вы ещё не установили Ollama, запустите `ollama serve` и загрузите модель командой `ollama pull llama3`. По умолчанию HTTP‑endpoint будет `http://localhost:11434/api/generate`. + +--- + +## Шаг 1: Установите необходимые пакеты + +Сначала добавьте пакеты NuGet Aspose.Words и Aspose.Words.AI в ваш проект. + +```bash +dotnet add package Aspose.Words +dotnet add package Aspose.Words.AI +``` + +Эти библиотеки предоставляют возможность **load word document** и лёгкую обёртку для **call local llm** без ручного формирования HTTP‑запросов. + +--- + +## Шаг 2: Подключитесь к локальному LLM‑endpoint + +Подключение к локально развернутой модели так же просто, как создание экземпляра `LocalLargeLanguageModel`. Конструктор ожидает полный URL endpoint‑а генерации. + +```csharp +using Aspose.Words.AI; +using Aspose.Words; +using System.Threading.Tasks; + +// Create a client that talks to the LLM running on localhost +var localLlm = new LocalLargeLanguageModel("http://localhost:11434/api/generate"); +``` + +Зачем мы оборачиваем endpoint в класс? `LocalLargeLanguageModel` обрабатывает сериализацию JSON, повторные попытки и потоковые ответы за вас — так что вы можете сосредоточиться на логике подсказки, а не возиться с `HttpClient`. + +--- + +## Шаг 3: Загрузите исходный документ Word + +Далее мы загружаем документ в память. Aspose.Words поддерживает практически любой формат Word, поэтому `Document` разберёт `input.docx` без необходимости установки Office. + +```csharp +// Path to the source file – adjust as needed +string inputPath = Path.Combine(Environment.CurrentDirectory, "input.docx"); + +// Load the document; throws if the file is missing or corrupted +Document sourceDocument = new Document(inputPath); +``` + +Если вам нужно работать со стримом (например, файл, загруженный через ASP.NET), просто замените путь к файлу на `MemoryStream` и передайте его в конструктор `Document`. + +--- + +## Шаг 4: Извлеките текущий текст абзаца + +Мы будем использовать `DocumentBuilder` для навигации по документу. В этом примере мы переписываем **the first paragraph**, но вы можете перебрать `sourceDocument.GetChildNodes(NodeType.Paragraph, true)`, чтобы обработать их множество. + +```csharp +// Builder gives us a cursor inside the document +DocumentBuilder docBuilder = new DocumentBuilder(sourceDocument); + +// Grab the text of the paragraph where the builder is currently positioned +string originalParagraph = docBuilder.CurrentParagraph?.GetText() ?? string.Empty; + +// Safety check – avoid sending empty strings to the LLM +if (string.IsNullOrWhiteSpace(originalParagraph)) +{ + Console.WriteLine("No paragraph found at the current cursor position."); + return; +} +``` + +Оператор `?.` предотвращает `NullReferenceException`, если документ окажется пустым. Это один из тех **edge cases**, которые ставят в тупик новичков. + +--- + +## Шаг 5: Подайте запрос LLM для переписывания абзаца + +Теперь мы действительно **prompt large language model**. Подсказка написана простым английским; обёртка отправит её как JSON на локальный endpoint. + +```csharp +// Build a friendly instruction for the model +string prompt = $"Rewrite the following sentence in a more formal tone:\n{originalParagraph}"; + +// Await the model's response – this is an async call +string rewrittenParagraph = await localLlm.PromptAsync(prompt); +``` + +Почему запрос сформулирован именно так? LLM лучше реагируют на чёткие, одно‑задачные инструкции. Добавление новой строки после двоеточия отделяет инструкцию от содержимого, уменьшая вероятность того, что модель просто повторит подсказку. + +**Expected output** – Если `originalParagraph` было `"Hey, what's up?"`, LLM может вернуть: + +> “Good day, how may I assist you?” + +Вы можете проверить результат, выведя его на печать: + +```csharp +Console.WriteLine("Original: " + originalParagraph); +Console.WriteLine("Rewritten: " + rewrittenParagraph); +``` + +--- + +## Шаг 6: Вставьте переписанный текст обратно в документ + +Имея новый текст, мы заменяем старый абзац. `DocumentBuilder.Writeln` пишет новую строку и перемещает курсор вперёд, что идеально подходит для добавления. Если вам нужно *replace* тот же самый абзац, вы можете вызвать `docBuilder.CurrentParagraph.RemoveAllChildren()` перед записью. + +```csharp +// Option A – Append a new paragraph (keeps the original) +docBuilder.Writeln(rewrittenParagraph); + +// Option B – Replace the existing paragraph (uncomment to use) +// docBuilder.CurrentParagraph.RemoveAllChildren(); +// docBuilder.CurrentParagraph.AppendChild(new Run(docBuilder.Document, rewrittenParagraph)); +``` + +Оба подхода показаны, чтобы вы могли выбрать тот, который соответствует вашему рабочему процессу. + +--- + +## Шаг 7: Сохраните обновлённый документ + +Наконец, мы сохраняем изменения в новый файл. Aspose.Words автоматически выбирает формат на основе расширения файла. + +```csharp +string outputPath = Path.Combine(Environment.CurrentDirectory, "output.docx"); +sourceDocument.Save(outputPath); + +Console.WriteLine($"Document saved to {outputPath}"); +``` + +Откройте `output.docx` в Word, и вы увидите, что абзац теперь написан в формальном тоне. + +--- + +## Полный рабочий пример + +Ниже представлен **complete, self‑contained program**. Скопируйте‑вставьте его в консольный проект, восстановите пакеты NuGet и запустите — никаких дополнительных настроек не требуется, кроме работающего локального LLM. + +```csharp +using Aspose.Words.AI; +using Aspose.Words; +using System; +using System.IO; +using System.Threading.Tasks; + +class Program +{ + static async Task Main() + { + // ------------------------------------------------- + // Step 1: Connect to the locally hosted LLM endpoint + // ------------------------------------------------- + var localLlm = new LocalLargeLanguageModel("http://localhost:11434/api/generate"); + + // ------------------------------------------------- + // Step 2: Load the source Word document + // ------------------------------------------------- + string inputPath = Path.Combine(Environment.CurrentDirectory, "input.docx"); + Document sourceDocument = new Document(inputPath); + + // ------------------------------------------------- + // Step 3: Retrieve the text of the current paragraph + // ------------------------------------------------- + DocumentBuilder docBuilder = new DocumentBuilder(sourceDocument); + string originalParagraph = docBuilder.CurrentParagraph?.GetText() ?? string.Empty; + + if (string.IsNullOrWhiteSpace(originalParagraph)) + { + Console.WriteLine("No paragraph found at the current cursor position."); + return; + } + + // ------------------------------------------------- + // Step 4: Ask the LLM to rewrite the paragraph in a formal tone + // ------------------------------------------------- + string prompt = $"Rewrite the following sentence in a more formal tone:\n{originalParagraph}"; + string rewrittenParagraph = await localLlm.PromptAsync(prompt); + + // ------------------------------------------------- + // Step 5: Insert the rewritten text back into the document + // ------------------------------------------------- + docBuilder.Writeln(rewrittenParagraph); + + // ------------------------------------------------- + // Step 6: Save the updated document + // ------------------------------------------------- + string outputPath = Path.Combine(Environment.CurrentDirectory, "output.docx"); + sourceDocument.Save(outputPath); + + Console.WriteLine("Original paragraph:"); + Console.WriteLine(originalParagraph); + Console.WriteLine("\nRewritten paragraph:"); + Console.WriteLine(rewrittenParagraph); + Console.WriteLine($"\nDocument saved to {outputPath}"); + } +} +``` + +### Что ожидать при запуске + +1. Консоль выводит оригинальный и переписанный абзацы. +2. `output.docx` появляется рядом с `input.docx`. +3. При открытии файла новый формальный абзац вставлен после оригинального (или заменён, если вы переключились на альтернативный код). + +--- + +## Обработка распространённых edge cases + +| Situation | Solution | +|-----------|----------| +| **Empty or whitespace‑only paragraph** | Проверьте `string.IsNullOrWhiteSpace` перед отправкой подсказки (см. Шаг 3). | +| **LLM returns an error or empty string** | Оберните `PromptAsync` в `try/catch` и вернитесь к оригинальному тексту в случае ошибки. | +| **Multiple paragraphs need rewriting** | Пройдите в цикле `sourceDocument.GetChildNodes(NodeType.Paragraph, true)` и примените ту же логику подсказки. | +| **Large documents cause latency** | Группируйте абзацы и отправляйте их одним запросом (подсказка до 4 KB за вызов). | +| **Non‑ASCII characters get garbled** | Убедитесь, что endpoint LLM использует UTF-8 (это делают большинство современных моделей). | + +--- + +## Следующие шаги и связанные темы + +- **Prompt large language model** с более подробными инструкциями (например, руководства по стилю, ограничения по длине). +- Используйте **call local llm** в веб‑API, чтобы предоставить автоматизацию документов как сервис. +- Исследуйте **load word document** в параллельных потоках для сценариев с высокой пропускной способностью. +- Скомбинируйте этот подход с **rewrite text automatically** для массовой генерации писем или стандартизации отчетов. + +Если хотите углубиться, ознакомьтесь с документацией Aspose по **document merging** и справочником API Ollama для настройки параметров сэмплинга. + +--- + +## Заключение + +Мы только что продемонстрировали, как **connect to local llm** из C#, **prompt large language model**, **load word document**, **call local llm** и **rewrite text automatically** — всё в одном готовом к запуску консольном приложении. Этот подход масштабируется: меняйте подсказку, перебирайте абзацы или открывайте логику через endpoint ASP.NET. Главный вывод — локальные AI‑модели могут быть тесно интегрированы с классическими библиотеками обработки документов, предоставляя мощную автоматизацию, не покидая надёжную on‑prem среду. + +Есть вопросы по потокам, + +{{< /blocks/products/pf/tutorial-page-section >}} +{{< /blocks/products/pf/main-container >}} +{{< /blocks/products/pf/main-wrap-class >}} +{{< blocks/products/products-backtop-button >}} \ No newline at end of file diff --git a/words/russian/net/programming-with-markdownsaveoptions/_index.md b/words/russian/net/programming-with-markdownsaveoptions/_index.md index 09cee05007..87871c1aae 100644 --- a/words/russian/net/programming-with-markdownsaveoptions/_index.md +++ b/words/russian/net/programming-with-markdownsaveoptions/_index.md @@ -41,6 +41,7 @@ | [Сохранить docx как markdown с Aspose.Words – Полное руководство C#](./save-docx-as-markdown-with-aspose-words-full-c-guide/) | Полное руководство по сохранению DOCX в Markdown с использованием Aspose.Words и C#. | | [Сохранить изображения Word – Конвертировать Word в Markdown с Aspose](./save-word-images-convert-word-to-markdown-with-aspose/) | Сохраните изображения из документов Word и конвертируйте их в Markdown с помощью Aspose.Words для .NET. | | [Конвертировать Word в Markdown на C# – Полное руководство с извлечением изображений](./convert-word-to-markdown-in-c-full-guide-with-image-extracti/) | Подробное руководство по конвертации документов Word в Markdown с извлечением изображений на C# с использованием Aspose.Words. | +| [Относительный путь к изображению Markdown – Конвертировать Word в Markdown](./markdown-image-relative-path-convert-word-to-markdown/) | Конвертируйте документы Word в Markdown, используя относительные пути к изображениям. | {{< /blocks/products/pf/tutorial-page-section >}} diff --git a/words/russian/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/_index.md b/words/russian/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/_index.md new file mode 100644 index 0000000000..a46af485eb --- /dev/null +++ b/words/russian/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/_index.md @@ -0,0 +1,207 @@ +--- +category: general +date: 2026-04-28 +description: Узнайте, как задать относительный путь к изображению в Markdown при конвертации + Word в Markdown, извлекать изображения из Word и создавать папку resources для экспортированных + изображений. +draft: false +keywords: +- markdown image relative path +- convert word to markdown +- extract images from word +- create resources folder +- export images from docx +language: ru +og_description: Установите относительный путь к изображению в markdown при конвертации + Word в markdown, извлеките изображения из Word и создайте папку resources для экспортированных + изображений. +og_title: Относительный путь к изображению в markdown – Конвертация Word в Markdown +tags: +- Aspose.Words +- C# +- Markdown +- Image Export +title: Относительный путь к изображению в markdown – Конвертация Word в Markdown +url: /ru/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/ +--- + +{{< blocks/products/pf/main-wrap-class >}} +{{< blocks/products/pf/main-container >}} +{{< blocks/products/pf/tutorial-page-section >}} + +# относительный путь к изображению markdown – Конвертация Word в Markdown + +Когда‑то вам нужен был **относительный путь к изображению markdown** при **конвертации Word в markdown**? Вы не одиноки. Большинство разработчиков сталкиваются с проблемой, когда сгенерированный Markdown ссылается на изображения в плоской папке, нарушая ожидаемую структуру относительных ссылок в статическом сайте или репозитории GitHub. + +В этом руководстве мы пройдем полный, сквозной процесс, который **извлекает изображения из Word**, **создаёт папку ресурсов**, и переписывает ссылки на изображения так, чтобы они использовали чистый *относительный путь к изображению markdown*. К концу вы получите готовый к публикации файл `.md` и аккуратно организованную директорию `Resources`, содержащую каждое изображение, извлечённое из исходного `.docx`. + +> **Что вы получите:** один C#‑программный файл (без внешних скриптов), чёткое объяснение *почему* каждый элемент важен, и несколько практических советов, которые можно скопировать‑вставить в свои проекты. + +--- + +## Предварительные требования + +Прежде чем погрузиться в код, убедитесь, что у вас есть: + +- **.NET 6.0** или новее (можно также целиться в .NET Framework 4.7+, но .NET 6 – оптимальный вариант для новых проектов). +- **Aspose.Words for .NET** (последний NuGet‑пакет на момент написания, версия 23.12). Установите его командой: + ```bash + dotnet add package Aspose.Words + ``` +- Word‑документ, действительно содержащий изображения — назовём его `WithImages.docx`. +- Папка, в которой вы хотите разместить выводимый markdown и изображения, например `C:\Projects\MarkdownExport`. + +Дополнительные библиотеки не требуются; всё остальное обрабатывается Aspose.Words. + +--- + +## Шаг 1: Загрузить исходный документ Word (отправная точка для convert word to markdown) + +```csharp +using System; +using System.IO; +using Aspose.Words; +using Aspose.Words.Saving; + +class Program +{ + static void Main() + { + // Adjust the path to point at your own .docx file. + string sourcePath = @"C:\Projects\MarkdownExport\WithImages.docx"; + + // Load the document – this is where Aspose.Words parses the Word file. + Document doc = new Document(sourcePath); + + // The rest of the workflow follows… + } +} +``` + +*Почему это важно:* загрузка документа даёт нам доступ к внутреннему дереву узлов, которое включает части изображений, которые позже нам понадобится **export images from docx**. Если загрузка не удалась, ни один из последующих шагов не выполнится, поэтому проверьте путь и права доступа к файлу. + +--- + +## Шаг 2: Настроить `MarkdownSaveOptions` с пользовательским обратным вызовом (сердце create resources folder) + +`ResourceSavingCallback` позволяет вмешаться каждый раз, когда Aspose.Words хочет записать файл изображения. Внутри обратного вызова мы **создадим подпапку Resources** и скорректируем ссылку, чтобы сгенерированный markdown использовал *относительный путь к изображению markdown*. + +```csharp +// Inside Main(), after loading the document: +string outputFolder = @"C:\Projects\MarkdownExport"; +string resourcesFolder = Path.Combine(outputFolder, "Resources"); + +// Make sure the folder exists before we start saving anything. +Directory.CreateDirectory(resourcesFolder); + +// Set up the Markdown save options. +MarkdownSaveOptions mdOptions = new MarkdownSaveOptions +{ + // Hook that runs for every image resource. + ResourceSavingCallback = new MyMarkdownResourceCallback(resourcesFolder) +}; + +// Save the document as Markdown. +string markdownPath = Path.Combine(outputFolder, "Doc.md"); +doc.Save(markdownPath, mdOptions); +``` + +Обратите внимание, что мы передали `resourcesFolder` в конструктор обратного вызова — это делает путь к папке гибким и избавляет от жёстко прописанных строк в коде. + +--- + +## Шаг 3: Реализовать обратный вызов, который **создаёт папку ресурсов** и переписывает путь + +```csharp +/// +/// Handles image extraction and path rewriting for markdown export. +/// +class MyMarkdownResourceCallback : IResourceSavingCallback +{ + private readonly string _resourcesFolder; + + public MyMarkdownResourceCallback(string resourcesFolder) + { + _resourcesFolder = resourcesFolder; + } + + public void ResourceSaving(ResourceSavingArgs args) + { + // 1️⃣ Build the full file system path where the image will be stored. + string targetPath = Path.Combine(_resourcesFolder, args.ResourceFileName); + + // 2️⃣ Ensure the directory exists (in case Aspose creates sub‑folders). + Directory.CreateDirectory(Path.GetDirectoryName(targetPath)); + + // 3️⃣ Write the image stream to disk. + using (FileStream fileStream = File.Create(targetPath)) + { + args.Stream.CopyTo(fileStream); + } + + // 4️⃣ Update the markdown reference to use a relative path. + // This is the crucial line that gives us the markdown image relative path. + args.ResourceFileName = Path.Combine("Resources", args.ResourceFileName); + } +} +``` + +*Почему это работает:* `args.Stream` содержит необработанные байты изображения. Копируя их в файл внутри нашей папки `Resources`, мы **export images from docx** безопасно. Затем мы заменяем `args.ResourceFileName` на относительный URL (`Resources/image.png`). Когда Aspose.Words позже запишет markdown, он вставит именно эту строку, давая нам желаемый *относительный путь к изображению markdown*. + +--- + +## Шаг 4: Проверить сгенерированный Markdown (как выглядит окончательный вывод) + +Откройте `Doc.md` в любом текстовом редакторе. Вы должны увидеть нечто подобное: + +```markdown +# Sample Heading + +Here is an inline picture: + +![Image 0](Resources/Image_0.png) + +And a picture inside a table: + +![Image 1](Resources/Image_1.jpg) +``` + +Главное, что каждая ссылка на изображение указывает на `Resources/...` — это **относительный путь к изображению markdown**, который мы искали. + +![пример относительного пути к изображению markdown](example.png "пример относительного пути к изображению markdown") + +*Подсказка:* если открыть markdown в просмотрщике, поддерживающем относительные ссылки (предпросмотр VS Code, GitHub или генератор статических сайтов), картинки отобразятся корректно без дополнительной настройки. + +--- + +## Шаг 5: Распространённые подводные камни и профессиональные советы + +| Проблема | Почему происходит | Как исправить | +|----------|-------------------|---------------| +| Изображения оказываются в корневой папке вместо `Resources` | Обратный вызов не был привязан или `args.ResourceFileName` не был переопределён. | Убедитесь, что `ResourceSavingCallback` установлен **до** вызова `doc.Save`. | +| Имена файлов содержат недопустимые символы | Word иногда даёт изображениям имена с пробелами или юникод‑символами. | Используйте `Path.GetInvalidFileNameChars()` для очистки `args.ResourceFileName` внутри обратного вызова. | +| Большие документы обрабатываются долго | Каждое изображение записывается синхронно. | Перейдите на асинхронный ввод‑вывод (`await args.Stream.CopyToAsync(fileStream)`) если вы на .NET 6+ и нужна производительность. | +| Относительные пути ломаются при перемещении markdown | Путь относителен расположению markdown‑файла. | Держите `Doc.md` и папку `Resources` вместе, либо измените обратный вызов, чтобы использовать другой относительный префикс (например, `../assets`). | + +--- + +## Шаг 6: Расширение решения (что делать, если нужен больший контроль?) + +- **Несколько форматов вывода:** замените `MarkdownSaveOptions` на `HtmlSaveOptions` или `PdfSaveOptions`, оставив тот же обратный вызов — Aspose.Words будет вызывать его для каждого изображения независимо от формата. +- **Пользовательское именование изображений:** если хотите переименовать изображения (например, `figure-01.png`), измените `args.ResourceFileName` внутри обратного вызова перед записью файла. +- **Встраивание изображений как Base64:** задайте `args.ResourceFileName` как data‑URI (`data:image/png;base64,...`) и пропустите запись файла. Это удобно для экспорта в один markdown‑файл. + +--- + +## Заключение + +Теперь у вас есть полностью рабочая C#‑программа, которая **конвертирует Word в markdown**, **извлекает изображения из word**, **создаёт папку ресурсов** и гарантирует чистый **относительный путь к изображению markdown** для каждой картинки. Код автономный, работает с последней версией Aspose.Words и может быть добавлен в любой .NET‑проект с минимальными усилиями. + +Что дальше? Попробуйте передать сгенерированный markdown в генератор статических сайтов, такой как Hugo или Jekyll, либо поэкспериментируйте с обратным вызовом, чтобы встраивать изображения напрямую как Base64‑строки. Если столкнётесь с особенными случаями — например, SVG‑изображениями или необычно большими файлами — обратитесь к таблице «Распространённые подводные камни»; небольшая правка обычно решает проблему. + +Счастливого кодинга, и пусть ваш markdown всегда указывает в правильную папку! + +{{< /blocks/products/pf/tutorial-page-section >}} +{{< /blocks/products/pf/main-container >}} +{{< /blocks/products/pf/main-wrap-class >}} +{{< blocks/products/products-backtop-button >}} \ No newline at end of file diff --git a/words/russian/net/programming-with-officemath/_index.md b/words/russian/net/programming-with-officemath/_index.md index 6e7781a74d..0f850a629e 100644 --- a/words/russian/net/programming-with-officemath/_index.md +++ b/words/russian/net/programming-with-officemath/_index.md @@ -23,6 +23,7 @@ | [Математические уравнения](./math-equations/) | Узнайте, как настраивать математические уравнения в документах Word с помощью Aspose.Words для .NET. Пошаговое руководство с примерами, часто задаваемыми вопросами и многим другим. | | [Сохранить docx как txt – экспортировать Word Math в LaTeX с C#](./save-docx-as-txt-export-word-math-to-latex-with-c/) | Узнайте, как экспортировать математические уравнения из docx в LaTeX, сохранив документ как txt, используя Aspose.Words для .NET и C#. | | [Сохранить документ как Txt – Экспортировать математические формулы Word в LaTeX на C#](./save-document-as-txt-export-word-math-to-latex-in-c/) | Узнайте, как сохранять документ в формате txt и экспортировать формулы Word в LaTeX с помощью C#. | +| [Конвертировать docx в txt – экспортировать уравнения Word в LaTeX на C#](./convert-docx-to-txt-export-word-equations-to-latex-in-c/) | Узнайте, как конвертировать DOCX в TXT и экспортировать уравнения Word в LaTeX с помощью C# и Aspose.Words. | {{< /blocks/products/pf/tutorial-page-section >}} diff --git a/words/russian/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/_index.md b/words/russian/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/_index.md new file mode 100644 index 0000000000..4ab60c7a85 --- /dev/null +++ b/words/russian/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/_index.md @@ -0,0 +1,208 @@ +--- +category: general +date: 2026-04-28 +description: Конвертировать DOCX в TXT и экспортировать уравнения Word в LaTeX с помощью + Aspose.Words. Узнайте, как сохранить документ Word в формате TXT и работать с математическими + объектами за несколько шагов. +draft: false +keywords: +- convert docx to txt +- convert word equations to latex +- convert word to plain text +- save word as txt +- export equations as latex +language: ru +og_description: Преобразуйте DOCX в TXT и экспортируйте уравнения Word в LaTeX с помощью + простого фрагмента C#. Полное руководство, код и советы. +og_title: Конвертировать DOCX в TXT – Экспортировать уравнения Word в LaTeX +tags: +- C# +- Aspose.Words +- Document Conversion +title: Преобразовать DOCX в TXT – экспортировать уравнения Word в LaTeX на C# +url: /ru/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/ +--- + +{{< blocks/products/pf/main-wrap-class >}} +{{< blocks/products/pf/main-container >}} +{{< blocks/products/pf/tutorial-page-section >}} + +# Преобразование DOCX в TXT – Экспорт уравнений Word в LaTeX + +Когда‑нибудь вам нужно было **convert docx to txt**, но вы боялись, что математические формулы в вашем файле Word превратятся в нечитаемый набор символов? Вы не одиноки. Во многих инженерных или академических проектах исходный документ находится в .docx, однако последующие инструменты понимают только обычный текст или LaTeX. Хорошая новость? С помощью нескольких строк кода на C# и Aspose.Words вы можете **convert docx to txt** *и* сохранить каждое уравнение в виде чистого кода LaTeX. + +В этом руководстве мы пройдем весь процесс: загрузим .docx, настроим параметры сохранения так, чтобы объекты Office Math преобразовывались в LaTeX, и, наконец, запишем результат в файл .txt. К концу вы узнаете, как **save word as txt**, **convert word to plain text**, и **export equations as latex** без необходимости искать информацию в документации API. + +## Что вы узнаете + +- Точные вызовы API, необходимые для **convert docx to txt** с сохранением уравнений. +- Почему выбор `OfficeMathExportMode.LaTeX` является рекомендуемым способом **convert word equations to latex**. +- Как обрабатывать распространённые граничные случаи, такие как отсутствие шрифтов или неподдерживаемые функции уравнений. +- Полный, готовый к запуску C#‑программный пример, который можно добавить в любой проект .NET. + +### Требования + +- .NET 6.0 или новее (код также работает на .NET Framework 4.7+). +- Лицензия на Aspose.Words for .NET (бесплатная пробная версия подходит для оценки). +- Документ Word (`input.docx`), содержащий как минимум один объект Office Math. + +Если всё это у вас есть, приступим. + +## Шаг 1: Установить Aspose.Words + +Прежде чем любой код выполнится, вам нужна библиотека. Откройте терминал в папке проекта и выполните: + +```bash +dotnet add package Aspose.Words +``` + +Это загрузит последнюю стабильную версию (по состоянию на 2026‑04‑28 v24.12). Дополнительные DLL не требуются. + +## Шаг 2: Загрузить исходный документ + +Первое, что мы делаем, — читаем файл .docx в объект `Document`. Этот объект предоставляет полный доступ к структуре файла, включая текстовые фрагменты, изображения и математические объекты. + +```csharp +using Aspose.Words; +using Aspose.Words.Saving; + +class Program +{ + static void Main() + { + // Step 2: Load the source document + Document doc = new Document(@"YOUR_DIRECTORY\input.docx"); +``` + +> **Почему это важно:** Загрузка документа создаёт представление в памяти, поэтому позже мы можем настроить, как каждый элемент будет записан. Если файл не найден, Aspose бросает `FileNotFoundException`, который вы, возможно, захотите отловить в продакшн‑коде. + +## Шаг 3: Настроить параметры сохранения TXT для LaTeX‑математики + +По умолчанию `Document.Save` записывает обычный текст и **отбрасывает** любые Office Math. Чтобы сохранить эти уравнения, мы устанавливаем `OfficeMathExportMode` в `LaTeX`. Это указывает экспортеру преобразовать каждое уравнение в его эквивалент LaTeX. + +```csharp + // Step 3: Configure TXT save options to export Office Math as LaTeX + TxtSaveOptions txtOptions = new TxtSaveOptions + { + OfficeMathExportMode = OfficeMathExportMode.LaTeX, + // Optional: preserve line breaks as they appear in the original Word file + PreserveTableLayout = true + }; +``` + +> **Совет:** Если вам нужны только необработанные Unicode‑символы уравнения (например, для быстрого предварительного просмотра), вы можете использовать `OfficeMathExportMode.Text`. Но для большинства научных конвейеров `LaTeX` является золотым стандартом, поскольку он универсально понимается процессорами LaTeX. + +## Шаг 4: Сохранить документ как обычный текст + +Теперь мы записываем преобразованное содержимое в файл `.txt`. Файл будет содержать обычные абзацы, маркированные списки и — благодаря предыдущему шагу — фрагменты LaTeX для каждого уравнения. + +```csharp + // Step 4: Save the document as plain‑text using the configured options + doc.Save(@"YOUR_DIRECTORY\Math.txt", txtOptions); + } +} +``` + +Когда вы откроете `Math.txt`, вы увидите примерно следующее: + +``` +In this report we derive the quadratic formula: +\[ +x = \frac{-b \pm \sqrt{b^{2} - 4ac}}{2a} +\] + +The end. +``` + +Обратите внимание на разделители `\[` … `\]`. Это LaTeX‑математические блоки, сгенерированные автоматически. + +## Шаг 5: Проверить результат (необязательно, но рекомендуется) + +Легко пропустить тонкую проблему преобразования, особенно когда уравнения содержат пользовательские символы. Быстрая проверка — передать сгенерированный `.txt` в компилятор LaTeX (например, `pdflatex`) и убедиться, что он компилируется без ошибок. + +```bash +pdflatex -interaction=nonstopmode Math.txt +``` + +Если компиляция прошла успешно, вы фактически **convert word equations to latex** и **convert docx to txt** за один раз. Если возникли ошибки, ищите сообщения о неопределённых командах — они обычно указывают на функцию уравнения, которую Aspose.Words не может преобразовать (например, некоторые обозначения матриц). В таких случаях можно вернуться к `OfficeMathExportMode.MathML` и пост‑обработать MathML в LaTeX с помощью другого инструмента. + +## Распространённые подводные камни и как их избежать + +| Issue | Why it Happens | Fix | +|-------|----------------|-----| +| Missing fonts | Aspose.Words нуждается в шрифте для корректного отображения символов. | Установите недостающий шрифт на машину или внедрите его в .docx. | +| Complex equations not exported | Некоторые новые функции Office Math ещё не сопоставлены с LaTeX. | Используйте `OfficeMathExportMode.MathML`, затем преобразуйте с помощью библиотеки MathML‑to‑LaTeX. | +| Extra blank lines | Сохранитель plain‑text сохраняет разрывы абзацев, что может добавить лишние пробелы. | Установите `txtOptions.AddBidiMarks = false` или пост‑обработайте файл простым скриптом. | + +## Полный рабочий пример (готовый к копированию и вставке) + +Ниже представлен весь код программы, готовый к компиляции. Замените `YOUR_DIRECTORY` на папку, где находится ваш `input.docx`. + +```csharp +using System; +using Aspose.Words; +using Aspose.Words.Saving; + +namespace DocxToTxtWithLatex +{ + class Program + { + static void Main() + { + try + { + // Load the source document + Document doc = new Document(@"C:\Docs\input.docx"); + + // Configure save options: export equations as LaTeX + TxtSaveOptions txtOptions = new TxtSaveOptions + { + OfficeMathExportMode = OfficeMathExportMode.LaTeX, + PreserveTableLayout = true, + AddBidiMarks = false + }; + + // Save as plain‑text + string outputPath = @"C:\Docs\Math.txt"; + doc.Save(outputPath, txtOptions); + + Console.WriteLine($"Successfully converted DOCX to TXT. Output at: {outputPath}"); + } + catch (Exception ex) + { + Console.Error.WriteLine($"Conversion failed: {ex.Message}"); + } + } + } +} +``` + +Запуск этой программы **save word as txt**, преобразуя каждый блок Office Math в LaTeX, предоставляя вам чистый, индексируемый файл обычного текста. + +## Следующие шаги и связанные темы + +- **Batch conversion:** Оберните вышеописанную логику в цикл `foreach`, чтобы обработать всю папку файлов .docx. +- **Combine with PDF generation:** После получения фрагментов LaTeX передайте их в PDF‑конвейер (например, `PdfSharp` + `MiKTeX`), чтобы создать PDF‑отчёты. +- **Export equations as latex** for other formats: Aspose.Words также поддерживает `SaveFormat.Markdown`, который может автоматически встраивать LaTeX. +- **Performance tuning:** Для больших документов переиспользуйте один экземпляр `TxtSaveOptions` и отключите ненужные функции, такие как `AddBidiMarks`. + +### Пример изображения (необязательно) + +Если вы предпочитаете визуальный пример, вот скриншот выходного файла в Notepad++. + +![вывод convert docx to txt с отображением уравнений LaTeX](convert-docx-to-txt-output.png) + +*(Alt text: “convert docx to txt output showing LaTeX equations” – удовлетворяет требованию основного ключевого слова.)* + +## Заключение + +Мы только что продемонстрировали надёжный способ **convert docx to txt**, сохраняющий каждое уравнение в виде чистого LaTeX. Ключом является флаг `OfficeMathExportMode.LaTeX`, который преобразует проприетарный формат математики Word в то, что понимает любой движок LaTeX. С полным примером кода выше вы можете **save word as txt**, **convert word to plain text** и **export equations as latex** в одном самостоятельном запуске. + +Не стесняйтесь экспериментировать — замените расширение вывода на `.md` для Markdown или интегрируйте фрагмент в более крупный конвейер обработки документов. Если столкнётесь с какими‑либо особенностями, оставьте комментарий ниже; я с радостью помогу разобраться. + +Удачной разработки! + +{{< /blocks/products/pf/tutorial-page-section >}} +{{< /blocks/products/pf/main-container >}} +{{< /blocks/products/pf/main-wrap-class >}} +{{< blocks/products/products-backtop-button >}} \ No newline at end of file diff --git a/words/spanish/net/ai-powered-document-processing/_index.md b/words/spanish/net/ai-powered-document-processing/_index.md index b59872e04f..1e66152d2b 100644 --- a/words/spanish/net/ai-powered-document-processing/_index.md +++ b/words/spanish/net/ai-powered-document-processing/_index.md @@ -42,6 +42,7 @@ Por último, no olvides visitar nuestra [Trabajar con opciones de resumen](./wor | [Trabajar con el modelo de inteligencia artificial de Google](./working-with-google-ai-model/) Mejore su procesamiento de documentos con Aspose.Words para .NET y Google AI para crear resúmenes concisos sin esfuerzo. | | [Trabajar con el modelo de IA abierta](./working-with-open-ai-model/) | Desbloquee la eficiencia de los resúmenes de documentos con Aspose.Words para .NET y los potentes modelos de OpenAI. Explore esta guía completa ahora. | | [Trabajar con opciones de resumen](./working-with-summarize-options/) | Aprenda a resumir eficazmente documentos de Word usando Aspose.Words para .NET con nuestra guía paso a paso sobre la integración de modelos de IA para obtener información rápida. | +| [Conectar a LLM local en C# – Guía completa de programación](./connect-to-local-llm-in-c-complete-programming-guide/) | Aprenda a conectar y usar un modelo de lenguaje local en C# con Aspose.Words, paso a paso. | {{< /blocks/products/pf/tutorial-page-section >}} diff --git a/words/spanish/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/_index.md b/words/spanish/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/_index.md new file mode 100644 index 0000000000..13cd1dc99d --- /dev/null +++ b/words/spanish/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/_index.md @@ -0,0 +1,284 @@ +--- +category: general +date: 2026-04-28 +description: Conectar a un LLM local desde C# y solicitar al modelo de lenguaje grande + que cargue un documento Word, llamar al LLM local y reescribir el texto automáticamente. + Código paso a paso incluido. +draft: false +keywords: +- connect to local llm +- prompt large language model +- load word document +- call local llm +- rewrite text automatically +language: es +og_description: Conéctate a un LLM local desde C# y descubre cómo interactuar con + un modelo de lenguaje grande, cargar un documento Word, invocar el LLM local y reescribir + el texto automáticamente en minutos. +og_title: Conectar a un LLM local en C# – Guía completa de programación +tags: +- Aspose.Words +- C# +- LLM +- AI Automation +title: Conectar a LLM local en C# – Guía completa de programación +url: /es/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/ +--- + +{{< blocks/products/pf/main-wrap-class >}} +{{< blocks/products/pf/main-container >}} +{{< blocks/products/pf/tutorial-page-section >}} + +# Conectar a un LLM local en C# – Guía completa de programación + +¿Alguna vez necesitaste **conectar a un LLM local** desde una aplicación .NET y te preguntaste cómo hacerlo hablar con un archivo Word? No estás solo. En esta guía recorreremos todo el proceso: conectar a un LLM local, **prompt large language model**, cargar un documento Word, **call local llm**, y finalmente **rewrite text automatically**. Al final tendrás un ejemplo ejecutable que transforma cualquier párrafo a un tono formal sin claves de API externas. + +## Qué cubre este tutorial + +Comenzaremos instalando los paquetes NuGet necesarios, luego pondremos en marcha un sencillo endpoint LLM local (piensa en Ollama en el puerto 11434). Después cargaremos un archivo `.docx` usando Aspose.Words, enviaremos un párrafo al LLM, recibiremos una versión reescrita y la volveremos a escribir en el mismo documento. También verás cómo manejar problemas comunes—párrafos nulos, eliminación asíncrona y peculiaridades de codificación—para que el código funcione en producción, no solo en una demo. + +### Prerrequisitos + +- .NET 6.0 SDK o posterior (también puedes usar .NET 8 si lo prefieres) +- Visual Studio 2022 o VS Code con la extensión C# +- **Aspose.Words for .NET** (la prueba gratuita funciona bien) +- Un LLM alojado localmente que siga el contrato `/api/generate` (p. ej., Ollama, LMStudio) +- Familiaridad básica con async/await en C# + +> **Pro tip:** Si aún no has instalado Ollama, ejecuta `ollama serve` y descarga un modelo con `ollama pull llama3`. El endpoint HTTP predeterminado será `http://localhost:11434/api/generate`. + +--- + +## Paso 1: Instalar los paquetes requeridos + +Primero, agrega los paquetes NuGet Aspose.Words y Aspose.Words.AI a tu proyecto. + +```bash +dotnet add package Aspose.Words +dotnet add package Aspose.Words.AI +``` + +Estas bibliotecas nos dan la capacidad de **load word document** y un contenedor ligero para **call local llm** sin crear manualmente solicitudes HTTP. + +--- + +## Paso 2: Conectar al endpoint LLM local + +Conectar a un modelo alojado localmente es tan simple como instanciar `LocalLargeLanguageModel`. El constructor espera la URL completa del endpoint de generación. + +```csharp +using Aspose.Words.AI; +using Aspose.Words; +using System.Threading.Tasks; + +// Create a client that talks to the LLM running on localhost +var localLlm = new LocalLargeLanguageModel("http://localhost:11434/api/generate"); +``` + +¿Por qué envolvemos el endpoint en una clase? `LocalLargeLanguageModel` maneja la serialización JSON, reintentos y respuestas en streaming por ti—para que puedas centrarte en la lógica del prompt en lugar de lidiar con `HttpClient`. + +--- + +## Paso 3: Cargar el documento Word de origen + +A continuación, traemos el documento a memoria. Aspose.Words soporta prácticamente todos los formatos de Word, así que `Document` analizará `input.docx` sin necesidad de tener Office instalado. + +```csharp +// Path to the source file – adjust as needed +string inputPath = Path.Combine(Environment.CurrentDirectory, "input.docx"); + +// Load the document; throws if the file is missing or corrupted +Document sourceDocument = new Document(inputPath); +``` + +Si necesitas trabajar con un stream (p. ej., un archivo subido vía ASP.NET), simplemente reemplaza la ruta del archivo con un `MemoryStream` y pásalo al constructor de `Document`. + +--- + +## Paso 4: Extraer el texto del párrafo actual + +Usaremos `DocumentBuilder` para navegar por el documento. En este ejemplo reescribimos **el primer párrafo**, pero puedes iterar sobre `sourceDocument.GetChildNodes(NodeType.Paragraph, true)` para procesar muchos. + +```csharp +// Builder gives us a cursor inside the document +DocumentBuilder docBuilder = new DocumentBuilder(sourceDocument); + +// Grab the text of the paragraph where the builder is currently positioned +string originalParagraph = docBuilder.CurrentParagraph?.GetText() ?? string.Empty; + +// Safety check – avoid sending empty strings to the LLM +if (string.IsNullOrWhiteSpace(originalParagraph)) +{ + Console.WriteLine("No paragraph found at the current cursor position."); + return; +} +``` + +El operador `?.` evita una `NullReferenceException` si el documento resulta estar vacío. Este es uno de esos **edge cases** que hacen tropezar a los principiantes. + +--- + +## Paso 5: Prompt the LLM to Rewrite the Paragraph + +Ahora realmente **prompt large language model**. El prompt está en inglés sencillo; el contenedor lo enviará como JSON al endpoint local. + +```csharp +// Build a friendly instruction for the model +string prompt = $"Rewrite the following sentence in a more formal tone:\n{originalParagraph}"; + +// Await the model's response – this is an async call +string rewrittenParagraph = await localLlm.PromptAsync(prompt); +``` + +¿Por qué formular la solicitud de esta manera? Los LLM responden mejor a instrucciones claras y de una sola tarea. Añadir una nueva línea después de los dos puntos separa la instrucción del contenido, reduciendo la probabilidad de que el modelo repita el prompt. + +**Salida esperada** – Si `originalParagraph` era `"Hey, what's up?"`, el LLM podría devolver: + +> “Good day, how may I assist you?” + +Puedes verificar el resultado imprimiéndolo: + +```csharp +Console.WriteLine("Original: " + originalParagraph); +Console.WriteLine("Rewritten: " + rewrittenParagraph); +``` + +--- + +## Paso 6: Insertar el texto reescrito de nuevo en el documento + +Con el nuevo texto en mano, reemplazamos el párrafo antiguo. `DocumentBuilder.Writeln` escribe una nueva línea y avanza el cursor, lo que es perfecto para añadir contenido. Si necesitas *reemplazar* exactamente el mismo párrafo, puedes usar `docBuilder.CurrentParagraph.RemoveAllChildren()` antes de escribir. + +```csharp +// Option A – Append a new paragraph (keeps the original) +docBuilder.Writeln(rewrittenParagraph); + +// Option B – Replace the existing paragraph (uncomment to use) +// docBuilder.CurrentParagraph.RemoveAllChildren(); +// docBuilder.CurrentParagraph.AppendChild(new Run(docBuilder.Document, rewrittenParagraph)); +``` + +Se muestran ambos enfoques para que elijas el que mejor se adapte a tu flujo de trabajo. + +--- + +## Paso 7: Guardar el documento actualizado + +Finalmente, persistimos los cambios en un archivo nuevo. Aspose.Words elige automáticamente el formato según la extensión del archivo. + +```csharp +string outputPath = Path.Combine(Environment.CurrentDirectory, "output.docx"); +sourceDocument.Save(outputPath); + +Console.WriteLine($"Document saved to {outputPath}"); +``` + +Abre `output.docx` en Word y verás que el párrafo ahora se lee en un tono formal. + +--- + +## Ejemplo completo y funcional + +A continuación tienes el **programa completo y autocontenido**. Copia‑pega en un proyecto de consola, restaura los paquetes NuGet y ejecútalo—no se requiere configuración adicional más allá de un LLM local en funcionamiento. + +```csharp +using Aspose.Words.AI; +using Aspose.Words; +using System; +using System.IO; +using System.Threading.Tasks; + +class Program +{ + static async Task Main() + { + // ------------------------------------------------- + // Step 1: Connect to the locally hosted LLM endpoint + // ------------------------------------------------- + var localLlm = new LocalLargeLanguageModel("http://localhost:11434/api/generate"); + + // ------------------------------------------------- + // Step 2: Load the source Word document + // ------------------------------------------------- + string inputPath = Path.Combine(Environment.CurrentDirectory, "input.docx"); + Document sourceDocument = new Document(inputPath); + + // ------------------------------------------------- + // Step 3: Retrieve the text of the current paragraph + // ------------------------------------------------- + DocumentBuilder docBuilder = new DocumentBuilder(sourceDocument); + string originalParagraph = docBuilder.CurrentParagraph?.GetText() ?? string.Empty; + + if (string.IsNullOrWhiteSpace(originalParagraph)) + { + Console.WriteLine("No paragraph found at the current cursor position."); + return; + } + + // ------------------------------------------------- + // Step 4: Ask the LLM to rewrite the paragraph in a formal tone + // ------------------------------------------------- + string prompt = $"Rewrite the following sentence in a more formal tone:\n{originalParagraph}"; + string rewrittenParagraph = await localLlm.PromptAsync(prompt); + + // ------------------------------------------------- + // Step 5: Insert the rewritten text back into the document + // ------------------------------------------------- + docBuilder.Writeln(rewrittenParagraph); + + // ------------------------------------------------- + // Step 6: Save the updated document + // ------------------------------------------------- + string outputPath = Path.Combine(Environment.CurrentDirectory, "output.docx"); + sourceDocument.Save(outputPath); + + Console.WriteLine("Original paragraph:"); + Console.WriteLine(originalParagraph); + Console.WriteLine("\nRewritten paragraph:"); + Console.WriteLine(rewrittenParagraph); + Console.WriteLine($"\nDocument saved to {outputPath}"); + } +} +``` + +### Qué esperar al ejecutarlo + +1. La consola imprime los párrafos original y reescrito. +2. `output.docx` aparece junto a `input.docx`. +3. Al abrir el archivo se muestra el nuevo párrafo formal insertado después del original (o reemplazado, si usaste el código alternativo). + +--- + +## Manejo de casos límite comunes + +| Situación | Solución | +|-----------|----------| +| **Párrafo vacío o solo con espacios** | Verifica `string.IsNullOrWhiteSpace` antes de hacer el prompt (ver Paso 3). | +| **LLM devuelve un error o cadena vacía** | Envuelve `PromptAsync` en un `try/catch` y recurre al texto original. | +| **Varios párrafos necesitan reescritura** | Recorre `sourceDocument.GetChildNodes(NodeType.Paragraph, true)` y aplica la misma lógica de prompt. | +| **Documentos grandes generan latencia** | Agrupa párrafos y envíalos en una sola solicitud (prompt de hasta 4 KB por llamada). | +| **Caracteres no ASCII se corrompen** | Asegúrate de que el endpoint LLM use UTF-8 (la mayoría de los modelos modernos lo hacen). | + +--- + +## Próximos pasos y temas relacionados + +- **Prompt large language model** con instrucciones más ricas (p. ej., guías de estilo, límites de longitud). +- Usa **call local llm** en una API web para exponer la automatización de documentos como servicio. +- Explora **load word document** en streams paralelos para escenarios de alto rendimiento. +- Combina este enfoque con **rewrite text automatically** para generación masiva de correos electrónicos o estandarización de informes. + +Si deseas profundizar, revisa la documentación de Aspose sobre **document merging** y la referencia de la API de Ollama para parámetros de muestreo personalizados. + +--- + +## Conclusión + +Acabamos de mostrarte cómo **connect to local llm** desde C#, **prompt large language model**, **load word document**, **call local llm**, y **rewrite text automatically**, todo en una única aplicación de consola ejecutable. El patrón escala: cambia el prompt, itera sobre párrafos o expón la lógica mediante un endpoint ASP.NET. La lección clave es que los modelos de IA locales pueden integrarse estrechamente con bibliotecas clásicas de procesamiento de documentos, brindándote una automatización poderosa sin salir de tu entorno on‑prem confiable. + +¿Tienes preguntas sobre threading? + +{{< /blocks/products/pf/tutorial-page-section >}} +{{< /blocks/products/pf/main-container >}} +{{< /blocks/products/pf/main-wrap-class >}} +{{< blocks/products/products-backtop-button >}} \ No newline at end of file diff --git a/words/spanish/net/programming-with-markdownsaveoptions/_index.md b/words/spanish/net/programming-with-markdownsaveoptions/_index.md index 22923377ed..f8555a64db 100644 --- a/words/spanish/net/programming-with-markdownsaveoptions/_index.md +++ b/words/spanish/net/programming-with-markdownsaveoptions/_index.md @@ -42,6 +42,7 @@ Procesamiento de textos con MarkdownSaveOptions es un recurso detallado que te g ### [Guardar docx como markdown con Aspose.Words – Guía completa en C#](./save-docx-as-markdown-with-aspose-words-full-c-guide/) Aprenda a guardar archivos DOCX como Markdown usando Aspose.Words con una guía completa en C#. ### [Guardar imágenes de Word – Convertir Word a Markdown con Aspose](./save-word-images-convert-word-to-markdown-with-aspose/) Aprenda a extraer y guardar imágenes de documentos Word al convertirlos a Markdown usando Aspose.Words. ### [Convertir Word a Markdown en C# – Guía completa con extracción de imágenes](./convert-word-to-markdown-in-c-full-guide-with-image-extracti/) Aprenda a convertir documentos Word a Markdown y extraer imágenes usando Aspose.Words para .NET. +### [Ruta relativa de imagen markdown – Convertir Word a Markdown](./markdown-image-relative-path-convert-word-to-markdown/) Aprenda a usar rutas relativas para imágenes al convertir documentos Word a Markdown con Aspose.Words para .NET. {{< /blocks/products/pf/tutorial-page-section >}} diff --git a/words/spanish/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/_index.md b/words/spanish/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/_index.md new file mode 100644 index 0000000000..eb2f86c8f2 --- /dev/null +++ b/words/spanish/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/_index.md @@ -0,0 +1,205 @@ +--- +category: general +date: 2026-04-28 +description: Aprende cómo establecer una ruta relativa de imagen en markdown al convertir + Word a markdown, extraer imágenes de Word y crear una carpeta de recursos para las + imágenes exportadas. +draft: false +keywords: +- markdown image relative path +- convert word to markdown +- extract images from word +- create resources folder +- export images from docx +language: es +og_description: Establece una ruta relativa de imagen en markdown mientras conviertes + Word a markdown, extraes imágenes de Word y creas una carpeta de recursos para las + imágenes exportadas. +og_title: ruta relativa de imagen markdown – Convertir Word a Markdown +tags: +- Aspose.Words +- C# +- Markdown +- Image Export +title: ruta relativa de imagen markdown – Convertir Word a Markdown +url: /es/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/ +--- + +{{< blocks/products/pf/main-wrap-class >}} +{{< blocks/products/pf/main-container >}} +{{< blocks/products/pf/tutorial-page-section >}} + +# markdown image relative path – Convertir Word a Markdown + +¿Alguna vez necesitaste una **markdown image relative path** mientras **convertías Word a markdown**? No estás solo. La mayoría de los desarrolladores se topan con un problema cuando el Markdown generado apunta a imágenes en una carpeta plana, rompiendo la estructura de enlaces relativos que esperas en un sitio estático o en un repositorio de GitHub. + +En este tutorial recorreremos una solución completa, de extremo a extremo, que **extrae imágenes de Word**, **crea una carpeta de recursos**, y reescribe las referencias de imágenes para que usen una *markdown image relative path* limpia. Al final tendrás un archivo `.md` listo para publicar y un directorio `Resources` ordenado que contiene cada imagen extraída del `.docx` original. + +> **Lo que obtendrás:** un único programa C# (sin scripts externos), una explicación clara de *por qué* cada pieza es importante, y un puñado de consejos prácticos que puedes copiar y pegar en tus propios proyectos. + +--- + +## Requisitos previos + +- **.NET 6.0** o posterior instalado (también puedes apuntar a .NET Framework 4.7+, pero .NET 6 es el punto óptimo para proyectos nuevos). +- **Aspose.Words for .NET** (el paquete NuGet más reciente al momento de escribir, versión 23.12). Instálalo con: + ```bash + dotnet add package Aspose.Words + ``` +- Un documento Word que realmente contenga imágenes — lo llamaremos `WithImages.docx`. +- Una carpeta donde deseas que vivan el markdown de salida y las imágenes, por ejemplo `C:\Projects\MarkdownExport`. + +No se requieren bibliotecas adicionales; todo lo demás lo maneja Aspose.Words. + +--- + +## Paso 1: Cargar el documento Word fuente (el punto de partida para convertir Word a markdown) + +```csharp +using System; +using System.IO; +using Aspose.Words; +using Aspose.Words.Saving; + +class Program +{ + static void Main() + { + // Adjust the path to point at your own .docx file. + string sourcePath = @"C:\Projects\MarkdownExport\WithImages.docx"; + + // Load the document – this is where Aspose.Words parses the Word file. + Document doc = new Document(sourcePath); + + // The rest of the workflow follows… + } +} +``` + +*Por qué es importante:* Cargar el documento nos da acceso al árbol interno de nodos, que incluye las partes de imagen que luego necesitaremos para **exportar imágenes del docx**. Si la carga falla, ninguno de los pasos posteriores se ejecutará, así que verifica dos veces la ruta y los permisos del archivo. + +--- + +## Paso 2: Configurar `MarkdownSaveOptions` con una devolución de llamada personalizada (el corazón de crear la carpeta de recursos) + +El `ResourceSavingCallback` nos permite intervenir cada vez que Aspose.Words quiere escribir un archivo de imagen. Dentro de la devolución de llamada **crearemos una sub‑carpeta Resources** y ajustaremos la referencia para que el markdown generado use una *markdown image relative path*. + +```csharp +// Inside Main(), after loading the document: +string outputFolder = @"C:\Projects\MarkdownExport"; +string resourcesFolder = Path.Combine(outputFolder, "Resources"); + +// Make sure the folder exists before we start saving anything. +Directory.CreateDirectory(resourcesFolder); + +// Set up the Markdown save options. +MarkdownSaveOptions mdOptions = new MarkdownSaveOptions +{ + // Hook that runs for every image resource. + ResourceSavingCallback = new MyMarkdownResourceCallback(resourcesFolder) +}; + +// Save the document as Markdown. +string markdownPath = Path.Combine(outputFolder, "Doc.md"); +doc.Save(markdownPath, mdOptions); +``` + +Observa que pasamos `resourcesFolder` al constructor de la devolución de llamada; esto mantiene la ruta de la carpeta flexible y evita codificar cadenas directamente en el código. + +--- + +## Paso 3: Implementar la devolución de llamada que **crea la carpeta de recursos** y reescribe la ruta + +```csharp +/// +/// Handles image extraction and path rewriting for markdown export. +/// +class MyMarkdownResourceCallback : IResourceSavingCallback +{ + private readonly string _resourcesFolder; + + public MyMarkdownResourceCallback(string resourcesFolder) + { + _resourcesFolder = resourcesFolder; + } + + public void ResourceSaving(ResourceSavingArgs args) + { + // 1️⃣ Build the full file system path where the image will be stored. + string targetPath = Path.Combine(_resourcesFolder, args.ResourceFileName); + + // 2️⃣ Ensure the directory exists (in case Aspose creates sub‑folders). + Directory.CreateDirectory(Path.GetDirectoryName(targetPath)); + + // 3️⃣ Write the image stream to disk. + using (FileStream fileStream = File.Create(targetPath)) + { + args.Stream.CopyTo(fileStream); + } + + // 4️⃣ Update the markdown reference to use a relative path. + // This is the crucial line that gives us the markdown image relative path. + args.ResourceFileName = Path.Combine("Resources", args.ResourceFileName); + } +} +``` + +*Por qué funciona:* `args.Stream` contiene los bytes crudos de la imagen. Al copiarlo a un archivo dentro de nuestra carpeta `Resources` **exportamos imágenes del docx** de forma segura. Luego reemplazamos `args.ResourceFileName` con una URL relativa (`Resources/image.png`). Cuando Aspose.Words escribe posteriormente el markdown, inserta exactamente esa cadena, dándonos la *markdown image relative path* deseada. + +--- + +## Paso 4: Verificar el Markdown generado (cómo se ve la salida final) + +Abre `Doc.md` en cualquier editor de texto. Deberías ver algo similar a: + +```markdown +# Sample Heading + +Here is an inline picture: + +![Image 0](Resources/Image_0.png) + +And a picture inside a table: + +![Image 1](Resources/Image_1.jpg) +``` + +La parte importante es que cada referencia de imagen apunte a `Resources/...` – esa es la **markdown image relative path** que buscábamos. + +![markdown image relative path example](example.png "markdown image relative path example") + +*Consejo:* Si abres el markdown en un visor que respete los enlaces relativos (vista previa de VS Code, GitHub o un generador de sitios estáticos), las imágenes se renderizarán correctamente sin configuración adicional. + +--- + +## Paso 5: Problemas comunes y consejos profesionales + +| Problema | Por qué ocurre | Cómo solucionarlo | +|----------|----------------|-------------------| +| Las imágenes terminan en la carpeta raíz en lugar de `Resources` | La devolución de llamada no se adjuntó o `args.ResourceFileName` no se sobrescribió. | Verifica que `ResourceSavingCallback` esté configurado **antes** de llamar a `doc.Save`. | +| Los nombres de archivo contienen caracteres ilegales | Word a veces nombra las imágenes con espacios o símbolos Unicode. | Utiliza `Path.GetInvalidFileNameChars()` para sanitizar `args.ResourceFileName` dentro de la devolución de llamada. | +| Los documentos grandes tardan mucho en procesarse | Cada imagen se escribe de forma sincrónica. | Cambia a I/O asíncrono (`await args.Stream.CopyToAsync(fileStream)`) si estás en .NET 6+ y necesitas rendimiento. | +| Las rutas relativas se rompen cuando el markdown se mueve | La ruta es relativa a la ubicación del archivo markdown. | Mantén `Doc.md` y la carpeta `Resources` juntos, o ajusta la devolución de llamada para usar un prefijo relativo diferente (p. ej., `../assets`). | + +--- + +## Paso 6: Extender la solución (¿qué pasa si necesitas más control?) + +- **Múltiples formatos de salida:** Reemplaza `MarkdownSaveOptions` con `HtmlSaveOptions` o `PdfSaveOptions` manteniendo la misma devolución de llamada—Aspose.Words la invocará para cada imagen sin importar el formato. +- **Nomenclatura personalizada de imágenes:** Si deseas renombrar imágenes (p. ej., `figure-01.png`), modifica `args.ResourceFileName` dentro de la devolución de llamada antes de escribir el archivo. +- **Incrustar imágenes como Base64:** Establece `args.ResourceFileName` a un URI de datos (`data:image/png;base64,...`) y omite la escritura del archivo. Esto es útil para exportaciones de markdown en un solo archivo. + +--- + +## Conclusión + +Ahora tienes un programa C# completamente funcional que **convierte Word a markdown**, **extrae imágenes de Word**, **crea una carpeta de recursos**, y garantiza una **markdown image relative path** limpia para cada imagen. El código es autónomo, funciona con la última versión de Aspose.Words, y puede integrarse en cualquier proyecto .NET con un esfuerzo mínimo. + +¿Próximos pasos? Prueba alimentar el markdown generado a un generador de sitios estáticos como Hugo o Jekyll, o experimenta con la devolución de llamada para incrustar imágenes directamente como cadenas Base64. Si te encuentras con casos extremos —por ejemplo, imágenes SVG o archivos inusualmente grandes— consulta la tabla de “Problemas comunes”; un pequeño ajuste suele resolver el problema. + +¡Feliz codificación, y que tu markdown siempre apunte a la carpeta correcta! + +{{< /blocks/products/pf/tutorial-page-section >}} +{{< /blocks/products/pf/main-container >}} +{{< /blocks/products/pf/main-wrap-class >}} +{{< blocks/products/products-backtop-button >}} \ No newline at end of file diff --git a/words/spanish/net/programming-with-officemath/_index.md b/words/spanish/net/programming-with-officemath/_index.md index 2cc7b02277..3d185cca96 100644 --- a/words/spanish/net/programming-with-officemath/_index.md +++ b/words/spanish/net/programming-with-officemath/_index.md @@ -23,6 +23,7 @@ Los tutoriales de Aspose.Words para .NET sobre procesamiento de textos con Offic | [Ecuaciones matemáticas](./math-equations/) Aprenda a configurar ecuaciones matemáticas en documentos de Word con Aspose.Words para .NET. Guía paso a paso con ejemplos, preguntas frecuentes y más. | [Guardar docx como txt – Exportar Word Math a LaTeX con C#](./save-docx-as-txt-export-word-math-to-latex-with-c/) Aprenda a guardar documentos DOCX como TXT y exportar ecuaciones Word Math a LaTeX con C#. | [Guardar documento como Txt – Exportar Word Math a LaTeX en C#](./save-document-as-txt-export-word-math-to-latex-in-c/) Exporta ecuaciones de Word a LaTeX guardando el documento como .txt. Guía paso a paso con ejemplos. +| [Convertir DOCX a TXT – Exportar ecuaciones de Word a LaTeX en C#](./convert-docx-to-txt-export-word-equations-to-latex-in-c/) Aprenda a convertir archivos DOCX a TXT y exportar ecuaciones de Word a LaTeX usando C#. {{< /blocks/products/pf/tutorial-page-section >}} diff --git a/words/spanish/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/_index.md b/words/spanish/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/_index.md new file mode 100644 index 0000000000..0dfb9c4d97 --- /dev/null +++ b/words/spanish/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/_index.md @@ -0,0 +1,211 @@ +--- +category: general +date: 2026-04-28 +description: Convertir DOCX a TXT y exportar ecuaciones de Word a LaTeX usando Aspose.Words. + Aprende cómo guardar Word como TXT y manejar objetos matemáticos en unos pocos pasos. +draft: false +keywords: +- convert docx to txt +- convert word equations to latex +- convert word to plain text +- save word as txt +- export equations as latex +language: es +og_description: Convierte DOCX a TXT y exporta ecuaciones de Word a LaTeX con un sencillo + fragmento de C#. Guía completa, código y consejos. +og_title: Convertir DOCX a TXT – Exportar ecuaciones de Word a LaTeX +tags: +- C# +- Aspose.Words +- Document Conversion +title: Convertir DOCX a TXT – Exportar ecuaciones de Word a LaTeX en C# +url: /es/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/ +--- + +{{< blocks/products/pf/main-wrap-class >}} +{{< blocks/products/pf/main-container >}} +{{< blocks/products/pf/tutorial-page-section >}} + +# Convertir DOCX a TXT – Exportar ecuaciones de Word a LaTeX + +¿Alguna vez necesitaste **convertir docx a txt** pero temías que las ecuaciones en tu archivo de Word se convirtieran en un desastre? No estás solo. En muchos proyectos de ingeniería o académicos, el documento fuente está en .docx, pero las herramientas posteriores solo entienden texto plano o LaTeX. ¿La buena noticia? Con unas pocas líneas de C# y Aspose.Words puedes **convertir docx a txt** *y* mantener cada ecuación como código LaTeX limpio. + +En este tutorial recorreremos todo el proceso: cargar un .docx, configurar las opciones de guardado para que los objetos Office Math se conviertan en LaTeX y, finalmente, escribir el resultado en un archivo .txt. Al final sabrás cómo **save word as txt**, **convert word to plain text**, y **export equations as latex** sin buscar en la documentación de la API. + +## Lo que aprenderás + +- Las llamadas exactas a la API necesarias para **convertir docx a txt** mientras se preservan las ecuaciones. +- Por qué elegir `OfficeMathExportMode.LaTeX` es la forma recomendada de **convert word equations to latex**. +- Cómo manejar casos límite comunes, como fuentes faltantes o características de ecuación no soportadas. +- Un programa C# completo, listo‑para‑ejecutar, que puedes incorporar a cualquier proyecto .NET. + +### Requisitos previos + +- .NET 6.0 o posterior (el código también funciona en .NET Framework 4.7+). +- Una licencia para Aspose.Words for .NET (la prueba gratuita sirve para evaluación). +- Un documento Word (`input.docx`) que contenga al menos un objeto Office Math. + +Si tienes todo eso, vamos a comenzar. + +## Paso 1: Instalar Aspose.Words + +Antes de que se ejecute cualquier código necesitas la biblioteca. Abre una terminal en la carpeta de tu proyecto y ejecuta: + +```bash +dotnet add package Aspose.Words +``` + +Eso descarga la última versión estable (a fecha de 2026‑04‑28 v24.12). No se requieren DLLs adicionales. + +## Paso 2: Cargar el documento fuente + +Lo primero que hacemos es leer el archivo .docx en un objeto `Document`. Este objeto nos brinda acceso completo a la estructura del archivo, incluyendo secuencias de texto, imágenes y objetos matemáticos. + +```csharp +using Aspose.Words; +using Aspose.Words.Saving; + +class Program +{ + static void Main() + { + // Step 2: Load the source document + Document doc = new Document(@"YOUR_DIRECTORY\input.docx"); +``` + +> **Por qué es importante:** Cargar el documento crea una representación en memoria, de modo que luego podemos ajustar cómo se escribe cada elemento. Si el archivo no se encuentra, Aspose lanza una `FileNotFoundException`, que podrías querer capturar en código de producción. + +## Paso 3: Configurar las opciones de guardado TXT para matemáticas LaTeX + +Por defecto, `Document.Save` escribe texto plano y **descarta** cualquier Office Math. Para conservar esas ecuaciones, establecemos `OfficeMathExportMode` a `LaTeX`. Esto indica al exportador que traduzca cada ecuación a su equivalente LaTeX. + +```csharp + // Step 3: Configure TXT save options to export Office Math as LaTeX + TxtSaveOptions txtOptions = new TxtSaveOptions + { + OfficeMathExportMode = OfficeMathExportMode.LaTeX, + // Optional: preserve line breaks as they appear in the original Word file + PreserveTableLayout = true + }; +``` + +> **Consejo profesional:** Si solo necesitas los caracteres Unicode sin procesar de la ecuación (por ejemplo, para una vista previa rápida), podrías usar `OfficeMathExportMode.Text`. Pero para la mayoría de los flujos científicos, `LaTeX` es el estándar de oro porque es universalmente entendido por los procesadores LaTeX. + +## Paso 4: Guardar el documento como texto plano + +Ahora escribimos el contenido transformado en un archivo `.txt`. El archivo contendrá párrafos normales, viñetas y—gracias al paso anterior—fragmentos LaTeX para cada ecuación. + +```csharp + // Step 4: Save the document as plain‑text using the configured options + doc.Save(@"YOUR_DIRECTORY\Math.txt", txtOptions); + } +} +``` + +Cuando abras `Math.txt` verás algo como: + +``` +In this report we derive the quadratic formula: +\[ +x = \frac{-b \pm \sqrt{b^{2} - 4ac}}{2a} +\] + +The end. +``` + +¿Observas los delimitadores `\[` … `\]`? Esos son los bloques de matemáticas LaTeX generados automáticamente. + +## Paso 5: Verificar la salida (Opcional pero recomendado) + +Es fácil pasar por alto un problema sutil de conversión, especialmente cuando las ecuaciones contienen símbolos personalizados. Una verificación rápida es alimentar el `.txt` generado a un compilador LaTeX (p. ej., `pdflatex`) y comprobar si compila sin errores. + +```bash +pdflatex -interaction=nonstopmode Math.txt +``` + +Si la compilación tiene éxito, has **convert word equations to latex** y **convert docx to txt** de una sola vez. Si aparecen errores, busca mensajes sobre comandos indefinidos—generalmente indican una característica de ecuación que Aspose.Words no puede traducir (p. ej., ciertas notaciones de matrices). En esos casos, puedes recurrir a `OfficeMathExportMode.MathML` y post‑procesar el MathML a LaTeX con otra herramienta. + +## Errores comunes y cómo evitarlos + +| Issue | Why it Happens | Fix | +|-------|----------------|-----| +| Missing fonts | Aspose.Words needs the font to render symbols correctly. | Install the missing font on the machine or embed it in the .docx. | +| Complex equations not exported | Some newer Office Math features aren’t yet mapped to LaTeX. | Use `OfficeMathExportMode.MathML` then convert with a MathML‑to‑LaTeX library. | +| Extra blank lines | Plain‑text saver preserves paragraph breaks, which can add whitespace. | Set `txtOptions.AddBidiMarks = false` or post‑process the file with a simple script. | + +## Ejemplo completo funcional (listo para copiar y pegar) + +A continuación se muestra el programa completo, listo para compilar. Reemplaza `YOUR_DIRECTORY` con la carpeta que contiene tu `input.docx`. + +```csharp +using System; +using Aspose.Words; +using Aspose.Words.Saving; + +namespace DocxToTxtWithLatex +{ + class Program + { + static void Main() + { + try + { + // Load the source document + Document doc = new Document(@"C:\Docs\input.docx"); + + // Configure save options: export equations as LaTeX + TxtSaveOptions txtOptions = new TxtSaveOptions + { + OfficeMathExportMode = OfficeMathExportMode.LaTeX, + PreserveTableLayout = true, + AddBidiMarks = false + }; + + // Save as plain‑text + string outputPath = @"C:\Docs\Math.txt"; + doc.Save(outputPath, txtOptions); + + Console.WriteLine($"Successfully converted DOCX to TXT. Output at: {outputPath}"); + } + catch (Exception ex) + { + Console.Error.WriteLine($"Conversion failed: {ex.Message}"); + } + } + } +} +``` + +Ejecutar este programa **save word as txt** mientras convierte cada bloque Office Math en LaTeX, dándote un archivo de texto plano limpio y buscable. + +## Próximos pasos y temas relacionados + +- **Conversión por lotes:** Envuelve la lógica anterior en un bucle `foreach` para procesar una carpeta completa de archivos .docx. +- **Combinar con generación de PDF:** Después de obtener los fragmentos LaTeX, introdúcelos en una cadena de procesamiento PDF (p. ej., `PdfSharp` + `MiKTeX`) para producir informes PDF. +- **Exportar ecuaciones como latex** para otros formatos: Aspose.Words también soporta `SaveFormat.Markdown`, que puede incrustar LaTeX automáticamente. +- **Ajuste de rendimiento:** Para documentos masivos, reutiliza la misma instancia de `TxtSaveOptions` y desactiva características innecesarias como `AddBidiMarks`. + +--- + +### Ejemplo de imagen (Opcional) + +Si prefieres una pista visual, aquí tienes una captura de pantalla del archivo de salida en Notepad++. + +![salida de convertir docx a txt mostrando ecuaciones LaTeX](convert-docx-to-txt-output.png) + +*(Texto alternativo: “salida de convertir docx a txt mostrando ecuaciones LaTeX” – cumple con el requisito de la palabra clave principal.)* + +--- + +## Conclusión + +Acabamos de demostrar una forma fiable de **convert docx to txt** mientras se preserva cada ecuación como LaTeX limpio. La clave es la bandera `OfficeMathExportMode.LaTeX`, que convierte el formato propietario de matemáticas de Word en algo que cualquier motor LaTeX entiende. Con el ejemplo completo de código anterior puedes **save word as txt**, **convert word to plain text**, y **export equations as latex** en una única ejecución autónoma. + +Siéntete libre de experimentar—cambia la extensión de salida a `.md` para Markdown, o integra el fragmento en una cadena de procesamiento de documentos más grande. Si encuentras algún problema, deja un comentario abajo; estaré encantado de ayudar a resolverlo. + +¡Feliz codificación! + +{{< /blocks/products/pf/tutorial-page-section >}} +{{< /blocks/products/pf/main-container >}} +{{< /blocks/products/pf/main-wrap-class >}} +{{< blocks/products/products-backtop-button >}} \ No newline at end of file diff --git a/words/swedish/net/ai-powered-document-processing/_index.md b/words/swedish/net/ai-powered-document-processing/_index.md index 79cdc3ed58..b433175cde 100644 --- a/words/swedish/net/ai-powered-document-processing/_index.md +++ b/words/swedish/net/ai-powered-document-processing/_index.md @@ -39,9 +39,10 @@ Slutligen, glöm inte att kolla in våra [Arbeta med sammanfattningsalternativ]( | Titel | Beskrivning | | --- | --- | | [Arbeta med AI-modell](./working-with-ai-model/) | Lär dig hur du använder Aspose.Words för .NET för att sammanfatta dokument med AI. Enkla steg för att förbättra dokumenthanteringen. | -| [Arbeta med Googles AI-modell](./working-with-google-ai-model/) Förbättra din dokumenthantering med Aspose.Words för .NET och Google AI för att enkelt skapa koncisa sammanfattningar. | +| [Arbeta med Googles AI-modell](./working-with-google-ai-model/) | Förbättra din dokumenthantering med Aspose.Words för .NET och Google AI för att enkelt skapa koncisa sammanfattningar. | | [Arbeta med öppen AI-modell](./working-with-open-ai-model/) | Lås upp effektiv dokumentsammanfattning med Aspose.Words för .NET och OpenAI:s kraftfulla modeller. Fördjupa dig i den här omfattande guiden nu. | | [Arbeta med sammanfattningsalternativ](./working-with-summarize-options/) | Lär dig att effektivt sammanfatta Word-dokument med Aspose.Words för .NET med vår steg-för-steg-guide om hur du integrerar AI-modeller för snabba insikter. | +| [Anslut till lokal LLM i C# – Komplett programmeringsguide](./connect-to-local-llm-in-c-complete-programming-guide/) | Lär dig hur du ansluter till en lokal LLM med C# och Aspose.Words för .NET i en komplett steg-för-steg-guide. | {{< /blocks/products/pf/tutorial-page-section >}} diff --git a/words/swedish/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/_index.md b/words/swedish/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/_index.md new file mode 100644 index 0000000000..44c8c44959 --- /dev/null +++ b/words/swedish/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/_index.md @@ -0,0 +1,284 @@ +--- +category: general +date: 2026-04-28 +description: Anslut till en lokal LLM från C# och be den stora språkmodellen att ladda + ett Word‑dokument, anropa den lokala LLM:n och automatiskt skriva om texten. Steg‑för‑steg‑kod + inkluderad. +draft: false +keywords: +- connect to local llm +- prompt large language model +- load word document +- call local llm +- rewrite text automatically +language: sv +og_description: Anslut till en lokal LLM från C# och se hur du kan ge en prompt till + en stor språkmodell, ladda ett Word‑dokument, anropa den lokala LLM:n och automatiskt + skriva om texten på några minuter. +og_title: Anslut till lokal LLM i C# – Komplett programmeringsguide +tags: +- Aspose.Words +- C# +- LLM +- AI Automation +title: Anslut till lokal LLM i C# – Komplett programmeringsguide +url: /sv/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/ +--- + +{{< blocks/products/pf/main-wrap-class >}} +{{< blocks/products/pf/main-container >}} +{{< blocks/products/pf/tutorial-page-section >}} + +# Anslut till lokal LLM i C# – Komplett programmeringsguide + +Har du någonsin behövt **ansluta till lokal llm** från en .NET‑app och funderat på hur du får den att prata med ett Word‑dokument? Du är inte ensam. I den här guiden går vi igenom hela processen – anslut till lokal llm, **prompt large language model**, ladda ett Word‑dokument, **call local llm** och slutligen **rewrite text automatically**. När du är klar har du ett körbart exempel som omvandlar vilket stycke som helst till en formell ton utan några externa API‑nycklar. + +## Vad den här handledningen täcker + +Vi börjar med att installera de nödvändiga NuGet‑paketen, sedan startar vi en enkel lokal LLM‑endpoint (tänk Ollama på port 11434). Därefter laddar vi en `.docx`‑fil med Aspose.Words, skickar ett stycke till LLM:n, får tillbaka en omskriven version och skriver tillbaka den i samma dokument. Du får också se hur du hanterar vanliga fallgropar – null‑stycken, async‑disposal och kodningsproblem – så att koden fungerar i produktion, inte bara i en demo. + +### Förutsättningar + +- .NET 6.0 SDK eller senare (du kan också använda .NET 8 om du vill) +- Visual Studio 2022 eller VS Code med C#‑tillägg +- **Aspose.Words for .NET** (gratis provversion räcker) +- En lokalt hostad LLM som följer `/api/generate`‑kontraktet (t.ex. Ollama, LMStudio) +- Grundläggande kunskap om async/await i C# + +> **Proffstips:** Om du ännu inte har installerat Ollama, kör `ollama serve` och hämta en modell med `ollama pull llama3`. Standard‑HTTP‑endpointen blir `http://localhost:11434/api/generate`. + +--- + +## Steg 1: Installera nödvändiga paket + +Först lägger vi till Aspose.Words‑ och Aspose.Words.AI‑NuGet‑paketen i ditt projekt. + +```bash +dotnet add package Aspose.Words +dotnet add package Aspose.Words.AI +``` + +Dessa bibliotek ger oss möjligheten att **load word document** samt ett lätt skal för att **call local llm** utan att manuellt bygga HTTP‑förfrågningar. + +--- + +## Steg 2: Anslut till den lokala LLM‑endpointen + +Att ansluta till en lokalt hostad modell är lika enkelt som att instansiera `LocalLargeLanguageModel`. Konstruktorn förväntar sig den fullständiga URL‑en till genererings‑endpointen. + +```csharp +using Aspose.Words.AI; +using Aspose.Words; +using System.Threading.Tasks; + +// Create a client that talks to the LLM running on localhost +var localLlm = new LocalLargeLanguageModel("http://localhost:11434/api/generate"); +``` + +Varför omsluter vi endpointen i en klass? `LocalLargeLanguageModel` sköter JSON‑serialisering, återförsök och strömmande svar åt dig – så att du kan fokusera på prompt‑logiken istället för att trassla med `HttpClient`. + +--- + +## Steg 3: Ladda källdokumentet + +Nästa steg är att läsa in dokumentet i minnet. Aspose.Words stödjer praktiskt taget alla Word‑format, så `Document` kan parsas från `input.docx` utan att Office behöver vara installerat. + +```csharp +// Path to the source file – adjust as needed +string inputPath = Path.Combine(Environment.CurrentDirectory, "input.docx"); + +// Load the document; throws if the file is missing or corrupted +Document sourceDocument = new Document(inputPath); +``` + +Om du behöver arbeta med en ström (t.ex. en fil som laddas upp via ASP.NET) ersätter du bara filsökvägen med en `MemoryStream` och skickar den till `Document`‑konstruktorn. + +--- + +## Steg 4: Extrahera texten i det aktuella stycket + +Vi använder `DocumentBuilder` för att navigera i dokumentet. I det här exemplet skriver vi om **det första stycket**, men du kan iterera över `sourceDocument.GetChildNodes(NodeType.Paragraph, true)` för att bearbeta många. + +```csharp +// Builder gives us a cursor inside the document +DocumentBuilder docBuilder = new DocumentBuilder(sourceDocument); + +// Grab the text of the paragraph where the builder is currently positioned +string originalParagraph = docBuilder.CurrentParagraph?.GetText() ?? string.Empty; + +// Safety check – avoid sending empty strings to the LLM +if (string.IsNullOrWhiteSpace(originalParagraph)) +{ + Console.WriteLine("No paragraph found at the current cursor position."); + return; +} +``` + +Operatorn `?.` förhindrar ett `NullReferenceException` om dokumentet skulle vara tomt. Detta är ett av de **edge cases** som ofta får nybörjare att fastna. + +--- + +## Steg 5: Prompt LLM:n att skriva om stycket + +Nu **prompt large language model** på riktigt. Prompten är skriven på vanlig engelska; wrapper‑klassen skickar den som JSON till den lokala endpointen. + +```csharp +// Build a friendly instruction for the model +string prompt = $"Rewrite the following sentence in a more formal tone:\n{originalParagraph}"; + +// Await the model's response – this is an async call +string rewrittenParagraph = await localLlm.PromptAsync(prompt); +``` + +Varför formulerar vi förfrågan på detta sätt? LLM:er svarar bäst på tydliga, enkla instruktioner. En radbrytning efter kolon separerar instruktionen från innehållet och minskar risken för att modellen återupprepar prompten. + +**Förväntat resultat** – Om `originalParagraph` var `"Hey, what's up?"` kan LLM:n svara: + +> “Good day, how may I assist you?” + +Du kan verifiera resultatet genom att skriva ut det: + +```csharp +Console.WriteLine("Original: " + originalParagraph); +Console.WriteLine("Rewritten: " + rewrittenParagraph); +``` + +--- + +## Steg 6: Sätt in den omskrivna texten i dokumentet + +När vi har den nya texten ersätter vi det gamla stycket. `DocumentBuilder.Writeln` skriver en ny rad och flyttar markören framåt, vilket är perfekt för att lägga till text. Om du vill *ersätta* exakt samma stycke kan du först anropa `docBuilder.CurrentParagraph.RemoveAllChildren()` innan du skriver. + +```csharp +// Option A – Append a new paragraph (keeps the original) +docBuilder.Writeln(rewrittenParagraph); + +// Option B – Replace the existing paragraph (uncomment to use) +// docBuilder.CurrentParagraph.RemoveAllChildren(); +// docBuilder.CurrentParagraph.AppendChild(new Run(docBuilder.Document, rewrittenParagraph)); +``` + +Båda metoderna visas så att du kan välja den som passar ditt arbetsflöde bäst. + +--- + +## Steg 7: Spara det uppdaterade dokumentet + +Till sist sparar vi ändringarna till en ny fil. Aspose.Words väljer automatiskt format baserat på filändelsen. + +```csharp +string outputPath = Path.Combine(Environment.CurrentDirectory, "output.docx"); +sourceDocument.Save(outputPath); + +Console.WriteLine($"Document saved to {outputPath}"); +``` + +Öppna `output.docx` i Word, så ser du att stycket nu har en formell ton. + +--- + +## Fullständigt fungerande exempel + +Nedan finns det **kompletta, självständiga programmet**. Kopiera och klistra in i ett konsolprojekt, återställ NuGet‑paketen och kör – ingen extra konfiguration behövs förutom en körande lokal LLM. + +```csharp +using Aspose.Words.AI; +using Aspose.Words; +using System; +using System.IO; +using System.Threading.Tasks; + +class Program +{ + static async Task Main() + { + // ------------------------------------------------- + // Step 1: Connect to the locally hosted LLM endpoint + // ------------------------------------------------- + var localLlm = new LocalLargeLanguageModel("http://localhost:11434/api/generate"); + + // ------------------------------------------------- + // Step 2: Load the source Word document + // ------------------------------------------------- + string inputPath = Path.Combine(Environment.CurrentDirectory, "input.docx"); + Document sourceDocument = new Document(inputPath); + + // ------------------------------------------------- + // Step 3: Retrieve the text of the current paragraph + // ------------------------------------------------- + DocumentBuilder docBuilder = new DocumentBuilder(sourceDocument); + string originalParagraph = docBuilder.CurrentParagraph?.GetText() ?? string.Empty; + + if (string.IsNullOrWhiteSpace(originalParagraph)) + { + Console.WriteLine("No paragraph found at the current cursor position."); + return; + } + + // ------------------------------------------------- + // Step 4: Ask the LLM to rewrite the paragraph in a formal tone + // ------------------------------------------------- + string prompt = $"Rewrite the following sentence in a more formal tone:\n{originalParagraph}"; + string rewrittenParagraph = await localLlm.PromptAsync(prompt); + + // ------------------------------------------------- + // Step 5: Insert the rewritten text back into the document + // ------------------------------------------------- + docBuilder.Writeln(rewrittenParagraph); + + // ------------------------------------------------- + // Step 6: Save the updated document + // ------------------------------------------------- + string outputPath = Path.Combine(Environment.CurrentDirectory, "output.docx"); + sourceDocument.Save(outputPath); + + Console.WriteLine("Original paragraph:"); + Console.WriteLine(originalParagraph); + Console.WriteLine("\nRewritten paragraph:"); + Console.WriteLine(rewrittenParagraph); + Console.WriteLine($"\nDocument saved to {outputPath}"); + } +} +``` + +### Vad du kan förvänta dig när du kör det + +1. Konsolen skriver ut både original‑ och omskrivna stycken. +2. `output.docx` dyker upp bredvid `input.docx`. +3. När du öppnar filen ser du det nya formella stycket antingen infogat efter originalet (eller ersatt, om du använde den alternativa koden). + +--- + +## Hantera vanliga edge cases + +| Situation | Lösning | +|-----------|----------| +| **Tomt eller endast blanksteg‑stycke** | Kontrollera `string.IsNullOrWhiteSpace` innan du promptar (se Steg 3). | +| **LLM returnerar fel eller tom sträng** | Omge `PromptAsync` med `try/catch` och falla tillbaka på originaltexten. | +| **Flera stycken ska skrivas om** | Loopa igenom `sourceDocument.GetChildNodes(NodeType.Paragraph, true)` och applicera samma prompt‑logik. | +| **Stora dokument ger hög latens** | Batcha stycken och skicka dem i en enda förfrågan (max ca 4 KB per anrop). | +| **Icke‑ASCII‑tecken blir felaktiga** | Säkerställ att LLM‑endpointen använder UTF‑8 (de flesta moderna modeller gör det). | + +--- + +## Nästa steg & relaterade ämnen + +- **Prompt large language model** med rikare instruktioner (t.ex. stilguider, längdgränser). +- Använd **call local llm** i ett web‑API för att exponera dokument‑automation som en tjänst. +- Utforska **load word document** i parallella strömmar för hög genomströmning. +- Kombinera detta med **rewrite text automatically** för massutskick av e‑mail eller standardisering av rapporter. + +Vill du fördjupa dig mer, kolla in Asposes dokumentation om **document merging** samt Ollama‑API‑referensen för anpassade sampling‑parametrar. + +--- + +## Slutsats + +Vi har just visat hur du **connect to local llm** från C#, **prompt large language model**, **load word document**, **call local llm** och **rewrite text automatically** – allt i ett enda körbart konsolprogram. Mönstret är skalbart: byt prompt, iterera över stycken eller exponera logiken via en ASP.NET‑endpoint. Det viktigaste att ta med sig är att lokala AI‑modeller kan integreras tätt med klassiska dokument‑bearbetningsbibliotek, vilket ger kraftfull automation utan att någonsin lämna din säkra on‑prem‑miljö. + +Har du frågor om trådar, + +{{< /blocks/products/pf/tutorial-page-section >}} +{{< /blocks/products/pf/main-container >}} +{{< /blocks/products/pf/main-wrap-class >}} +{{< blocks/products/products-backtop-button >}} \ No newline at end of file diff --git a/words/swedish/net/programming-with-markdownsaveoptions/_index.md b/words/swedish/net/programming-with-markdownsaveoptions/_index.md index 57a9d3bdab..2a883a3381 100644 --- a/words/swedish/net/programming-with-markdownsaveoptions/_index.md +++ b/words/swedish/net/programming-with-markdownsaveoptions/_index.md @@ -75,6 +75,9 @@ Lär dig spara DOCX som Markdown med en komplett C#‑guide som använder Aspose ### [Spara Word-bilder – Konvertera Word till Markdown med Aspose](./save-word-images-convert-word-to-markdown-with-aspose/) Lär dig spara bilder från Word och konvertera dokument till Markdown med Aspose.Words för .NET. +### [Markdown‑bild relativ sökväg – Konvertera Word till Markdown](./markdown-image-relative-path-convert-word-to-markdown/) +Lär dig hur du använder relativa bildsökvägar när du konverterar Word-dokument till Markdown med Aspose.Words för .NET. + {{< /blocks/products/pf/tutorial-page-section >}} diff --git a/words/swedish/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/_index.md b/words/swedish/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/_index.md new file mode 100644 index 0000000000..a15ce694cd --- /dev/null +++ b/words/swedish/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/_index.md @@ -0,0 +1,206 @@ +--- +category: general +date: 2026-04-28 +description: Lär dig hur du ställer in en relativ sökväg för markdown‑bilder när du + konverterar Word till markdown, extraherar bilder från Word och skapar en resurser‑mapp + för exporterade bilder. +draft: false +keywords: +- markdown image relative path +- convert word to markdown +- extract images from word +- create resources folder +- export images from docx +language: sv +og_description: Ställ in en relativ bildväg i markdown när du konverterar Word till + markdown, extraherar bilder från Word och skapar en resursmapp för exporterade bilder. +og_title: markdown bild relativ sökväg – Konvertera Word till Markdown +tags: +- Aspose.Words +- C# +- Markdown +- Image Export +title: markdown‑bild relativ sökväg – Konvertera Word till Markdown +url: /sv/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/ +--- + +{{< blocks/products/pf/main-wrap-class >}} +{{< blocks/products/pf/main-container >}} +{{< blocks/products/pf/tutorial-page-section >}} + +# markdown image relative path – Konvertera Word till Markdown + +Har du någonsin behövt en **markdown image relative path** när du **convert Word to markdown**? Du är inte ensam. De flesta utvecklare stöter på ett problem när den genererade Markdown pekar på bilder i en platt mapp, vilket bryter den relativa länkstrukturen du förväntar dig i en statisk webbplats eller ett GitHub‑repo. + +I den här handledningen går vi igenom en komplett, end‑to‑end‑lösning som **extraherar bilder från Word**, **skapar en resources‑mapp**, och skriver om bildreferenserna så att de använder en ren *markdown image relative path*. När du är klar har du en färdig att publicera `.md`‑fil och en prydligt organiserad `Resources`‑katalog som innehåller varje bild som extraherats från den ursprungliga `.docx`‑filen. + +> **Vad du får:** ett enda C#‑program (utan externa skript), en tydlig förklaring av *varför* varje del är viktig, och ett antal praktiska tips som du kan kopiera‑klistra in i dina egna projekt. + +--- + +## Förutsättningar + +Innan vi dyker in i koden, se till att du har: + +- **.NET 6.0** eller senare installerat (du kan också rikta in dig på .NET Framework 4.7+, men .NET 6 är den bästa versionen för nya projekt). +- **Aspose.Words for .NET** (det senaste NuGet‑paketet vid skrivtillfället, version 23.12). Installera det med: + ```bash + dotnet add package Aspose.Words + ``` +- Ett Word‑dokument som faktiskt innehåller bilder—vi kallar det `WithImages.docx`. +- En mapp där du vill att den genererade markdown‑filen och bilderna ska ligga, t.ex. `C:\Projects\MarkdownExport`. + +Inga ytterligare bibliotek krävs; allt annat hanteras av Aspose.Words. + +--- + +## Steg 1: Ladda källdokumentet Word (utgångspunkten för convert word to markdown) + +```csharp +using System; +using System.IO; +using Aspose.Words; +using Aspose.Words.Saving; + +class Program +{ + static void Main() + { + // Adjust the path to point at your own .docx file. + string sourcePath = @"C:\Projects\MarkdownExport\WithImages.docx"; + + // Load the document – this is where Aspose.Words parses the Word file. + Document doc = new Document(sourcePath); + + // The rest of the workflow follows… + } +} +``` + +*Varför detta är viktigt:* Att ladda dokumentet ger oss åtkomst till det interna nodträdet, som inkluderar bilddelarna vi senare behöver för att **export images from docx**. Om laddningen misslyckas körs ingen av de efterföljande stegen, så dubbelkolla sökvägen och filbehörigheterna. + +--- + +## Steg 2: Konfigurera `MarkdownSaveOptions` med en anpassad callback (hjärtat i create resources folder) + +`ResourceSavingCallback` låter oss ingripa varje gång Aspose.Words vill skriva en bildfil. Inuti callbacken kommer vi att **create a Resources sub‑folder** och justera referensen så att den genererade markdown‑filen använder en *markdown image relative path*. + +```csharp +// Inside Main(), after loading the document: +string outputFolder = @"C:\Projects\MarkdownExport"; +string resourcesFolder = Path.Combine(outputFolder, "Resources"); + +// Make sure the folder exists before we start saving anything. +Directory.CreateDirectory(resourcesFolder); + +// Set up the Markdown save options. +MarkdownSaveOptions mdOptions = new MarkdownSaveOptions +{ + // Hook that runs for every image resource. + ResourceSavingCallback = new MyMarkdownResourceCallback(resourcesFolder) +}; + +// Save the document as Markdown. +string markdownPath = Path.Combine(outputFolder, "Doc.md"); +doc.Save(markdownPath, mdOptions); +``` + +Observera att vi skickade `resourcesFolder` till callback‑konstruktorn—detta gör mappens sökväg flexibel och undviker hårdkodade strängar i hela koden. + +--- + +## Steg 3: Implementera callbacken som **creates resources folder** och skriver om sökvägen + +```csharp +/// +/// Handles image extraction and path rewriting for markdown export. +/// +class MyMarkdownResourceCallback : IResourceSavingCallback +{ + private readonly string _resourcesFolder; + + public MyMarkdownResourceCallback(string resourcesFolder) + { + _resourcesFolder = resourcesFolder; + } + + public void ResourceSaving(ResourceSavingArgs args) + { + // 1️⃣ Build the full file system path where the image will be stored. + string targetPath = Path.Combine(_resourcesFolder, args.ResourceFileName); + + // 2️⃣ Ensure the directory exists (in case Aspose creates sub‑folders). + Directory.CreateDirectory(Path.GetDirectoryName(targetPath)); + + // 3️⃣ Write the image stream to disk. + using (FileStream fileStream = File.Create(targetPath)) + { + args.Stream.CopyTo(fileStream); + } + + // 4️⃣ Update the markdown reference to use a relative path. + // This is the crucial line that gives us the markdown image relative path. + args.ResourceFileName = Path.Combine("Resources", args.ResourceFileName); + } +} +``` + +*Varför detta fungerar:* `args.Stream` innehåller de råa bildbytarna. Genom att kopiera dem till en fil i vår `Resources`‑mapp **export images from docx** säkert. Sedan ersätter vi `args.ResourceFileName` med en relativ URL (`Resources/image.png`). När Aspose.Words senare skriver markdown‑filen injicerar den exakt den strängen, vilket ger oss den önskade *markdown image relative path*. + +--- + +## Steg 4: Verifiera den genererade Markdown (hur det slutliga resultatet ser ut) + +Öppna `Doc.md` i en textredigerare. Du bör se något liknande: + +```markdown +# Sample Heading + +Here is an inline picture: + +![Image 0](Resources/Image_0.png) + +And a picture inside a table: + +![Image 1](Resources/Image_1.jpg) +``` + +Det viktiga är att varje bildreferens pekar på `Resources/...` – det är den **markdown image relative path** vi eftersträvade. + +![markdown image relative path example](example.png "markdown image relative path example") + +*Tips:* Om du öppnar markdown‑filen i en visare som respekterar relativa länkar (VS Code‑förhandsgranskning, GitHub eller en statisk webbplatsgenerator), kommer bilderna att visas korrekt utan någon extra konfiguration. + +--- + +## Steg 5: Vanliga fallgropar och pro‑tips + +| Problem | Varför det händer | Hur man åtgärdar det | +|---------|-------------------|----------------------| +| Bilder hamnar i rotmappen istället för `Resources` | Callbacken var inte ansluten eller `args.ResourceFileName` överskrevdes inte. | Dubbelkolla att `ResourceSavingCallback` är satt **före** anropet `doc.Save`. | +| Filnamn innehåller otillåtna tecken | Word namnger ibland bilder med mellanslag eller Unicode‑symboler. | Använd `Path.GetInvalidFileNameChars()` för att sanera `args.ResourceFileName` i callbacken. | +| Stora dokument tar lång tid att bearbeta | Varje bild skrivs synkront. | Byt till asynkron I/O (`await args.Stream.CopyToAsync(fileStream)`) om du kör på .NET 6+ och behöver prestanda. | +| Relativa sökvägar går sönder när markdown‑filen flyttas | Sökvägen är relativ till markdown‑filens plats. | Behåll `Doc.md` och `Resources`‑mappen tillsammans, eller justera callbacken för att använda ett annat relativt prefix (t.ex. `../assets`). | + +--- + +## Steg 6: Utöka lösningen (vad om du behöver mer kontroll?) + +- **Multiple output formats:** Ersätt `MarkdownSaveOptions` med `HtmlSaveOptions` eller `PdfSaveOptions` samtidigt som du behåller samma callback—Aspose.Words kommer att anropa den för varje bild oavsett format. +- **Custom image naming:** Om du vill byta namn på bilder (t.ex. `figure-01.png`), ändra `args.ResourceFileName` i callbacken innan du skriver filen. +- **Embedding images as Base64:** Sätt `args.ResourceFileName` till en data‑URI (`data:image/png;base64,...`) och hoppa över filskrivningen. Detta är praktiskt för markdown‑exporter i en enda fil. + +--- + +## Slutsats + +Du har nu ett fullt funktionellt C#‑program som **converts Word to markdown**, **extracts images from word**, **creates a resources folder**, och garanterar en ren **markdown image relative path** för varje bild. Koden är självständig, fungerar med den senaste versionen av Aspose.Words, och kan enkelt läggas in i vilket .NET‑projekt som helst med minimal ansträngning. + +Nästa steg? Prova att mata in den genererade markdown‑filen i en statisk webbplatsgenerator som Hugo eller Jekyll, eller experimentera med callbacken för att bädda in bilder direkt som Base64‑strängar. Om du stöter på kantfall—t.ex. SVG‑bilder eller ovanligt stora filer—titta tillbaka på tabellen “Vanliga fallgropar”; en liten justering löser oftast problemet. + +Lycka till med kodandet, och må din markdown alltid peka på rätt mapp! + +{{< /blocks/products/pf/tutorial-page-section >}} +{{< /blocks/products/pf/main-container >}} +{{< /blocks/products/pf/main-wrap-class >}} +{{< blocks/products/products-backtop-button >}} \ No newline at end of file diff --git a/words/swedish/net/programming-with-officemath/_index.md b/words/swedish/net/programming-with-officemath/_index.md index 94585573ed..75a7288b22 100644 --- a/words/swedish/net/programming-with-officemath/_index.md +++ b/words/swedish/net/programming-with-officemath/_index.md @@ -23,6 +23,7 @@ Aspose.Words för .NET-handledningarna om ordbehandling med OfficeMath guidar di | [Matematiska ekvationer](./math-equations/) | Lär dig hur du konfigurerar matematiska ekvationer i Word-dokument med Aspose.Words för .NET. Steg-för-steg-guide med exempel, vanliga frågor och mer. | | [Spara docx som txt – Exportera Word Math till LaTeX med C#](./save-docx-as-txt-export-word-math-to-latex-with-c/) | Lär dig hur du sparar ett docx som txt och exporterar matematiska ekvationer till LaTeX med C#. | | [Spara dokument som Txt – Exportera Word Math till LaTeX i C#](./save-document-as-txt-export-word-math-to-latex-in-c/) | Lär dig hur du sparar ett Word-dokument som txt och exporterar matematiska ekvationer till LaTeX med C#. | +| [Konvertera DOCX till TXT – Exportera Word-ekvationer till LaTeX i C#](./convert-docx-to-txt-export-word-equations-to-latex-in-c/) | Lär dig hur du konverterar ett DOCX till TXT och exporterar Word-ekvationer till LaTeX med C#. | {{< /blocks/products/pf/tutorial-page-section >}} diff --git a/words/swedish/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/_index.md b/words/swedish/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/_index.md new file mode 100644 index 0000000000..18661c7050 --- /dev/null +++ b/words/swedish/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/_index.md @@ -0,0 +1,210 @@ +--- +category: general +date: 2026-04-28 +description: Konvertera DOCX till TXT och exportera Word‑ekvationer till LaTeX med + Aspose.Words. Lär dig hur du sparar Word som TXT och hanterar matematiska objekt + på några få steg. +draft: false +keywords: +- convert docx to txt +- convert word equations to latex +- convert word to plain text +- save word as txt +- export equations as latex +language: sv +og_description: Konvertera DOCX till TXT och exportera Word‑ekvationer till LaTeX + med ett enkelt C#‑snutt. Fullständig guide, kod och tips. +og_title: Konvertera DOCX till TXT – Exportera Word‑ekvationer till LaTeX +tags: +- C# +- Aspose.Words +- Document Conversion +title: Konvertera DOCX till TXT – Exportera Word‑ekvationer till LaTeX i C# +url: /sv/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/ +--- + +{{< blocks/products/pf/main-wrap-class >}} +{{< blocks/products/pf/main-container >}} +{{< blocks/products/pf/tutorial-page-section >}} + +# Konvertera DOCX till TXT – Exportera Word-ekvationer till LaTeX + +Har du någonsin behövt **convert docx to txt** men oroat dig för att matematiken i din Word‑fil skulle bli en rörig röra? Du är inte ensam. I många ingenjörs‑ eller akademiska projekt ligger källdokumentet i .docx, men nedströmsverktyg förstår bara vanlig text eller LaTeX. Den goda nyheten? Med några rader C# och Aspose.Words kan du **convert docx to txt** *och* behålla varje ekvation som ren LaTeX‑kod. + +I den här handledningen går vi igenom hela processen: läsa in en .docx, konfigurera sparalternativen så att Office‑Math‑objekt blir LaTeX, och slutligen skriva resultatet till en .txt‑fil. När du är klar vet du hur du **save word as txt**, **convert word to plain text**, och **export equations as latex** utan att gräva i API‑dokumentationen. + +## Vad du kommer att lära dig + +- De exakta API‑anropen som behövs för att **convert docx to txt** samtidigt som ekvationer bevaras. +- Varför valet av `OfficeMathExportMode.LaTeX` är det rekommenderade sättet att **convert word equations to latex**. +- Hur du hanterar vanliga kantfall såsom saknade typsnitt eller ej stödda ekvationsfunktioner. +- Ett komplett, färdigt‑att‑köra C#‑program som du kan släppa in i vilket .NET‑projekt som helst. + +### Förutsättningar + +- .NET 6.0 eller senare (koden fungerar även på .NET Framework 4.7+). +- En licens för Aspose.Words för .NET (gratis provversion fungerar för utvärdering). +- Ett Word‑dokument (`input.docx`) som innehåller minst ett Office Math‑objekt. + +Om du har dem, låt oss köra igång. + +## Steg 1: Installera Aspose.Words + +Innan någon kod körs behöver du biblioteket. Öppna en terminal i din projektmapp och kör: + +```bash +dotnet add package Aspose.Words +``` + +Det hämtar den senaste stabila versionen (från 2026‑04‑28 v24.12). Inga extra DLL‑filer behövs. + +## Steg 2: Läs in källdokumentet + +Det första vi gör är att läsa .docx‑filen till ett `Document`‑objekt. Detta objekt ger oss full åtkomst till filens struktur, inklusive textkörningar, bilder och matteobjekt. + +```csharp +using Aspose.Words; +using Aspose.Words.Saving; + +class Program +{ + static void Main() + { + // Step 2: Load the source document + Document doc = new Document(@"YOUR_DIRECTORY\input.docx"); +``` + +> **Varför detta är viktigt:** Att ladda dokumentet skapar en representation i minnet, så att vi senare kan justera hur varje element skrivs ut. Om filen inte hittas kastar Aspose en `FileNotFoundException`, vilket du kanske vill fånga i produktionskod. + +## Steg 3: Konfigurera TXT‑sparalternativ för LaTeX‑matematik + +Som standard skriver `Document.Save` vanlig text och **slänger** all Office Math. För att behålla dessa ekvationer sätter vi `OfficeMathExportMode` till `LaTeX`. Detta instruerar exportören att översätta varje ekvation till dess LaTeX‑motsvarighet. + +```csharp + // Step 3: Configure TXT save options to export Office Math as LaTeX + TxtSaveOptions txtOptions = new TxtSaveOptions + { + OfficeMathExportMode = OfficeMathExportMode.LaTeX, + // Optional: preserve line breaks as they appear in the original Word file + PreserveTableLayout = true + }; +``` + +> **Proffstips:** Om du bara behöver de råa Unicode‑tecknen för ekvationen (t.ex. för en snabb förhandsgranskning) kan du använda `OfficeMathExportMode.Text`. Men för de flesta vetenskapliga pipelines är `LaTeX` guldstandarden eftersom den är universellt förstådd av LaTeX‑processorer. + +## Steg 4: Spara dokumentet som vanlig text + +Nu skriver vi det omvandlade innehållet till en `.txt`‑fil. Filen kommer att innehålla vanliga stycken, punktlistor och—tack vare föregående steg—LaTeX‑snuttar för varje ekvation. + +```csharp + // Step 4: Save the document as plain‑text using the configured options + doc.Save(@"YOUR_DIRECTORY\Math.txt", txtOptions); + } +} +``` + +När du öppnar `Math.txt` kommer du att se något liknande: + +``` +In this report we derive the quadratic formula: +\[ +x = \frac{-b \pm \sqrt{b^{2} - 4ac}}{2a} +\] + +The end. +``` + +Lägger du märke till avgränsarna `\[` … `\]`? Det är LaTeX‑matematikblocken som genereras automatiskt. + +## Steg 5: Verifiera resultatet (valfritt men rekommenderat) + +Det är lätt att missa ett subtilt konverteringsproblem, särskilt när ekvationer innehåller anpassade symboler. En snabb kontroll är att mata in den genererade `.txt`‑filen i en LaTeX‑kompilator (t.ex. `pdflatex`) och se om den kompileras utan fel. + +```bash +pdflatex -interaction=nonstopmode Math.txt +``` + +Om kompileringen lyckas har du effektivt **convert word equations to latex** och **convert docx to txt** i ett svep. Om du får fel, leta efter meddelanden om odefinierade kommandon—det indikerar ofta en ekvationsfunktion som Aspose.Words inte kan översätta (t.ex. vissa matrisnotationer). I sådana fall kan du falla tillbaka på `OfficeMathExportMode.MathML` och efterbehandla MathML till LaTeX med ett annat verktyg. + +## Vanliga fallgropar & hur du undviker dem + +| Problem | Varför det händer | Lösning | +|-------|-------------------|--------| +| Saknade typsnitt | Aspose.Words behöver typsnittet för att rendera symboler korrekt. | Installera det saknade typsnittet på maskinen eller bädda in det i .docx‑filen. | +| Komplexa ekvationer exporteras inte | Vissa nyare Office Math‑funktioner är ännu inte mappade till LaTeX. | Använd `OfficeMathExportMode.MathML` och konvertera sedan med ett MathML‑till‑LaTeX‑bibliotek. | +| Extra tomma rader | Vanlig‑text‑spararen bevarar styckebrytningar, vilket kan lägga till onödig vitrymd. | Sätt `txtOptions.AddBidiMarks = false` eller efterbehandla filen med ett enkelt skript. | + +## Fullt fungerande exempel (Klar att kopiera och klistra in) + +Nedan är hela programmet, redo att kompileras. Ersätt `YOUR_DIRECTORY` med mappen som innehåller din `input.docx`. + +```csharp +using System; +using Aspose.Words; +using Aspose.Words.Saving; + +namespace DocxToTxtWithLatex +{ + class Program + { + static void Main() + { + try + { + // Load the source document + Document doc = new Document(@"C:\Docs\input.docx"); + + // Configure save options: export equations as LaTeX + TxtSaveOptions txtOptions = new TxtSaveOptions + { + OfficeMathExportMode = OfficeMathExportMode.LaTeX, + PreserveTableLayout = true, + AddBidiMarks = false + }; + + // Save as plain‑text + string outputPath = @"C:\Docs\Math.txt"; + doc.Save(outputPath, txtOptions); + + Console.WriteLine($"Successfully converted DOCX to TXT. Output at: {outputPath}"); + } + catch (Exception ex) + { + Console.Error.WriteLine($"Conversion failed: {ex.Message}"); + } + } + } +} +``` + +Att köra detta program kommer att **save word as txt** samtidigt som varje Office Math‑block omvandlas till LaTeX, vilket ger dig en ren, sökbar vanlig‑text‑fil. + +## Nästa steg & relaterade ämnen + +- **Batch conversion:** Packa in ovanstående logik i en `foreach`‑loop för att bearbeta en hel mapp med .docx‑filer. +- **Combine with PDF generation:** Efter att du har LaTeX‑snuttarna, mata in dem i en PDF‑pipeline (t.ex. `PdfSharp` + `MiKTeX`) för att skapa PDF‑rapporter. +- **Export equations as latex** för andra format: Aspose.Words stöder också `SaveFormat.Markdown`, vilket kan bädda in LaTeX automatiskt. +- **Performance tuning:** För enorma dokument, återanvänd samma `TxtSaveOptions`‑instans och inaktivera onödiga funktioner som `AddBidiMarks`. + +--- + +### Bildexempel (Valfritt) + +Om du föredrar en visuell ledtråd, här är en skärmdump av utdatafilen i Notepad++. + +![convert docx to txt-utdata som visar LaTeX ekvationer](convert-docx-to-txt-output.png) + +*(Alt text: “convert docx to txt output showing LaTeX equations” – uppfyller huvudnyckelordskravet.)* + +## Slutsats + +Vi har just demonstrerat ett pålitligt sätt att **convert docx to txt** samtidigt som varje ekvation bevaras som ren LaTeX. Nyckeln är flaggan `OfficeMathExportMode.LaTeX`, som omvandlar Words proprietära matematikformat till något som alla LaTeX‑motorer förstår. Med hela kodexemplet ovan kan du **save word as txt**, **convert word to plain text**, och **export equations as latex** i ett enda, självständigt kör. + +Känn dig fri att experimentera—byt utdatafilens filändelse till `.md` för Markdown, eller integrera kodsnutten i en större dokument‑bearbetningspipeline. Om du stöter på några konstigheter, lämna en kommentar nedan; jag hjälper gärna till att felsöka. + +Lycka till med kodandet! + +{{< /blocks/products/pf/tutorial-page-section >}} +{{< /blocks/products/pf/main-container >}} +{{< /blocks/products/pf/main-wrap-class >}} +{{< blocks/products/products-backtop-button >}} \ No newline at end of file diff --git a/words/thai/net/ai-powered-document-processing/_index.md b/words/thai/net/ai-powered-document-processing/_index.md index 5a3df63e35..1bb5d64d39 100644 --- a/words/thai/net/ai-powered-document-processing/_index.md +++ b/words/thai/net/ai-powered-document-processing/_index.md @@ -41,7 +41,8 @@ - [การทำงานกับโมเดล AI](./working-with-ai-model/) | เรียนรู้วิธีใช้ Aspose.Words สำหรับ .NET เพื่อสรุปเอกสารด้วย AI ขั้นตอนง่ายๆ เพื่อเพิ่มประสิทธิภาพการจัดการเอกสาร - | [การทำงานกับโมเดล AI ของ Google](./working-with-google-ai-model/) ยกระดับการประมวลผลเอกสารของคุณด้วย Aspose.Words สำหรับ .NET และ Google AI เพื่อสร้างบทสรุปสั้นๆ ได้โดยไม่ต้องใช้ความพยายาม - | [การทำงานกับโมเดล AI แบบเปิด](./working-with-open-ai-model/) | ปลดล็อกการสรุปเอกสารอย่างมีประสิทธิภาพโดยใช้ Aspose.Words สำหรับ .NET พร้อมด้วยโมเดลอันทรงพลังของ OpenAI เจาะลึกคู่มือฉบับสมบูรณ์นี้เลยตอนนี้ - -| [การทำงานกับตัวเลือกการสรุป](./working-with-summarize-options/) | เรียนรู้การสรุปเอกสาร Word ได้อย่างมีประสิทธิภาพโดยใช้ Aspose.Words สำหรับ .NET พร้อมคำแนะนำทีละขั้นตอนในการบูรณาการโมเดล AI เพื่อรับข้อมูลเชิงลึกอย่างรวดเร็ว | +| [การทำงานกับตัวเลือกการสรุป](./working-with-summarize-options/) | เรียนรู้การสรุปเอกสาร Word ได้อย่างมีประสิทธิภาพโดยใช้ Aspose.Words สำหรับ .NET พร้อมคำแนะนำทีละขั้นตอนในการบูรณาการโมเดล AI เพื่อรับข้อมูลเชิงลึกอย่างรวดเร็ว - +| [เชื่อมต่อกับ LLM ภายในเครื่องใน C# – คู่มือการเขียนโปรแกรมฉบับสมบูรณ์](./connect-to-local-llm-in-c-complete-programming-guide/) | เรียนรู้วิธีเชื่อมต่อและใช้ LLM ภายในเครื่องด้วย C# ผ่าน Aspose.Words เพื่อประมวลผลเอกสารอัจฉริยะ - {{< /blocks/products/pf/tutorial-page-section >}} diff --git a/words/thai/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/_index.md b/words/thai/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/_index.md new file mode 100644 index 0000000000..77c9c03d3d --- /dev/null +++ b/words/thai/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/_index.md @@ -0,0 +1,282 @@ +--- +category: general +date: 2026-04-28 +description: เชื่อมต่อกับ LLM ภายในจาก C# และสั่งให้โมเดลภาษาขนาดใหญ่โหลดเอกสาร Word, + เรียกใช้ LLM ภายในและเขียนข้อความใหม่โดยอัตโนมัติ. มีโค้ดขั้นตอนโดยละเอียดรวมอยู่. +draft: false +keywords: +- connect to local llm +- prompt large language model +- load word document +- call local llm +- rewrite text automatically +language: th +og_description: เชื่อมต่อกับ LLM ภายในจาก C# และดูวิธีการส่งพรอมต์ให้โมเดลภาษาขนาดใหญ่ + โหลดเอกสาร Word เรียกใช้ LLM ภายใน และเขียนข้อความใหม่โดยอัตโนมัติภายในไม่กี่นาที. +og_title: เชื่อมต่อกับ LLM ภายในเครื่องใน C# – คู่มือการเขียนโปรแกรมฉบับสมบูรณ์ +tags: +- Aspose.Words +- C# +- LLM +- AI Automation +title: เชื่อมต่อกับ LLM ภายในเครื่องใน C# – คู่มือการเขียนโปรแกรมฉบับสมบูรณ์ +url: /th/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/ +--- + +{{< blocks/products/pf/main-wrap-class >}} +{{< blocks/products/pf/main-container >}} +{{< blocks/products/pf/tutorial-page-section >}} + +# เชื่อมต่อกับ Local LLM ใน C# – คู่มือการเขียนโปรแกรมฉบับเต็ม + +เคยต้องการ **เชื่อมต่อกับ local llm** จากแอป .NET และสงสัยว่าจะทำให้มันสื่อสารกับไฟล์ Word ได้อย่างไรไหม? คุณไม่ได้เป็นคนเดียว ในคู่มือนี้เราจะพาคุณผ่านกระบวนการทั้งหมด—เชื่อมต่อกับ local llm, **prompt large language model**, โหลดเอกสาร Word, **call local llm**, และสุดท้าย **rewrite text automatically**. เมื่อเสร็จคุณจะได้ตัวอย่างที่สามารถรันได้ซึ่งจะแปลงย่อหน้าใด ๆ ให้เป็นโทนทางการโดยไม่ต้องใช้คีย์ API ภายนอก. + +## สิ่งที่บทเรียนนี้ครอบคลุม + +เราจะเริ่มด้วยการติดตั้งแพคเกจ NuGet ที่จำเป็น จากนั้นเปิด endpoint ของ Local LLM อย่างง่าย (เช่น Ollama ที่พอร์ต 11434) หลังจากนั้นเราจะโหลดไฟล์ `.docx` ด้วย Aspose.Words ส่งย่อหน้าไปยัง LLM รับเวอร์ชันที่เขียนใหม่และเขียนกลับไปยังเอกสารเดียวกัน คุณยังจะได้เห็นวิธีจัดการกับปัญหาที่พบบ่อย—ย่อหน้าเป็น null, การทำลายแบบ async, และปัญหา encoding—เพื่อให้โค้ดทำงานในสภาพแวดล้อมการผลิต ไม่ใช่แค่การสาธิต + +### ข้อกำหนดเบื้องต้น + +- .NET 6.0 SDK หรือใหม่กว่า (คุณสามารถใช้ .NET 8 ได้เช่นกัน) +- Visual Studio 2022 หรือ VS Code พร้อมส่วนขยาย C# +- **Aspose.Words for .NET** (รุ่นทดลองฟรีใช้งานได้) +- LLM ที่โฮสต์ในเครื่องซึ่งรองรับสัญญา `/api/generate` (เช่น Ollama, LMStudio) +- ความคุ้นเคยพื้นฐานกับ async/await ใน C# + +> **เคล็ดลับมืออาชีพ:** หากคุณยังไม่ได้ติดตั้ง Ollama ให้รัน `ollama serve` และดึงโมเดลด้วย `ollama pull llama3` จุดเชื่อมต่อ HTTP เริ่มต้นจะเป็น `http://localhost:11434/api/generate`. + +--- + +## ขั้นตอนที่ 1: ติดตั้งแพคเกจที่จำเป็น + +ขั้นแรก ให้เพิ่มแพคเกจ NuGet ของ Aspose.Words และ Aspose.Words.AI ไปยังโปรเจกต์ของคุณ + +```bash +dotnet add package Aspose.Words +dotnet add package Aspose.Words.AI +``` + +ไลบรารีเหล่านี้ให้ความสามารถ **load word document** และ wrapper เบาที่ **call local llm** โดยไม่ต้องสร้างคำขอ HTTP ด้วยตนเอง + +--- + +## ขั้นตอนที่ 2: เชื่อมต่อกับ Local LLM Endpoint + +การเชื่อมต่อกับโมเดลที่โฮสต์ในเครื่องง่ายเพียงแค่สร้างอินสแตนซ์ของ `LocalLargeLanguageModel` ตัวสร้างคาดหวัง URL เต็มของ endpoint การสร้าง + +```csharp +using Aspose.Words.AI; +using Aspose.Words; +using System.Threading.Tasks; + +// Create a client that talks to the LLM running on localhost +var localLlm = new LocalLargeLanguageModel("http://localhost:11434/api/generate"); +``` + +ทำไมเราถึงห่อหุ้ม endpoint ไว้ในคลาส? `LocalLargeLanguageModel` จะจัดการการแปลงเป็น JSON, การลองใหม่, และการสตรีมผลตอบกลับให้คุณ—เพื่อให้คุณมุ่งเน้นที่ตรรกะของ prompt แทนการจัดการกับ `HttpClient` + +--- + +## ขั้นตอนที่ 3: โหลดเอกสาร Word ต้นฉบับ + +ต่อไป เรานำเอกสารเข้าสู่หน่วยความจำ Aspose.Words รองรับรูปแบบ Word เกือบทั้งหมด ดังนั้น `Document` จะทำการพาร์ส `input.docx` โดยไม่ต้องติดตั้ง Office + +```csharp +// Path to the source file – adjust as needed +string inputPath = Path.Combine(Environment.CurrentDirectory, "input.docx"); + +// Load the document; throws if the file is missing or corrupted +Document sourceDocument = new Document(inputPath); +``` + +หากคุณต้องทำงานกับสตรีม (เช่นไฟล์ที่อัปโหลดผ่าน ASP.NET) เพียงแทนที่เส้นทางไฟล์ด้วย `MemoryStream` แล้วส่งให้กับตัวสร้าง `Document` + +--- + +## ขั้นตอนที่ 4: ดึงข้อความย่อหน้าปัจจุบัน + +เราจะใช้ `DocumentBuilder` เพื่อเดินทางในเอกสาร ในตัวอย่างนี้เราจะแก้ไข **ย่อหน้าแรก** แต่คุณสามารถวนลูปผ่าน `sourceDocument.GetChildNodes(NodeType.Paragraph, true)` เพื่อประมวลผลหลายย่อหน้าได้ + +```csharp +// Builder gives us a cursor inside the document +DocumentBuilder docBuilder = new DocumentBuilder(sourceDocument); + +// Grab the text of the paragraph where the builder is currently positioned +string originalParagraph = docBuilder.CurrentParagraph?.GetText() ?? string.Empty; + +// Safety check – avoid sending empty strings to the LLM +if (string.IsNullOrWhiteSpace(originalParagraph)) +{ + Console.WriteLine("No paragraph found at the current cursor position."); + return; +} +``` + +ตัวดำเนินการ `?.` ป้องกัน `NullReferenceException` หากเอกสารว่างเปล่า นี่เป็นหนึ่งใน **edge cases** ที่ทำให้ผู้เริ่มต้นหลง + +--- + +## ขั้นตอนที่ 5: Prompt LLM เพื่อเขียนย่อหน้าใหม่ + +ตอนนี้เราจริง ๆ แล้ว **prompt large language model** คำสั่งเป็นภาษาอังกฤษธรรมดา; wrapper จะส่งเป็น JSON ไปยัง endpoint ในเครื่อง + +```csharp +// Build a friendly instruction for the model +string prompt = $"Rewrite the following sentence in a more formal tone:\n{originalParagraph}"; + +// Await the model's response – this is an async call +string rewrittenParagraph = await localLlm.PromptAsync(prompt); +``` + +ทำไมต้องวางรูปแบบคำขอแบบนี้? LLM จะตอบสนองดีที่สุดต่อคำสั่งที่ชัดเจนและทำงานเดียว การเพิ่มบรรทัดใหม่หลังเครื่องหมายโคลอนจะแยกคำสั่งออกจากเนื้อหา ลดความเป็นไปได้ที่โมเดลจะเอาคำสั่งกลับมา + +**ผลลัพธ์ที่คาดหวัง** – หาก `originalParagraph` มีค่าเป็น `"Hey, what's up?"` LLM อาจคืนค่า: + +> “Good day, how may I assist you?” + +คุณสามารถตรวจสอบผลลัพธ์โดยการพิมพ์ออกมาดู: + +```csharp +Console.WriteLine("Original: " + originalParagraph); +Console.WriteLine("Rewritten: " + rewrittenParagraph); +``` + +--- + +## ขั้นตอนที่ 6: แทรกข้อความที่เขียนใหม่กลับเข้าไปในเอกสาร + +เมื่อได้ข้อความใหม่แล้ว เราจะแทนที่ย่อหน้าเดิม `DocumentBuilder.Writeln` จะเขียนบรรทัดใหม่และเลื่อนเคอร์เซอร์ไปข้างหน้า ซึ่งเหมาะสำหรับการต่อท้าย หากคุณต้องการ *แทนที่* ย่อหน้าเดิมโดยตรง คุณสามารถใช้ `docBuilder.CurrentParagraph.RemoveAllChildren()` ก่อนเขียน + +```csharp +// Option A – Append a new paragraph (keeps the original) +docBuilder.Writeln(rewrittenParagraph); + +// Option B – Replace the existing paragraph (uncomment to use) +// docBuilder.CurrentParagraph.RemoveAllChildren(); +// docBuilder.CurrentParagraph.AppendChild(new Run(docBuilder.Document, rewrittenParagraph)); +``` + +ทั้งสองวิธีถูกแสดงไว้เพื่อให้คุณเลือกตามกระบวนการทำงานของคุณ + +--- + +## ขั้นตอนที่ 7: บันทึกเอกสารที่อัปเดต + +สุดท้าย เราจะบันทึกการเปลี่ยนแปลงลงไฟล์ใหม่ Aspose.Words จะเลือกฟอร์แมตโดยอัตโนมัติตามส่วนขยายของไฟล์ + +```csharp +string outputPath = Path.Combine(Environment.CurrentDirectory, "output.docx"); +sourceDocument.Save(outputPath); + +Console.WriteLine($"Document saved to {outputPath}"); +``` + +เปิด `output.docx` ด้วย Word แล้วคุณจะเห็นย่อหน้าตอนนี้อ่านในโทนทางการ + +--- + +## ตัวอย่างทำงานเต็มรูปแบบ + +ด้านล่างเป็น **โปรแกรมที่สมบูรณ์และเป็นอิสระ** คัดลอกและวางลงในโปรเจกต์คอนโซล, รีสโตร์แพคเกจ NuGet, แล้วรัน—ไม่ต้องตั้งค่าเพิ่มเติมนอกจากต้องมี Local LLM ที่ทำงานอยู่ + +```csharp +using Aspose.Words.AI; +using Aspose.Words; +using System; +using System.IO; +using System.Threading.Tasks; + +class Program +{ + static async Task Main() + { + // ------------------------------------------------- + // Step 1: Connect to the locally hosted LLM endpoint + // ------------------------------------------------- + var localLlm = new LocalLargeLanguageModel("http://localhost:11434/api/generate"); + + // ------------------------------------------------- + // Step 2: Load the source Word document + // ------------------------------------------------- + string inputPath = Path.Combine(Environment.CurrentDirectory, "input.docx"); + Document sourceDocument = new Document(inputPath); + + // ------------------------------------------------- + // Step 3: Retrieve the text of the current paragraph + // ------------------------------------------------- + DocumentBuilder docBuilder = new DocumentBuilder(sourceDocument); + string originalParagraph = docBuilder.CurrentParagraph?.GetText() ?? string.Empty; + + if (string.IsNullOrWhiteSpace(originalParagraph)) + { + Console.WriteLine("No paragraph found at the current cursor position."); + return; + } + + // ------------------------------------------------- + // Step 4: Ask the LLM to rewrite the paragraph in a formal tone + // ------------------------------------------------- + string prompt = $"Rewrite the following sentence in a more formal tone:\n{originalParagraph}"; + string rewrittenParagraph = await localLlm.PromptAsync(prompt); + + // ------------------------------------------------- + // Step 5: Insert the rewritten text back into the document + // ------------------------------------------------- + docBuilder.Writeln(rewrittenParagraph); + + // ------------------------------------------------- + // Step 6: Save the updated document + // ------------------------------------------------- + string outputPath = Path.Combine(Environment.CurrentDirectory, "output.docx"); + sourceDocument.Save(outputPath); + + Console.WriteLine("Original paragraph:"); + Console.WriteLine(originalParagraph); + Console.WriteLine("\nRewritten paragraph:"); + Console.WriteLine(rewrittenParagraph); + Console.WriteLine($"\nDocument saved to {outputPath}"); + } +} +``` + +### สิ่งที่คาดว่าจะเกิดขึ้นเมื่อคุณรัน + +1. คอนโซลจะแสดงย่อดั้งเดิมและย่อหน้าใหม่ที่เขียนใหม่ +2. `output.docx` ปรากฏข้าง `input.docx` +3. การเปิดไฟล์จะแสดงย่อหน้าใหม่ในโทนทางการที่แทรกหลังย่อดั้งเดิม (หรือแทนที่ หากคุณเปลี่ยนเป็นโค้ดทางเลือก) + +--- + +## การจัดการ Edge Cases ที่พบบ่อย + +| Situation | Solution | +|-----------|----------| +| **ย่อหน้าเป็นค่าว่างหรือมีเพียงช่องว่าง** | ตรวจสอบ `string.IsNullOrWhiteSpace` ก่อนทำ prompt (ดูขั้นตอน 3) | +| **LLM ส่งคืนข้อผิดพลาดหรือสตริงว่าง** | ห่อ `PromptAsync` ด้วย `try/catch` แล้วใช้ข้อความเดิมเป็นสำรอง | +| **หลายย่อหน้าต้องการการเขียนใหม่** | วนลูปผ่าน `sourceDocument.GetChildNodes(NodeType.Paragraph, true)` และใช้ตรรกะ prompt เดียวกัน | +| **เอกสารขนาดใหญ่ทำให้เกิดความล่าช้า** | จัดกลุ่มย่อหน้าและส่งในคำขอเดียว (prompt สูงสุด 4 KB ต่อการเรียก) | +| **อักขระที่ไม่ใช่ ASCII เกิดการบิดเบือน** | ตรวจสอบให้ endpoint ของ LLM ใช้ UTF-8 (โมเดลสมัยใหม่ส่วนใหญ่ทำเช่นนั้น) | + +--- + +## ขั้นตอนต่อไปและหัวข้อที่เกี่ยวข้อง + +- **Prompt large language model** ด้วยคำสั่งที่ละเอียดขึ้น (เช่น คู่มือสไตล์, ขีดจำกัดความยาว). +- ใช้ **call local llm** ใน Web API เพื่อเปิดเผยการทำงานอัตโนมัติของเอกสารเป็นบริการ. +- สำรวจ **load word document** ในสตรีมแบบขนานสำหรับสถานการณ์ที่ต้องการ throughput สูง. +- รวมวิธีนี้กับ **rewrite text automatically** เพื่อสร้างอีเมลจำนวนมากหรือมาตรฐานรายงาน. + +หากคุณต้องการเจาะลึกเพิ่มเติม ให้ดูเอกสารของ Aspose เกี่ยวกับ **document merging** และอ้างอิง API ของ Ollama สำหรับพารามิเตอร์การสุ่มแบบกำหนดเอง + +--- + +## สรุป + +เราได้แสดงให้คุณเห็นวิธี **connect to local llm** จาก C#, **prompt large language model**, **load word document**, **call local llm**, และ **rewrite text automatically**—ทั้งหมดในแอปคอนโซลที่สามารถรันได้หนึ่งเดียว รูปแบบนี้สามารถขยายได้: เปลี่ยน prompt, วนลูปผ่านย่อหน้า, หรือเปิดเผยตรรกะผ่าน endpoint ของ ASP.NET สิ่งที่สำคัญคือโมเดล AI ในเครื่องสามารถบูรณาการอย่างแน่นหนากับไลบรารีการประมวลผลเอกสารแบบคลาสสิก ทำให้คุณได้อัตโนมัติที่ทรงพลังโดยไม่ต้องออกจากสภาพแวดล้อม on‑prem ที่คุณเชื่อถือ + +มีคำถามเกี่ยวกับ threading หรือไม่, + +{{< /blocks/products/pf/tutorial-page-section >}} +{{< /blocks/products/pf/main-container >}} +{{< /blocks/products/pf/main-wrap-class >}} +{{< blocks/products/products-backtop-button >}} \ No newline at end of file diff --git a/words/thai/net/programming-with-markdownsaveoptions/_index.md b/words/thai/net/programming-with-markdownsaveoptions/_index.md index 729f669cef..0da62d0f9b 100644 --- a/words/thai/net/programming-with-markdownsaveoptions/_index.md +++ b/words/thai/net/programming-with-markdownsaveoptions/_index.md @@ -65,6 +65,9 @@ ### [แปลง Word เป็น Markdown ใน C# – คู่มือเต็มพร้อมการสกัดรูปภาพ](./convert-word-to-markdown-in-c-full-guide-with-image-extracti/) +### [เส้นทางรูปภาพแบบสัมพันธ์ใน Markdown – แปลง Word เป็น Markdown](./markdown-image-relative-path-convert-word-to-markdown/) +เรียนรู้วิธีกำหนดเส้นทางรูปภาพแบบสัมพันธ์เมื่อแปลงไฟล์ Word เป็น Markdown ด้วย Aspose.Words สำหรับ .NET + {{< /blocks/products/pf/tutorial-page-section >}} diff --git a/words/thai/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/_index.md b/words/thai/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/_index.md new file mode 100644 index 0000000000..684c7d6fde --- /dev/null +++ b/words/thai/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/_index.md @@ -0,0 +1,189 @@ +--- +category: general +date: 2026-04-28 +description: เรียนรู้วิธีตั้งเส้นทางภาพแบบสัมพัทธ์ใน Markdown เมื่อคุณแปลง Word เป็น + Markdown, ดึงภาพจาก Word และสร้างโฟลเดอร์ resources สำหรับภาพที่ส่งออก +draft: false +keywords: +- markdown image relative path +- convert word to markdown +- extract images from word +- create resources folder +- export images from docx +language: th +og_description: กำหนดเส้นทางภาพแบบสัมพันธ์ใน Markdown ขณะแปลงไฟล์ Word เป็น Markdown, + ดึงภาพจาก Word, และสร้างโฟลเดอร์ resources สำหรับภาพที่ส่งออก +og_title: เส้นทางสัมพัทธ์ของรูปภาพ markdown – แปลง Word เป็น Markdown +tags: +- Aspose.Words +- C# +- Markdown +- Image Export +title: เส้นทางสัมพัทธ์ของรูปภาพใน Markdown – แปลง Word เป็น Markdown +url: /th/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/ +--- + +{{< blocks/products/pf/main-wrap-class >}} +{{< blocks/products/pf/main-container >}} +{{< blocks/products/pf/tutorial-page-section >}} + +# markdown image relative path – แปลง Word เป็น Markdown + +เคยต้องการ **markdown image relative path** ขณะ **convert Word to markdown** หรือไม่? คุณไม่ได้เป็นคนเดียว นักพัฒนาส่วนใหญ่เจอปัญหาเมื่อ Markdown ที่สร้างขึ้นชี้ไปที่ภาพในโฟลเดอร์แบน ทำให้โครงสร้างลิงก์สัมพัทธ์ที่คุณคาดหวังในเว็บไซต์สถิตหรือรีโป GitHub แตกหัก + +ในบทแนะนำนี้เราจะพาคุณผ่านโซลูชันครบวงจรแบบ end‑to‑end ที่ **extracts images from Word**, **creates a resources folder**, และเขียนทับการอ้างอิงภาพเพื่อให้ใช้ *markdown image relative path* ที่สะอาดตา เมื่อเสร็จคุณจะได้ไฟล์ `.md` พร้อมเผยแพร่และไดเรกทอรี `Resources` ที่จัดระเบียบอย่างเป็นระเบียบซึ่งบรรจุภาพทั้งหมดที่ดึงจากไฟล์ `.docx` ดั้งเดิม + +> **What you’ll get:** โปรแกรม C# เดียว (ไม่มีสคริปต์ภายนอก) คำอธิบายที่ชัดเจนเกี่ยวกับ *why* แต่ละส่วนสำคัญ, และเคล็ดลับเชิงปฏิบัติที่คุณสามารถคัดลอก‑วางไปใช้ในโปรเจกต์ของคุณเอง. + +--- + +## ข้อกำหนดเบื้องต้น + +- **.NET 6.0** หรือใหม่กว่า (คุณสามารถกำหนดเป้าหมายเป็น .NET Framework 4.7+ ได้เช่นกัน, แต่ .NET 6 เป็นจุดที่เหมาะสมสำหรับโปรเจกต์ใหม่). +- **Aspose.Words for .NET** (แพคเกจ NuGet ล่าสุด ณ เวลาที่เขียน, เวอร์ชัน 23.12). ติดตั้งด้วย: + ```bash + dotnet add package Aspose.Words + ``` +- เอกสาร Word ที่มีภาพจริง ๆ — สมมติว่าไฟล์ชื่อ `WithImages.docx`. +- โฟลเดอร์ที่คุณต้องการให้ markdown ผลลัพธ์และภาพอยู่, เช่น `C:\Projects\MarkdownExport`. + +ไม่ต้องการไลบรารีเพิ่มเติม; ส่วนอื่นทั้งหมดจัดการโดย Aspose.Words. + +## ขั้นตอนที่ 1: โหลดเอกสาร Word ต้นฉบับ (จุดเริ่มต้นสำหรับ convert word to markdown) + +```csharp +using System; +using System.IO; +using Aspose.Words; +using Aspose.Words.Saving; + +class Program +{ + static void Main() + { + // Adjust the path to point at your own .docx file. + string sourcePath = @"C:\Projects\MarkdownExport\WithImages.docx"; + + // Load the document – this is where Aspose.Words parses the Word file. + Document doc = new Document(sourcePath); + + // The rest of the workflow follows… + } +} +``` + +*Why this matters:* การโหลดเอกสารทำให้เราสามารถเข้าถึงโครงสร้าง node ภายใน, ซึ่งรวมถึงส่วนของภาพที่เราต้อง **export images from docx** ในภายหลัง หากการโหลดล้มเหลว ขั้นตอนต่อ ๆ ไปจะไม่ทำงาน ดังนั้นตรวจสอบเส้นทางและสิทธิ์ไฟล์อีกครั้ง. + +## ขั้นตอนที่ 2: กำหนดค่า `MarkdownSaveOptions` ด้วย callback แบบกำหนดเอง (หัวใจของ create resources folder) + +`ResourceSavingCallback` ช่วยให้เราสามารถแทรกแซงทุกครั้งที่ Aspose.Words ต้องการเขียนไฟล์ภาพ ภายใน callback เราจะ **create a Resources sub‑folder** และปรับการอ้างอิงเพื่อให้ markdown ที่สร้างใช้ *markdown image relative path*. + +```csharp +// Inside Main(), after loading the document: +string outputFolder = @"C:\Projects\MarkdownExport"; +string resourcesFolder = Path.Combine(outputFolder, "Resources"); + +// Make sure the folder exists before we start saving anything. +Directory.CreateDirectory(resourcesFolder); + +// Set up the Markdown save options. +MarkdownSaveOptions mdOptions = new MarkdownSaveOptions +{ + // Hook that runs for every image resource. + ResourceSavingCallback = new MyMarkdownResourceCallback(resourcesFolder) +}; + +// Save the document as Markdown. +string markdownPath = Path.Combine(outputFolder, "Doc.md"); +doc.Save(markdownPath, mdOptions); +``` + +สังเกตว่าเราได้ส่ง `resourcesFolder` เข้าไปในคอนสตรัคเตอร์ของ callback—ทำให้เส้นทางโฟลเดอร์ยืดหยุ่นและหลีกเลี่ยงการ hard‑coding สตริงทั่วทั้งโค้ด. + +## ขั้นตอนที่ 3: Implement the callback that **creates resources folder** and rewrites the path + +```csharp +/// +/// Handles image extraction and path rewriting for markdown export. +/// +class MyMarkdownResourceCallback : IResourceSavingCallback +{ + private readonly string _resourcesFolder; + + public MyMarkdownResourceCallback(string resourcesFolder) + { + _resourcesFolder = resourcesFolder; + } + + public void ResourceSaving(ResourceSavingArgs args) + { + // 1️⃣ Build the full file system path where the image will be stored. + string targetPath = Path.Combine(_resourcesFolder, args.ResourceFileName); + + // 2️⃣ Ensure the directory exists (in case Aspose creates sub‑folders). + Directory.CreateDirectory(Path.GetDirectoryName(targetPath)); + + // 3️⃣ Write the image stream to disk. + using (FileStream fileStream = File.Create(targetPath)) + { + args.Stream.CopyTo(fileStream); + } + + // 4️⃣ Update the markdown reference to use a relative path. + // This is the crucial line that gives us the markdown image relative path. + args.ResourceFileName = Path.Combine("Resources", args.ResourceFileName); + } +} +``` + +*Why this works:* `args.Stream` มีไบต์ของภาพดิบ. โดยคัดลอกไปยังไฟล์ในโฟลเดอร์ `Resources` ของเรา เรา **export images from docx** อย่างปลอดภัย จากนั้นเราจะแทนที่ `args.ResourceFileName` ด้วย URL สัมพัทธ์ (`Resources/image.png`). เมื่อ Aspose.Words เขียน markdown ต่อไป มันจะใส่สตริงนั้นลงไป ทำให้เราได้ *markdown image relative path* ที่ต้องการ. + +## ขั้นตอนที่ 4: ตรวจสอบ Markdown ที่สร้างขึ้น (รูปแบบผลลัพธ์สุดท้าย) + +เปิด `Doc.md` ด้วยโปรแกรมแก้ไขข้อความใดก็ได้ คุณควรเห็นสิ่งที่คล้ายกับ: + +```markdown +# Sample Heading + +Here is an inline picture: + +![Image 0](Resources/Image_0.png) + +And a picture inside a table: + +![Image 1](Resources/Image_1.jpg) +``` + +ส่วนสำคัญคือการอ้างอิงภาพแต่ละรายการชี้ไปที่ `Resources/...` – นั่นคือ **markdown image relative path** ที่เราต้องการ. + +![ตัวอย่าง markdown image relative path](example.png "ตัวอย่าง markdown image relative path") + +*Tip:* หากคุณเปิด markdown ด้วยตัวดูที่เคารพลิงก์สัมพัทธ์ (preview ของ VS Code, GitHub, หรือ static site generator), ภาพจะถูกแสดงอย่างถูกต้องโดยไม่ต้องตั้งค่าเพิ่มเติม. + +## ขั้นตอนที่ 5: ข้อผิดพลาดทั่วไปและ pro‑tips + +| ปัญหา | สาเหตุ | วิธีแก้ไข | +|-------|--------|-----------| +| ภาพถูกเก็บไว้ในโฟลเดอร์รากแทน `Resources` | Callback ไม่ได้ถูกแนบหรือ `args.ResourceFileName` ไม่ได้ถูกเขียนทับ | ตรวจสอบให้แน่ใจว่าได้ตั้งค่า `ResourceSavingCallback` **ก่อน** เรียก `doc.Save` | +| ชื่อไฟล์มีอักขระที่ไม่ถูกต้อง | Word บางครั้งตั้งชื่อภาพด้วยช่องว่างหรือสัญลักษณ์ Unicode | ใช้ `Path.GetInvalidFileNameChars()` เพื่อลบอักขระที่ไม่ถูกต้องจาก `args.ResourceFileName` ภายใน callback | +| เอกสารขนาดใหญ่ใช้เวลาประมวลผลนาน | แต่ละภาพถูกเขียนแบบ synchronous | เปลี่ยนเป็น I/O แบบ asynchronous (`await args.Stream.CopyToAsync(fileStream)`) หากคุณใช้ .NET 6+ และต้องการประสิทธิภาพ | +| เส้นทางสัมพัทธ์เสียหายเมื่อย้าย markdown | เส้นทางเป็นสัมพัทธ์ต่อที่ตั้งไฟล์ markdown | เก็บ `Doc.md` และโฟลเดอร์ `Resources` ไว้ด้วยกัน, หรือปรับ callback ให้ใช้ prefix สัมพัทธ์อื่น (เช่น `../assets`) | + +## ขั้นตอนที่ 6: ขยายโซลูชัน (ถ้าต้องการการควบคุมเพิ่มเติม?) + +- **Multiple output formats:** แทนที่ `MarkdownSaveOptions` ด้วย `HtmlSaveOptions` หรือ `PdfSaveOptions` ขณะยังคงใช้ callback เดียว—Aspose.Words จะเรียกใช้มันสำหรับทุกภาพโดยไม่คำนึงถึงรูปแบบ. +- **Custom image naming:** หากต้องการเปลี่ยนชื่อภาพ (เช่น `figure-01.png`), แก้ไข `args.ResourceFileName` ภายใน callback ก่อนเขียนไฟล์. +- **Embedding images as Base64:** ตั้งค่า `args.ResourceFileName` เป็น data URI (`data:image/png;base64,...`) และข้ามการเขียนไฟล์. วิธีนี้สะดวกสำหรับการส่งออก markdown เป็นไฟล์เดียว. + +## สรุป + +คุณมีโปรแกรม C# ที่ทำงานเต็มรูปแบบที่ **converts Word to markdown**, **extracts images from word**, **creates a resources folder**, และรับประกัน **markdown image relative path** ที่สะอาดสำหรับทุกภาพ โค้ดเป็นอิสระ, ทำงานกับเวอร์ชันล่าสุดของ Aspose.Words, และสามารถนำไปใส่ในโปรเจกต์ .NET ใดก็ได้ด้วยความพยายามน้อย. + +ขั้นตอนต่อไป? ลองนำ markdown ที่สร้างไปใช้กับ static site generator เช่น Hugo หรือ Jekyll, หรือทดลองปรับ callback เพื่อฝังภาพโดยตรงเป็นสตริง Base64. หากเจอกรณีขอบเช่น ภาพ SVG หรือไฟล์ขนาดใหญ่มาก ให้กลับไปดูตาราง “ข้อผิดพลาดทั่วไป”; การปรับเล็กน้อยมักแก้ปัญหาได้. + +ขอให้เขียนโค้ดอย่างสนุกสนาน, และขอให้ markdown ของคุณชี้ไปยังโฟลเดอร์ที่ถูกต้องเสมอ! + +{{< /blocks/products/pf/tutorial-page-section >}} +{{< /blocks/products/pf/main-container >}} +{{< /blocks/products/pf/main-wrap-class >}} +{{< blocks/products/products-backtop-button >}} \ No newline at end of file diff --git a/words/thai/net/programming-with-officemath/_index.md b/words/thai/net/programming-with-officemath/_index.md index babd1398cc..1354ccba43 100644 --- a/words/thai/net/programming-with-officemath/_index.md +++ b/words/thai/net/programming-with-officemath/_index.md @@ -23,6 +23,7 @@ - [สมการคณิตศาสตร์](./math-equations/) | เรียนรู้วิธีการกำหนดค่าสมการทางคณิตศาสตร์ในเอกสาร Word โดยใช้ Aspose.Words สำหรับ .NET คำแนะนำทีละขั้นตอนพร้อมตัวอย่าง คำถามที่พบบ่อย และอื่นๆ | - [บันทึก docx เป็น txt – ส่งออก Word Math ไปเป็น LaTeX ด้วย C#](./save-docx-as-txt-export-word-math-to-latex-with-c/) | เรียนรู้วิธีบันทึกไฟล์ docx เป็น txt และส่งออกสมการ Word Math เป็น LaTeX ด้วย C# | - [บันทึกเอกสารเป็น Txt – ส่งออก Word Math เป็น LaTeX ใน C#](./save-document-as-txt-export-word-math-to-latex-in-c/) | | +- [แปลง DOCX เป็น TXT – ส่งออกสมการ Word เป็น LaTeX ใน C#](./convert-docx-to-txt-export-word-equations-to-latex-in-c/) | เรียนรู้วิธีแปลงไฟล์ DOCX เป็น TXT และส่งออกสมการ Word เป็น LaTeX ด้วย C# | {{< /blocks/products/pf/tutorial-page-section >}} diff --git a/words/thai/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/_index.md b/words/thai/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/_index.md new file mode 100644 index 0000000000..7fdf7b7fdc --- /dev/null +++ b/words/thai/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/_index.md @@ -0,0 +1,211 @@ +--- +category: general +date: 2026-04-28 +description: แปลง DOCX เป็น TXT และส่งออกสมการ Word ไปเป็น LaTeX ด้วย Aspose.Words + เรียนรู้วิธีบันทึก Word เป็น TXT และจัดการกับวัตถุคณิตศาสตร์ในไม่กี่ขั้นตอน. +draft: false +keywords: +- convert docx to txt +- convert word equations to latex +- convert word to plain text +- save word as txt +- export equations as latex +language: th +og_description: แปลง DOCX เป็น TXT และส่งออกสมการ Word เป็น LaTeX ด้วยสคริปต์ C# ง่าย + ๆ พร้อมคู่มือเต็ม, โค้ด, และเคล็ดลับ. +og_title: แปลง DOCX เป็น TXT – ส่งออกสมการ Word ไปยัง LaTeX +tags: +- C# +- Aspose.Words +- Document Conversion +title: แปลง DOCX เป็น TXT – ส่งออกสมการ Word ไปเป็น LaTeX ด้วย C# +url: /th/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/ +--- + +{{< blocks/products/pf/main-wrap-class >}} +{{< blocks/products/pf/main-container >}} +{{< blocks/products/pf/tutorial-page-section >}} + +# แปลง DOCX เป็น TXT – ส่งออกสมการ Word เป็น LaTeX + +เคยต้อง **convert docx to txt** แต่กังวลว่าสมการในไฟล์ Word ของคุณจะกลายเป็นข้อความยุ่งเหยิงหรือไม่? คุณไม่ได้เป็นคนเดียว ในหลายโครงการด้านวิศวกรรมหรือการศึกษา เอกสารต้นฉบับอยู่ในรูปแบบ .docx แต่เครื่องมือต่อไปมักเข้าใจแค่ plain‑text หรือ LaTeX ข่าวดีคือ ด้วยไม่กี่บรรทัดของ C# และ Aspose.Words คุณสามารถ **convert docx to txt** *และ* รักษาสมการทุกสมการให้เป็นโค้ด LaTeX ที่สะอาดได้ + +ในบทแนะนำนี้เราจะเดินผ่านกระบวนการทั้งหมด: โหลดไฟล์ .docx, ตั้งค่าตัวเลือกการบันทึกเพื่อให้ Office Math กลายเป็น LaTeX, และสุดท้ายเขียนผลลัพธ์ลงไฟล์ .txt เมื่อเสร็จคุณจะรู้วิธี **save word as txt**, **convert word to plain text**, และ **export equations as latex** โดยไม่ต้องค้นหาในเอกสาร API + +## สิ่งที่คุณจะได้เรียนรู้ + +- การเรียก API ที่จำเป็นเพื่อ **convert docx to txt** พร้อมคงสมการไว้ +- ทำไมการเลือก `OfficeMathExportMode.LaTeX` ถึงเป็นวิธีที่แนะนำสำหรับ **convert word equations to latex** +- วิธีจัดการกับกรณีขอบที่พบบ่อย เช่น ฟอนต์หายหรือฟีเจอร์สมการที่ไม่รองรับ +- โปรแกรม C# เต็มรูปแบบพร้อมรันที่คุณสามารถนำไปใส่ในโครงการ .NET ใดก็ได้ + +### ข้อกำหนดเบื้องต้น + +- .NET 6.0 หรือใหม่กว่า (โค้ดนี้ทำงานบน .NET Framework 4.7+ ด้วย) +- ไลเซนส์สำหรับ Aspose.Words for .NET (รุ่นทดลองฟรีใช้เพื่อประเมินผลได้) +- เอกสาร Word (`input.docx`) ที่มีอย่างน้อยหนึ่ง Office Math object + +ถ้าคุณมีทั้งหมดนี้แล้ว ไปเริ่มกันเลย + +## ขั้นตอนที่ 1: ติดตั้ง Aspose.Words + +ก่อนจะรันโค้ดใด ๆ คุณต้องมีไลบรารีนี้ เปิดเทอร์มินัลในโฟลเดอร์โปรเจกต์ของคุณและรัน: + +```bash +dotnet add package Aspose.Words +``` + +คำสั่งนี้จะดึงเวอร์ชันล่าสุดที่เสถียร (ณ 2026‑04‑28 v24.12) ไม่ต้องใช้ DLL เพิ่มเติม + +## ขั้นตอนที่ 2: โหลดเอกสารต้นฉบับ + +สิ่งแรกที่เราทำคืออ่านไฟล์ .docx เข้าไปในอ็อบเจกต์ `Document` อ็อบเจกต์นี้ให้การเข้าถึงโครงสร้างไฟล์ทั้งหมด รวมถึงข้อความ รูปภาพ และวัตถุคณิตศาสตร์ + +```csharp +using Aspose.Words; +using Aspose.Words.Saving; + +class Program +{ + static void Main() + { + // Step 2: Load the source document + Document doc = new Document(@"YOUR_DIRECTORY\input.docx"); +``` + +> **ทำไมเรื่องนี้สำคัญ:** การโหลดเอกสารสร้างการแสดงผลในหน่วยความจำ ดังนั้นต่อมาเราจึงสามารถปรับวิธีการเขียนแต่ละองค์ประกอบออกได้ หากไฟล์ไม่พบ Aspose จะโยน `FileNotFoundException` ซึ่งคุณอาจต้องจับในโค้ด production + +## ขั้นตอนที่ 3: ตั้งค่า TXT Save Options สำหรับ LaTeX Math + +โดยค่าเริ่มต้น `Document.Save` จะเขียนเป็น plain text และ **ทิ้ง** Office Math ไป เพื่อคงสมการเหล่านั้น เราตั้งค่า `OfficeMathExportMode` เป็น `LaTeX` ซึ่งบอกให้ตัวแปลงแปลงสมการแต่ละอันเป็นรูปแบบ LaTeX ที่สอดคล้องกัน + +```csharp + // Step 3: Configure TXT save options to export Office Math as LaTeX + TxtSaveOptions txtOptions = new TxtSaveOptions + { + OfficeMathExportMode = OfficeMathExportMode.LaTeX, + // Optional: preserve line breaks as they appear in the original Word file + PreserveTableLayout = true + }; +``` + +> **เคล็ดลับ:** หากคุณต้องการเพียงอักขระ Unicode ดิบของสมการ (เช่นเพื่อดูตัวอย่างอย่างเร็ว) คุณสามารถใช้ `OfficeMathExportMode.Text` แต่สำหรับสายงานวิทยาศาสตร์ส่วนใหญ่ `LaTeX` คือมาตรฐานทองคำเพราะเข้าใจได้โดยโปรเซสเซอร์ LaTeX ทุกตัว + +## ขั้นตอนที่ 4: บันทึกเอกสารเป็น Plain‑Text + +ตอนนี้เราจะเขียนเนื้อหาที่แปลงแล้วลงไฟล์ `.txt` ไฟล์นี้จะมีย่อหน้าปกติ รายการหัวข้อย่อย และ—ขอบคุณขั้นตอนก่อนหน้า—ส่วนโค้ด LaTeX สำหรับทุกสมการ + +```csharp + // Step 4: Save the document as plain‑text using the configured options + doc.Save(@"YOUR_DIRECTORY\Math.txt", txtOptions); + } +} +``` + +เมื่อคุณเปิด `Math.txt` คุณจะเห็นอย่างนี้: + +``` +In this report we derive the quadratic formula: +\[ +x = \frac{-b \pm \sqrt{b^{2} - 4ac}}{2a} +\] + +The end. +``` + +สังเกตเครื่องหมาย `\[` … `\]` หรือไม่? นั่นคือบล็อกคณิตศาสตร์ LaTeX ที่สร้างโดยอัตโนมัติ + +## ขั้นตอนที่ 5: ตรวจสอบผลลัพธ์ (ไม่บังคับแต่แนะนำ) + +ง่ายต่อการพลาดปัญหาการแปลงเล็ก ๆ โดยเฉพาะเมื่อสมการมีสัญลักษณ์กำหนดเอง การตรวจสอบอย่างเร็วคือการส่งไฟล์ `.txt` ที่สร้างไปยังคอมไพเลอร์ LaTeX (เช่น `pdflatex`) แล้วดูว่าคอมไพล์สำเร็จหรือไม่ + +```bash +pdflatex -interaction=nonstopmode Math.txt +``` + +หากการคอมไพล์สำเร็จ คุณก็ได้ **convert word equations to latex** และ **convert docx to txt** ไปพร้อมกัน หากเจอข้อผิดพลาด ให้มองหาข้อความเกี่ยวกับคำสั่งที่ไม่ได้กำหนด—มักบ่งบอกว่ามีฟีเจอร์สมการที่ Aspose.Words ไม่สามารถแปลงได้ (เช่นบางรูปแบบเมทริกซ์) ในกรณีนั้นคุณสามารถสลับไปใช้ `OfficeMathExportMode.MathML` แล้วแปลง MathML เป็น LaTeX ด้วยเครื่องมืออื่น + +## ข้อผิดพลาดทั่วไป & วิธีหลีกเลี่ยง + +| Issue | Why it Happens | Fix | +|-------|----------------|-----| +| Missing fonts | Aspose.Words ต้องการฟอนต์เพื่อเรนเดอร์สัญลักษณ์อย่างถูกต้อง | ติดตั้งฟอนต์ที่หายไปบนเครื่องหรือฝังฟอนต์ในไฟล์ .docx | +| Complex equations not exported | ฟีเจอร์ Office Math ใหม่บางอย่างยังไม่ได้แมปเป็น LaTeX | ใช้ `OfficeMathExportMode.MathML` แล้วแปลงด้วยไลบรารี MathML‑to‑LaTeX | +| Extra blank lines | ตัวบันทึก plain‑text เก็บการแบ่งย่อหน้าไว้ ทำให้มีช่องว่างเพิ่ม | ตั้งค่า `txtOptions.AddBidiMarks = false` หรือทำ post‑process ด้วยสคริปต์ง่าย ๆ | + +## ตัวอย่างทำงานเต็มรูปแบบ (คัดลอก‑วางได้) + +ด้านล่างเป็นโปรแกรมทั้งหมดพร้อมคอมไพล์ แทนที่ `YOUR_DIRECTORY` ด้วยโฟลเดอร์ที่เก็บ `input.docx` ของคุณ + +```csharp +using System; +using Aspose.Words; +using Aspose.Words.Saving; + +namespace DocxToTxtWithLatex +{ + class Program + { + static void Main() + { + try + { + // Load the source document + Document doc = new Document(@"C:\Docs\input.docx"); + + // Configure save options: export equations as LaTeX + TxtSaveOptions txtOptions = new TxtSaveOptions + { + OfficeMathExportMode = OfficeMathExportMode.LaTeX, + PreserveTableLayout = true, + AddBidiMarks = false + }; + + // Save as plain‑text + string outputPath = @"C:\Docs\Math.txt"; + doc.Save(outputPath, txtOptions); + + Console.WriteLine($"Successfully converted DOCX to TXT. Output at: {outputPath}"); + } + catch (Exception ex) + { + Console.Error.WriteLine($"Conversion failed: {ex.Message}"); + } + } + } +} +``` + +การรันโปรแกรมนี้จะ **save word as txt** พร้อมแปลงทุก Office Math block ให้เป็น LaTeX ทำให้คุณได้ไฟล์ plain‑text ที่สะอาดและค้นหาได้ง่าย + +## ขั้นตอนต่อไป & หัวข้อที่เกี่ยวข้อง + +- **Batch conversion:** ห่อหุ้มตรรกะข้างต้นในลูป `foreach` เพื่อประมวลผลโฟลเดอร์ .docx ทั้งหมด +- **Combine with PDF generation:** หลังได้ส่วน LaTeX แล้ว ส่งต่อไปยัง pipeline PDF (เช่น `PdfSharp` + `MiKTeX`) เพื่อสร้างรายงาน PDF +- **Export equations as latex** สำหรับรูปแบบอื่น: Aspose.Words ยังรองรับ `SaveFormat.Markdown` ที่สามารถฝัง LaTeX อัตโนมัติ +- **Performance tuning:** สำหรับเอกสารขนาดใหญ่ ให้ใช้ instance `TxtSaveOptions` เดียวกันและปิดฟีเจอร์ที่ไม่จำเป็น เช่น `AddBidiMarks` + +--- + +### ตัวอย่างรูปภาพ (ไม่บังคับ) + +หากคุณต้องการสัญญาณภาพ นี่คือสกรีนช็อตของไฟล์ผลลัพธ์ใน Notepad++ + +![convert docx to txt output showing LaTeX equations](convert-docx-to-txt-output.png) + +*(Alt text: “convert docx to txt output showing LaTeX equations” – satisfies the primary keyword requirement.)* + +--- + +## สรุป + +เราได้สาธิตวิธีที่เชื่อถือได้ในการ **convert docx to txt** พร้อมคงสมการทุกสมการเป็น LaTeX ที่สะอาด คีย์สำคัญคือแฟล็ก `OfficeMathExportMode.LaTeX` ที่แปลงรูปแบบคณิตศาสตร์ของ Word ให้เป็นสิ่งที่เครื่อง LaTeX ใด ๆ ก็เข้าใจได้ ด้วยโค้ดตัวอย่างเต็มที่ให้ไว้ข้างต้น คุณสามารถ **save word as txt**, **convert word to plain text**, และ **export equations as latex** ในการรันเดียวที่เป็นอิสระ + +ลองปรับเปลี่ยน—เช่นเปลี่ยนนามสกุลผลลัพธ์เป็น `.md` เพื่อ Markdown หรือรวมสคริปต์นี้เข้าไปใน pipeline การประมวลผลเอกสารที่ใหญ่ขึ้น หากเจอข้อผิดพลาดใด ๆ แสดงความคิดเห็นด้านล่างได้เลย ฉันยินดีช่วยแก้ไข + +Happy coding! + +{{< /blocks/products/pf/tutorial-page-section >}} +{{< /blocks/products/pf/main-container >}} +{{< /blocks/products/pf/main-wrap-class >}} +{{< blocks/products/products-backtop-button >}} \ No newline at end of file diff --git a/words/turkish/net/ai-powered-document-processing/_index.md b/words/turkish/net/ai-powered-document-processing/_index.md index c55bdad0a5..45f5c21054 100644 --- a/words/turkish/net/ai-powered-document-processing/_index.md +++ b/words/turkish/net/ai-powered-document-processing/_index.md @@ -41,7 +41,8 @@ Son olarak, şuraya göz atmayı unutmayın: [Özetleme Seçenekleriyle Çalış | [AI Modeli ile Çalışma](./working-with-ai-model/) | AI ile belgeleri özetlemek için Aspose.Words for .NET'i nasıl kullanacağınızı öğrenin. Belge yönetimini geliştirmek için kolay adımlar. | | [Google AI Modeli ile Çalışma](./working-with-google-ai-model/) Aspose.Words for .NET ve Google AI ile belge işleme sürecinizi bir üst seviyeye taşıyın ve zahmetsizce özlü özetler oluşturun. | | [Açık AI Modeli ile Çalışma](./working-with-open-ai-model/) | OpenAI'nin güçlü modelleriyle Aspose.Words for .NET'i kullanarak verimli belge özetlemenin kilidini açın. Şimdi bu kapsamlı kılavuza dalın. | -| [Özetleme Seçenekleriyle Çalışma](./working-with-summarize-options/) | Hızlı içgörüler için AI modellerini entegre etmeye yönelik adım adım kılavuzumuzla Aspose.Words for .NET kullanarak Word belgelerini etkili bir şekilde özetlemeyi öğrenin. +| [Özetleme Seçenekleriyle Çalışma](./working-with-summarize-options/) | Hızlı içgörüler için AI modellerini entegre etmeye yönelik adım adım kılavuzumuzla Aspose.Words for .NET kullanarak Word belgelerini etkili bir şekilde özetlemeyi öğrenin. | +| [C#'ta Yerel LLM'ye Bağlan – Tam Programlama Kılavuzu](./connect-to-local-llm-in-c-complete-programming-guide/) | C# kullanarak yerel LLM'ye bağlanmayı ve tam programlama rehberiyle entegrasyonu öğrenin. | {{< /blocks/products/pf/tutorial-page-section >}} diff --git a/words/turkish/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/_index.md b/words/turkish/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/_index.md new file mode 100644 index 0000000000..3a09e2ea4a --- /dev/null +++ b/words/turkish/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/_index.md @@ -0,0 +1,284 @@ +--- +category: general +date: 2026-04-28 +description: C#'tan yerel LLM'ye bağlan ve büyük dil modeline Word belgesini yüklemesi + için komut ver, yerel LLM'yi çağır ve metni otomatik olarak yeniden yaz. Adım adım + kod dahil. +draft: false +keywords: +- connect to local llm +- prompt large language model +- load word document +- call local llm +- rewrite text automatically +language: tr +og_description: C#'tan yerel LLM'ye bağlanın ve büyük dil modelini nasıl yönlendireceğinizi + görün, Word belgesini yükleyin, yerel LLM'yi çağırın ve metni dakikalar içinde otomatik + olarak yeniden yazın. +og_title: C#'de Yerel LLM'ye Bağlan – Tam Programlama Rehberi +tags: +- Aspose.Words +- C# +- LLM +- AI Automation +title: C#'ta Yerel LLM'ye Bağlan – Tam Programlama Rehberi +url: /tr/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/ +--- + +{{< blocks/products/pf/main-wrap-class >}} +{{< blocks/products/pf/main-container >}} +{{< blocks/products/pf/tutorial-page-section >}} + +# C#'ta Yerel LLM'ye Bağlan – Tam Programlama Rehberi + +Hiç .NET uygulamasından **yerel llm'ye bağlan**manız ve bunun bir Word dosyasıyla nasıl konuşacağını merak ettiniz mi? Yalnız değilsiniz. Bu rehberde tüm süreci adım adım inceleyeceğiz—yerel llm'ye bağlan, **büyük dil modelini yönlendir**, bir Word belgesi yükle, **yerel llm'yi çağır**, ve sonunda **metni otomatik olarak yeniden yaz**. Sonunda, dış API anahtarları olmadan herhangi bir paragrafı resmi bir üsluba dönüştüren çalıştırılabilir bir örnek elde edeceksiniz. + +## Bu Öğreticide Neler Kapsanıyor + +İlk olarak gerekli NuGet paketlerini kuracağız, ardından basit bir yerel LLM uç noktasını (örneğin Ollama 11434 portunda) çalıştıracağız. Daha sonra Aspose.Words kullanarak bir `.docx` dosyasını yükleyecek, bir paragrafı LLM'ye gönderecek, yeniden yazılmış bir sürüm alacak ve aynı belgeye geri yazacağız. Ayrıca yaygın tuzakları—null paragraflar, async disposal ve kodlama sorunları—nasıl ele alacağınızı göreceksiniz; böylece kod sadece bir demo değil, üretimde de çalışır. + +### Önkoşullar + +- .NET 6.0 SDK veya daha yeni bir sürüm (isteğe bağlı olarak .NET 8 de kullanabilirsiniz) +- Visual Studio 2022 veya C# uzantılı VS Code +- **Aspose.Words for .NET** (ücretsiz deneme yeterli) +- `/api/generate` sözleşmesini destekleyen yerel bir LLM (ör. Ollama, LMStudio) +- C#'ta async/await konusunda temel bilgi + +> **Pro tip:** Eğer henüz Ollama'yı kurmadıysanız, `ollama serve` komutunu çalıştırın ve `ollama pull llama3` ile bir model indirin. Varsayılan HTTP uç noktası `http://localhost:11434/api/generate` olacaktır. + +--- + +## Adım 1: Gerekli Paketleri Kurun + +İlk olarak, projenize Aspose.Words ve Aspose.Words.AI NuGet paketlerini ekleyin. + +```bash +dotnet add package Aspose.Words +dotnet add package Aspose.Words.AI +``` + +Bu kütüphaneler bize **Word belgesini yükleme** yeteneği ve **yerel llm'yi çağırma** için HTTP isteklerini elle oluşturmak zorunda kalmadan ince bir sarmalayıcı sağlar. + +--- + +## Adım 2: Yerel LLM Uç Noktasına Bağlan + +Yerel olarak barındırılan bir modele bağlanmak, `LocalLargeLanguageModel` sınıfını örneklemek kadar basittir. Yapıcı, oluşturma uç noktasının tam URL'sini bekler. + +```csharp +using Aspose.Words.AI; +using Aspose.Words; +using System.Threading.Tasks; + +// Create a client that talks to the LLM running on localhost +var localLlm = new LocalLargeLanguageModel("http://localhost:11434/api/generate"); +``` + +Neden uç noktayı bir sınıf içinde sarmalıyoruz? `LocalLargeLanguageModel`, JSON serileştirmesini, yeniden denemeleri ve akış yanıtlarını sizin için yönetir—böylece `HttpClient` ile uğraşmak yerine istem (prompt) mantığına odaklanabilirsiniz. + +--- + +## Adım 3: Kaynak Word Belgesini Yükle + +Sonra belgeyi belleğe alıyoruz. Aspose.Words neredeyse tüm Word formatlarını destekler, bu yüzden `Document`, Office yüklü olmadan `input.docx` dosyasını ayrıştıracaktır. + +```csharp +// Path to the source file – adjust as needed +string inputPath = Path.Combine(Environment.CurrentDirectory, "input.docx"); + +// Load the document; throws if the file is missing or corrupted +Document sourceDocument = new Document(inputPath); +``` + +Eğer bir akış (ör. ASP.NET üzerinden yüklenen bir dosya) ile çalışmanız gerekiyorsa, dosya yolunu bir `MemoryStream` ile değiştirin ve `Document` yapıcısına geçirin. + +--- + +## Adım 4: Mevcut Paragraf Metnini Çıkar + +`DocumentBuilder` kullanarak belge içinde gezineceğiz. Bu örnekte **ilk paragrafı** yeniden yazıyoruz, ancak birden çok paragrafı işlemek için `sourceDocument.GetChildNodes(NodeType.Paragraph, true)` üzerinde dönebilirsiniz. + +```csharp +// Builder gives us a cursor inside the document +DocumentBuilder docBuilder = new DocumentBuilder(sourceDocument); + +// Grab the text of the paragraph where the builder is currently positioned +string originalParagraph = docBuilder.CurrentParagraph?.GetText() ?? string.Empty; + +// Safety check – avoid sending empty strings to the LLM +if (string.IsNullOrWhiteSpace(originalParagraph)) +{ + Console.WriteLine("No paragraph found at the current cursor position."); + return; +} +``` + +`?.` operatörü, belge boş olduğunda `NullReferenceException` oluşmasını önler. Bu, yeni başlayanların sıkça karşılaştığı **kenar durum**lardan biridir. + +--- + +## Adım 5: LLM'yi Paragrafı Yeniden Yazması İçin İstemi Gönder + +Şimdi gerçekten **büyük dil modeline istem gönderiyoruz**. İstem düz İngilizcedir; sarmalayıcı bunu JSON olarak yerel uç noktaya gönderir. + +```csharp +// Build a friendly instruction for the model +string prompt = $"Rewrite the following sentence in a more formal tone:\n{originalParagraph}"; + +// Await the model's response – this is an async call +string rewrittenParagraph = await localLlm.PromptAsync(prompt); +``` + +İsteği bu şekilde ifade etmemizin nedeni nedir? LLM'ler net, tek‑görevli talimatlara en iyi yanıt verir. İki nokta üstünden sonra bir yeni satır eklemek, talimatı içerikten ayırır ve modelin istemi geri tekrarlama olasılığını azaltır. + +**Beklenen çıktı** – `originalParagraph` `"Hey, what's up?"` ise, LLM şu şekilde dönebilir: + +> “İyi günler, size nasıl yardımcı olabilirim?” + +Sonucu, ekrana yazdırarak doğrulayabilirsiniz: + +```csharp +Console.WriteLine("Original: " + originalParagraph); +Console.WriteLine("Rewritten: " + rewrittenParagraph); +``` + +--- + +## Adım 6: Yeniden Yazılmış Metni Belgeye Geri Yerleştir + +Yeni metni elde ettiğimizde, eski paragrafı değiştiririz. `DocumentBuilder.Writeln` yeni bir satır yazar ve imleci ileri hareket ettirir; ekleme için mükemmeldir. Aynı paragrafı *tamamen* değiştirmek isterseniz, yazmadan önce `docBuilder.CurrentParagraph.RemoveAllChildren()` kullanabilirsiniz. + +```csharp +// Option A – Append a new paragraph (keeps the original) +docBuilder.Writeln(rewrittenParagraph); + +// Option B – Replace the existing paragraph (uncomment to use) +// docBuilder.CurrentParagraph.RemoveAllChildren(); +// docBuilder.CurrentParagraph.AppendChild(new Run(docBuilder.Document, rewrittenParagraph)); +``` + +Her iki yaklaşım da gösterildi, böylece iş akışınıza uyanı seçebilirsiniz. + +--- + +## Adım 7: Güncellenmiş Belgeyi Kaydet + +Son olarak, değişiklikleri yeni bir dosyaya kaydediyoruz. Aspose.Words dosya uzantısına göre formatı otomatik seçer. + +```csharp +string outputPath = Path.Combine(Environment.CurrentDirectory, "output.docx"); +sourceDocument.Save(outputPath); + +Console.WriteLine($"Document saved to {outputPath}"); +``` + +`output.docx` dosyasını Word'de açın, paragrafın artık resmi bir üslupla okunduğunu göreceksiniz. + +--- + +## Tam Çalışan Örnek + +Aşağıda **tam, bağımsız program** yer alıyor. Bir konsol projesine kopyalayıp yapıştırın, NuGet paketlerini geri yükleyin ve çalıştırın—çalışan bir yerel LLM dışında ekstra yapılandırma gerekmez. + +```csharp +using Aspose.Words.AI; +using Aspose.Words; +using System; +using System.IO; +using System.Threading.Tasks; + +class Program +{ + static async Task Main() + { + // ------------------------------------------------- + // Step 1: Connect to the locally hosted LLM endpoint + // ------------------------------------------------- + var localLlm = new LocalLargeLanguageModel("http://localhost:11434/api/generate"); + + // ------------------------------------------------- + // Step 2: Load the source Word document + // ------------------------------------------------- + string inputPath = Path.Combine(Environment.CurrentDirectory, "input.docx"); + Document sourceDocument = new Document(inputPath); + + // ------------------------------------------------- + // Step 3: Retrieve the text of the current paragraph + // ------------------------------------------------- + DocumentBuilder docBuilder = new DocumentBuilder(sourceDocument); + string originalParagraph = docBuilder.CurrentParagraph?.GetText() ?? string.Empty; + + if (string.IsNullOrWhiteSpace(originalParagraph)) + { + Console.WriteLine("No paragraph found at the current cursor position."); + return; + } + + // ------------------------------------------------- + // Step 4: Ask the LLM to rewrite the paragraph in a formal tone + // ------------------------------------------------- + string prompt = $"Rewrite the following sentence in a more formal tone:\n{originalParagraph}"; + string rewrittenParagraph = await localLlm.PromptAsync(prompt); + + // ------------------------------------------------- + // Step 5: Insert the rewritten text back into the document + // ------------------------------------------------- + docBuilder.Writeln(rewrittenParagraph); + + // ------------------------------------------------- + // Step 6: Save the updated document + // ------------------------------------------------- + string outputPath = Path.Combine(Environment.CurrentDirectory, "output.docx"); + sourceDocument.Save(outputPath); + + Console.WriteLine("Original paragraph:"); + Console.WriteLine(originalParagraph); + Console.WriteLine("\nRewritten paragraph:"); + Console.WriteLine(rewrittenParagraph); + Console.WriteLine($"\nDocument saved to {outputPath}"); + } +} +``` + +### Çalıştırdığınızda Ne Beklemelisiniz + +1. Konsol, orijinal ve yeniden yazılmış paragrafları yazdırır. +2. `output.docx`, `input.docx`'nin yanında görünür. +3. Dosyayı açtığınızda yeni resmi paragrafın orijinalin ardından (veya alternatif koda geçerseniz değiştirilmiş olarak) eklendiğini görürsünüz. + +--- + +## Yaygın Kenar Durumlarını Ele Alma + +| Situation | Solution | +|-----------|----------| +| **Boş veya sadece boşluk içeren paragraf** | İstem göndermeden önce `string.IsNullOrWhiteSpace` kontrol edin (Adım 3'e bakın). | +| **LLM bir hata döndürür veya boş string verir** | `PromptAsync`'i bir `try/catch` bloğuna alın ve orijinal metne geri dönün. | +| **Birden fazla paragrafın yeniden yazılması gerekiyor** | `sourceDocument.GetChildNodes(NodeType.Paragraph, true)` üzerinde döngü kurun ve aynı istem mantığını uygulayın. | +| **Büyük belgeler gecikmeye neden olur** | Paragrafları toplu hâle getirip tek bir istekle gönderin (istek başına 4 KB'ye kadar). | +| **ASCII olmayan karakterler bozulur** | LLM uç noktasının UTF‑8 kullandığından emin olun (çoğu modern model bunu yapar). | + +--- + +## Sonraki Adımlar ve İlgili Konular + +- **Büyük dil modeline** daha zengin talimatlarla (ör. stil rehberleri, uzunluk sınırları) istem gönderin. +- Belge otomasyonunu bir hizmet olarak sunmak için bir web API'de **yerel llm'yi çağır**. +- Yüksek verim senaryoları için paralel akışlarda **Word belgesini yükle** özelliğini keşfedin. +- Bu yaklaşımı toplu e‑posta üretimi veya rapor standartlaştırması için **metni otomatik olarak yeniden yaz** ile birleştirin. + +Daha derine inmek isterseniz, Aspose'un **belge birleştirme** dokümantasyonuna ve özel örnekleme parametreleri için Ollama API referansına göz atın. + +--- + +## Sonuç + +C#'ta **yerel llm'ye bağlan**, **büyük dil modeline istem gönder**, **Word belgesini yükle**, **yerel llm'yi çağır** ve **metni otomatik olarak yeniden yaz** nasıl yapılacağını az önce gösterdik—hepsi tek bir çalıştırılabilir konsol uygulamasında. Bu desen ölçeklenebilir: istemi değiştirin, paragraflar üzerinde döngü kurun veya mantığı bir ASP.NET uç noktası üzerinden dışa aktarın. Ana çıkarım, yerel AI modellerinin klasik belge işleme kütüphaneleriyle sıkı bir şekilde bütünleştirilebileceği ve güvenilir on‑prem ortamınızdan çıkmadan güçlü otomasyon sağlayabileceğidir. + +İş parçacığı (threading) hakkında sorularınız varsa, + +{{< /blocks/products/pf/tutorial-page-section >}} +{{< /blocks/products/pf/main-container >}} +{{< /blocks/products/pf/main-wrap-class >}} +{{< blocks/products/products-backtop-button >}} \ No newline at end of file diff --git a/words/turkish/net/programming-with-markdownsaveoptions/_index.md b/words/turkish/net/programming-with-markdownsaveoptions/_index.md index 4ad3c251c7..8c35467771 100644 --- a/words/turkish/net/programming-with-markdownsaveoptions/_index.md +++ b/words/turkish/net/programming-with-markdownsaveoptions/_index.md @@ -41,6 +41,7 @@ Words Processing with MarkdownSaveOptions, .NET için Aspose.Words kütüphanesi | [Aspose.Words ile docx'i markdown olarak kaydet – Tam C# Rehberi](./save-docx-as-markdown-with-aspose-words-full-c-guide/) | Aspose.Words kullanarak docx dosyasını markdown formatına tam C# rehberiyle dönüştürün. | | [Word Görüntülerini Kaydet – Aspose ile Word'u Markdown'a Dönüştür](./save-word-images-convert-word-to-markdown-with-aspose/) | Aspose.Words for .NET kullanarak Word belgelerindeki görüntüleri kaydedin ve Word'u Markdown'a dönüştürün. | | [Word'ü C# ile Markdown'a Dönüştür – Görüntü Çıkarma ile Tam Rehber](./convert-word-to-markdown-in-c-full-guide-with-image-extracti/) | Aspose.Words for .NET kullanarak Word belgelerini markdown formatına dönüştürün ve görüntüleri ayıklayın. Tam C# rehberi. | +| [Markdown Görüntü Göreli Yolu – Word'ü Markdown'a Dönüştür](./markdown-image-relative-path-convert-word-to-markdown/) | Aspose.Words for .NET kullanarak Word belgelerindeki görüntülerin göreli yolunu ayarlayarak Markdown'a dönüştürmeyi öğrenin. | {{< /blocks/products/pf/tutorial-page-section >}} diff --git a/words/turkish/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/_index.md b/words/turkish/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/_index.md new file mode 100644 index 0000000000..2d74e5fe2c --- /dev/null +++ b/words/turkish/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/_index.md @@ -0,0 +1,207 @@ +--- +category: general +date: 2026-04-28 +description: Word'ü markdown'a dönüştürürken markdown görüntü göreli yolunu nasıl + ayarlayacağınızı, Word'ten görüntüleri nasıl çıkaracağınızı ve dışa aktarılan görüntüler + için kaynak klasörü nasıl oluşturacağınızı öğrenin. +draft: false +keywords: +- markdown image relative path +- convert word to markdown +- extract images from word +- create resources folder +- export images from docx +language: tr +og_description: Word'ü markdown'a dönüştürürken markdown görüntü göreli yolunu ayarlayın, + Word'ten görüntüleri çıkarın ve dışa aktarılan görüntüler için bir kaynak klasörü + oluşturun. +og_title: Markdown resim göreceli yolu – Word'ü Markdown'a dönüştür +tags: +- Aspose.Words +- C# +- Markdown +- Image Export +title: markdown görsel göreceli yolu – Word'ü Markdown'a dönüştür +url: /tr/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/ +--- + +{{< blocks/products/pf/main-wrap-class >}} +{{< blocks/products/pf/main-container >}} +{{< blocks/products/pf/tutorial-page-section >}} + +# markdown image relative path – Word'ü Markdown'a Dönüştürme + +Word'ü **markdown**'a **dönüştürürken** **markdown image relative path**'e ihtiyaç duydunuz mu? Tek başınıza değilsiniz. Çoğu geliştirici, oluşturulan Markdown'ın görüntüleri düz bir klasöre işaret etmesi ve statik bir site ya da GitHub deposunda beklediğiniz göreli bağlantı yapısını bozan bir sorunla karşılaşıyor. + +Bu öğreticide, **Word'ten görüntüleri çıkaran**, **bir resources klasörü oluşturan** ve görüntü referanslarını temiz bir *markdown image relative path* kullanacak şekilde yeniden yazan eksiksiz, uçtan uca bir çözümü adım adım inceleyeceğiz. Sonunda, yayınlamaya hazır bir `.md` dosyanız ve orijinal `.docx` dosyasından çıkarılan tüm resimleri içeren düzenli bir `Resources` dizininiz olacak. + +> **Neler elde edeceksiniz:** tek bir C# programı (harici script yok), *neden* her parçanın önemli olduğunu açıklayan net bir anlatım ve kendi projelerinizde kopyala‑yapıştır yapabileceğiniz birkaç pratik ipucu. + +--- + +## Prerequisites + +Kodlara geçmeden önce şunların yüklü olduğundan emin olun: + +- **.NET 6.0** veya daha yeni bir sürüm (isteğe bağlı olarak .NET Framework 4.7+ hedefleyebilirsiniz, ancak yeni projeler için .NET 6 ideal). +- **Aspose.Words for .NET** (yazım anındaki en yeni NuGet paketi, sürüm 23.12). Şu komutla kurun: + ```bash + dotnet add package Aspose.Words + ``` +- Görüntü içeren bir Word belgesi – örneğin `WithImages.docx`. +- Markdown ve görüntülerin kaydedileceği bir klasör, örnek: `C:\Projects\MarkdownExport`. + +Ek bir kütüphane gerekmez; geri kalan her şey Aspose.Words tarafından halledilir. + +--- + +## Step 1: Load the source Word document (the starting point for convert word to markdown) + +```csharp +using System; +using System.IO; +using Aspose.Words; +using Aspose.Words.Saving; + +class Program +{ + static void Main() + { + // Adjust the path to point at your own .docx file. + string sourcePath = @"C:\Projects\MarkdownExport\WithImages.docx"; + + // Load the document – this is where Aspose.Words parses the Word file. + Document doc = new Document(sourcePath); + + // The rest of the workflow follows… + } +} +``` + +*Why this matters:* Belgeyi yüklemek, daha sonra **docx'ten görüntüleri dışa aktarmak** için ihtiyacımız olan görüntü parçalarını içeren iç node ağacına erişmemizi sağlar. Yükleme başarısız olursa sonraki adımlar çalışmaz, bu yüzden yol ve dosya izinlerini iki kez kontrol edin. + +--- + +## Step 2: Configure `MarkdownSaveOptions` with a custom callback (the heart of create resources folder) + +`ResourceSavingCallback`, Aspose.Words bir görüntü dosyası yazmak istediğinde devreye girmenizi sağlar. Callback içinde **Resources alt klasörünü** oluşturacak ve oluşturulan markdown'ın bir *markdown image relative path* kullanmasını sağlayacağız. + +```csharp +// Inside Main(), after loading the document: +string outputFolder = @"C:\Projects\MarkdownExport"; +string resourcesFolder = Path.Combine(outputFolder, "Resources"); + +// Make sure the folder exists before we start saving anything. +Directory.CreateDirectory(resourcesFolder); + +// Set up the Markdown save options. +MarkdownSaveOptions mdOptions = new MarkdownSaveOptions +{ + // Hook that runs for every image resource. + ResourceSavingCallback = new MyMarkdownResourceCallback(resourcesFolder) +}; + +// Save the document as Markdown. +string markdownPath = Path.Combine(outputFolder, "Doc.md"); +doc.Save(markdownPath, mdOptions); +``` + +`resourcesFolder` değişkenini callback'in yapıcısına gönderdiğimize dikkat edin—bu, klasör yolunu esnek tutar ve kod içinde sabit string kullanmaktan kaçınır. + +--- + +## Step 3: Implement the callback that **creates resources folder** and rewrites the path + +```csharp +/// +/// Handles image extraction and path rewriting for markdown export. +/// +class MyMarkdownResourceCallback : IResourceSavingCallback +{ + private readonly string _resourcesFolder; + + public MyMarkdownResourceCallback(string resourcesFolder) + { + _resourcesFolder = resourcesFolder; + } + + public void ResourceSaving(ResourceSavingArgs args) + { + // 1️⃣ Build the full file system path where the image will be stored. + string targetPath = Path.Combine(_resourcesFolder, args.ResourceFileName); + + // 2️⃣ Ensure the directory exists (in case Aspose creates sub‑folders). + Directory.CreateDirectory(Path.GetDirectoryName(targetPath)); + + // 3️⃣ Write the image stream to disk. + using (FileStream fileStream = File.Create(targetPath)) + { + args.Stream.CopyTo(fileStream); + } + + // 4️⃣ Update the markdown reference to use a relative path. + // This is the crucial line that gives us the markdown image relative path. + args.ResourceFileName = Path.Combine("Resources", args.ResourceFileName); + } +} +``` + +*Why this works:* `args.Stream` ham görüntü baytlarını içerir. Bu akışı `Resources` klasörümüz içinde bir dosyaya kopyalayarak **docx'ten görüntüleri dışa aktarmış** oluruz. Ardından `args.ResourceFileName`'i göreli bir URL (`Resources/image.png`) ile değiştiririz. Aspose.Words daha sonra markdown'ı yazdığında tam olarak bu dizeyi enjekte eder ve istediğimiz *markdown image relative path* elde edilir. + +--- + +## Step 4: Verify the generated Markdown (what the final output looks like) + +`Doc.md` dosyasını herhangi bir metin editöründe açın. Aşağıdakine benzer bir içerik görmelisiniz: + +```markdown +# Sample Heading + +Here is an inline picture: + +![Image 0](Resources/Image_0.png) + +And a picture inside a table: + +![Image 1](Resources/Image_1.jpg) +``` + +Önemli kısım, her görüntü referansının `Resources/...` dizinine işaret etmesidir – aradığımız **markdown image relative path** budur. + +![markdown image relative path example](example.png "markdown image relative path example") + +*Tip:* Markdown'ı göreli bağlantıları destekleyen bir görüntüleyicide (VS Code önizlemesi, GitHub veya bir static site generator) açarsanız, ek bir yapılandırma gerekmeden resimler doğru şekilde gösterilir. + +--- + +## Step 5: Common pitfalls and pro‑tips + +| Issue | Why it happens | How to fix it | +|-------|----------------|---------------| +| Images end up in the root folder instead of `Resources` | Callback eklenmemiş veya `args.ResourceFileName` üzerine yazılmamış. | `ResourceSavingCallback`'in **doc.Save** çağrısından **önce** ayarlandığından emin olun. | +| Filenames contain illegal characters | Word bazen görüntülere boşluk veya Unicode karakterli isimler verir. | Callback içinde `args.ResourceFileName`'i temizlemek için `Path.GetInvalidFileNameChars()` kullanın. | +| Large documents take a long time to process | Her görüntü senkron olarak yazılıyor. | .NET 6+ üzerindeyseniz ve performans gerekiyorsa asenkron I/O'ya geçin (`await args.Stream.CopyToAsync(fileStream)`). | +| Relative paths break when the markdown is moved | Yol, markdown dosyasının konumuna görecelidir. | `Doc.md` ve `Resources` klasörünü birlikte tutun veya callback'i farklı bir göreli önek (`../assets` gibi) kullanacak şekilde ayarlayın. | + +--- + +## Step 6: Extending the solution (what if you need more control?) + +- **Multiple output formats:** `MarkdownSaveOptions` yerine `HtmlSaveOptions` veya `PdfSaveOptions` kullanın, aynı callback'i koruyun—Aspose.Words format ne olursa olsun her görüntü için callback'i tetikler. +- **Custom image naming:** Görüntüleri yeniden adlandırmak isterseniz (ör. `figure-01.png`), dosyayı yazmadan önce `args.ResourceFileName`'i callback içinde değiştirin. +- **Embedding images as Base64:** `args.ResourceFileName`'i bir data URI (`data:image/png;base64,...`) olarak ayarlayın ve dosya yazımını atlayın. Tek dosyalı markdown dışa aktarımları için kullanışlıdır. + +--- + +## Conclusion + +Artık **Word'ü markdown'a dönüştüren**, **word'ten görüntüleri çıkaran**, **resources klasörü oluşturan** ve her resim için temiz bir **markdown image relative path** garantileyen tam işlevsel bir C# programınız var. Kod bağımsız, en yeni Aspose.Words sürümüyle çalışıyor ve minimal çabayla herhangi bir .NET projesine eklenebilir. + +Sonraki adım? Oluşturduğunuz markdown'ı Hugo ya da Jekyll gibi bir static site generator'a beslemek ya da callback'i doğrudan Base64 gömmek için denemek. SVG görüntüler ya da aşırı büyük dosyalar gibi kenar durumlarıyla karşılaşırsanız, “Common pitfalls” tablosuna geri dönün; genellikle küçük bir ayar sorunu çözüm getirir. + +İyi kodlamalar, ve markdown'ınız her zaman doğru klasöre işaret etsin! + +{{< /blocks/products/pf/tutorial-page-section >}} +{{< /blocks/products/pf/main-container >}} +{{< /blocks/products/pf/main-wrap-class >}} +{{< blocks/products/products-backtop-button >}} \ No newline at end of file diff --git a/words/turkish/net/programming-with-officemath/_index.md b/words/turkish/net/programming-with-officemath/_index.md index 49828288be..1626dd7df8 100644 --- a/words/turkish/net/programming-with-officemath/_index.md +++ b/words/turkish/net/programming-with-officemath/_index.md @@ -23,6 +23,7 @@ OfficeMath ile Word İşleme konusunda Aspose.Words for .NET eğitimleri, Word b | [Matematik Denklemleri](./math-equations/) | Aspose.Words for .NET kullanarak Word belgelerinde matematiksel denklemlerin nasıl yapılandırılacağını öğrenin. Örnekler, SSS ve daha fazlasıyla adım adım kılavuz. | | [docx'i txt olarak kaydet – Word Math'i LaTeX'e C# ile dışa aktar](./save-docx-as-txt-export-word-math-to-latex-with-c/) | C# kullanarak docx dosyasını txt olarak kaydedin ve Word Math ifadelerini LaTeX formatına dönüştürün. | | [Belgeyi Txt Olarak Kaydet – Word Matematiğini LaTeX'e C# ile Dışa Aktar](./save-document-as-txt-export-word-math-to-latex-in-c/) | C# ile Aspose.Words for .NET kullanarak Word belgelerini txt dosyası olarak kaydedin ve matematik denklemlerini LaTeX formatına dönüştürün. | +| [DOCX'i TXT'ye Dönüştür – Word Denklemlerini LaTeX'e C# ile Dışa Aktar](./convert-docx-to-txt-export-word-equations-to-latex-in-c/) | C# kullanarak DOCX dosyasını TXT'ye dönüştürün ve Word denklemlerini LaTeX formatına aktarın. | {{< /blocks/products/pf/tutorial-page-section >}} diff --git a/words/turkish/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/_index.md b/words/turkish/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/_index.md new file mode 100644 index 0000000000..f653f3dbf0 --- /dev/null +++ b/words/turkish/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/_index.md @@ -0,0 +1,212 @@ +--- +category: general +date: 2026-04-28 +description: Aspose.Words kullanarak DOCX'i TXT'ye dönüştürün ve Word denklemlerini + LaTeX'e aktarın. Word'ü TXT olarak kaydetmeyi ve matematik nesnelerini birkaç adımda + nasıl yöneteceğinizi öğrenin. +draft: false +keywords: +- convert docx to txt +- convert word equations to latex +- convert word to plain text +- save word as txt +- export equations as latex +language: tr +og_description: Basit bir C# kod parçacığıyla DOCX'i TXT'ye dönüştürün ve Word denklemlerini + LaTeX'e aktarın. Tam kılavuz, kod ve ipuçları. +og_title: DOCX'i TXT'ye Dönüştür – Word Denklemlerini LaTeX'e Aktar +tags: +- C# +- Aspose.Words +- Document Conversion +title: DOCX'i TXT'ye Dönüştür – Word Denklemlerini C#'ta LaTeX'e Aktar +url: /tr/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/ +--- + +{{< blocks/products/pf/main-wrap-class >}} +{{< blocks/products/pf/main-container >}} +{{< blocks/products/pf/tutorial-page-section >}} + +# DOCX'i TXT'ye Dönüştür – Word Denklemlerini LaTeX Olarak Dışa Aktar + +**docx to txt** dönüştürmeniz gerektiğinde, Word dosyanızdaki matematiğin karışık bir hâle gelmesinden endişe ettiniz mi? Tek başınıza değilsiniz. Birçok mühendislik ya da akademik projede kaynak belge .docx formatında olur, ancak sonraki araçlar yalnızca düz metin ya da LaTeX'i anlayabilir. İyi haber? Birkaç satır C# ve Aspose.Words ile **docx to txt** dönüştürürken her denklemi temiz LaTeX kodu olarak tutabilirsiniz. + +Bu öğreticide tüm süreci adım adım inceleyeceğiz: bir .docx dosyasını yükleme, Office Math nesnelerinin LaTeX olmasını sağlayacak kaydetme seçeneklerini yapılandırma ve son olarak sonucu bir .txt dosyasına yazma. Sonuna geldiğinizde **save word as txt**, **convert word to plain text** ve **export equations as latex** işlemlerini API dokümanlarını karıştırmadan nasıl yapacağınızı öğreneceksiniz. + +## Öğrenecekleriniz + +- Denklemleri koruyarak **docx to txt** dönüştürmek için gereken tam API çağrıları. +- `OfficeMathExportMode.LaTeX` seçiminin **convert word equations to latex** için önerilen yol olmasının nedeni. +- Eksik fontlar ya da desteklenmeyen denklem özellikleri gibi yaygın kenar durumlarını nasıl ele alacağınız. +- Herhangi bir .NET projesine ekleyebileceğiniz, çalıştırmaya hazır tam C# programı. + +### Ön Koşullar + +- .NET 6.0 veya üzeri (kod .NET Framework 4.7+ üzerinde de çalışır). +- Aspose.Words for .NET lisansı (ücretsiz deneme sürümü değerlendirme için yeterlidir). +- En az bir Office Math nesnesi içeren bir Word belgesi (`input.docx`). + +Eğer bunlara sahipseniz, hemen başlayalım. + +## Adım 1: Aspose.Words'ü Kurun + +Kod çalışmadan önce kütüphaneye ihtiyacınız var. Proje klasörünüzde bir terminal açın ve şu komutu çalıştırın: + +```bash +dotnet add package Aspose.Words +``` + +Bu, (2026‑04‑28 itibarıyla) v24.12 sürümünün en son kararlı versiyonunu çeker. Ek DLL'lere gerek yok. + +## Adım 2: Kaynak Belgeyi Yükleyin + +İlk olarak .docx dosyasını bir `Document` nesnesine okuruz. Bu nesne, metin akışları, görseller ve matematik nesneleri dahil dosyanın yapısına tam erişim sağlar. + +```csharp +using Aspose.Words; +using Aspose.Words.Saving; + +class Program +{ + static void Main() + { + // Step 2: Load the source document + Document doc = new Document(@"YOUR_DIRECTORY\input.docx"); +``` + +> **Neden önemli:** Belgeyi belleğe yüklemek, daha sonra her öğenin nasıl yazılacağını ayarlamamıza olanak tanır. Dosya bulunamazsa Aspose bir `FileNotFoundException` fırlatır; bunu üretim kodunda yakalamak isteyebilirsiniz. + +## Adım 3: LaTeX Matematik İçin TXT Kaydetme Seçeneklerini Yapılandırın + +Varsayılan olarak `Document.Save` düz metin yazar ve **Office Math** nesnelerini atar. Bu denklemleri tutmak için `OfficeMathExportMode` değerini `LaTeX` olarak ayarlarız. Bu, ihracatçının her denklemi LaTeX eşdeğerine çevirmesini sağlar. + +```csharp + // Step 3: Configure TXT save options to export Office Math as LaTeX + TxtSaveOptions txtOptions = new TxtSaveOptions + { + OfficeMathExportMode = OfficeMathExportMode.LaTeX, + // Optional: preserve line breaks as they appear in the original Word file + PreserveTableLayout = true + }; +``` + +> **İpucu:** Sadece denklemin ham Unicode karakterlerine ihtiyacınız varsa (örneğin hızlı bir ön izleme için), `OfficeMathExportMode.Text` kullanabilirsiniz. Ancak çoğu bilimsel iş akışı için `LaTeX` en iyi standarttır; çünkü LaTeX işlemcileri tarafından evrensel olarak anlaşılır. + +## Adım 4: Belgeyi Düz Metin Olarak Kaydedin + +Şimdi dönüştürülmüş içeriği bir `.txt` dosyasına yazarız. Dosya normal paragraflar, madde işaretleri ve—önceki adım sayesinde—her denklem için LaTeX parçacıkları içerecek. + +```csharp + // Step 4: Save the document as plain‑text using the configured options + doc.Save(@"YOUR_DIRECTORY\Math.txt", txtOptions); + } +} +``` + +`Math.txt` dosyasını açtığınızda şu şekilde bir içerik göreceksiniz: + +``` +In this report we derive the quadratic formula: +\[ +x = \frac{-b \pm \sqrt{b^{2} - 4ac}}{2a} +\] + +The end. +``` + +`\[` … `\]` sınırlayıcılarını fark ettiniz mi? Bunlar otomatik olarak oluşturulan LaTeX matematik bloklarıdır. + +## Adım 5: Çıktıyı Doğrulayın (Opsiyonel ama Tavsiye Edilir) + +Denkliklerde özel semboller olduğunda ince bir dönüşüm sorunu kaçırmak kolaydır. Hızlı bir kontrol, oluşturulan `.txt` dosyasını bir LaTeX derleyicisine (ör. `pdflatex`) beslemek ve hatasız derlenip derlenmediğini görmek olabilir. + +```bash +pdflatex -interaction=nonstopmode Math.txt +``` + +Derleme başarılı olursa, **convert word equations to latex** ve **convert docx to txt** işlemlerini tek seferde gerçekleştirmiş olursunuz. Hata alırsanız, tanımsız komutlarla ilgili mesajları arayın—bunlar genellikle Aspose.Words'ün çeviremediği bir denklem özelliğini (ör. belirli matris gösterimleri) işaret eder. Böyle durumlarda `OfficeMathExportMode.MathML`'e geri dönüp MathML'i başka bir araçla LaTeX'e dönüştürebilirsiniz. + +## Yaygın Tuzaklar ve Çözümleri + +| Sorun | Neden Oluşur | Çözüm | +|-------|--------------|------| +| Eksik fontlar | Aspose.Words, sembolleri doğru render etmek için fonta ihtiyaç duyar. | Eksik fontu makinede kurun veya .docx içine gömün. | +| Karmaşık denklemler dışa aktarılmıyor | Yeni Office Math özelliklerinden bazıları henüz LaTeX'e eşlenmemiştir. | `OfficeMathExportMode.MathML` kullanın, ardından bir MathML‑to‑LaTeX kütüphanesiyle dönüştürün. | +| Fazladan boş satırlar | Düz metin kaydedici paragraf aralarını korur, bu da boşluk ekleyebilir. | `txtOptions.AddBidiMarks = false` ayarlayın veya basit bir betikle dosyayı sonradan işleyin. | + +## Tam Çalışan Örnek (Kopyala‑Yapıştır Hazır) + +Aşağıda, derlenmeye hazır tüm program yer alıyor. `YOUR_DIRECTORY` kısmını `input.docx` dosyanızın bulunduğu klasörle değiştirin. + +```csharp +using System; +using Aspose.Words; +using Aspose.Words.Saving; + +namespace DocxToTxtWithLatex +{ + class Program + { + static void Main() + { + try + { + // Load the source document + Document doc = new Document(@"C:\Docs\input.docx"); + + // Configure save options: export equations as LaTeX + TxtSaveOptions txtOptions = new TxtSaveOptions + { + OfficeMathExportMode = OfficeMathExportMode.LaTeX, + PreserveTableLayout = true, + AddBidiMarks = false + }; + + // Save as plain‑text + string outputPath = @"C:\Docs\Math.txt"; + doc.Save(outputPath, txtOptions); + + Console.WriteLine($"Successfully converted DOCX to TXT. Output at: {outputPath}"); + } + catch (Exception ex) + { + Console.Error.WriteLine($"Conversion failed: {ex.Message}"); + } + } + } +} +``` + +Bu programı çalıştırdığınızda **save word as txt** yaparken her Office Math bloğu LaTeX'e dönüştürülecek ve temiz, aranabilir bir düz metin dosyası elde edeceksiniz. + +## Sonraki Adımlar ve İlgili Konular + +- **Toplu dönüşüm:** Yukarıdaki mantığı bir `foreach` döngüsü içinde sararak bir klasördeki tüm .docx dosyalarını işleyin. +- **PDF üretimiyle birleştirme:** LaTeX parçacıklarını elde ettikten sonra bir PDF akışına (ör. `PdfSharp` + `MiKTeX`) besleyerek PDF raporları oluşturun. +- **Diğer formatlar için denklemleri latex olarak dışa aktar:** Aspose.Words ayrıca `SaveFormat.Markdown`'i destekler; bu da LaTeX'i otomatik olarak gömebilir. +- **Performans ayarı:** Büyük belgeler için aynı `TxtSaveOptions` örneğini yeniden kullanın ve `AddBidiMarks` gibi gereksiz özellikleri devre dışı bırakın. + +--- + +### Görsel Örnek (Opsiyonel) + +Görsel bir ipucu isterseniz, Notepad++'ta çıktı dosyasının ekran görüntüsü aşağıdadır. + +![convert docx to txt çıktısı LaTeX denklemlerini gösteriyor](convert-docx-to-txt-output.png) + +*(Alt metin: “convert docx to txt çıktısı LaTeX denklemlerini gösteriyor” – anahtar kelime gereksinimini karşılar.)* + +--- + +## Sonuç + +**docx to txt** dönüşümünü, her denklemi temiz LaTeX olarak koruyarak nasıl güvenilir bir şekilde yapacağınızı gösterdik. Anahtar, `OfficeMathExportMode.LaTeX` bayrağıdır; bu, Word'ün özel matematik formatını herhangi bir LaTeX motorunun anlayabileceği bir forma çevirir. Yukarıdaki tam kod örneğiyle **save word as txt**, **convert word to plain text** ve **export equations as latex** işlemlerini tek bir, bağımsız çalıştırmada gerçekleştirebilirsiniz. + +Deney yapmaktan çekinmeyin—çıktı uzantısını `.md` olarak değiştirerek Markdown elde edebilir veya snippet'i daha büyük bir belge‑işleme hattına entegre edebilirsiniz. Herhangi bir tuhaflıkla karşılaşırsanız, aşağıya yorum bırakın; yardımcı olmaktan memnuniyet duyarım. + +İyi kodlamalar! + +{{< /blocks/products/pf/tutorial-page-section >}} +{{< /blocks/products/pf/main-container >}} +{{< /blocks/products/pf/main-wrap-class >}} +{{< blocks/products/products-backtop-button >}} \ No newline at end of file diff --git a/words/vietnamese/net/ai-powered-document-processing/_index.md b/words/vietnamese/net/ai-powered-document-processing/_index.md index c297f59718..7f55143d58 100644 --- a/words/vietnamese/net/ai-powered-document-processing/_index.md +++ b/words/vietnamese/net/ai-powered-document-processing/_index.md @@ -42,6 +42,7 @@ Cuối cùng, đừng quên kiểm tra [Làm việc với tùy chọn tóm tắt | [Làm việc với mô hình AI của Google](./working-with-google-ai-model/) Nâng cao khả năng xử lý tài liệu của bạn với Aspose.Words cho .NET và Google AI để tạo bản tóm tắt ngắn gọn một cách dễ dàng. | | [Làm việc với mô hình AI mở](./working-with-open-ai-model/) | Mở khóa tóm tắt tài liệu hiệu quả bằng Aspose.Words cho .NET với các mô hình mạnh mẽ của OpenAI. Khám phá hướng dẫn toàn diện này ngay. | | [Làm việc với tùy chọn tóm tắt](./working-with-summarize-options/) | Học cách tóm tắt tài liệu Word hiệu quả bằng Aspose.Words cho .NET với hướng dẫn từng bước của chúng tôi về cách tích hợp các mô hình AI để có thông tin chi tiết nhanh chóng. | +| [Kết nối với LLM cục bộ trong C# – Hướng dẫn lập trình hoàn chỉnh](./connect-to-local-llm-in-c-complete-programming-guide/) | Hướng dẫn chi tiết cách kết nối Aspose.Words với LLM cục bộ trong C# để xử lý tài liệu thông minh. | {{< /blocks/products/pf/tutorial-page-section >}} diff --git a/words/vietnamese/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/_index.md b/words/vietnamese/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/_index.md new file mode 100644 index 0000000000..4b9ce0a39e --- /dev/null +++ b/words/vietnamese/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/_index.md @@ -0,0 +1,283 @@ +--- +category: general +date: 2026-04-28 +description: Kết nối tới LLM cục bộ từ C# và yêu cầu mô hình ngôn ngữ lớn tải tài + liệu Word, gọi LLM cục bộ và tự động viết lại văn bản. Bao gồm mã từng bước. +draft: false +keywords: +- connect to local llm +- prompt large language model +- load word document +- call local llm +- rewrite text automatically +language: vi +og_description: Kết nối tới LLM cục bộ từ C# và xem cách đưa ra lời nhắc cho mô hình + ngôn ngữ lớn, tải tài liệu Word, gọi LLM cục bộ và tự động viết lại văn bản trong + vài phút. +og_title: Kết nối tới LLM cục bộ trong C# – Hướng dẫn lập trình toàn diện +tags: +- Aspose.Words +- C# +- LLM +- AI Automation +title: Kết nối với LLM cục bộ trong C# – Hướng dẫn lập trình đầy đủ +url: /vi/net/ai-powered-document-processing/connect-to-local-llm-in-c-complete-programming-guide/ +--- + +{{< blocks/products/pf/main-wrap-class >}} +{{< blocks/products/pf/main-container >}} +{{< blocks/products/pf/tutorial-page-section >}} + +# Kết nối tới LLM cục bộ trong C# – Hướng dẫn lập trình đầy đủ + +Bạn đã bao giờ cần **kết nối tới LLM cục bộ** từ một ứng dụng .NET và tự hỏi làm sao để nó giao tiếp với một tệp Word chưa? Bạn không phải là người duy nhất. Trong hướng dẫn này, chúng ta sẽ đi qua toàn bộ quá trình — kết nối tới LLM cục bộ, **gửi lời nhắc tới mô hình ngôn ngữ lớn**, tải tài liệu Word, **gọi LLM cục bộ**, và cuối cùng **tự động viết lại văn bản**. Khi hoàn thành, bạn sẽ có một mẫu có thể chạy được, chuyển đổi bất kỳ đoạn văn nào thành giọng điệu trang trọng mà không cần khóa API bên ngoài. + +## Những gì hướng dẫn này sẽ đề cập + +Chúng ta sẽ bắt đầu bằng việc cài đặt các gói NuGet cần thiết, sau đó khởi động một endpoint LLM cục bộ đơn giản (nghĩa là Ollama trên cổng 11434). Tiếp theo, chúng ta sẽ tải một tệp `.docx` bằng Aspose.Words, gửi một đoạn văn tới LLM, nhận phiên bản đã được viết lại, và ghi lại vào cùng tài liệu. Bạn cũng sẽ thấy cách xử lý các vấn đề thường gặp — đoạn văn rỗng, việc giải phóng async, và các vấn đề mã hoá — để mã hoạt động trong môi trường sản xuất, không chỉ là bản demo. + +### Các điều kiện tiên quyết + +- .NET 6.0 SDK hoặc mới hơn (bạn cũng có thể dùng .NET 8 nếu muốn) +- Visual Studio 2022 hoặc VS Code với extension C# +- **Aspose.Words for .NET** (bản dùng thử miễn phí vẫn ổn) +- Một LLM được lưu trữ cục bộ hỗ trợ hợp đồng `/api/generate` (ví dụ: Ollama, LMStudio) +- Kiến thức cơ bản về async/await trong C# + +> **Mẹo chuyên nghiệp:** Nếu bạn chưa cài đặt Ollama, chạy `ollama serve` và tải mô hình bằng `ollama pull llama3`. Endpoint HTTP mặc định sẽ là `http://localhost:11434/api/generate`. + +--- + +## Bước 1: Cài đặt các gói cần thiết + +Đầu tiên, thêm các gói NuGet Aspose.Words và Aspose.Words.AI vào dự án của bạn. + +```bash +dotnet add package Aspose.Words +dotnet add package Aspose.Words.AI +``` + +Các thư viện này cung cấp khả năng **tải tài liệu Word** và một lớp bao bọc nhẹ để **gọi LLM cục bộ** mà không cần tự viết các yêu cầu HTTP. + +--- + +## Bước 2: Kết nối tới Endpoint LLM cục bộ + +Kết nối tới một mô hình được lưu trữ cục bộ đơn giản như việc khởi tạo `LocalLargeLanguageModel`. Constructor yêu cầu URL đầy đủ của endpoint tạo nội dung. + +```csharp +using Aspose.Words.AI; +using Aspose.Words; +using System.Threading.Tasks; + +// Create a client that talks to the LLM running on localhost +var localLlm = new LocalLargeLanguageModel("http://localhost:11434/api/generate"); +``` + +Tại sao chúng ta lại bọc endpoint trong một lớp? `LocalLargeLanguageModel` xử lý việc tuần tự hoá JSON, retry, và streaming response cho bạn — vì vậy bạn có thể tập trung vào logic lời nhắc thay vì phải loay hoay với `HttpClient`. + +--- + +## Bước 3: Tải tài liệu Word nguồn + +Tiếp theo, chúng ta đưa tài liệu vào bộ nhớ. Aspose.Words hỗ trợ hầu hết mọi định dạng Word, vì vậy `Document` sẽ phân tích `input.docx` mà không cần cài Office. + +```csharp +// Path to the source file – adjust as needed +string inputPath = Path.Combine(Environment.CurrentDirectory, "input.docx"); + +// Load the document; throws if the file is missing or corrupted +Document sourceDocument = new Document(inputPath); +``` + +Nếu bạn cần làm việc với một stream (ví dụ: tệp được tải lên qua ASP.NET), chỉ cần thay thế đường dẫn tệp bằng một `MemoryStream` và truyền nó vào constructor của `Document`. + +--- + +## Bước 4: Trích xuất văn bản đoạn văn hiện tại + +Chúng ta sẽ dùng `DocumentBuilder` để di chuyển trong tài liệu. Trong ví dụ này, chúng ta viết lại **đoạn văn đầu tiên**, nhưng bạn có thể lặp qua `sourceDocument.GetChildNodes(NodeType.Paragraph, true)` để xử lý nhiều đoạn. + +```csharp +// Builder gives us a cursor inside the document +DocumentBuilder docBuilder = new DocumentBuilder(sourceDocument); + +// Grab the text of the paragraph where the builder is currently positioned +string originalParagraph = docBuilder.CurrentParagraph?.GetText() ?? string.Empty; + +// Safety check – avoid sending empty strings to the LLM +if (string.IsNullOrWhiteSpace(originalParagraph)) +{ + Console.WriteLine("No paragraph found at the current cursor position."); + return; +} +``` + +Toán tử `?.` ngăn chặn `NullReferenceException` nếu tài liệu vô tình rỗng. Đây là một trong những **trường hợp góc cạnh** thường làm người mới bối rối. + +--- + +## Bước 5: Gửi lời nhắc tới LLM để viết lại đoạn văn + +Bây giờ chúng ta thực sự **gửi lời nhắc tới mô hình ngôn ngữ lớn**. Lời nhắc bằng tiếng Anh thuần túy; lớp bao bọc sẽ gửi nó dưới dạng JSON tới endpoint cục bộ. + +```csharp +// Build a friendly instruction for the model +string prompt = $"Rewrite the following sentence in a more formal tone:\n{originalParagraph}"; + +// Await the model's response – this is an async call +string rewrittenParagraph = await localLlm.PromptAsync(prompt); +``` + +Tại sao lại diễn đạt yêu cầu theo cách này? Các LLM phản hồi tốt nhất khi nhận được chỉ dẫn rõ ràng, đơn nhiệm. Thêm một dòng mới sau dấu hai chấm giúp tách biệt chỉ dẫn khỏi nội dung, giảm khả năng mô hình lặp lại lời nhắc. + +**Kết quả mong đợi** – Nếu `originalParagraph` là `"Hey, what's up?"`, LLM có thể trả về: + +> “Good day, how may I assist you?” + +Bạn có thể xác minh kết quả bằng cách in ra: + +```csharp +Console.WriteLine("Original: " + originalParagraph); +Console.WriteLine("Rewritten: " + rewrittenParagraph); +``` + +--- + +## Bước 6: Chèn văn bản đã viết lại trở lại tài liệu + +Với văn bản mới trong tay, chúng ta thay thế đoạn văn cũ. `DocumentBuilder.Writeln` ghi một dòng mới và di chuyển con trỏ về phía trước, rất phù hợp cho việc thêm vào. Nếu bạn muốn *thay thế* đúng đoạn văn hiện tại, có thể dùng `docBuilder.CurrentParagraph.RemoveAllChildren()` trước khi ghi. + +```csharp +// Option A – Append a new paragraph (keeps the original) +docBuilder.Writeln(rewrittenParagraph); + +// Option B – Replace the existing paragraph (uncomment to use) +// docBuilder.CurrentParagraph.RemoveAllChildren(); +// docBuilder.CurrentParagraph.AppendChild(new Run(docBuilder.Document, rewrittenParagraph)); +``` + +Cả hai cách đều được trình bày để bạn có thể chọn phương pháp phù hợp với quy trình của mình. + +--- + +## Bước 7: Lưu tài liệu đã cập nhật + +Cuối cùng, chúng ta ghi lại các thay đổi vào một tệp mới. Aspose.Words tự động chọn định dạng dựa trên phần mở rộng tệp. + +```csharp +string outputPath = Path.Combine(Environment.CurrentDirectory, "output.docx"); +sourceDocument.Save(outputPath); + +Console.WriteLine($"Document saved to {outputPath}"); +``` + +Mở `output.docx` trong Word, và bạn sẽ thấy đoạn văn bây giờ được viết bằng giọng điệu trang trọng. + +--- + +## Ví dụ hoàn chỉnh hoạt động + +Dưới đây là **chương trình đầy đủ, tự chứa**. Sao chép‑dán vào một dự án console, khôi phục các gói NuGet, và chạy — không cần cấu hình thêm nào ngoài việc có một LLM cục bộ đang chạy. + +```csharp +using Aspose.Words.AI; +using Aspose.Words; +using System; +using System.IO; +using System.Threading.Tasks; + +class Program +{ + static async Task Main() + { + // ------------------------------------------------- + // Step 1: Connect to the locally hosted LLM endpoint + // ------------------------------------------------- + var localLlm = new LocalLargeLanguageModel("http://localhost:11434/api/generate"); + + // ------------------------------------------------- + // Step 2: Load the source Word document + // ------------------------------------------------- + string inputPath = Path.Combine(Environment.CurrentDirectory, "input.docx"); + Document sourceDocument = new Document(inputPath); + + // ------------------------------------------------- + // Step 3: Retrieve the text of the current paragraph + // ------------------------------------------------- + DocumentBuilder docBuilder = new DocumentBuilder(sourceDocument); + string originalParagraph = docBuilder.CurrentParagraph?.GetText() ?? string.Empty; + + if (string.IsNullOrWhiteSpace(originalParagraph)) + { + Console.WriteLine("No paragraph found at the current cursor position."); + return; + } + + // ------------------------------------------------- + // Step 4: Ask the LLM to rewrite the paragraph in a formal tone + // ------------------------------------------------- + string prompt = $"Rewrite the following sentence in a more formal tone:\n{originalParagraph}"; + string rewrittenParagraph = await localLlm.PromptAsync(prompt); + + // ------------------------------------------------- + // Step 5: Insert the rewritten text back into the document + // ------------------------------------------------- + docBuilder.Writeln(rewrittenParagraph); + + // ------------------------------------------------- + // Step 6: Save the updated document + // ------------------------------------------------- + string outputPath = Path.Combine(Environment.CurrentDirectory, "output.docx"); + sourceDocument.Save(outputPath); + + Console.WriteLine("Original paragraph:"); + Console.WriteLine(originalParagraph); + Console.WriteLine("\nRewritten paragraph:"); + Console.WriteLine(rewrittenParagraph); + Console.WriteLine($"\nDocument saved to {outputPath}"); + } +} +``` + +### Những gì sẽ xảy ra khi bạn chạy + +1. Console in ra đoạn văn gốc và đoạn văn đã được viết lại. +2. `output.docx` xuất hiện cạnh `input.docx`. +3. Mở tệp, bạn sẽ thấy đoạn văn trang trọng mới được chèn sau đoạn gốc (hoặc được thay thế, nếu bạn dùng đoạn mã thay thế). + +--- + +## Xử lý các trường hợp góc cạnh thường gặp + +| Tình huống | Giải pháp | +|-----------|----------| +| **Đoạn văn rỗng hoặc chỉ chứa khoảng trắng** | Kiểm tra `string.IsNullOrWhiteSpace` trước khi gửi lời nhắc (xem Bước 3). | +| **LLM trả về lỗi hoặc chuỗi rỗng** | Bao `PromptAsync` trong `try/catch` và dùng lại văn bản gốc nếu cần. | +| **Nhiều đoạn văn cần viết lại** | Lặp qua `sourceDocument.GetChildNodes(NodeType.Paragraph, true)` và áp dụng cùng logic lời nhắc. | +| **Tài liệu lớn gây độ trễ** | Gộp các đoạn văn và gửi chúng trong một yêu cầu duy nhất (lời nhắc tối đa ~4 KB mỗi lần). | +| **Ký tự không phải ASCII bị lỗi** | Đảm bảo endpoint LLM sử dụng UTF‑8 (hầu hết các mô hình hiện đại đều vậy). | + +--- + +## Các bước tiếp theo & Chủ đề liên quan + +- **Gửi lời nhắc tới mô hình ngôn ngữ lớn** với chỉ dẫn chi tiết hơn (ví dụ: hướng dẫn phong cách, giới hạn độ dài). +- Sử dụng **gọi LLM cục bộ** trong một Web API để cung cấp dịch vụ tự động hoá tài liệu. +- Khám phá **tải tài liệu Word** trong các stream song song để đạt hiệu suất cao. +- Kết hợp cách tiếp cận này với **viết lại văn bản tự động** để tạo email hàng loạt hoặc chuẩn hoá báo cáo. + +Nếu muốn đào sâu hơn, hãy tham khảo tài liệu của Aspose về **document merging** và tham chiếu API của Ollama để tùy chỉnh các tham số sampling. + +--- + +## Kết luận + +Chúng ta vừa minh họa cách **kết nối tới LLM cục bộ** từ C#, **gửi lời nhắc tới mô hình ngôn ngữ lớn**, **tải tài liệu Word**, **gọi LLM cục bộ**, và **viết lại văn bản tự động** — tất cả trong một ứng dụng console có thể chạy ngay. Mô hình này có thể mở rộng: thay đổi lời nhắc, lặp qua các đoạn văn, hoặc đưa logic ra một endpoint ASP.NET. Điều quan trọng là các mô hình AI cục bộ có thể được tích hợp chặt chẽ với các thư viện xử lý tài liệu truyền thống, mang lại khả năng tự động hoá mạnh mẽ mà không cần rời khỏi môi trường on‑prem đáng tin cậy của bạn. + +Có câu hỏi nào về threading không, + +{{< /blocks/products/pf/tutorial-page-section >}} +{{< /blocks/products/pf/main-container >}} +{{< /blocks/products/pf/main-wrap-class >}} +{{< blocks/products/products-backtop-button >}} \ No newline at end of file diff --git a/words/vietnamese/net/programming-with-markdownsaveoptions/_index.md b/words/vietnamese/net/programming-with-markdownsaveoptions/_index.md index 402ae5906c..986af96f9b 100644 --- a/words/vietnamese/net/programming-with-markdownsaveoptions/_index.md +++ b/words/vietnamese/net/programming-with-markdownsaveoptions/_index.md @@ -41,6 +41,7 @@ Words Processing with MarkdownSaveOptions là một nguồn tài nguyên chuyên | [Cách xuất LaTeX: Chuyển DOCX sang Markdown & TXT](./how-to-export-latex-convert-docx-to-markdown-txt/) | Hướng dẫn chi tiết cách xuất nội dung LaTeX từ DOCX sang Markdown và TXT bằng C# và Aspose.Words. | | [Cách đổi tên hình ảnh khi chuyển DOCX sang Markdown](./how-to-rename-images-when-converting-docx-to-markdown/) | Hướng dẫn chi tiết cách đổi tên các hình ảnh trong quá trình chuyển đổi tài liệu DOCX sang định dạng Markdown bằng Aspose.Words cho .NET. | | [Lưu docx thành markdown với Aspose.Words – Hướng dẫn đầy đủ C#](./save-docx-as-markdown-with-aspose-words-full-c-guide/) | Hướng dẫn chi tiết cách lưu file docx thành markdown bằng C# với Aspose.Words, bao gồm các tùy chọn và ví dụ thực tế. | +| [Đường dẫn tương đối hình ảnh markdown – Chuyển Word sang Markdown](./markdown-image-relative-path-convert-word-to-markdown/) | Hướng dẫn cách sử dụng đường dẫn tương đối cho hình ảnh khi chuyển tài liệu Word sang Markdown bằng Aspose.Words cho .NET. | {{< /blocks/products/pf/tutorial-page-section >}} diff --git a/words/vietnamese/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/_index.md b/words/vietnamese/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/_index.md new file mode 100644 index 0000000000..fcc68035d0 --- /dev/null +++ b/words/vietnamese/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/_index.md @@ -0,0 +1,188 @@ +--- +category: general +date: 2026-04-28 +description: Tìm hiểu cách đặt đường dẫn tương đối cho ảnh markdown khi chuyển Word + sang markdown, trích xuất ảnh từ Word và tạo thư mục resources cho các ảnh đã xuất. +draft: false +keywords: +- markdown image relative path +- convert word to markdown +- extract images from word +- create resources folder +- export images from docx +language: vi +og_description: Đặt đường dẫn tương đối cho ảnh markdown khi bạn chuyển đổi Word sang + markdown, trích xuất ảnh từ Word và tạo thư mục resources cho các ảnh đã xuất. +og_title: đường dẫn tương đối của hình ảnh markdown – Chuyển Word sang Markdown +tags: +- Aspose.Words +- C# +- Markdown +- Image Export +title: Đường dẫn tương đối cho hình ảnh markdown – Chuyển Word sang Markdown +url: /vi/net/programming-with-markdownsaveoptions/markdown-image-relative-path-convert-word-to-markdown/ +--- + +{{< blocks/products/pf/main-wrap-class >}} +{{< blocks/products/pf/main-container >}} +{{< blocks/products/pf/tutorial-page-section >}} + +# Đường dẫn ảnh markdown tương đối – Chuyển Word sang Markdown + +Bạn đã bao giờ cần một **markdown image relative path** khi **convert Word to markdown** chưa? Bạn không phải là người duy nhất. Hầu hết các nhà phát triển gặp khó khăn khi Markdown được tạo ra trỏ tới các ảnh trong một thư mục phẳng, làm phá vỡ cấu trúc liên kết tương đối mà bạn mong đợi trong một trang tĩnh hoặc repo GitHub. + +Trong tutorial này, chúng ta sẽ đi qua một giải pháp hoàn chỉnh, từ đầu đến cuối mà **extracts images from Word**, **creates a resources folder**, và ghi lại các tham chiếu ảnh sao cho chúng sử dụng một *markdown image relative path* sạch sẽ. Khi kết thúc, bạn sẽ có một tệp `.md` sẵn sàng xuất bản và một thư mục `Resources` được tổ chức gọn gàng chứa mọi hình ảnh được trích xuất từ tệp `.docx` gốc. + +> **Bạn sẽ nhận được:** một chương trình C# duy nhất (không có script bên ngoài), một giải thích rõ ràng về *tại sao* mỗi phần quan trọng, và một vài mẹo thực tế mà bạn có thể sao chép‑dán vào dự án của mình. + +--- + +## Yêu cầu trước + +- **.NET 6.0** hoặc phiên bản mới hơn đã được cài đặt (bạn cũng có thể nhắm mục tiêu .NET Framework 4.7+, nhưng .NET 6 là lựa chọn tối ưu cho các dự án mới). +- **Aspose.Words for .NET** (gói NuGet mới nhất tại thời điểm viết, phiên bản 23.12). Cài đặt bằng: + ```bash + dotnet add package Aspose.Words + ``` +- Một tài liệu Word thực sự chứa ảnh—gọi nó là `WithImages.docx`. +- Một thư mục nơi bạn muốn lưu markdown đầu ra và các ảnh, ví dụ `C:\Projects\MarkdownExport`. +- Không cần thư viện bổ sung; mọi thứ khác được Aspose.Words xử lý. + +## Bước 1: Tải tài liệu Word nguồn (điểm khởi đầu để convert word to markdown) + +```csharp +using System; +using System.IO; +using Aspose.Words; +using Aspose.Words.Saving; + +class Program +{ + static void Main() + { + // Adjust the path to point at your own .docx file. + string sourcePath = @"C:\Projects\MarkdownExport\WithImages.docx"; + + // Load the document – this is where Aspose.Words parses the Word file. + Document doc = new Document(sourcePath); + + // The rest of the workflow follows… + } +} +``` + +*Tại sao điều này quan trọng:* Việc tải tài liệu cho phép chúng ta truy cập vào cây node nội bộ, bao gồm các phần ảnh mà sau này chúng ta cần **export images from docx**. Nếu việc tải thất bại, các bước sau sẽ không chạy, vì vậy hãy kiểm tra lại đường dẫn và quyền truy cập tệp. + +## Bước 2: Cấu hình `MarkdownSaveOptions` với callback tùy chỉnh (trái tim của việc tạo thư mục resources) + +`ResourceSavingCallback` cho phép chúng ta can thiệp mỗi khi Aspose.Words muốn ghi một tệp ảnh. Trong callback, chúng ta sẽ **create a Resources sub‑folder** và điều chỉnh tham chiếu sao cho markdown được tạo ra sử dụng một *markdown image relative path*. + +```csharp +// Inside Main(), after loading the document: +string outputFolder = @"C:\Projects\MarkdownExport"; +string resourcesFolder = Path.Combine(outputFolder, "Resources"); + +// Make sure the folder exists before we start saving anything. +Directory.CreateDirectory(resourcesFolder); + +// Set up the Markdown save options. +MarkdownSaveOptions mdOptions = new MarkdownSaveOptions +{ + // Hook that runs for every image resource. + ResourceSavingCallback = new MyMarkdownResourceCallback(resourcesFolder) +}; + +// Save the document as Markdown. +string markdownPath = Path.Combine(outputFolder, "Doc.md"); +doc.Save(markdownPath, mdOptions); +``` + +Lưu ý chúng tôi đã truyền `resourcesFolder` vào constructor của callback—điều này giữ cho đường dẫn thư mục linh hoạt và tránh việc hard‑coding chuỗi trong toàn bộ mã. + +## Bước 3: Triển khai callback **creates resources folder** và ghi lại đường dẫn + +```csharp +/// +/// Handles image extraction and path rewriting for markdown export. +/// +class MyMarkdownResourceCallback : IResourceSavingCallback +{ + private readonly string _resourcesFolder; + + public MyMarkdownResourceCallback(string resourcesFolder) + { + _resourcesFolder = resourcesFolder; + } + + public void ResourceSaving(ResourceSavingArgs args) + { + // 1️⃣ Build the full file system path where the image will be stored. + string targetPath = Path.Combine(_resourcesFolder, args.ResourceFileName); + + // 2️⃣ Ensure the directory exists (in case Aspose creates sub‑folders). + Directory.CreateDirectory(Path.GetDirectoryName(targetPath)); + + // 3️⃣ Write the image stream to disk. + using (FileStream fileStream = File.Create(targetPath)) + { + args.Stream.CopyTo(fileStream); + } + + // 4️⃣ Update the markdown reference to use a relative path. + // This is the crucial line that gives us the markdown image relative path. + args.ResourceFileName = Path.Combine("Resources", args.ResourceFileName); + } +} +``` + +*Tại sao cách này hoạt động:* `args.Stream` chứa dữ liệu ảnh thô. Bằng cách sao chép nó vào một tệp trong thư mục `Resources` của chúng ta, chúng ta **export images from docx** một cách an toàn. Sau đó chúng ta thay thế `args.ResourceFileName` bằng một URL tương đối (`Resources/image.png`). Khi Aspose.Words sau này ghi markdown, nó sẽ chèn chính chuỗi đó, mang lại cho chúng ta *markdown image relative path* mong muốn. + +## Bước 4: Xác minh Markdown đã tạo (kết quả cuối cùng trông như thế nào) + +Mở `Doc.md` trong bất kỳ trình soạn thảo văn bản nào. Bạn sẽ thấy một nội dung tương tự như: + +```markdown +# Sample Heading + +Here is an inline picture: + +![Image 0](Resources/Image_0.png) + +And a picture inside a table: + +![Image 1](Resources/Image_1.jpg) +``` + +Phần quan trọng là mỗi tham chiếu ảnh đều trỏ tới `Resources/...` – đó là **markdown image relative path** mà chúng ta đang tìm kiếm. + +![markdown image relative path example](example.png "markdown image relative path example") + +*Mẹo:* Nếu bạn mở markdown trong một trình xem hỗ trợ liên kết tương đối (xem trước VS Code, GitHub, hoặc một trình tạo site tĩnh), các hình ảnh sẽ hiển thị đúng mà không cần cấu hình thêm. + +## Bước 5: Những lỗi thường gặp và mẹo chuyên nghiệp + +| Vấn đề | Tại sao xảy ra | Cách khắc phục | +|-------|----------------|---------------| +| Ảnh bị lưu vào thư mục gốc thay vì `Resources` | Callback chưa được gắn hoặc `args.ResourceFileName` chưa được ghi đè. | Kiểm tra lại rằng `ResourceSavingCallback` được thiết lập **trước** khi gọi `doc.Save`. | +| Tên tệp chứa ký tự không hợp lệ | Word đôi khi đặt tên ảnh có dấu cách hoặc ký tự Unicode. | Sử dụng `Path.GetInvalidFileNameChars()` để làm sạch `args.ResourceFileName` trong callback. | +| Tài liệu lớn mất thời gian xử lý lâu | Mỗi ảnh được ghi đồng bộ. | Chuyển sang I/O bất đồng bộ (`await args.Stream.CopyToAsync(fileStream)`) nếu bạn đang dùng .NET 6+ và cần hiệu năng. | +| Đường dẫn tương đối bị hỏng khi markdown được di chuyển | Đường dẫn tương đối với vị trí tệp markdown. | Giữ `Doc.md` và thư mục `Resources` cùng nhau, hoặc điều chỉnh callback để sử dụng tiền tố tương đối khác (ví dụ, `../assets`). | + +## Bước 6: Mở rộng giải pháp (nếu bạn cần kiểm soát nhiều hơn?) + +- **Multiple output formats:** Thay thế `MarkdownSaveOptions` bằng `HtmlSaveOptions` hoặc `PdfSaveOptions` trong khi vẫn giữ callback giống nhau—Aspose.Words sẽ gọi nó cho mọi ảnh bất kể định dạng. +- **Custom image naming:** Nếu bạn muốn đổi tên ảnh (ví dụ, `figure-01.png`), sửa `args.ResourceFileName` trong callback trước khi ghi tệp. +- **Embedding images as Base64:** Đặt `args.ResourceFileName` thành một data URI (`data:image/png;base64,...`) và bỏ qua việc ghi tệp. Điều này hữu ích cho việc xuất markdown thành một tệp duy nhất. + +## Kết luận + +Bây giờ bạn đã có một chương trình C# hoàn chỉnh có khả năng **converts Word to markdown**, **extracts images from word**, **creates a resources folder**, và đảm bảo một **markdown image relative path** sạch sẽ cho mọi hình ảnh. Mã nguồn tự chứa, hoạt động với phiên bản Aspose.Words mới nhất, và có thể được đưa vào bất kỳ dự án .NET nào với ít nỗ lực. + +Bước tiếp theo? Hãy thử đưa markdown đã tạo vào một trình tạo site tĩnh như Hugo hoặc Jekyll, hoặc thử nghiệm callback để nhúng ảnh trực tiếp dưới dạng chuỗi Base64. Nếu gặp các trường hợp đặc biệt—ví dụ, ảnh SVG hoặc tệp rất lớn—hãy quay lại bảng “Common pitfalls”; một chỉnh sửa nhỏ thường giải quyết được vấn đề. + +Chúc lập trình vui vẻ, và mong markdown của bạn luôn trỏ tới đúng thư mục! + +{{< /blocks/products/pf/tutorial-page-section >}} +{{< /blocks/products/pf/main-container >}} +{{< /blocks/products/pf/main-wrap-class >}} +{{< blocks/products/products-backtop-button >}} \ No newline at end of file diff --git a/words/vietnamese/net/programming-with-officemath/_index.md b/words/vietnamese/net/programming-with-officemath/_index.md index 4541987239..e7bd4b1f4e 100644 --- a/words/vietnamese/net/programming-with-officemath/_index.md +++ b/words/vietnamese/net/programming-with-officemath/_index.md @@ -23,6 +23,7 @@ Hướng dẫn Aspose.Words for .NET về Xử lý Words với OfficeMath sẽ h | [Phương trình toán học](./math-equations/) | Tìm hiểu cách cấu hình các phương trình toán học trong tài liệu Word bằng Aspose.Words cho .NET. Hướng dẫn từng bước với các ví dụ, câu hỏi thường gặp và nhiều hơn nữa. | | [Lưu docx thành txt – Xuất Word Math sang LaTeX bằng C#](./save-docx-as-txt-export-word-math-to-latex-with-c/) | Hướng dẫn lưu tài liệu docx dưới dạng txt và xuất các công thức Word Math sang LaTeX bằng C#. | | [Lưu tài liệu dưới dạng Txt – Xuất công thức Word sang LaTeX trong C#](./save-document-as-txt-export-word-math-to-latex-in-c/) | Hướng dẫn lưu tài liệu Word dưới dạng file txt và xuất các công thức toán học sang LaTeX bằng C#. | +| [Chuyển DOCX sang TXT – Xuất công thức Word sang LaTeX trong C#](./convert-docx-to-txt-export-word-equations-to-latex-in-c/) | Hướng dẫn lưu tài liệu DOCX dưới dạng TXT và xuất các công thức Word sang LaTeX bằng C#. | {{< /blocks/products/pf/tutorial-page-section >}} diff --git a/words/vietnamese/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/_index.md b/words/vietnamese/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/_index.md new file mode 100644 index 0000000000..deee2a1b93 --- /dev/null +++ b/words/vietnamese/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/_index.md @@ -0,0 +1,211 @@ +--- +category: general +date: 2026-04-28 +description: Chuyển đổi DOCX sang TXT và xuất các công thức Word sang LaTeX bằng Aspose.Words. + Tìm hiểu cách lưu Word dưới dạng TXT và xử lý các đối tượng toán học trong vài bước. +draft: false +keywords: +- convert docx to txt +- convert word equations to latex +- convert word to plain text +- save word as txt +- export equations as latex +language: vi +og_description: Chuyển DOCX sang TXT và xuất các phương trình Word sang LaTeX bằng + đoạn mã C# đơn giản. Hướng dẫn đầy đủ, mã nguồn và mẹo. +og_title: Chuyển DOCX sang TXT – Xuất các phương trình Word sang LaTeX +tags: +- C# +- Aspose.Words +- Document Conversion +title: Chuyển DOCX sang TXT – Xuất các phương trình Word sang LaTeX trong C# +url: /vi/net/programming-with-officemath/convert-docx-to-txt-export-word-equations-to-latex-in-c/ +--- + +{{< blocks/products/pf/main-wrap-class >}} +{{< blocks/products/pf/main-container >}} +{{< blocks/products/pf/tutorial-page-section >}} + +# Chuyển DOCX sang TXT – Xuất Phương Trình Word sang LaTeX + +Bạn đã bao giờ cần **convert docx to txt** nhưng lo lắng rằng các công thức trong file Word sẽ biến thành một mớ hỗn độn? Bạn không đơn độc. Trong nhiều dự án kỹ thuật hoặc học thuật, tài liệu nguồn ở dạng .docx, nhưng các công cụ hạ nguồn chỉ hiểu plain‑text hoặc LaTeX. Tin tốt là gì? Chỉ với vài dòng C# và Aspose.Words, bạn có thể **convert docx to txt** *và* giữ mọi công thức dưới dạng mã LaTeX sạch sẽ. + +Trong tutorial này, chúng ta sẽ đi qua toàn bộ quy trình: tải một .docx, cấu hình các tùy chọn lưu để các đối tượng Office Math chuyển thành LaTeX, và cuối cùng ghi kết quả vào file .txt. Khi kết thúc, bạn sẽ biết cách **save word as txt**, **convert word to plain text**, và **export equations as latex** mà không phải mò mẫm tài liệu API. + +## Những gì bạn sẽ học + +- Các lời gọi API chính xác cần thiết để **convert docx to txt** đồng thời bảo toàn các công thức. +- Tại sao việc chọn `OfficeMathExportMode.LaTeX` là cách được khuyến nghị để **convert word equations to latex**. +- Cách xử lý các trường hợp biên thường gặp như thiếu phông chữ hoặc tính năng công thức không được hỗ trợ. +- Một chương trình C# hoàn chỉnh, sẵn sàng chạy mà bạn có thể đưa vào bất kỳ dự án .NET nào. + +### Yêu cầu trước + +- .NET 6.0 hoặc mới hơn (mã cũng hoạt động trên .NET Framework 4.7+). +- Giấy phép Aspose.Words for .NET (bản dùng thử miễn phí đủ cho việc đánh giá). +- Một tài liệu Word (`input.docx`) chứa ít nhất một đối tượng Office Math. + +Nếu đã có những thứ trên, chúng ta bắt đầu thôi. + +## Bước 1: Cài đặt Aspose.Words + +Trước khi bất kỳ đoạn mã nào chạy, bạn cần thư viện. Mở terminal trong thư mục dự án và thực thi: + +```bash +dotnet add package Aspose.Words +``` + +Lệnh này sẽ tải phiên bản ổn định mới nhất (tính đến 2026‑04‑28 v24.12). Không cần DLL bổ sung nào. + +## Bước 2: Tải tài liệu nguồn + +Điều đầu tiên chúng ta làm là đọc file .docx vào một đối tượng `Document`. Đối tượng này cho phép truy cập đầy đủ vào cấu trúc file, bao gồm các đoạn văn bản, hình ảnh và đối tượng toán học. + +```csharp +using Aspose.Words; +using Aspose.Words.Saving; + +class Program +{ + static void Main() + { + // Step 2: Load the source document + Document doc = new Document(@"YOUR_DIRECTORY\input.docx"); +``` + +> **Tại sao điều này quan trọng:** Việc tải tài liệu tạo ra một biểu diễn trong bộ nhớ, vì vậy sau này chúng ta có thể tinh chỉnh cách mỗi thành phần được ghi ra. Nếu file không tồn tại, Aspose sẽ ném `FileNotFoundException`, bạn có thể muốn bắt lỗi này trong code production. + +## Bước 3: Cấu hình TXT Save Options cho LaTeX Math + +Mặc định, `Document.Save` ghi plain text và **bỏ qua** mọi Office Math. Để giữ lại các công thức, chúng ta đặt `OfficeMathExportMode` thành `LaTeX`. Điều này yêu cầu bộ xuất chuyển đổi mỗi công thức sang dạng LaTeX tương ứng. + +```csharp + // Step 3: Configure TXT save options to export Office Math as LaTeX + TxtSaveOptions txtOptions = new TxtSaveOptions + { + OfficeMathExportMode = OfficeMathExportMode.LaTeX, + // Optional: preserve line breaks as they appear in the original Word file + PreserveTableLayout = true + }; +``` + +> **Mẹo chuyên nghiệp:** Nếu bạn chỉ cần các ký tự Unicode thô của công thức (ví dụ, để xem nhanh), có thể dùng `OfficeMathExportMode.Text`. Nhưng đối với hầu hết các pipeline khoa học, `LaTeX` là tiêu chuẩn vàng vì nó được mọi bộ xử lý LaTeX hiểu chung. + +## Bước 4: Lưu tài liệu dưới dạng Plain‑Text + +Bây giờ chúng ta ghi nội dung đã chuyển đổi vào file `.txt`. File sẽ chứa các đoạn văn thông thường, danh sách gạch đầu dòng, và—nhờ bước trước—các đoạn mã LaTeX cho mọi công thức. + +```csharp + // Step 4: Save the document as plain‑text using the configured options + doc.Save(@"YOUR_DIRECTORY\Math.txt", txtOptions); + } +} +``` + +Khi mở `Math.txt` bạn sẽ thấy nội dung tương tự: + +``` +In this report we derive the quadratic formula: +\[ +x = \frac{-b \pm \sqrt{b^{2} - 4ac}}{2a} +\] + +The end. +``` + +Chú ý các dấu `\[` … `\]`? Đó là các khối toán học LaTeX được tạo tự động. + +## Bước 5: Kiểm tra đầu ra (Tùy chọn nhưng nên làm) + +Rất dễ bỏ lỡ một vấn đề chuyển đổi tinh tế, đặc biệt khi công thức chứa ký hiệu tùy chỉnh. Một kiểm tra nhanh là đưa file `.txt` đã tạo vào trình biên dịch LaTeX (ví dụ, `pdflatex`) và xem nó có biên dịch thành công hay không. + +```bash +pdflatex -interaction=nonstopmode Math.txt +``` + +Nếu biên dịch thành công, bạn đã thực hiện **convert word equations to latex** và **convert docx to txt** trong một bước. Nếu gặp lỗi, tìm các thông báo về lệnh không xác định—thường chỉ ra tính năng công thức mà Aspose.Words không thể dịch (ví dụ, một số ký hiệu ma trận). Trong trường hợp đó, bạn có thể quay lại `OfficeMathExportMode.MathML` và chuyển đổi MathML sang LaTeX bằng công cụ khác. + +## Các lỗi thường gặp & Cách tránh + +| Vấn đề | Nguyên nhân | Giải pháp | +|-------|------------|-----------| +| Thiếu phông chữ | Aspose.Words cần phông chữ để hiển thị ký hiệu đúng. | Cài đặt phông chữ còn thiếu trên máy hoặc nhúng nó vào file .docx. | +| Công thức phức tạp không được xuất | Một số tính năng Office Math mới chưa được ánh xạ sang LaTeX. | Dùng `OfficeMathExportMode.MathML` rồi chuyển đổi sang LaTeX bằng thư viện MathML‑to‑LaTeX. | +| Dòng trống thừa | Trình lưu plain‑text giữ lại ngắt đoạn, có thể tạo ra khoảng trắng dư. | Đặt `txtOptions.AddBidiMarks = false` hoặc xử lý hậu kỳ file bằng script đơn giản. | + +## Ví dụ Hoàn chỉnh (Sẵn sàng Copy‑Paste) + +Dưới đây là toàn bộ chương trình, sẵn sàng biên dịch. Thay `YOUR_DIRECTORY` bằng thư mục chứa `input.docx` của bạn. + +```csharp +using System; +using Aspose.Words; +using Aspose.Words.Saving; + +namespace DocxToTxtWithLatex +{ + class Program + { + static void Main() + { + try + { + // Load the source document + Document doc = new Document(@"C:\Docs\input.docx"); + + // Configure save options: export equations as LaTeX + TxtSaveOptions txtOptions = new TxtSaveOptions + { + OfficeMathExportMode = OfficeMathExportMode.LaTeX, + PreserveTableLayout = true, + AddBidiMarks = false + }; + + // Save as plain‑text + string outputPath = @"C:\Docs\Math.txt"; + doc.Save(outputPath, txtOptions); + + Console.WriteLine($"Successfully converted DOCX to TXT. Output at: {outputPath}"); + } + catch (Exception ex) + { + Console.Error.WriteLine($"Conversion failed: {ex.Message}"); + } + } + } +} +``` + +Chạy chương trình này sẽ **save word as txt** đồng thời biến mọi khối Office Math thành LaTeX, cho bạn một file plain‑text sạch sẽ, có thể tìm kiếm được. + +## Các bước tiếp theo & Chủ đề liên quan + +- **Chuyển đổi hàng loạt:** Đặt logic trên trong một vòng `foreach` để xử lý toàn bộ thư mục các file .docx. +- **Kết hợp với tạo PDF:** Sau khi có các đoạn LaTeX, đưa chúng vào pipeline PDF (ví dụ, `PdfSharp` + `MiKTeX`) để tạo báo cáo PDF. +- **Export equations as latex** cho các định dạng khác: Aspose.Words cũng hỗ trợ `SaveFormat.Markdown`, có thể nhúng LaTeX tự động. +- **Tối ưu hiệu năng:** Đối với tài liệu lớn, tái sử dụng cùng một thể hiện `TxtSaveOptions` và tắt các tính năng không cần như `AddBidiMarks`. + +--- + +### Ví dụ Hình ảnh (Tùy chọn) + +Nếu bạn thích một gợi ý trực quan, đây là ảnh chụp màn hình của file đầu ra trong Notepad++. + +![convert docx to txt output showing LaTeX equations](convert-docx-to-txt-output.png) + +*(Alt text: “convert docx to txt output showing LaTeX equations” – đáp ứng yêu cầu từ khóa chính.)* + +--- + +## Kết luận + +Chúng ta vừa trình bày một cách đáng tin cậy để **convert docx to txt** đồng thời bảo toàn mọi công thức dưới dạng LaTeX sạch sẽ. Yếu tố then chốt là cờ `OfficeMathExportMode.LaTeX`, chuyển đổi định dạng toán học độc quyền của Word sang thứ mà bất kỳ engine LaTeX nào cũng hiểu. Với mẫu mã đầy đủ ở trên, bạn có thể **save word as txt**, **convert word to plain text**, và **export equations as latex** trong một lần chạy tự chứa. + +Hãy thử nghiệm—đổi phần mở rộng đầu ra thành `.md` để có Markdown, hoặc tích hợp đoạn mã vào pipeline xử lý tài liệu lớn hơn. Nếu gặp bất kỳ vấn đề nào, để lại bình luận bên dưới; mình sẵn sàng hỗ trợ khắc phục. + +Chúc lập trình vui vẻ! + +{{< /blocks/products/pf/tutorial-page-section >}} +{{< /blocks/products/pf/main-container >}} +{{< /blocks/products/pf/main-wrap-class >}} +{{< blocks/products/products-backtop-button >}} \ No newline at end of file