Default font for RDLC report

Is it possible to set default font in RDLC report ? I have report where i want to change font by I don't want to change it in every element of the report.


There isn't a way to change the font used for the entire report using the design interface. However if you are trying to replace one font with another, eg Tahoma with Verdana, then you can open the code view (View > Code) and do a Find and Replace there.

Note that Arial is the default font for Reporting Services reports and therefore the font is only defined in the code for fonts other than Arial. If you need to change from Arial to another font you will have to do this manually in the designer.


There is a way to do this. It's actually fairly simple. Backup your rdl file before starting. This answer requires a simple app to be written:

Open the rdl as an XML document. Find all TextRun nodes. Look for a Style node in each. If no Style node is found, add one with a FontFamily node inside with the desired font specified. If the Style node is found, look for the FontFamily node. If it is found you can either leave it alone or replace the value with the desired font, depending on your requirement. If there is no FontFamily node, add it with the specified font.

ETA: I have the code and it works fabulously for me. Just note this is destructive, ie. your file will be overwritten.

pivate static void AddFontsToRdlc(string fileName, string defaultFont)
{
  if (!File.Exists(fileName))
  {
    // Report file does not exist
    return;
  }
  XmlDocument document = new XmlDocument();
  document.Load(fileName);
  string documentNamespace = document.DocumentElement.NamespaceURI;
  XmlNodeList nodes = document.GetElementsByTagName("TextRun");
  bool foundStyle = false;
  bool foundFontFamily = false;
  foreach (XmlNode node in nodes)
  {
    foundStyle = false;
    foundFontFamily = false;
    foreach (XmlNode childNode in node.ChildNodes)
    {
      if (childNode.Name == "Style")
      {
        foundStyle = true;
        foreach (XmlNode styleNode in childNode.ChildNodes)
        {
          if (styleNode.Name == "FontFamily")
          {
            // Change the font here if changing all fonts to the default font
            // Alternatively, specify what font should change to what font with a switch
            foundFontFamily = true;
            break;
          }
        }
        if (!foundFontFamily)
        {
          XmlElement fontElement = document.CreateElement("FontFamily", documentNamespace);
          fontElement.InnerText = defaultFont;
          childNode.AppendChild(fontElement);
        }
        break;
      }
    }
    if (!foundStyle)
    {
      XmlNode styleElement = document.CreateElement("Style", documentNamespace);
      XmlElement fontElement = document.CreateElement("FontFamily", documentNamespace);
      fontElement.InnerText = defaultFont;
      styleElement.AppendChild(fontElement);
      node.AppendChild(styleElement);
    }
  }
  document.Save(fileName);
}

转到工具>选项>环境>字体和颜色,然后将其更改为所需的字体。

链接地址: http://www.djcxy.com/p/11270.html

上一篇: paren`(...)宏

下一篇: RDLC报告的默认字体