แก้ไขไฟล์ PDF โดยใช้ C#

ข้อมูลการแก้ไขที่ละเอียดอ่อนของเอกสาร PDFใช้ Aspose.PDF สำหรับ .NET เพื่อแก้ไขเอกสาร PDF แบบโปรแกรม

วิธีแก้ไขไฟล์ PDF โดยใช้ห้องสมุด C#

ในการแก้ไขไฟล์ PDF เราจะใช้ Aspose.PDF for .NET API ซึ่งเป็น API การจัดการเอกสารที่อุดมไปด้วยคุณสมบัติ ทรงพลัง และใช้งานง่ายสำหรับแพลตฟอร์ม netเปิดตัวจัดการแพคเกจ NuGet ค้นหาAspose.PDF และติดตั้งคุณอาจใช้คำสั่งต่อไปนี้จากคอนโซลตัวจัดการแพคเกจ

แก้ไขเอกสาร PDF ผ่าน C#


คุณต้อง Aspose.PDF for .NET เพื่อลองใช้รหัสในสภาพแวดล้อมของคุณ

1.โหลด PDF ด้วยอินสแตนซ์ของเอกสาร 1.สร้างวัตถุ TextFragmentAbsorber ด้วยคำค้นหาเป็นอาร์กิวเมนต์ 1.ตั้งค่าตัวเลือกการค้นหา 1.ลูปผ่านแต่ละส่วนที่รวบรวมเพื่อแก้ไข 1.บันทึกไฟล์ PDF

แก้ไขไฟล์ PDF - C#

var inputFile = Path.Combine(dataDir, "input.pdf");
var outputFile = Path.Combine(dataDir, "output.pdf");
var pdfDocument = new Aspose.Pdf.Document(inputFile);
var searchTerm = "Secret";
var textFragmentAbsorber = new Aspose.Pdf.Text.TextFragmentAbsorber(searchTerm);
var textSearchOptions = new Aspose.Pdf.Text.TextSearchOptions(true);
textFragmentAbsorber.TextSearchOptions = textSearchOptions;

pdfDocument.Pages.Accept(textFragmentAbsorber);
var textFragmentCollection = textFragmentAbsorber.TextFragments;
        
foreach (var textFragment in textFragmentCollection)
{
    var page = textFragment.Page;
    var annotationRectangle = textFragment.Rectangle;

    var annot = new Aspose.Pdf.Annotations.RedactionAnnotation(page, annotationRectangle)
    {
        FillColor = Aspose.Pdf.Color.Black
    };
    pdfDocument.Pages[textFragment.Page.Number].Annotations.Add(annot, true);
    annot.Redact();
}
pdfDocument.Save(outputFile);