يصف هذا الدليل السريع ** كيفية البحث عن نص واستبداله في PDF باستخدام C# ** بمساعدة خطوات مفصلة وكود قابل للتشغيل. يساعد في تكوين البيئة ثم يوفر عملية خطوة بخطوة ** لاستبدال النص في PDF باستخدام C# **. بمجرد تحديث الملف ، يمكنك حفظه مرة أخرى على القرص بالتنسيق الأصلي مثل PDF أو كـ DOCX ، Excel ، HTML ، إلخ على سبيل المثال لا الحصر.
خطوات البحث عن النص واستبداله في PDF باستخدام C#
- تكوين المشروع لاستخدام Aspose.PDF for .NET باستخدام مدير حزمة NuGet
- قم بإنشاء أو تحميل ملف PDF يحتوي على نموذج نص باستخدام كائن فئة Document
- باستخدام كائن فئة TextFragmentAbsorber ، قم بتعيين النص الذي سيتم البحث فيه
- لجميع الصفحات في ملف PDF المدخل ، اقبل ممتص النص
- احصل على مجموعة الأجزاء حيث يتم استخراج النص من ملف PDF المحمل
- تحليل من خلال جميع الأجزاء ووضع نص جديد
- احفظ ملف PDF المحدث
تصف هذه الخطوات كيفية البحث في * PDF واستبدال النص باستخدام C# *. يتم إنشاء ملف جديد مع بعض نماذج النص ولكن يمكنك تحميل ملف PDF موجود سيتم استبدال نصه. هناك مجموعة متنوعة من الخيارات المتاحة للبحث عن نص في ملف PDF مثل تجاهل نص الظل ، وقصر البحث على الصفحة ، وما إلى ذلك.
كود لاستبدال النص في PDF باستخدام C#
using Aspose.Pdf; | |
using Aspose.Pdf.Text; | |
namespace FindAndReplaceTextInPdfUsingCSharp | |
{ | |
class Program | |
{ | |
static void Main(string[] args) // Main function to create 7z archive in CSharp | |
{ | |
// Instantiate a license to avoid watermark in output PDF | |
Aspose.Pdf.License licForPdf= new Aspose.Pdf.License(); | |
licForPdf.SetLicense("Aspose.Pdf.lic"); | |
// Create an empty PDF document | |
Document newPDFFile = new Document(); | |
// Add an empty page in the newly created PDF | |
Page page = newPDFFile.Pages.Add(); | |
// Add sample text in the PDF file | |
for(int iTxtCounter = 0 ; iTxtCounter < 15; iTxtCounter++) | |
page.Paragraphs.Add(new Aspose.Pdf.Text.TextFragment($"my_data\nanother data")); | |
// Save the newly created PDF file containing the test data in it | |
newPDFFile.Save("InputPDFToReplaceText.pdf"); | |
// Open PDF document to replace text in it | |
Document inputPDFFile = new Document("InputPDFToReplaceText.pdf"); | |
// Set the text that is to be searched in the TextAbsorber object | |
TextFragmentAbsorber txtAbsorber = new TextFragmentAbsorber("my_data"); | |
// Apply the text absorber for all the pages in the input PDF file | |
inputPDFFile.Pages.Accept(txtAbsorber); | |
// Get the collection of fragments containing extracted text from the PDF | |
TextFragmentCollection textFragmentCollection = txtAbsorber.TextFragments; | |
// Parse all the fragments and replace text using particular font, size and foreground/background color | |
foreach (TextFragment txtFragment in textFragmentCollection) | |
txtFragment.Text = "MY_DATA"; | |
// Save resulting PDF document. | |
inputPDFFile.Save("OutputPDFAfterReplacingText.pdf"); | |
System.Console.WriteLine("Done"); | |
} | |
} | |
} |
يستخدم هذا الرمز TextFragmentAbsorber و TextFragment للنص * للبحث والاستبدال في PDF باستخدام C# *. لا يمكنك استبدال النص فحسب ، بل يمكنك أيضًا تغيير عائلة الخط والحجم ولون المقدمة ولون الخلفية في ملف PDF الناتج. تتوفر الخيارات أيضًا لاستبدال النص في ملف PDF بأكمله مرة واحدة أو استبدال النص بناءً على التعبير العادي.
لقد تعلمنا في هذا الموضوع البحث عن النص واستبداله في PDF ، ومع ذلك ، إذا كنت تريد تعلم تقسيم ملفات PDF حسب الصفحات ، فراجع المقالة على كيفية تقسيم ملف PDF عن طريق الصفحات في C#.