I have Rectangle annotations in XFDF XML format, and after loading them into a PDF, I want to replace some of them with different images.
The annotation size and position should not change. However the issue I have is that if the pages are rotated, then the image appears rotated to the user.
How to add a custom image to an annotation, with out changing its size/position, but the image is always right side up.
The code below adds an image that will have the opposite rotation of the page it is on, so the top of the image will be the same as the top of the source image (unless that source image contains rotation information in its EXIF metadata, if that is a possible case, you should use an image library to get the EXIF data and apply the additional rotation, or use pdftron.PDF.Convert.ToPDF and make an appearance out of the PDF page).
// given: "annot" the pdftron.PDF.Annot you want to edit // given: "page" is pdftron.PDF.Page instance that the above "annot" is on // given: "doc" is the document containing the page and annotation pdftron.PDF.Image img = pdftron.PDF.Image.Create(doc, PathToYourImage); ElementWriter writer = new ElementWriter(); ElementBuilder builder = new ElementBuilder(); writer.Begin(doc); Matrix2D origin = new Matrix2D(1, 0, 0, 1, -0.5, -0.5); pdftron.PDF.Page.Rotate pageRotation = page.GetRotation(); // do reverse rotation of the page rotation, so the image on the page has same orientation of the image (unless that image itself contains a rotation in EXIF metadata) Matrix2D rot = Matrix2D.RotationMatrix(pdftron.PDF.Page.RotationToDegree(pageRotation) / 180.0 * -Math.PI); Matrix2D mtx = rot * origin; writer.WriteElement(builder.CreateImage(img, mtx)); Obj newAppearance = writer.End(); newAppearance.PutRect("BBox", -0.5, -0.5, 0.5, 0.5); annot.SetAppearance(newAppearance);