คู่มือฉบับย่อนี้อธิบายถึง วิธีค้นหาและแทนที่ข้อความใน PDF โดยใช้ C# ด้วยความช่วยเหลือจากขั้นตอนโดยละเอียดและโค้ดที่รันได้ ช่วยในการกำหนดค่าสภาพแวดล้อม จากนั้นให้กระบวนการทีละขั้นตอนเพื่อ แทนที่ข้อความใน PDF โดยใช้ C# เมื่ออัปเดตไฟล์แล้ว คุณสามารถบันทึกกลับลงในดิสก์ในรูปแบบดั้งเดิม เช่น PDF หรือเป็น DOCX, Excel, HTML เป็นต้น
ขั้นตอนในการค้นหาและแทนที่ข้อความใน PDF โดยใช้ C#
- กำหนดค่าโปรเจ็กต์เพื่อใช้ Aspose.PDF for .NET โดยใช้ NuGet package manager
- สร้างหรือโหลดไฟล์ 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 #