Hi Team,
We are using Aspose.PDF for .NET (version 21.6.0) on .NET Core 3.1, and we need to convert PDFs to fully black & white (grayscale) when a flag is enabled.
Our requirement:
The final output PDF must be completely grayscale — no color in any images, shapes, vectors, or text.
We are currently using RgbToDeviceGrayConversionStrategy, but we are seeing that some PDFs still contain colored elements after conversion. This happens especially with images, vector graphics, and certain complex content.
Code We Are Using Now
private void ConvertToPdf(string inputFilePath, string outputFilePath, bool blackAndWhite)
{
try
{
using (var pdfDocument = new Document(inputFilePath))
{
if (blackAndWhite)
{
Logger.InfoFormat("PDF to PDF- Applying black and white conversion");
ApplyGrayscaleConversion(pdfDocument);
}
PdfFormatConversionOptions options = new PdfFormatConversionOptions(PdfFormat.PDF_A_3A);
pdfDocument.Convert(options);
Logger.InfoFormat("PDF to PDF- File saving process started");
pdfDocument.Save(outputFilePath);
Logger.InfoFormat("PDF to PDF- File saving process finished");
}
}
catch (Exception ex)
{
Logger.ErrorFormat(ex, "Conversion failed for {0}. Error:{1}", inputFilePath, ex.Message);
throw;
}
}
private void ApplyGrayscaleConversion(Document pdfDocument)
{
var strategy = new RgbToDeviceGrayConversionStrategy();
for (int i = 1; i <= pdfDocument.Pages.Count; i++)
{
try
{
strategy.Convert(pdfDocument.Pages[i]);
if (pdfDocument.Pages.Count > 100 && i % 50 == 0)
{
System.Threading.Thread.Sleep(10);
}
}
catch (Exception ex)
{
Logger.ErrorFormat(ex, "Page {0} grayscale conversion failed. Error{1}", i, ex.Message);
}
}
}
Question
What is the correct or recommended way in Aspose.PDF (21.6.0) to ensure that the entire PDF, including images and vector elements, is fully converted to black & white?
We are looking for the proper API or official approach that guarantees that no color remains in the final output.