Click here to Skip to main content
15,886,806 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am using crystal reports 11 with visual studio 2008. I have requirement to export the report directly to PDF(v9) from code behind. I am using windows 7 and installed required Unicode Fonts (Urdu/Arabic).
When I export report from Report Viewer to PDF then all urdu fonts are displayed in proper order but when I bypass report viewer then I have font problem and are not displayed in proper order.
I have used following code to export to PDF from code behind.

C#
        //getReportDocument(); this function will return ReportDocument  object
        ReportDocument repDoc = getReportDocument();
        Response.Buffer = false;
        Response.ClearContent();
        Response.ClearHeaders();
        try
        {
repDoc.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Response, true, "Customers");
            
        }  

I have also try ExportToDisk but in vain.

My Question is that I want to embed proper font to PDF to display Data Properly.

Any Immediate Help would be appreciated.

Regards

Azhar Iqbal
Posted
Updated 24-Apr-18 0:58am
Comments
ShaikhM 20-Jun-12 0:00am    
As far as I know, crystal reports supports only True Type fonts.
Check link to understand the difference between both the fonts
( http://answers.yahoo.com/question/index?qid=20080621145425AAuKtgk ).
The font you are using may be a Open type font, that CR doesnt support. Just for testing try using a True type font and see if PDF prints, if it doesnt then the problem may not be fonts, and may be somewhere else.

I am sure this has to do with Open Type fonts and True Type fonts.
As I mentioned in the comment above, try using a True type font and see if PDF prints.

Check the following link of a thread from SAP itself.

http://scn.sap.com/thread/2076266#10817872[^]

You should be able to convert the font to TrueType and then run it again.
 
Share this answer
 
Comments
[no name] 20-Jun-12 1:26am    
Thanks for your help.
I have ttf fonts.
I give "Jameel Noori Nastaleeq 2.ttf" font to field at design time in crystal report and it generated right font for me but its size is shrinked.
What Should I do to have proper size?
Thanks again.
ShaikhM 20-Jun-12 18:35pm    
Ok. So atleast you know that your font is working. For size, I would suggest that you try different sizes and also try including enough space in the text field for the text to fit in it. Play some more with it, because thats what I would do.
I add two keys in registry and my problem solved

CSS
HKCR\Software\Business Objects\Suite 11.5\Crystal Reports\Export\PDF\ForceLargerFonts
Type: DWORD
Recognized Values: 0,1
Default Value: 1
Creation Method: manual

HKLM\Software\Business Objects\Suite 11.5\Crystal Reports\Export\PDF\ForceLargerFonts
Type: DWORD
Recognized Values: 0,1
Default Value: 1
Creation Method: manual


Thanks for help.
 
Share this answer
 
v2
i think you have to create path for pdf fonts in ur crystal reports tool..and set pdf font then may be ur problem will solve..
 
Share this answer
 
If you need to use the TTF version of a font for PDF export and do not want to have it installed on the box (maybe the business uses the OTF version for everything else), you can programmatically add the TTF font to your process memory (application scope only) and remove the conflicting OTF fonts. It will only work for Export. The View and Print commands do not use fonts installed in process memory.

1. Add TTF files to your project as resources (Add Resource --> Add Existing File)
2. Add methods and classes below to check for required font names and install/remove fonts before exporting the Crystal Report object to PDF

VB
Dim RequiredFontNames As New List(Of String)
Dim RequiredFontFileNames As New List(Of String)
Dim ConflictingFontFileNames As New List(Of String)

RequiredFontNames.Add("Gotham-Book")
RequiredFontNames.Add("Gotham-Medium")
RequiredFontFileNames.Add("GOTHMBOK.TTF")
RequiredFontFileNames.Add("GOTHMLIG.TTF")
RequiredFontFileNames.Add("GOTHMLIT.TTF")
RequiredFontFileNames.Add("GOTHMMED.TTF")
RequiredFontFileNames.Add("GOTHMMIT.TTF")
RequiredFontFileNames.Add("GOTHMZIT.TTF")
ConflictingFontFileNames.Add("Gotham-Book.otf")
ConflictingFontFileNames.Add("Gotham-BookItalic.otf")
ConflictingFontFileNames.Add("Gotham-Light.otf")
ConflictingFontFileNames.Add("Gotham-LightItalic.otf")
ConflictingFontFileNames.Add("Gotham-Medium.otf")
ConflictingFontFileNames.Add("Gotham-MediumItalic.otf")

  Private Function CheckForRequiredFonts(Optional ByVal manageFonts As Boolean = False) As Boolean
    Dim hasRequiredFonts As Boolean = True
    If manageFonts Then
      FontUtil.RemoveFontFiles(ConflictingFontFileNames)
    End If

    For Each item As String In RequiredFontNames
      If FontUtil.FontExists(item) = False Then
        mReasonForFailure = String.Format("The font '{0}' is not installed on your system.", item)
        hasRequiredFonts = False
        Exit For
      End If
    Next
    If Not hasRequiredFonts And manageFonts Then
      Try
        FontUtil.InstallFontFiles(RequiredFontFileNames)
        hasRequiredFonts = True
        mReasonForFailure = ""
      Catch ex As Exception
        mReasonForFailure &= vbCrLf & "Required font files failed to install."
      End Try
    End If
    Return hasRequiredFonts
  End Function


VB
Imports System.Drawing.Text
Imports System.Drawing
Imports System.Runtime.InteropServices

Public Class FontUtil

  <DllImport("gdi32.dll")>
  Private Shared Function RemoveFontResource(lpFileName As String) As Boolean
  End Function

  <DllImport("gdi32.dll")>
  Private Shared Function AddFontMemResourceEx(pbFont As IntPtr, cbFont As UInteger, pdv As IntPtr, <[In]> ByRef pcFonts As UInteger) As IntPtr
  End Function

  Public Shared Function FontExists(fontName As String) As Boolean
    Dim ifc As New InstalledFontCollection()
    For Each item As FontFamily In ifc.Families
      If item.Name = fontName Then
        Return True
      End If
    Next
    Return False
  End Function

  Public Sub AddFontResource(ByVal resourceName As String)
    Dim fontStream As IO.Stream = Me.GetType().Assembly.GetManifestResourceStream(resourceName)

    'create an unsafe memory block for the data
    Dim data As System.IntPtr = Marshal.AllocCoTaskMem(CInt(fontStream.Length))
    'create a buffer to read in to
    Dim fontData As [Byte]() = New [Byte](fontStream.Length - 1) {}
    'fetch the font program from the resource
    fontStream.Read(fontData, 0, CInt(fontStream.Length))
    'copy the bytes to the unsafe memory block
    Marshal.Copy(fontData, 0, data, CInt(fontStream.Length))

    ' We HAVE to do this to register the font to the system 
    Dim cFonts As UInteger = 0
    AddFontMemResourceEx(data, CUInt(fontData.Length), IntPtr.Zero, cFonts)

    'close the resource stream
    fontStream.Close()
    'free the unsafe memory
    Marshal.FreeCoTaskMem(data)
  End Sub

  Public Shared Sub InstallFontFiles(ByVal fontFileNameList As List(Of String))
    Dim assemblyName As String = "MyAssembly.ReportLib." 'This need to be the namespace of your assembly with trailing period
    Dim myFontUtil As New FontUtil
    For Each fileName In fontFileNameList
      myFontUtil.AddFontResource(assemblyName & fileName) 'All resource stream names start with the assembly namespace 
    Next
  End Sub

  Public Shared Sub RemoveFontFiles(ByVal fontFileNameList As List(Of String))
    For Each fileName In fontFileNameList
      Dim isDeleted As Boolean = RemoveFontResource(fileName)
    Next
  End Sub

End Class
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900