! Basis Pengetahuan ITEXT Dalam posting kami sebelumnya, kami berbicara tentang ITEXTPDF API untuk bekerja dengan file PDF menggunakan C#/VB.NET di aplikasi .net. API memungkinkan Anda membuat, mengedit, dan memanipulasi dokumen PDF tanpa masuk ke detail format file internal dari format file PDF. Menggunakan ITEXTPDF mudah dikerjakan dan hanya dengan beberapa baris kode, Anda dapat mulai membuat, membaca, dan memanipulasi file PDF. Dalam artikel ini, kita akan berbicara tentang menggunakan ITEXTPDF di aplikasi .net untuk membuat, membaca, dan menyimpan file PDF secara terprogram dalam aplikasi C# kami. Jadi, mari kita mulai dan lihat bagaimana kita dapat membuat PDF di C#.

Instalasi ## ITEXTPDF {.wp-block-heading} Anda dapat menginstal ITEXTPDF API baik dari Nuget atau dari ITEXT ARTIFACTORY Server . Sebelum Anda dapat membuat aplikasi C# untuk menggunakan API ITEXTPDF, Anda perlu menginstalnya dari salah satu sumber ini. Anda dapat merujuk ke artikel instruksi untuk menginstal ITEXTPDF API untuk menyiapkan aplikasi konsol Anda untuk tujuan ini.

Ikhtisar Kelas API ITEXTPDF utama

Beberapa kelas ITEXTPDF utama adalah sebagai berikut.

pdfdocument

Setiap dokumen PDF yang dibuat dengan ITEXTPDF dimulai menggunakan objek kelas PDFDocument.

pdfwriter

Kelas PDFWriter bertanggung jawab untuk menulis konten PDF ke tujuan, seperti file atau streaming. Ini menyediakan fungsionalitas untuk membuat dokumen PDF dan menentukan tujuan output. Beberapa fitur utama dan tanggung jawab kelas PDFWriter adalah sebagai berikut.

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.

paragraf

Kelas paragraf mewakili paragraf teks dalam dokumen PDF. Ini digunakan untuk menambahkan konten tekstual ke dokumen PDF. Berikut adalah beberapa fitur utama dan tanggung jawab kelas paragraf:

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.

Bagaimana cara membuat file PDF di C#?

Sekarang kami memiliki ide bagus tentang ITEXTPDF dan kelas utamanya, mari kita lanjutkan untuk membuat dokumen PDF di C# menggunakan ITEXTPDF API. Ini dapat dilakukan hanya dengan beberapa langkah seperti di bawah ini.

  1. Buat proyek baru di Visual Studio.
  2. Instal perpustakaan ITEXTPDF C# menggunakan Nuget Package Manager.
  3. Buat contoh kelas PDFDocument dan PDFWriter
  4. Buat contoh kelas dokumen dan paragraf
  5. Tutup dokumen menggunakan metode document.close ()

c# cuplikan kode untuk menghasilkan file pdf

Kode C# berikut dapat digunakan untuk menghasilkan file 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.");
    }
}

Bagaimana cara memperbarui file PDF di C#?

Memperbarui/mengedit file PDF di C# dapat dengan mudah dilakukan dengan menggunakan ITEXTPDF.

  1. Buka dokumen PDF yang ada menggunakan objek PDFREADER .
  2. Buat objek PDFWriter dengan tujuan output baru atau yang dimodifikasi (seperti file atau aliran).
  3. Buat PDFDocument Objek Menggunakan kedua PdFreader dan PDFWriter Objek.
  4. Akses halaman dan konten yang ada dari dokumen menggunakan instance PDFDocument.
  5. Buat modifikasi yang diperlukan untuk dokumen, seperti menambahkan atau menghapus konten, memperbarui teks, memodifikasi anotasi, dll.
  6. Tutup PDFDocument , yang secara otomatis menutup PDFREADER yang terkait dan PDFWriter juga, dan menyimpan perubahan pada tujuan output.

C# Code Cuplikan untuk memperbarui file pdf

Kode C# berikut dapat digunakan untuk memperbarui file 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.");
    }
}

kesimpulan

Dalam artikel ini, kami menjelajahi API ITEXTPDF untuk .NET untuk mempelajari tentang membuat dan memanipulasi file PDF dari dalam aplikasi .net kami. API adalah open-source dan di-host di repositori GitHub sebagai ITEXT-DOTNET. Di blog kami yang akan datang, kami akan mengeksplorasi API ini lebih lanjut untuk bekerja dengan berbagai komponen dokumen PDF seperti tabel, gambar, anotasi, PDF yang dihapus, dan banyak lainnya. Jadi tetaplah disini.