使用Aspose.Pdf替换文本时,偶尔会抛出异常

您好!我的在使用Aspose.Pdf(23.4.0.0)进行文本内容替换时,同一份文件偶尔抛出异常,每次的异常信息还不一样,相同代码逻辑再次重试时又是正常了。这个什么原因呢?
代码:

var textFragmentAbsorber = new TextFragmentAbsorber("$TC");  //$TP $CC $CP
                        page.Accept(textFragmentAbsorber);
                        var textFragmentCollection = textFragmentAbsorber.TextFragments;
                        foreach (TextFragment textFragment in textFragmentCollection)
                        {
                            textFragment.Text = dto.Copies.ToString();
                        }

可能出现的异常
异常1:

Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')
at #=zmcmFK44ua_78K0uhsdt3OukSaDirQsLRaTrUdqs$516BL4EYqg==.#=ziCVv_iZTUnWW()\n   at Aspose.Pdf.Operators.SelectFont.#=zAW42pz0=(#=zq4EcwWUNU5$UAcJIvUU02qli1hB3pbAeoM3D2JHj$pcq #=zoeMx1bA=)\n   at Aspose.Pdf.Operator..ctor(Int32 #=zKp$zJ6o=, #=zq4EcwWUNU5$UAcJIvUU02qli1hB3pbAeoM3D2JHj$pcq #=zoeMx1bA=)\n   at Aspose.Pdf.Operators.TextOperator..ctor(Int32 #=zKp$zJ6o=, #=zq4EcwWUNU5$UAcJIvUU02qli1hB3pbAeoM3D2JHj$pcq #=zoeMx1bA=)\n   at Aspose.Pdf.Operators.TextStateOperator..ctor(Int32 #=zKp$zJ6o=, #=zq4EcwWUNU5$UAcJIvUU02qli1hB3pbAeoM3D2JHj$pcq #=zoeMx1bA=)\n   at Aspose.Pdf.Operators.SelectFont..ctor(Int32 #=zKp$zJ6o=, #=zq4EcwWUNU5$UAcJIvUU02qli1hB3pbAeoM3D2JHj$pcq #=zoeMx1bA=)\n   at #=zmcmFK44ua_78K0uhsdt3OukSaDirQsLRaTrUdqs$516BL4EYqg==.#=zyB$s5sE=(Int32 #=zKp$zJ6o=)\n   at Aspose.Pdf.OperatorCollection.#=zbqFTI7j2fI8L()\n   at Aspose.Pdf.OperatorCollection.get_Count()\n   at #=zUCCFEnc8iGlSEATxRvUz6VFAeCi9GguWaxCbrW5mia30H5_uYQDo9lc=.#=z2MD1OwE=(Page #=zZ6Izcgs=)\n   at #=zgW__vYWXclFRJ$eSEvkQ_BouyV1jDDz02y9SNcy6TwvMx43oqx4uR2poRaVX.#=zj5UQKjym9Rxa(BaseOperatorCollection #=z$sTblxg=, Resources #=zXYgPCp8=, Page #=zZ6Izcgs=, Rectangle #=z31JYStOBdhnu)\n   at #=zgW__vYWXclFRJ$eSEvkQ_BouyV1jDDz02y9SNcy6TwvMx43oqx4uR2poRaVX.#=zj5UQKjym9Rxa(BaseOperatorCollection #=z$sTblxg=, Resources #=zXYgPCp8=, Rectangle #=z31JYStOBdhnu)\n   at #=zgW__vYWXclFRJ$eSEvkQ_BouyV1jDDz02y9SNcy6TwvMx43oqx4uR2poRaVX.#=z9l_CJQs=()\n   at #=zgW__vYWXclFRJ$eSEvkQ_BouyV1jDDz02y9SNcy6TwvMx43oqx4uR2poRaVX..ctor(Page #=zZ6Izcgs=, TextSearchOptions #=zZjC4mu51d6MV, Boolean #=z3E0VV7T8a39t)\n   at Aspose.Pdf.Text.TextFragmentAbsorber.Visit(Page page)

异常2:

