Hi Team, We are using latest version of Aspose.PDF (25.10).
Requirement : We have to apply tags on the PDF ( Searchable PDF).
I am using below code to add the tags but getting an exception like “Specified Cast is not Valid.”
Please find the sample pdf file and tagging.xml files.
TestScanned_Searchable.pdf (1.9 MB)
I am unable to upload the tagging.xml file. Please find the below screenshot for xml file.
image.png (93.7 KB)
public void CreateTaggedPdfFromUnTaggedPdf()
{
string input = @"E:\TestFiles\TaggedPDFFile\TestScanned_Searchable.pdf";
string output = @"E:\TestFiles\TaggedPDFFile\TestScanned_Searchable_Output.pdf";
string normalizedTemp = @"E:\TestFiles\TaggedPDFFile\Temp_Normalized.pdf";
string logFile = @"E:\TestFiles\TaggedPDFFile\TaggingLog.xml"; // or .txt
// Load the existing PDF
Document pdfDocument = new Document(input);
foreach (var page in pdfDocument.Pages)
{
page.Flatten();
}
// (Optional but safer) Normalize the PDF first to reduce complexity
pdfDocument.Convert(logFile, PdfFormat.PDF_A_1B, ConvertErrorAction.Delete);
pdfDocument.Save(normalizedTemp);
// Reload the normalized document
pdfDocument = new Document(normalizedTemp);
// Prepare conversion options for tagging
var options = new PdfFormatConversionOptions(
PdfFormat.PDF_A_1A, // or if you prefer PDF_UA_1
ConvertErrorAction.Delete
);
var autoTagSettings = new AutoTaggingSettings
{
EnableAutoTagging = true,
HeadingRecognitionStrategy = HeadingRecognitionStrategy.Auto
};
options.AutoTaggingSettings = autoTagSettings;
//// Optional: hook progress events for debugging
//pdfDocument.ConvertProgress += (sender, e) =>
//{
// Console.WriteLine($"Progress: page {e.PageNumber}, message: {e.Message}");
//};
try
{
// This should create the tagged structure during conversion
//pdfDocument.Convert(options);
try
{
pdfDocument.Convert(options);
}
catch (Aspose.Pdf.ConvertException ex)
{
if (ex.Message.Contains("q/Q"))
{
Console.WriteLine($"Page issue detected. Flattening...");
foreach (var page in pdfDocument.Pages)
page.Flatten();
pdfDocument.Convert(options);
}
}
pdfDocument.Save(output);
Console.WriteLine("✅ PDF tagged successfully!");
}
catch (InvalidCastException ex)
{
Console.WriteLine("⚠️ Auto-tagging failed: " + ex.Message);
Console.WriteLine("Attempting fallback logical structure repair...");
}
}