How to add custom font in Free text annotaion font family dropdown

Hi, Everyone

How can I add new font family into Font Family dropdown list? (.Net Core Pdf Viewer)



3 Replies

AC ArunKumar Chandrakesan Syncfusion Team September 16, 2022 01:56 PM UTC

Hi Nattapong ,


Query: Can I customize the free text annotation FontFamily list on PDFViewer?


Currently, the Syncfusion PDF Viewer supports four font types in the free text annotation. But we have another solution that you can embed the required fonts on download.  You can copy the content within the free text and then embed the font on download. This will maintain the font within the document. For your reference, we have provided the code snippet and sample below.
Please let us know if the solution provided is helpful.


Code snippet:


 public IActionResult Download([FromBody] Dictionary<string, string> jsonObject)

        {

            //Initialize the PDF Viewer object with memory cache object

            PdfRenderer pdfviewer = new PdfRenderer(_cache);

            string documentBase = pdfviewer.GetDocumentAsBase64(jsonObject);

            string base64Data = documentBase.Split("base64,")[1];

 

            byte[] document1 = Convert.FromBase64String(base64Data);

            PdfLoadedDocument ldoc = new PdfLoadedDocument(document1);

            FileStream fontStream = new FileStream(GetDocumentPath("tahoma.ttf"), FileMode.Open, FileAccess.ReadWrite);

            for (int i = 0; i < ldoc.Pages.Count; i++)

            {

                PdfLoadedPage lpage = ldoc.Pages[i] as PdfLoadedPage;

                for (int j = 0; j < lpage.Annotations.Count; j++)

                {

                    PdfLoadedFreeTextAnnotation lAnnot = lpage.Annotations[j] as PdfLoadedFreeTextAnnotation;

                    if (lAnnot != null)

                    {

                        // If this condition is required then you can use it.

                        //if (IsThaiCharacter(lAnnot.MarkUpText))

                        {

                            fontStream.Position = 0;

                            lAnnot.Font = new PdfTrueTypeFont(fontStream, lAnnot.Font.Size);

                            lAnnot.ComplexScript = true;

                            lAnnot.SetAppearance(true);

                        }

                    }

                }

            }

            MemoryStream stream = new MemoryStream();

            ldoc.Save(stream);

            string base64string = Convert.ToBase64String((stream as MemoryStream).ToArray());

            ldoc.EnableMemoryOptimization = true;

            ldoc.Close(true);

            stream.Dispose();

            return Content("data:application/pdf;base64," + base64string);

        }


Sample link: https://www.syncfusion.com/downloads/support/directtrac/general/ze/pdfviewer_modified_service-696322863




Regards,

Arun kumar



NP Nattapong Pongrat September 19, 2022 03:15 AM UTC

Hi, Arun kumar

Thanks for your response. Your sample works perfectly.



AC ArunKumar Chandrakesan Syncfusion Team September 19, 2022 09:04 AM UTC

Hi Nattapong,

 

Thank you for your updates

 

Regards,

Arun kumar


Loader.
Up arrow icon