Visual Studio Report Designer: Embed font

3.5k Views Asked by At

I have some special font: EAN-13 and I need to insert a barcode into my report, so i have no idea how I can use embed font for that.

I can load my font in runtime:

    private static PrivateFontCollection Pfc;
    private static async Task<FontFamily> EAN13()
    {
        if (Pfc == null)
        {
            Pfc = new PrivateFontCollection();

            Stream fontStream = typeof(DefferedPurchase)
                .Assembly
                .GetManifestResourceStream("ActionsC.Resources.EAN-13.ttf");

            byte[] fontdata = new byte[fontStream.Length];
            await fontStream.ReadAsync(fontdata, 0, (int)fontStream.Length);
            fontStream.Close();
            unsafe
            {
                fixed (byte* pFontData = fontdata)
                {
                    Pfc.AddMemoryFont((IntPtr)pFontData, fontdata.Length);
                }
            }
        }
        return Pfc.Families.FirstOrDefault();
    }

But how i can apply it to this rdlc xml node?

      <Textbox Name="textbox19">
        <CanGrow>true</CanGrow>
        <ToggleImage>
          <InitialState>true</InitialState>
        </ToggleImage>
        <KeepTogether>true</KeepTogether>
        <Paragraphs>
          <Paragraph>
            <TextRuns>
              <TextRun>
                <Value>=Parameters!BarCode.Value</Value>
                <Style>
                  <FontFamily><!--     


                        see here       


                   --></FontFamily>
                  <FontSize>12pt</FontSize>
                </Style>
              </TextRun>
            </TextRuns>
            <Style />
          </Paragraph>
        </Paragraphs>
        <rd:DefaultName>textbox1</rd:DefaultName>
        <Top>13.4995cm</Top>
        <Left>21.53213cm</Left>
        <Height>3.10467cm</Height>
        <Width>5.6385cm</Width>
        <ZIndex>30</ZIndex>
        <Style>
          <PaddingLeft>2pt</PaddingLeft>
          <PaddingRight>2pt</PaddingRight>
          <PaddingTop>2pt</PaddingTop>
          <PaddingBottom>2pt</PaddingBottom>
        </Style>
      </Textbox>

Any ideas?

Opened font window: enter image description here

3

There are 3 best solutions below

2
Mitch On BEST ANSWER

Counterintuitively, the best way to get a barcode into a PDF is often to use a bitmap. They are typically smaller to store in the resultant pdf than the equivalent vector or font based solution, and most barcodes are by definition centered around a single unit-module-width, which can be directly represented as a single pixel.

SSRS and RDLC do support this workflow, but it looks terrible on-screen as it uses a different interpolation mode for onscreen than it does for print and export-to-pdf.

To generate the image, I use Zen Barcode Rendering Framework which is licensed under a royalty free license, but the theory would apply equally to any barcode renderer.

I use an application-hosted http server to allow dynamic barcode rendering:

' Make sure to use EscapeDataString if parameter is not URL safe
=Parameters!BarcodeServiceUri.Value & "2d/datamatrix?moduleSize=2&data=" & Parameters!ToteID.Value

Resultant image:

Code128:
code128 example
Datamatrix:
datamatrix example

How it looks in the designer:
From SSRS Designer

Example PDF from "Export PDF" demoing proper resize:

Screenshot of barcode having been resized properlyenter image description here

The same occurs when printed.

4
Sagar V On

A very small answer is here Modify dynamically a rdlc report (c#)

In the answer from Loïc Haye

Stream st = this.GetType().Assembly.GetManifestResourceStream(_NomRessourceRpt);

// convert stream to string
StreamReader reader = new StreamReader(st);
string reportDef = reader.ReadToEnd();

XmlDocument document = new XmlDocument();
document.LoadXml(reportDef);

There is a resource here about generating bar code

Step 1: For the Basic of RDLS report follow this link:

Step 2: Download the bar code font 3 of 9 from this site:

Step 3: Then go to your rdlc report page:

Step 4: Right click on the Expression(TextBox) which you want to make barcode->select->"TextBox Properties"

Step 5: Text Box Properties->Font->Select font type->ok

Step 6: Then right click on the Expression(TextBox) which you want to make barcode->select->'Expression'

Step 7: Append '*' to the expression value and click OK.

Step 8: Run your application and you can see your barcode in your report like this.

The above content is copied exactly from the site mentioned and I am in no way affiliated with them

If you visit the site, they have image descriptions too and links to some other resources.

3
Oceans On

To be able to embed the font at all, the font has to meet the following conditions:

  • Font embedding privileges are granted by the font author. Installed fonts include a property that indicates whether the font author intends to allow embedding a font in a document. If the property value is EMBED_NOEMBEDDING, the font is not embedded in the PDF file. For more information, see "TTGetEmbeddingType" on msdn.microsoft.com.
  • The Font is TrueType.

If one or more of these conditions is not met, you won't be able to embed the font the way you'd like to. And I believe your only other option is by code, at run time, like you've already discovered.

If both conditions are met then it's quite simple. All you have to do is install this font on the report server.
When any user exports a report to PDF it will automatically be embedded inside the PDF document and the font will be displayed correctly. So you save the trouble of having to install this font on client computers.

The computer designing the report will obviously also need to have this font installed. You simply select the font as shown by your own screenshot, which should register it in the .rdlc file by font name and other necessary properties.


However, if you're programming this for a desktop application without the use of a report server. As far as I know there is no way to make this work other than doing it as you are right now (by code).

Unfortunately SSRS (rdlc) is not advanced enough to accomplish this just by xml nodes.

The official MSDN documentation article can be found here