I’m hitting an issue with Aspose.Pdf.Facedes.PdfExtractor ExtractImage() function. There is a QR code in the cover page but this function cannot extract it.
If I try to open the pdf file in MS Word and save as PDF, the function will able to extract the QR Code image.
This issue just happens in a few documents in my project. The code snippet below are what I’m using to extract the QR code:
private static List<string> ReadQRCodeFromPdfDocument(string filePath)
{
try
{
Aspose.Pdf.License pdfLicense = new Aspose.Pdf.License();
pdfLicense.SetLicense("Aspose.Pdf.lic");
}
catch
{
//ignore case if not found license
}
try
{
Aspose.BarCode.License barCodeLicense = new Aspose.BarCode.License();
barCodeLicense.SetLicense("Aspose.Barcode.lic");
}
catch
{
//ignore case if not found license
}
List<string> qrCodetexts = new List<string>();
Aspose.Pdf.Facades.PdfExtractor pdfExtractor = new Aspose.Pdf.Facades.PdfExtractor();
pdfExtractor.BindPdf(filePath);
// Set page range for image extraction
// Get images in the first page of document
pdfExtractor.StartPage = 1;
pdfExtractor.EndPage = 1;
// Extract the images
pdfExtractor.ExtractImage();
// Save images to stream in a loop
while (pdfExtractor.HasNextImage())
{
// Save image to stream
MemoryStream imgStream = new MemoryStream();
pdfExtractor.GetNextImage(imgStream);
imgStream.Position = 0;
Bitmap bmp;
//Normal case
try
{
bmp = new Bitmap(imgStream);
}
//If convert image error: Parameter is not valid then save image to png for read QR code
catch
{
MemoryStream memoryStream = new MemoryStream(imgStream.GetBuffer());
memoryStream.Seek(0, SeekOrigin.Begin);
bmp = new Bitmap(memoryStream);
bmp.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png);
}
// Recognize the barcode from the image stream above
using (BarCodeReader reader = new BarCodeReader(bmp, DecodeType.QR))
{
reader.RecognitionMode = RecognitionMode.MaxQuality;
reader.RecognitionMode = RecognitionMode.MaxPerformance;
while (reader.Read())
{
var qrCodetext = reader.GetCodeText();
if (!string.IsNullOrWhiteSpace(qrCodetext) && qrCodetext.IndexOf(_stringPattern) > -1)
{
qrCodetexts.Add(qrCodetext);
}
}
}
if (qrCodetexts.Count > 0) break;
}
return qrCodetexts;
}
Could you guys have any idea what is causing this? and any solution? I will send the source file privately because it contains sensitive data