Issue with Setting Font in PDF (startIndex, length error)

Hello,

I have written the following code to modify the font of a specific PDF file. My goal is to replace the text font in the PDF with a specified font.

However, during execution, I encounter two error messages:

  1. "startIndex is greater than the length of the string.\r\nParameter name: startIndex"
  2. "Index and length must refer to a location within the string.\r\nParameter name: length"

I am unable to determine the cause of these errors. The text is correctly extracted and read from the PDF.

Below is my code:

System.IO.MemoryStream ms =
new System.IO.MemoryStream(AsposePDF_FontTest.Properties.Resources.Aspose_Total_NET);

Aspose.Pdf.License license = new Aspose.Pdf.License();
license.SetLicense(ms);

string inputPath = “test.pdf”;
// 수정된 PDF 저장 경로
string outputPath = “result.pdf”;

Document pdfDocument = new Document(inputPath);

Font font = FontRepository.OpenFont(@“C:\WINDOWS\Fonts\h2gtrm.ttf”);
//Font font = FontRepository.OpenFont(@“C:\WINDOWS\Fonts\h2gsrb.ttf”);

// PDF 페이지의 텍스트 수정
foreach (Page page in pdfDocument.Pages)
{
// 텍스트 조작 (필요시 텍스트 추가 등)
TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber();
//page.Accept(textFragmentAbsorber);
textFragmentAbsorber.Visit(page); // 페이지에 있는 텍스트를 추출

  // 글꼴을 시스템 글꼴로 설정

  foreach (TextFragment textFragment in textFragmentAbsorber.TextFragments)
      {
      try
      {
          // 텍스트 길이 체크
          if (textFragment.Text.Length > 0)
          {
              // 폰트 변경 전에 TextState가 올바르게 설정되었는지 확인
              if (textFragment.TextState != null && textFragment.TextState.Font != null)
              {
                  // 텍스트의 폰트 변경
                  textFragment.TextState.Font = font;
              }
          }
      }
      catch (Exception ex)
      {
          Console.WriteLine($"텍스트 '{textFragment.Text ?? "(null)"}' 처리 중 오류 발생: {ex.Message}");
      }

  }

}

// 글꼴을 임베딩한 PDF 파일로 저장
pdfDocument.Save(outputPath);

@dayoung

Would you please share your sample PDF document for our reference? We will test the scenario in our environment and address it accordingly.