! ฐานความรู้ ITEXT ในโพสต์ก่อนหน้าของเราเราได้พูดคุยเกี่ยวกับ ITEXTPDF API สำหรับการทำงานกับไฟล์ PDF โดยใช้ C#/VB.NET ในแอปพลิเคชัน. NET API ช่วยให้คุณสร้างแก้ไขและจัดการเอกสาร PDF โดยไม่ต้องเข้าไปในรายละเอียดรูปแบบไฟล์ภายในใด ๆ ของรูปแบบไฟล์ PDF การใช้ ITEXTPDF นั้นใช้งานง่ายและมีรหัสเพียงไม่กี่บรรทัดคุณสามารถเริ่มสร้างการอ่านและจัดการ ไฟล์ PDF ในบทความนี้เราจะพูดคุยเกี่ยวกับการใช้ itextpdf ในแอปพลิเคชัน .NET เพื่อสร้างอ่านและบันทึกไฟล์ PDF ในแอปพลิเคชัน C# ของเรา ดังนั้นให้เริ่มต้นกันและดูว่าเราสามารถสร้าง PDF ใน C#ได้อย่างไร

itextpdf การติดตั้ง

คุณสามารถติดตั้ง itextpdf api ได้จาก nuget หรือจาก itext artifactory Server ก่อนที่คุณจะสามารถสร้างแอปพลิเคชัน C# ของคุณสำหรับการใช้ ITEXTPDF API คุณต้องติดตั้งจากแหล่งข้อมูลเหล่านี้อย่างใดอย่างหนึ่ง คุณสามารถอ้างถึง บทความคำแนะนำสำหรับการติดตั้ง ITEXTPDF API สำหรับการตั้งค่าแอปพลิเคชันคอนโซลของคุณเพื่อจุดประสงค์นี้

ภาพรวมของคลาส ITEXTPDF API หลัก

คลาส ITEXTPDF หลักบางส่วนมีดังนี้

pdfdocument

เอกสาร PDF ทุกฉบับที่สร้างด้วย ITEXTPDF จะเริ่มต้นโดยใช้วัตถุของคลาส PDFDocument

pdfwriter

คลาส PDFWriter รับผิดชอบในการเขียนเนื้อหา PDF ไปยังปลายทางเช่นไฟล์หรือสตรีม มันมีฟังก์ชันการทำงานเพื่อสร้างเอกสาร PDF และระบุปลายทางของเอาต์พุต คุณสมบัติที่สำคัญและความรับผิดชอบของคลาส PDFWriter มีดังนี้

Destination ConfigurationThe PdfWriter constructor allows you to specify the output destination for the PDF content. It can accept parameters like a file path, a Stream object, or an instance of IOutputStreamCounter. This determines where the PDF content will be written.
PDF Document CreationWhen you create a new instance of PdfWriter, it automatically creates a new PdfDocument object associated with it. The PdfDocument represents the logical structure of a PDF file and provides methods to manipulate its content.
PDF Compression and Version ControlThe PdfWriter class allows you to configure various aspects of the PDF file, such as compression settings and PDF version compatibility.
Writing PDF ContentOnce you have a PdfWriter instance, you can use it to write content to the associated PdfDocument. You can add pages, create annotations, add text, images, and other graphical elements to the PDF document using the provided methods.
Closing the WriterAfter you finish writing the PDF content, it’s important to close the PdfWriter to ensure the document is finalized and any necessary resources are released.

ย่อหน้า

คลาสย่อหน้าแสดงถึงย่อหน้าของข้อความในเอกสาร PDF มันถูกใช้เพื่อเพิ่มเนื้อหาที่เป็นข้อความลงในเอกสาร PDF นี่คือคุณสมบัติที่สำคัญและความรับผิดชอบของคลาสย่อหน้า:

Text ContentThe primary purpose of the Paragraph class is to hold and display textual content within a PDF document. You can pass a string or any other text representation to the Paragraph constructor to initialize its content.
Text FormattingThe Paragraph class allows you to apply various formatting options to the text, such as font size, font family, text color, bold, italic, underline, and more. You can use methods like SetFontSize(), SetFont(), SetBold(), SetItalic(), SetUnderline(), etc., to specify the desired formatting.
Alignment and IndentationThe Paragraph class provides methods to set the alignment of the text within the paragraph. You can align the text to the left, right, or center, or justify it. Additionally, you can apply indentation to control the left and right margins of the paragraph.
Inline ElementsApart from plain text, you can also add inline elements within a Paragraph. For example, you can include phrases or words with different formatting styles, add hyperlinks, insert images, or include other elements supported by iText.
NestingYou can nest multiple paragraphs within each other or combine them with other iText elements like tables, lists, or chunks to create complex document structures.
Integration with DocumentThe Paragraph instances can be added to the Document object using the Add() method. This allows you to include paragraphs in your PDF document at the desired location.

