public void PaintText(string textColor, string fontPath, float fontSize, float textLeading, float textSpacing, string textContent, float textX, float textY, bool isH) { CMYK_Color cmyk_titleColor = new CMYK_Color().GetCMYK_Color(textColor); FontFactory.Register(fontPath); BaseFont bfChinese = BaseFont.CreateFont(fontPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); PdfContentByte contentByte = pdfWriter.DirectContent; contentByte.BeginText(); contentByte.SetCMYKColorFill(cmyk_titleColor.C, cmyk_titleColor.M, cmyk_titleColor.Y, cmyk_titleColor.K); contentByte.SetCMYKColorStroke(cmyk_titleColor.C, cmyk_titleColor.M, cmyk_titleColor.Y, cmyk_titleColor.K); contentByte.SetFontAndSize(bfChinese, fontSize); contentByte.SetLeading(textLeading); contentByte.SetCharacterSpacing(textSpacing); //contentByte.SetTextMatrix(textX, textY); string p1 = "^[]】}》>))』@[{}【{《<((『‖︱ ︳♀♂]$"; for (int i = 0; i < textContent.Length; i++) { contentByte.SetTextMatrix(textX, textY - (10f * i)); if (Regex.IsMatch(textContent[i].ToString(), p1)) { contentByte.ShowTextAligned(Element.ALIGN_CENTER, textContent[i].ToString(), textX + 1f, textY - (10f * i), -90); } else { contentByte.ShowText(textContent[i].ToString()); } contentByte.NewlineText(); } contentByte.Stroke(); contentByte.EndText(); }
/// <summary> /// 填充背景色 /// </summary> /// <param name="bgColor"></param> /// <param name="x"></param> /// <param name="y"></param> /// <param name="width"></param> /// <param name="height"></param> public void PaintBGColor(string bgColor, float x, float y, float width, float height) { CMYK_Color cmyk = new CMYK_Color().GetCMYK_Color(bgColor.ToUpper()); PdfContentByte contentByte = pdfWriter.DirectContentUnder; contentByte.SetCMYKColorFill(cmyk.C, cmyk.M, cmyk.Y, cmyk.K); contentByte.SetCMYKColorStroke(cmyk.C, cmyk.M, cmyk.Y, cmyk.K); contentByte.Rectangle(x, y, width, height); contentByte.Fill(); contentByte.Stroke(); //contentByte.FillStroke(); }
public void PaintText(string textColor, string fontPath, float fontSize, string text, float textX, float textY, float textWidth) { CMYK_Color cmyk_titleColor = new CMYK_Color().GetCMYK_Color(textColor); FontFactory.Register(fontPath); BaseFont bfChinese = BaseFont.CreateFont(fontPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); Phrase Phr_Content = new Phrase(text, new iTextSharp.text.Font(bfChinese, fontSize)); PdfContentByte ConByte_Up = pdfWriter.DirectContent; ColumnText.ShowTextAligned(ConByte_Up, Element.ALIGN_CENTER, Phr_Content, textX, textY, 0); }
public void PaintContent(string textColor, string text, string fontPath, float fontSize, float textLeading, float textSpacing, float x, float y, float height, float width, int align) { CMYK_Color cmyk_titleColor = new CMYK_Color().GetCMYK_Color(textColor); FontFactory.Register(fontPath); BaseFont bfChinese = BaseFont.CreateFont(fontPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); Paragraph para = new Paragraph(text, new iTextSharp.text.Font(bfChinese, fontSize)); PdfContentByte cb = pdfWriter.DirectContent; cb.SetCMYKColorFill(cmyk_titleColor.C, cmyk_titleColor.M, cmyk_titleColor.Y, cmyk_titleColor.K); cb.SetCMYKColorStroke(cmyk_titleColor.C, cmyk_titleColor.M, cmyk_titleColor.Y, cmyk_titleColor.K); ColumnText column = new ColumnText(cb); column.SetSimpleColumn(para, x, y, x + width, y + height, textLeading, align); //cb.Rectangle(x, y, width, height); //cb.Stroke(); column.Go(); }
/// <summary> /// 向pdf页输出文字 /// </summary> /// <param name="textColor">字体颜色</param> /// <param name="fontPath">字体</param> /// <param name="fontSize">字体大小 pt</param> /// <param name="textLeading">行距</param> /// <param name="textSpacing">字间距</param> /// <param name="textContent">文字内容</param> /// <param name="textX">输出位置的X坐标</param> /// <param name="textY">输出位置的Y坐标</param> public void PaintText(string textColor, string fontPath, float fontSize, float textLeading, float textSpacing, string textContent, float textX, float textY, float maxWidth) { CMYK_Color cmyk_titleColor = new CMYK_Color().GetCMYK_Color(textColor); FontFactory.Register(fontPath); BaseFont bfChinese = BaseFont.CreateFont(fontPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); PdfContentByte contentByte = pdfWriter.DirectContent; contentByte.BeginText(); contentByte.SetCMYKColorFill(cmyk_titleColor.C, cmyk_titleColor.M, cmyk_titleColor.Y, cmyk_titleColor.K); contentByte.SetCMYKColorStroke(cmyk_titleColor.C, cmyk_titleColor.M, cmyk_titleColor.Y, cmyk_titleColor.K); contentByte.SetFontAndSize(bfChinese, fontSize); contentByte.SetLeading(textLeading); contentByte.SetCharacterSpacing(textSpacing); contentByte.SetTextMatrix(textX, textY); float floLineContentSize = bfChinese.GetWidthPointKerned(textContent, fontSize); if (floLineContentSize > maxWidth) { string strOutputContent = textContent.Substring(0, textContent.Length - 1); floLineContentSize = bfChinese.GetWidthPointKerned(strOutputContent, fontSize); while (floLineContentSize > maxWidth) { strOutputContent = textContent.Substring(0, strOutputContent.Length - 1); floLineContentSize = bfChinese.GetWidthPointKerned(strOutputContent, fontSize); } contentByte.ShowText(strOutputContent); //contentByte.ShowTextAligned((int)ContentAlignment.TopCenter, strOutputContent, textX, textY, 0); } else { contentByte.ShowText(textContent); } contentByte.Stroke(); contentByte.EndText(); }
public CMYK_Color GetCMYK_Color(string strRGB) { Dictionary <string, CMYK_Color> dicCMYK = new Dictionary <string, CMYK_Color>(); dicCMYK.Add("#000000", new CMYK_Color { C = 0, M = 0, Y = 0, K = 100 }); dicCMYK.Add("#444444", new CMYK_Color { C = 0, M = 0, Y = 0, K = 85 }); dicCMYK.Add("#E4DFD6", new CMYK_Color { C = 14, M = 12, Y = 16, K = 0 }); dicCMYK.Add("#F9DCDA", new CMYK_Color { C = 2, M = 20, Y = 10, K = 0 }); dicCMYK.Add("#D6EAF3", new CMYK_Color { C = 20, M = 2, Y = 3, K = 0 }); dicCMYK.Add("#FFE99D", new CMYK_Color { C = 0, M = 10, Y = 50, K = 0 }); dicCMYK.Add("#FCDCAB", new CMYK_Color { C = 2, M = 18, Y = 40, K = 0 }); dicCMYK.Add("#FBE7D4", new CMYK_Color { C = 2, M = 13, Y = 18, K = 0 }); dicCMYK.Add("#D2DC9F", new CMYK_Color { C = 25, M = 5, Y = 50, K = 0 }); dicCMYK.Add("#B90000", new CMYK_Color { C = 35, M = 100, Y = 100, K = 0 }); dicCMYK.Add("#175724", new CMYK_Color { C = 90, M = 55, Y = 95, K = 30 }); dicCMYK.Add("#CABCAC", new CMYK_Color { C = 28, M = 28, Y = 34, K = 0 }); dicCMYK.Add("#A7BBC1", new CMYK_Color { C = 43, M = 21, Y = 21, K = 0 }); dicCMYK.Add("#FBBBAB", new CMYK_Color { C = 0, M = 38, Y = 30, K = 0 }); dicCMYK.Add("#6789A4", new CMYK_Color { C = 70, M = 45, Y = 25, K = 0 }); dicCMYK.Add("#F8F2E4", new CMYK_Color { C = 4, M = 5, Y = 12, K = 0 }); dicCMYK.Add("#EF006B", new CMYK_Color { C = 0, M = 100, Y = 30, K = 0 }); dicCMYK.Add("#009FE0", new CMYK_Color { C = 100, M = 0, Y = 0, K = 5 }); dicCMYK.Add("#FFC400", new CMYK_Color { C = 0, M = 30, Y = 100, K = 0 }); dicCMYK.Add("#F98C00", new CMYK_Color { C = 0, M = 60, Y = 100, K = 0 }); dicCMYK.Add("#DF0000", new CMYK_Color { C = 0, M = 100, Y = 100, K = 10 }); dicCMYK.Add("#93BC00", new CMYK_Color { C = 60, M = 0, Y = 100, K = 0 }); dicCMYK.Add("#C2891B", new CMYK_Color { C = 33, M = 55, Y = 95, K = 0 }); dicCMYK.Add("#77420D", new CMYK_Color { C = 70, M = 85, Y = 100, K = 0 }); dicCMYK.Add("#FFFFFF", new CMYK_Color { C = 0, M = 0, Y = 0, K = 0 }); dicCMYK.Add("#ffffff", new CMYK_Color { C = 0, M = 0, Y = 0, K = 0 }); CMYK_Color newcmyk = new CMYK_Color { C = (int)((float)dicCMYK[strRGB].C * (float)2.55), M = (int)((float)dicCMYK[strRGB].M * (float)2.55), Y = (int)((float)dicCMYK[strRGB].Y * (float)2.55), K = (int)((float)dicCMYK[strRGB].K * (float)2.55) }; return(newcmyk); }
/// <summary> /// 输出多行文字 /// </summary> /// <param name="textColor"></param> /// <param name="fontPath"></param> /// <param name="fontSize"></param> /// <param name="textLeading"></param> /// <param name="textSpacing"></param> /// <param name="textContent"></param> /// <param name="textX"></param> /// <param name="textY"></param> public void PaintContent(string textColor, string fontPath, float fontSize, float textLeading, float textSpacing, string textContent, float textX, float textY, float maxWidth, int maxLine) { textContent = textContent.Replace("\n", "&hh;"); textContent = textContent.Replace("&hh;", "\r\n"); CMYK_Color cmyk_titleColor = new CMYK_Color().GetCMYK_Color(textColor); FontFactory.Register(fontPath); BaseFont bfChinese = BaseFont.CreateFont(fontPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); PdfContentByte contentByte = pdfWriter.DirectContent; contentByte.BeginText(); contentByte.SetCMYKColorFill(cmyk_titleColor.C, cmyk_titleColor.M, cmyk_titleColor.Y, cmyk_titleColor.K); contentByte.SetCMYKColorStroke(cmyk_titleColor.C, cmyk_titleColor.M, cmyk_titleColor.Y, cmyk_titleColor.K); contentByte.SetFontAndSize(bfChinese, fontSize); contentByte.SetLeading(textLeading); contentByte.SetCharacterSpacing(textSpacing); contentByte.SetTextMatrix(textX, textY); string strOutputContent = ""; int linesCount = 1; for (int i = 0; i < textContent.Length; i++) { if (linesCount > maxLine) { break; } strOutputContent += textContent[i].ToString(); float floLineContentSize = bfChinese.GetWidthPointKerned(strOutputContent, fontSize); if ((strOutputContent.Contains("\r") == true && textContent.Substring(i, 2).Equals("\r\n") == true) || floLineContentSize >= maxWidth) { string nextNeedStr = ""; if (floLineContentSize >= maxWidth && (i + 1) < textContent.Length && textContent[i] != ' ' && IsNatural_Number(textContent[i].ToString()) && IsNatural_Number(textContent[i].ToString())) { if (textContent[i + 1] != ' ') { int lastWhiteStrIndex = strOutputContent.LastIndexOf(' '); if (lastWhiteStrIndex > 20) { nextNeedStr = strOutputContent.Substring(lastWhiteStrIndex); strOutputContent = strOutputContent.Substring(0, lastWhiteStrIndex + 1); nextNeedStr = nextNeedStr.TrimStart(' '); } } else { i = i + 1; } } string p = @"[].,,。;;》>、!!??】}}]"; if (floLineContentSize >= maxWidth && (i + 1) < textContent.Length && Regex.IsMatch(textContent[i + 1].ToString(), p)) { strOutputContent += textContent[i + 1].ToString(); i = i + 1; } strOutputContent = strOutputContent.TrimEnd('r').TrimEnd('\\');// Remove(strOutputContent.Length - 1, 1); contentByte.ShowTextAligned(PdfContentByte.ALIGN_LEFT, strOutputContent, textX, textY, 0); contentByte.NewlineText(); linesCount++; strOutputContent = nextNeedStr; continue; } } if (linesCount <= maxLine) { contentByte.ShowText(strOutputContent); } contentByte.Stroke(); contentByte.EndText(); }