IMPORTANT
- MUST Rename Attached File SampleQR.jpg to SampleQR.bmp.
- The forum does not allow me to upload a bmp file. So I had to rename the extension.
Attached are 3 PDFs. The file names indicate which one needs to use reversed y-axis coordinate.
- This method will add the sample QR code image to the bottom right corner of each page.
- Uncomment this line in the code to bypass the heuristics to see the QR code going to the wrong corner on 2 of the attached PDFs
- //isReverseYCoordinate = false;
QUESTION: What do I need to check to know for sure if the PDF needs to use reversed y-coordinate? The heuristics I implemented is a band-aid and is not reliable.
/
Platform: .Net 4.5.1
Version: Aspose.pdf 6.7.0.0
This method accepts a path to a pdf file.
- Opens pdf.
- Iterates pages.
- Add QR code to bottom right corner of each page.
- Heuristics - the code checks some pdf properties to determine if need to reverse y-coordinate when embedding a QR code.
/
private void AddQrCode(String docPath)
{
// determine img width - matches sample image
Int32 imgWidth = 40;
//set license
License license = new License();
license.SetLicense(String.Format(@"{0}\Aspose.Pdf.lic", HttpRuntime.AppDomainAppPath));
// load file
Document pdfDoc = new Document(docPath);
// HEURISTICS TO DETERMINE IF NEED TO REVSERVE Y-AXIS COORDINATE
Boolean isReverseYCoordinate = false;
// check creator
String[] checkPdfCreatorsArr = “FrameMaker 7.|PScript5.dll Version 5.”.Split(’|’);
foreach (String s in checkPdfCreatorsArr) {
if (pdfDoc.Info.Creator.StartsWith(s)) {
isReverseYCoordinate = true;
break;
}
}
// check version
if (!isReverseYCoordinate) {
if (String.CompareOrdinal(pdfDoc.Version, “1.5”) < 0) {
// less than 1.5
isReverseYCoordinate = true;
}
}
// override flag for testing
//isReverseYCoordinate = false;
// END HEURISTICS
// iterate pages
foreach (Aspose.Pdf.Page pdfPage in pdfDoc.Pages) {
// create QR image and convert to memory stream
Bitmap bmp = (Bitmap)System.Drawing.Image.FromFile(@“C:\TestQR\SampleQR.bmp”);
// NORMALLY - when y-axis does not need to be reversed, we need to rotate the QR code by 270-deg.
// BUT, if y-axis needs to be reversed, no need to rotate QR code.
if (!isReverseYCoordinate) {
bmp.RotateFlip(RotateFlipType.Rotate270FlipNone);
}
MemoryStream memStream = new MemoryStream();
bmp.Save(memStream, ImageFormat.Bmp);
memStream.ToArray();
// add image to page resources
pdfPage.Resources.Images.Add(memStream);
//using GSave operator: this operator saves current graphics state
pdfPage.Contents.Add(new Operator.GSave());
// coordinates for QR code - bottom right corner
Double pageWidth = pdfPage.Rect.Width;
Double pageHeight = pdfPage.Rect.Height;
Double lowerLeftX = pageWidth - imgWidth;
Double lowerLeftY = pageHeight;
Double upperRightX = pageWidth;
Double upperRightY = pageHeight - imgWidth;
// REVERSE THE RECTANGLE Y-AXIS COORDINATES TO ADD IMAGE TO TOP RIGHT, SO IT WILL BE PLACED ON BOTTOM RIGHT!
if (isReverseYCoordinate) {
lowerLeftY = _defaultImgWidth;
upperRightY = 0;
}
//create Rectangle and Matrix objects
Aspose.Pdf.Rectangle rectangle = new Aspose.Pdf.Rectangle(lowerLeftX, lowerLeftY, upperRightX, upperRightY);
Matrix matrix = new Aspose.Pdf.DOM.Matrix(new Double[] { rectangle.URX - rectangle.LLX, 0, 0, rectangle.URY - rectangle.LLY, rectangle.LLX, rectangle.LLY });
//using ConcatenateMatrix (concatenate matrix) operator: defines how image must be placed
pdfPage.Contents.Add(new Operator.ConcatenateMatrix(matrix));
XImage ximage = pdfPage.Resources.Images[pdfPage.Resources.Images.Count];
//using Do operator: this operator draws image
pdfPage.Contents.Add(new Operator.Do(ximage.Name));
//using GRestore operator: this operator restores graphics state
pdfPage.Contents.Add(new Operator.GRestore());
}
// save file
String newFileName = String.Concat(“Output-”, DateTime.Now.Ticks, “.pdf”);
pdfDoc.Save(String.Format(@“c:\TestQR{0}”, newFileName));
}