แก้ไขไฟล์ PDF ผ่าน C#

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

วิธีการแก้ไขไฟล์ PDF โดยใช้ไลบรารี C#

เพื่อที่จะแก้ไขไฟล์ PDF เราจะใช้ Aspose.PDF for .NET API ซึ่งเป็นคุณลักษณะที่อุดมไปด้วยที่มีประสิทธิภาพและง่ายต่อการใช้ API การจัดการเอกสาร {{}} แพลตฟอร์มเปิดตัวจัดการแพคเกจ 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);