Unable to cast object of type '#=zIsAc_nO1daVH6$_ho4Qseg0BV57LV0exeg==' to type '#=zmH2Vwqc0D5kHK5Bv9ROKbnroRd5Jf$S4QA=='.
at #=zmcmFK44ua_78K0uhsdt3OukSaDirQsLRaTrUdqs$516BL4EYqg==.#=ziCVv_iZTUnWW()\n   at Aspose.Pdf.Operators.SelectFont.#=zAW42pz0=(#=zq4EcwWUNU5$UAcJIvUU02qli1hB3pbAeoM3D2JHj$pcq #=zoeMx1bA=)\n   at Aspose.Pdf.Operator..ctor(Int32 #=zKp$zJ6o=, #=zq4EcwWUNU5$UAcJIvUU02qli1hB3pbAeoM3D2JHj$pcq #=zoeMx1bA=)\n   at Aspose.Pdf.Operators.TextOperator..ctor(Int32 #=zKp$zJ6o=, #=zq4EcwWUNU5$UAcJIvUU02qli1hB3pbAeoM3D2JHj$pcq #=zoeMx1bA=)\n   at Aspose.Pdf.Operators.TextStateOperator..ctor(Int32 #=zKp$zJ6o=, #=zq4EcwWUNU5$UAcJIvUU02qli1hB3pbAeoM3D2JHj$pcq #=zoeMx1bA=)\n   at Aspose.Pdf.Operators.SelectFont..ctor(Int32 #=zKp$zJ6o=, #=zq4EcwWUNU5$UAcJIvUU02qli1hB3pbAeoM3D2JHj$pcq #=zoeMx1bA=)\n   at #=zmcmFK44ua_78K0uhsdt3OukSaDirQsLRaTrUdqs$516BL4EYqg==.#=zyB$s5sE=(Int32 #=zKp$zJ6o=)\n   at Aspose.Pdf.OperatorCollection.#=zbqFTI7j2fI8L()\n   at Aspose.Pdf.OperatorCollection.get_Count()\n   at #=zUCCFEnc8iGlSEATxRvUz6VFAeCi9GguWaxCbrW5mia30H5_uYQDo9lc=.#=z2MD1OwE=(Page #=zZ6Izcgs=)\n   at #=zgW__vYWXclFRJ$eSEvkQ_BouyV1jDDz02y9SNcy6TwvMx43oqx4uR2poRaVX.#=zj5UQKjym9Rxa(BaseOperatorCollection #=z$sTblxg=, Resources #=zXYgPCp8=, Page #=zZ6Izcgs=, Rectangle #=z31JYStOBdhnu)\n   at #=zgW__vYWXclFRJ$eSEvkQ_BouyV1jDDz02y9SNcy6TwvMx43oqx4uR2poRaVX.#=zj5UQKjym9Rxa(BaseOperatorCollection #=z$sTblxg=, Resources #=zXYgPCp8=, Rectangle #=z31JYStOBdhnu)\n   at #=zgW__vYWXclFRJ$eSEvkQ_BouyV1jDDz02y9SNcy6TwvMx43oqx4uR2poRaVX.#=z9l_CJQs=()\n   at #=zgW__vYWXclFRJ$eSEvkQ_BouyV1jDDz02y9SNcy6TwvMx43oqx4uR2poRaVX..ctor(Page #=zZ6Izcgs=, TextSearchOptions #=zZjC4mu51d6MV, Boolean #=z3E0VV7T8a39t)\n   at Aspose.Pdf.Text.TextFragmentAbsorber.Visit(Page page)

异常3:

Index was outside the bounds of the array.
at #=zUCCFEnc8iGlSEATxRvUz6VFAeCi9GguWaxCbrW5mia30H5_uYQDo9lc=.#=zBZcNrWE=(Operator #=zN2yFfQ0=)\n   at #=zUCCFEnc8iGlSEATxRvUz6VFAeCi9GguWaxCbrW5mia30H5_uYQDo9lc=.#=z2MD1OwE=(Page #=zZ6Izcgs=)\n   at #=zgW__vYWXclFRJ$eSEvkQ_BouyV1jDDz02y9SNcy6TwvMx43oqx4uR2poRaVX.#=zj5UQKjym9Rxa(BaseOperatorCollection #=z$sTblxg=, Resources #=zXYgPCp8=, Page #=zZ6Izcgs=, Rectangle #=z31JYStOBdhnu)\n   at #=zgW__vYWXclFRJ$eSEvkQ_BouyV1jDDz02y9SNcy6TwvMx43oqx4uR2poRaVX.#=zj5UQKjym9Rxa(BaseOperatorCollection #=z$sTblxg=, Resources #=zXYgPCp8=, Rectangle #=z31JYStOBdhnu)\n   at #=zgW__vYWXclFRJ$eSEvkQ_BouyV1jDDz02y9SNcy6TwvMx43oqx4uR2poRaVX.#=z9l_CJQs=()\n   at #=zgW__vYWXclFRJ$eSEvkQ_BouyV1jDDz02y9SNcy6TwvMx43oqx4uR2poRaVX..ctor(Page #=zZ6Izcgs=, TextSearchOptions #=zZjC4mu51d6MV, Boolean #=z3E0VV7T8a39t)\n   at Aspose.Pdf.Text.TextFragmentAbsorber.Visit(Page page)

pdf文件:
Demo.pdf (123.9 KB)

还有时会出现,没有抛出异常信息,pdf里有要查找的内容,但textFragmentAbsorber.TextFragments.Count为0的情况。

@cqhg90258
The code you provided is not entirely complete and I tried to reproduce the issue by changing it a little.

var doc = new Document(dataDir + "Demo.pdf");
var textFragmentAbsorber = new TextFragmentAbsorber("$TC");  //$TP $CC $CP
Page page = doc.Pages[1];
page.Accept(textFragmentAbsorber);
var textFragmentCollection = textFragmentAbsorber.TextFragments;
Console.WriteLine("num: " + textFragmentCollection.Count);
foreach (TextFragment textFragment in textFragmentCollection)
{
    textFragment.Text = "123";//dto.Copies.ToString();
}

But I had no exceptions and the number of fragments found was always one.
Please check how the above code works in your environment to be sure that this is not the cause.

这种情况偶发性很低,在生产环境可能几千次会遇到一次,但在我们的测试环境一直没有够重现这个问题。
我看在 TextSearchOptions 这个属性里有 IgnoreResourceFontErrors 这个属性,当设置为true时是不是就不会抛出异常了呢?设置为true会不会影响影查找文本的结果呢?

@cqhg90258

Unfortunately, not being able to reproduce the problem, I can only make some judgments.

The first two exceptions you cited are related to setting the font during output. And there is a possibility that there are some violations in the document regarding the description of fonts, and then setting IgnoreResourceFontErrors to true can help you. When this flag is set, if any violations for fonts are encountered, an exception is not immediately thrown, but an attempt is made to somehow circumvent the problem in cases where this is possible.
Well, let’s say substitute some default font instead of the missing one, some parameter instead of the missing or clearly incorrect one.

好的,谢谢,我尝试一下!!

@cqhg90258
Yes, try it, it should have an effect.