iText Core/Community for .NET

repository·develop·Indexed 23 days ago

https://github.com/itext/itext-dotnet

A high-performance .NET library for creating, manipulating, inspecting, and maintaining PDF documents. It features a powerful layout engine for elements like Paragraphs, Tables, and Lists, and supports advanced PDF features including digital signatures, forms, accessibility standards (PDF/A, PDF/UA), and barcode generation. The library includes specialized modules for WebP image support and experimental Brotli compression for PDF streams.

Tokens
6.3K
Snippets
10
Records
39
Agent score
84%

What's inside iText .NET

  1. Overview of iText Core/Community features

    develop

    iText Core/Community (formerly iTextSharp) is a high-performance library for PDF manipulation.

    Core capabilities include:

    • PDF Creation: Using the built-in layout engine.
    • Manipulation: Merging PDFs, adding new content, and more.
    • Security & Forms: Digital signing, form creation/manipulation, and FIPS-compliant cryptography.
    • Standards Compliance: Support for PDF/A and PDF/UA documents.
    • Other: Barcode generation, SVG support, and advanced cryptography via Bouncy Castle.
  2. Use Dynamic Margins and Footnotes

    develop

    iText supports dynamic margins and automatic footnote management through the PageMarginBoxes and PageMarginContent classes.

    Dynamic Margins

    You can adjust page margins on the fly using the SectionBreak element or by setting margins via Document#setPageMargins. The DocumentRenderer#computeLayoutMargins method reserves this space at the start of a new page.

    Footnotes

    Footnotes are handled automatically within paragraphs:

    • Anchors: FootnoteAnchor is added to a paragraph.
    • Placement: The engine checks if there is enough space at the bottom of the current area for the corresponding Footnote. If not, it reduces the available layout area and triggers a relayout.
    • Tagging:
      • The anchor in the text is tagged as Reference (or Lbl in PDF 2.0).
      • The footnote text is tagged as Note (or FENote in PDF 2.0).
      • The label inside the footnote is tagged as Lbl.
  3. Implement Multicol (Newspaper-style) layouting

    develop

    Multicol layouting allows for horizontal content layouting within a fixed height, similar to a newspaper.

    Using MulticolContainer

    To implement this, use the MulticolContainer class.

    • Role: It acts as an aggregator for content that should be laid out in columns and stores properties like Property#COLUMN_COUNT or Property#COLUMN_WIDTH.
    • Constraint: A MulticolContainer instance must have exactly one child, which represents the entire block of content to be distributed into columns.

    How it works

    The MulticolRenderer performs a balancing algorithm:

    1. Calculates column count and width.
    2. Performs a temporary layout with infinite height to estimate column height.
    3. Iteratively adjusts the height (up to MulticolRenderer#MAX_RELAYOUT_COUNT times) until all content fits into the columns.
    4. Enables TREAT_AS_CONTINUOUS_CONTAINER to ensure continuous layout between columns.
  4. Understand the iText Layout Rendering Engine

    develop

    The Layout module transforms abstract model elements (like Paragraph, Table, or List) into low-level PDF drawing operations. This process is handled by a rendering engine that separates the model structure from the rendering logic.

    Key Abstractions

    • Model Elements (IElement): Objects like Text, Paragraph, and Table that represent the structure of your document. You build a tree of these elements in your code.
    • Renderers (IRenderer): Internal classes (e.g., ParagraphRenderer, TableRenderer) that handle the actual placement and drawing of elements. While usually used internally, you can subclass them to customize rendering behavior.
    • Property Containers (IPropertyContainer): An interface implemented by both IElement and IRenderer. It uses a map-based system to store and inherit properties (like font size or color) across the element/renderer tree, which is more memory-efficient than standard fields.

    The Rendering Lifecycle

    1. Tree Construction: You build a tree of IElement objects (e.g., adding a Paragraph to a Div).
    2. Renderer Conversion: The engine calls IElement#createRendererSubTree to convert the element tree into a corresponding tree of renderers via a depth-first traversal.
    3. Layout Phase: The engine calls Layout() on the renderers. This calculates the position and area (LayoutBBox) each element occupies. If an element doesn't fit on a page, it is split into a split renderer (the part that fits) and an overflow renderer (the part to be moved to the next page).
    4. Draw Phase: Once layout is complete, Draw() is called to generate the actual PDF syntax and drawing instructions.
    Div container = new Div();
    Paragraph paragraph = new Paragraph("New paragraph.");
    container.add(paragraph);
  5. Understand iText licensing (AGPL vs Commercial)

    develop

    iText is dual-licensed:

    1. AGPL: A free/open-source copyleft license. Any derivative work must also be licensed under the same AGPL terms.
    2. Commercial: Available for users whose software or services cannot comply with the AGPL terms. This license exempts you from the copyleft obligations.

    If you require a commercial license, contact the sales team.

  6. Access iText tutorials and FAQs

    develop

    For learning resources, tutorials, and troubleshooting, use the following official channels:

    • iText Knowledge Base: Contains the iText Jump-start tutorial, other tutorials, and FAQs.
    • Digital Signatures Hub: For specific information and examples regarding digital signatures.
    • Stack Overflow: Search for questions tagged with itext or itext7 for community-driven solutions.
  7. Perform OCR on images or PDFs

    develop

    Optical Character Recognition (OCR) can be performed using the itext-pdfocr-dotnet library. You can use OCR to:

    • Create a PDF from images.
    • Make an existing PDF searchable.
    • Extract text from images into a .txt file.

    Supported engines include:

    • ONNX models
    • Tesseract 4
  8. Convert SVG to PDF

    develop
    iText provides several ways to handle SVG conversion in C#. Depending on your requirements, you can convert an SVG file directly to a PDF, use the layout engine for more complex rendering, treat the SVG as a string, convert it to a PdfPage, or render it as an XObject.
  9. Basic layout examples in iText .NET

    develop

    The following tasks can be accomplished using iText Core/Community for basic PDF layout and manipulation. Detailed C# implementations are available in the official samples repository:

    • Change text properties and fonts: Modify paragraph styles and font selection.
    • Create tables: Implement simple table structures.
    • Add images: Insert one or multiple images into a document.
    • Create lists: Implement nested lists.
    • Add watermarks: Apply watermarks to pages.
    • Add annotations:
      • Create links for internal document navigation.
      • Create popup annotations.