1. 在文件流中添加颜色块,输出的文字相对于颜色块居中显示。文本旋转只处理了0、90、180、270的旋转角度。

public static void PrinterCornerCode(List<CornerCodeInfo> list, PdfReader preader, PdfStamper pstamper)
{
    PdfContentByte pcontent = pstamper.GetOverContent(preader.NumberOfPages);

    foreach (CornerCodeInfo oCorner in list)
    {
        if (oCorner.FontSize > 0 && !string.IsNullOrEmpty(oCorner.Description))
        {
            pcontent.SaveState();

            float x = oCorner.X * 0.72F;
            float y = oCorner.Y * 0.72F;
            float w = oCorner.Width * 0.72F;
            float h = oCorner.Height * 0.72F;

            //基本字體對象
            BaseFont baseFont = BaseFont.CreateFont(GetFontFilePath(ParseFontName(oCorner.FontFamily), false, false), BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

            //==================== 輸出角碼的背景顏色(繪製顏色矩形) ====================
            BaseColor backgroundColor = new BaseColor(Convertor.ToColor(oCorner.BackgroundColor));

            pcontent.SaveState();
            pcontent.SetColorStroke(backgroundColor);
            pcontent.SetColorFill(backgroundColor);
            pcontent.Rectangle(x, y, w, h);
            pcontent.FillStroke();
            pcontent.Stroke();
            pcontent.RestoreState();

            //==================== 輸出文本描述 ====================
            float bx;
            float by;

            string[] text = oCorner.Description.Split('');
            int textCount = text.Length;

            float fs = oCorner.FontSize * 0.72F;
            float pw = (w - (fs * textCount)) / 2;
            float ph = (h - (fs * textCount)) / 2;

            //文本輸出相對於矩形背景顏色塊居中顯示的x、y處理
            switch (oCorner.Rotate)
            {
                case 90:
                    bx = x + pw + fs;
                    by = y + h / 2F;
                    break;
                case 270:
                    bx = x + pw + (fs * textCount - fs);
                    by = y + h / 2F;
                    break;
                case 180:
                    bx = x + (w / 2F);
                    by = y + ph + fs;
                    break;
                case 0:
                default:
                    bx = x + (w / 2F);
                    by = y + ph + (fs * textCount - fs);
                    break;
            }

            for (int r = 0; r < textCount; r++)
            {
                //如果是第一行時則不用處理(文字換行處理)
                if (r > 0)
                {
                    //輸出橫向文字
                    if (oCorner.Rotate == 0)
                        by -= fs;
                    else if (oCorner.Rotate == 180)
                        by += fs;
                    //輸出縱向文字
                    else if (oCorner.Rotate == 90)
                        bx += fs;
                    else if (oCorner.Rotate == 270)
                        bx -= fs;
                }

                pcontent.BeginText();
                pcontent.SetFontAndSize(baseFont, oCorner.FontSize);
                pcontent.SetColorFill(new BaseColor(Convertor.ToColor(oCorner.Color)));
                pcontent.ShowTextAligned(PdfContentByte.ALIGN_CENTER, text[r], bx, by, oCorner.Rotate);

                pcontent.EndText();
            }
        }
    }
}

#region 解析字體名
/// <summary>
/// 解析字體名
/// </summary>
/// <param name="fontName">字體名</param>
/// <returns></returns>
private static string ParseFontName(string fontName)
{
    if (fontName.IndexOf("_") != -1)
        return fontName.Substring(0, fontName.LastIndexOf("_"));
    else
        return fontName;
}

/// <summary>
/// 取得字體對應的文件名
/// </summary>
/// <param name="fontFamily">字體名稱</param>
/// <param name="bWeight">是否粗體</param>
/// <param name="bItalic">是否斜體</param>
/// <returns></returns>
private static string GetFontFilePath(string fontFamily, bool bWeight, bool bItalic)
{
    switch (fontFamily.ToUpper())
    {
        case "TIMES NEW ROMAN":
        case "TIMESNEWROMAN":
            fontFamily = "TIMES";
            break;
    }

    if (bWeight && bItalic)
        fontFamily += "BI";
    else if (bWeight)
        fontFamily += "BD";
    else if (bItalic)
        fontFamily += "I";

    return string.Format(@"C:\WINDOWS\Fonts\{0}.TTF", fontFamily);
}
#endregion
View Code
public class CornerCodeInfo
{
    #region 字段

    private string _FontFamily;
    private float _FontSize;
    private string _Color = "#FFFFFF";
    private string _BackgroundColor = "#000000";
    private float _Width;
    private float _Height;
    private int _Rotate;
    private float _HorizontalPadding;
    private float _VerticalPadding;
    private string _Position;
    private string _Description;
    private float _X;
    private float _Y;

    #endregion

    #region 構造函數

    public PrinterCornerCodeInfo() { }

    public PrinterCornerCodeInfo(PrinterCornerCodeInfo oBaseCorner, string description, float x, float y)
    {
        _FontFamily = oBaseCorner.FontFamily;
        _FontSize = oBaseCorner.FontSize;
        _Color = oBaseCorner.Color;
        _BackgroundColor = oBaseCorner.BackgroundColor;
        _Width = oBaseCorner.Width;
        _Height = oBaseCorner.Height;
        _Rotate = oBaseCorner.Rotate;
        _Description = description;
        _X = x;
        _Y = y;
    }

    #endregion

    #region 屬性

    /// <summary>
    /// 字體樣式
    /// <summary>
    public string FontFamily
    {
        get { return _FontFamily; }
        set { _FontFamily = value; }
    }

    /// <summary>
    /// 字體大小
    /// <summary>
    public float FontSize
    {
        get { return _FontSize; }
        set { _FontSize = value; }
    }

    /// <summary>
    /// 字體顏色
    /// <summary>
    public string Color
    {
        get { return _Color; }
        set { _Color = value; }
    }

    /// <summary>
    /// 背景色
    /// <summary>
    public string BackgroundColor
    {
        get { return _BackgroundColor; }
        set { _BackgroundColor = value; }
    }

    /// <summary>
    /// 旋轉後的寬度
    /// <summary>
    public float Width
    {
        get { return _Width; }
        set { _Width = value; }
    }

    /// <summary>
    /// 旋轉後的高度
    /// <summary>
    public float Height
    {
        get { return _Height; }
        set { _Height = value; }
    }

    /// <summary>
    /// 旋轉角度
    /// <summary>
    public int Rotate
    {
        get { return _Rotate; }
        set { _Rotate = value; }
    }

    /// <summary>
    /// 水平方向的内边距
    /// <summary>
    public float HorizontalPadding
    {
        get { return _HorizontalPadding; }
        set { _HorizontalPadding = value; }
    }

    /// <summary>
    /// 垂直方向的内边距
    /// <summary>
    public float VerticalPadding
    {
        get { return _VerticalPadding; }
        set { _VerticalPadding = value; }
    }

    /// <summary>
    /// 起始位置(值為:LEFT_TOP、LEFT_BOTTOM、RIGHT_TOP、RIGHT_BOTTOM)
    /// </summary>
    public string Position
    {
        get { return _Position; }
        set { _Position = value; }
    }

    /// <summary>
    /// 文本描述
    /// <summary>
    public string Description
    {
        get { return _Description; }
        set { _Description = value; }
    }

    /// <summary>
    /// X坐標
    /// <summary>
    public float X
    {
        get { return _X; }
        set { _X = value; }
    }

    /// <summary>
    /// Y坐標
    /// <summary>
    public float Y
    {
        get { return _Y; }
        set { _Y = value; }
    }

    #endregion
}
View Code

 

posted on 2022-09-27 09:51  欲穷  阅读(150)  评论(0编辑  收藏  举报