Exemple #1
0
    private static Font GetPdfFontFromFile(String text, String fontPath, float fontSize, Boolean isBold, System.Windows.Media.Color color)
    {
      var path = Helper.GetSplitStrings(fontPath, true, ',');
      if (path.Count == 0 || !System.IO.File.Exists(path[0]))
        return GetPdfFont(null, fontSize, isBold, color);

      var fontDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Fonts);
      var isEmbedded = true;
      if (System.IO.Path.GetDirectoryName(fontPath) == fontDirectory)
        isEmbedded = false;
      var baseFont = BaseFont.CreateFont(fontPath, BaseFont.IDENTITY_H, isEmbedded ? BaseFont.EMBEDDED : BaseFont.NOT_EMBEDDED);
      if (!CharExists(text, baseFont))
      {
        var chineseFontPath = String.Format("{0}\\{1}", fontDirectory, "simsun.ttc,0");
        baseFont = BaseFont.CreateFont(chineseFontPath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
        //if (!CharExists(text, baseFont))
        //{
        //  var europeanFontPath = String.Format("{0}\\{1}", fontDirectory, "verdana.ttf");
        //  baseFont = BaseFont.CreateFont(europeanFontPath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
        //}
      }
      return new Font(baseFont, fontSize, isBold ? 1 : 0, PdfPrinter.GetBaseColor(color));
    }
Exemple #2
0
 private static Font GetPdfFont(String fontName, float fontSize, Boolean isBold, System.Windows.Media.Color color)
 {
   if (!Helper.SystemFontNames.Contains(fontName))
     fontName = Helper.SystemFontNames.FirstOrDefault();
   return FontFactory.GetFont(fontName, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED, fontSize, isBold ? 1 : 0, PdfPrinter.GetBaseColor(color));
 }
Exemple #3
0
    public void CellLayout(PdfPCell cell, iTextSharp.text.Rectangle position, PdfContentByte[] canvases)
    {
      if (Helper.IsTransparent(_backgroundBrush))
        position.BackgroundColor = null;

      var gradientBrush = _backgroundBrush as GradientBrush;
      if (gradientBrush != null)
      {
        PdfShading shading = null;

        var fromColor = PdfPrinter.GetBaseColor(gradientBrush.GradientStops.FirstOrDefault().Color);
        var toColor = PdfPrinter.GetBaseColor(gradientBrush.GradientStops.LastOrDefault().Color);

        var linearGradientBrush = _backgroundBrush as LinearGradientBrush;
        if (linearGradientBrush != null)
        {
          float x0, y0, x1, y1;
          var brushSlopeAbs = Math.Abs(linearGradientBrush.EndPoint.Y - linearGradientBrush.StartPoint.Y) / Math.Abs(linearGradientBrush.EndPoint.X - linearGradientBrush.StartPoint.X);
          var cellSlopeAbs = position.Height / position.Width;
          if (linearGradientBrush.EndPoint.X > linearGradientBrush.StartPoint.X) // from left to right
          {
            x0 = position.Left;
            if (linearGradientBrush.EndPoint.Y > linearGradientBrush.StartPoint.Y) // from top to bottom
            {
              y0 = position.Top;
              if (cellSlopeAbs >= brushSlopeAbs)
              {
                x1 = position.Left + (float)(position.Height / brushSlopeAbs);
                y1 = position.Bottom;
              }
              else
              {
                x1 = position.Right;
                y1 = position.Top - (float)(position.Width * brushSlopeAbs);
              }
            }
            else // from bottom to top
            {
              y0 = position.Bottom;
              if (cellSlopeAbs >= brushSlopeAbs)
              {
                x1 = position.Left + (float)(position.Height / brushSlopeAbs);
                y1 = position.Top;
              }
              else
              {
                x1 = position.Right;
                y1 = position.Bottom + (float)(position.Width * brushSlopeAbs);
              }
            }
          }
          else // from right to left
          {
            x0 = position.Right;
            if (linearGradientBrush.EndPoint.Y > linearGradientBrush.StartPoint.Y) // from top to bottom
            {
              y0 = position.Top;
              if (cellSlopeAbs >= brushSlopeAbs)
              {
                x1 = position.Right - (float)(position.Height / brushSlopeAbs);
                y1 = position.Bottom;
              }
              else
              {
                x1 = position.Right;
                y1 = position.Top - (float)(position.Width * brushSlopeAbs);
              }
            }
            else // from bottom to top
            {
              y0 = position.Bottom;
              if (cellSlopeAbs >= brushSlopeAbs)
              {
                x1 = position.Right - (float)(position.Height / brushSlopeAbs);
                y1 = position.Top;
              }
              else
              {
                x1 = position.Right;
                y1 = position.Bottom + (float)(position.Width * brushSlopeAbs);
              }
            }
          }

          shading = PdfShading.SimpleAxial(_writer, x0, y0, x1, y1, fromColor, toColor, false, false);
        }

        var radialGradientBrush = _backgroundBrush as RadialGradientBrush;
        if (radialGradientBrush != null)
        {
          float xCenter = 0, yCenter = 0, radius = 0;
          shading = PdfShading.SimpleRadial(_writer, xCenter, yCenter, 0, xCenter, yCenter, radius, fromColor, toColor, true, true);
        }

        if (shading != null)
          position.BackgroundColor = new ShadingColor(new PdfShadingPattern(shading));
      }

      //Fill the rectangle
      canvases[PdfPTable.BACKGROUNDCANVAS].Rectangle(position);
    }