จะสร้างไฟล์ PDF ใน C#ได้อย่างไร?

ตอนนี้เรามีความคิดที่ดีเกี่ยวกับ ITEXTPDF และคลาสหลักของมันให้ดำเนินการต่อเพื่อสร้างเอกสาร PDF ใน C# โดยใช้ ITEXTPDF API สามารถทำได้เพียงไม่กี่ขั้นตอนด้านล่าง

  1. สร้างโครงการใหม่ใน Visual Studio
  2. ติดตั้งไลบรารี ITEXTPDF C# โดยใช้ NUGET Package Manager
  3. สร้างอินสแตนซ์ของคลาส PDFDoCument และ PDFWRITER
  4. สร้างอินสแตนซ์ของคลาสเอกสารและย่อหน้า
  5. ปิดเอกสารโดยใช้วิธีการ document.close ()

c# รหัสตัวอย่างเพื่อสร้างไฟล์ pdf

สามารถใช้รหัส C# ต่อไปนี้เพื่อสร้างไฟล์ PDF

using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;

class Program
{
    static void Main()
    {
        // Specify the output file path
        string outputPath = "example.pdf";
        // Create a new PDF document
        PdfWriter writer = new PdfWriter(outputPath);
        PdfDocument pdf = new PdfDocument(writer);
        // Create a document instance
        Document document = new Document(pdf);
        // Add content to the document
        document.Add(new Paragraph("Hello, World!"));
        // Close the document
        document.Close();
        Console.WriteLine("PDF created successfully.");
    }
}

จะอัปเดตไฟล์ PDF ใน C#ได้อย่างไร?

การอัปเดต/แก้ไขไฟล์ PDF ใน C# สามารถทำได้อย่างง่ายดายโดยใช้ ITEXTPDF

  1. เปิดเอกสาร PDF ที่มีอยู่โดยใช้วัตถุ pdfreader
  2. สร้างวัตถุ pdfwriter ด้วยปลายทางเอาต์พุตใหม่หรือแก้ไข (เช่นไฟล์หรือสตรีม)
  3. สร้างวัตถุ pdfdocument โดยใช้ทั้ง pdfreader และ pdfwriter วัตถุ
  4. เข้าถึงหน้าและเนื้อหาที่มีอยู่ของเอกสารโดยใช้อินสแตนซ์ pdfDocument
  5. ทำการปรับเปลี่ยนที่จำเป็นสำหรับเอกสารเช่นการเพิ่มหรือลบเนื้อหาการอัปเดตข้อความการแก้ไขคำอธิบายประกอบ ฯลฯ
  6. ปิด pdfdocument ซึ่งปิดโดยอัตโนมัติ pdfreader และ pdfwriter โดยอัตโนมัติและบันทึกการเปลี่ยนแปลงปลายทางปลายทาง

c# รหัสตัวอย่างเพื่ออัปเดตไฟล์ pdf

สามารถใช้รหัส C# ต่อไปนี้เพื่ออัปเดตไฟล์ PDF

using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;

class Program
{
    static void Main()
    {
        string filePath = "existing.pdf";
        string outputPath = "updated.pdf";
        // Open the existing PDF document
        PdfReader reader = new PdfReader(filePath);
        // Create a new PDF document with modified output destination
        PdfWriter writer = new PdfWriter(outputPath);
        // Create a PdfDocument object with both the reader and writer
        PdfDocument pdfDoc = new PdfDocument(reader, writer);
        // Access the first page of the document
        PdfPage firstPage = pdfDoc.GetPage(1);
        // Create a document instance for the page
        Document document = new Document(pdfDoc, firstPage);
        // Add a new paragraph to the document
        document.Add(new Paragraph("This is a new paragraph added to the existing PDF."));
        // Close the document, which saves the changes
        document.Close();
        // Close the reader
        reader.Close();
        Console.WriteLine("PDF updated successfully.");
    }
}

บทสรุป

ในบทความนี้เราสำรวจ ITEXTPDF API สำหรับ. NET เพื่อเรียนรู้เกี่ยวกับการสร้างและจัดการไฟล์ PDF จากภายในแอปพลิเคชัน. NET ของเรา API เป็นโอเพ่นซอร์สและโฮสต์บนพื้นที่เก็บข้อมูลของ GitHub เป็น itext-dotnet ในบล็อกที่กำลังจะมาถึงเราจะสำรวจ API นี้ต่อไปเพื่อทำงานกับส่วนประกอบต่าง ๆ ของเอกสาร PDF เช่นตารางรูปภาพคำอธิบายประกอบ PDF redacted และอื่น ๆ อีกมากมาย ดังนั้นคอยติดตาม