Encountering an error while generating a PDF with Hindi content, but unable to identify the exact cause due to the obfuscated error message

System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
at System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource)
at „‰ž.Šž.„(• , Int32 , UInt16[] , ‰ž , Boolean )
at „‰ž..‚(• , Int32 , Int32 )
at „‰ž..(• , Int32& )
at „‰ž.›.(• , Int32& )
at „‰ž.›.(• )
at „‰ž.”.(‘‰ž , • )
at  ..ށ( ,  , • , String )
at  ..(String )
at ..(String )
at .‹.( Font,  encodedString, ‚& registeredPdfString)
at .‹.( encodedString, ‚& registeredPdfString)
at .ž–.(String str, ‚& registeredPdfString)
at –.š.(String unicodeString, ‹ resources,  noCharacterAction, Boolean isEmbedded, Boolean isSubset, ‚& encodedString, & selectedFont, String& fontResourceKey)
at ”.“.’ž(•[] choosingStrategies, String unicodeString, ‹ resources,  noCharacterAction, Boolean isEmbedded, Boolean isSubset, ‚& encodedText, & selectedFont, String& fontResourceKey)
at Œ..( , TextEditOptions )
at Aspose.Pdf.Text.TextSegment.set_Text(String value)
at Aspose.Pdf.Text.TextSegment.Œ(‹ )
at Aspose.Pdf.Text.TextBuilder.AppendText(TextFragment , Int32 , Boolean )
at š..( )
at •..Accept(Œ )
at •..Accept(Œ )
at •..Accept(Œ )
at •..Accept(Œ )
at •..Accept(Œ )
at •..Accept(Œ )
at •..Accept(Œ )
at •..Accept(Œ )
at •..Accept(Œ )
at •..Accept(Œ )
at š..Convert(Ž , RectangleF )
at Aspose.Pdf.Page.Accept(Ž , RectangleF , Boolean , Boolean , Boolean )
at Aspose.Pdf.HtmlFragment.‘‡“(RectangleF ,  , Page , RectangleF )
at Aspose.Pdf.HtmlFragment.Process(Double& , Double& , Boolean , Double , Double , List1 , Page , MarginInfo , Table ) at š..Process() at Aspose.Pdf.FloatingBox.Process(Double& , Double& , Double , Double , MarginInfo , Page , Double , List1 )
at š..Process()
at Aspose.Pdf.Page.(Page )
at Aspose.Pdf.Page.ProcessParagraphs()
at Aspose.Pdf.Document.ProcessParagraphs()
at Aspose.Pdf.Document.Save(Stream output)
at CABI.FactsheetAdmin.Pmdg.PdfBuilder.PMDG.PmdgPdf.SavePdfDocument(Object paramsForSave) in D:\Development-02_work\47\s\CABI.FactsheetAdmin.Pmdg.PdfBuilder\PMDG\PmdgPdf.cs:line 1049
at CABI.FactsheetAdmin.Pmdg.PdfBuilder.PMDG.PmdgPdf.Build(Dictionary2 pmdgIds, Boolean exportToFile, StringBuilder diagnosticLog, Boolean includeContentsPage, Boolean getAprovedVersion) in D:\Development-02\_work\47\s\CABI.FactsheetAdmin.Pmdg.PdfBuilder\PMDG\PmdgPdf.cs:line 982 at CABI.FactsheetAdmin.Api.UI.Controllers.PmdgController.GetPdf(Int64 id, Nullable1 diagnosticMode, Nullable`1 getApprovedVersion) in D:\Development-02_work\47\s\CABI.FactsheetAdmin.Api.Controllers\PmdgController.cs:line 139

@bhushanravi20091

Cause

The error you’re encountering, System.ArgumentOutOfRangeException, typically occurs when trying to access an index in a collection that is not valid. In your case, this seems to be related to handling Hindi text in the PDF generation process. This can happen if the font being used does not support the Hindi characters or if there is an issue with how the text is being processed.

Solution

To resolve this issue, ensure that you are using a font that fully supports Hindi characters. You can embed the font in your PDF to avoid any rendering issues. Here’s how you can do that:

  1. Select a Suitable Font: Choose a font that supports Hindi, such as “Mangal” or “Devanagari”.

  2. Embed the Font: When creating your PDF, make sure to embed the font. This can be done by setting the font in your text segments.

Code

Here’s a sample code snippet to help you embed a font while creating a PDF:

using Aspose.Pdf;
using Aspose.Pdf.Text;

Document pdfDocument = new Document();
Page pdfPage = pdfDocument.Pages.Add();

// Load the Hindi font
Font font = FontRepository.FindFont("Mangal");

// Create a TextBuilder to add text
TextBuilder textBuilder = new TextBuilder(pdfPage);
TextFragment textFragment = new TextFragment("नमस्ते"); // Example Hindi text
textFragment.TextState.Font = font;
textFragment.TextState.FontSize = 12;

// Append the text fragment to the page
textBuilder.AppendText(textFragment);

// Save the document
pdfDocument.Save("HindiText.pdf");

Next Steps

  1. Test the above code with your Hindi text.
  2. If the issue persists, check if the font is correctly installed and accessible in your environment.
  3. Ensure that you are using the latest version of Aspose.PDF, as updates often include bug fixes and improvements related to font handling.

If you follow these steps and still encounter issues, please provide additional details about the error, and I will assist you further.