Webiant Logo Webiant Logo
  1. No results found.

    Try your search with a different keyword or use * as a wildcard.

PdfDocumentHelper.cs

using iTextSharp.text;
using iTextSharp.text.pdf;
using PdfRpt.Core.Contracts;

namespace Nop.Services.Common.Pdf;

/// 
/// Represents PDF document helper
/// 
public static partial class PdfDocumentHelper
{
    #region Constants

    public const float RELATIVE_LEADING = 1.3f;

    #endregion

    #region Methods

    /// 
    /// Build a cell with the given table
    /// 
    /// PDF table
    /// Preferential run direction
    /// The number of columns occupied by a cell
    /// Horizontal alignment
    /// A cell for PDF table
    public static PdfPCell BuildPdfPCell(PdfGrid table, int runDirection, int collSpan = 1, int horizontalAlign = Element.ALIGN_CENTER)
    {
        var cell = new PdfPCell(table)
        {
            RunDirection = runDirection,
            Colspan = collSpan,
            HorizontalAlignment = horizontalAlign,
            Border = 0
        };

        cell.SetLeading(0f, RELATIVE_LEADING);

        return cell;
    }

    /// 
    /// Build default PDF table
    /// 
    /// The number of columns
    /// Preferential run direction
    /// A PDF table
    public static PdfGrid BuildPdfGrid(int numColumns, int runDirection)
    {
        return new PdfGrid(numColumns: numColumns)
        {
            WidthPercentage = 100,
            RunDirection = runDirection,
            HorizontalAlignment = Element.ALIGN_LEFT,
            SpacingAfter = 10
        };
    }

    /// 
    /// Get a font
    /// 
    /// The name of the font
    /// The size of this font
    /// The style of this font
    /// A font object
    public static Font GetFont(string fontName, float size, DocumentFontStyle style = DocumentFontStyle.Normal)
    {
        return FontFactory.GetFont(fontName, BaseFont.IDENTITY_H, true, size, (int)style, BaseColor.Black);
    }

    /// 
    /// Get a font
    /// 
    /// Font to calculate BaseFont
    /// The size of this font
    /// The style of this font
    /// A font object
    public static Font GetFont(Font font, float size, DocumentFontStyle style = DocumentFontStyle.Normal)
    {
        return new Font(font.GetCalculatedBaseFont(false), size, (int)style, font.Color);
    }

    #endregion
}