Add an image as an overlay to a PDF

Q: I try to use PDFSDK.NET to add an image as overlay on a PDF file. I
use the sample code from PDFLayerTestCS and try to modify for our
testing. Here is my sample code,

static Obj CreateGroup1(PDFDoc doc, Obj layer) {
  ...
  Element element = builder.CreateImage(img, 200, 200, 10, 10);
  ..
}

CreateImage(img, x, y, hscale, yscale) will place the image on (x,y)
position, however, for the scale,
What values should I place for these? If I want to keep the size of
the image, should I put 1 for the scale? How about scale to Half?

By the way, the coordinates for x,y, are they measured from the lower-
left corner?
-----------------
A: For a more basic image stamping approach you may want to take a
look at AddImage sample project instead of PDFLayerTestCS . PDFLayer
sample is only useful if you have a strict requirement to create PDFs
with OCGs (Optional Content Groups).

You could use img.GetImageWidth()/Height() multiplied by some scaling
factor:

Element element = builder.CreateImage(img, 10, 10, img.GetImageWidth()
* 0.5, img.GetImageHeight() * 0.5);

Alternatively you could use the ratio between img.GetImageWidth()/
Height() as the aspect ratio and use it a multiplier for a fixed
dimensions of the image.

coordinates for x,y, are they measured from the lower-left corner?

Correct.

Q: I now use this statement to put an image on (x,y) coordinates (from
upper left corner)

Element element = element_builder.CreateImage(img, new Matrix2D(100,
0, 0, 100, x, page.GetPageHeight() - y));

I guess the 100 are used to keep the image as 100% (no resize)

If I need to resize image to half-size, should I use 50 instead of 100
in above statement?
-----------------
A: Yes this would resize image to half-size. Please keep in mind that
the image would be drawn in a 50x50 point square, so if the image is
not square it would be squished. To preserve the aspect ratio you
would also need to use the ration of image width and height
(img.GetImageWidth()/img.GetImageHeight()).

Q: I have tried to stamp two images (let’s say first image is 100%
actual size (Laptop_100.gif), another is 50% resize of
the first image (Laptop_50.gif)) onto two PDF (Same PDF).

But it turned out that two images are shown at the same size. Would
you kindly advise which part of my codes go wrong??

    Page page = doc.GetPage(page_no);

            writer.Begin(page); // Begin
writing to this page

            System.Drawing.Bitmap bmp =
MiscUtility.BytesToBitmap(overlay);
            Image img = Image.Create(doc, bmp);

            Element element = bld.CreateImage(img, new Matrix2D(100,
0, 0, 100, x, page.GetPageHeight() - y));

            writer.WritePlacedElement(element);
            writer.End(); // Finish writing to the page
--------------

A: The problem is in the code. It is stretching any given image in a
100x100 point square located at location (x, page.GetPageHeight() –
y).

The first parameter in the Matrix2D is the horizontal scaling factor
(the width of the image).
The fourth parameter in the Matrix2D is the vertical scaling factor
(the height of the image).
The last two parameters in Matrix2D are horizontal and vertical
offsets.
The second and third parameters in Matrix2D can be used for things
such as rotation, skew, etc.

So if you want that image with greater number of pixels is larger on
PDF page, you need to take this into account when placing the image on
the page. For example:

double scale = 0.5;
Element element = bld.CreateImage(img, new
Matrix2D(img.GetImageWidth() * scale, 0, 0, img.GetImageHeight() *
scale, x, page.GetPageHeight() - y));