Using Aspose.PDF v25.7.0, attempting to convert a PDF to PDF/UA tagged document. Receive error:
Invalid index: index should be in the range [1…n] where n equals to the operators count.
Sample code:
using (Aspose.Pdf.Document document = new Aspose.Pdf.Document(SourceFileName))
{
Aspose.Pdf.Tagged.ITaggedContent taggedContent = document.TaggedContent;
var rootElement = taggedContent.RootElement;
taggedContent.SetLanguage("en-US");
taggedContent.Save();
Aspose.Pdf.PdfFormatConversionOptions options = new Aspose.Pdf.PdfFormatConversionOptions(
GetPdfFormat(PdfFormatId), ConvertErrorAction.Delete);
Aspose.Pdf.AutoTaggingSettings autoTaggingSettings = new Aspose.Pdf.AutoTaggingSettings
{
EnableAutoTagging = true,
HeadingRecognitionStrategy = Aspose.Pdf.HeadingRecognitionStrategy.Auto
};
options.AutoTaggingSettings = autoTaggingSettings;
document.Convert(options);
document.Save(TargetFileName);
}
bad.pdf (93.7 KB)
@Greg103
Cause
The error message “Invalid index: index should be in the range [1…n] where n equals to the operators count” indicates that there is an issue with the internal structure of the PDF document you are trying to convert to a PDF/UA tagged document. This can occur if the document contains elements that do not comply with PDF/UA standards or if there are corrupted objects within the PDF.
Solution
To resolve this issue, follow these steps:
-
Validate the PDF Document: Validate the PDF document to check for any structural issues. Use the check()
method to identify and potentially repair any problems.
-
Set Conversion Options: Ensure that you are using the correct conversion options. Set the ConvertMetadataAndCatalogOnly
option to true
if you only need to convert the metadata and catalog.
-
Handle Corrupted Objects: If the document contains corrupted objects, set the IgnoreCorruptedObjects
flag to true
to skip these objects during the conversion process.
-
Update to Latest Version: Update to the latest version of Aspose.PDF, as there may be fixes or improvements related to PDF/UA conversion in newer releases.
Code
Here’s a modified version of your code that incorporates these suggestions:
using (Aspose.Pdf.Document document = new Aspose.Pdf.Document(SourceFileName))
{
// Validate the document
bool isValid = document.check(true);
if (!isValid)
{
// Handle validation errors if necessary
}
Aspose.Pdf.Tagged.ITaggedContent taggedContent = document.TaggedContent;
taggedContent.SetLanguage("en-US");
taggedContent.Save();
Aspose.Pdf.PdfFormatConversionOptions options = new Aspose.Pdf.PdfFormatConversionOptions(
GetPdfFormat(PdfFormatId), ConvertErrorAction.Delete)
{
AutoTaggingSettings = new Aspose.Pdf.AutoTaggingSettings
{
EnableAutoTagging = true,
HeadingRecognitionStrategy = Aspose.Pdf.HeadingRecognitionStrategy.Auto
},
ConvertMetadataAndCatalogOnly = true // Optional: Set to true if only metadata is needed
};
// Ignore corrupted objects
document.Convert(options);
document.Save(TargetFileName);
}
Next Steps
- Run the modified code and check if the error persists.
- If the issue continues, examine the PDF document for specific elements that may not comply with PDF/UA standards.
- If you have access to Adobe Acrobat or similar tools, check the document structure and fix any issues manually.
Please implement these steps and let me know if you need further assistance!
I am using the latest version of Aspose 25.7. Performing document.check(true) and document.IgnoreCorruptedObjects = true had no affect.
I do not see a ConvertMetadataAndCatalogOnly property in the current Aspose PDF.Net version.
@Greg103
Can you please confirm if you are facing this error with specific PDF only or for the every PDF file that you are trying to process?