I've searched all over, but all of the documentation focuses on Java answers which don't have the same methods in the .NET version. I am trying to insert an image to specific positions on a PDF, but I am unable to insert them properly. I can get accurate X/Y coordinates but when it inserts into the PDF, it inserts all of them onto the first page only.
I'm also finding that the height and width don't come out the same as the fields they're replacing. The variables in my watch window show the correct height and width, but it ends up compressed on the PDF. This wasn't a problem with itextSharp when I used to use that.
var widgets = toSet.GetWidgets(); // toSet is the field to get position from
Rectangle position = widgets[0].GetRectangle().ToRectangle();
PdfPage page = widgets[0].GetPage();
if (position == null)
{
throw new ApplicationException("Error locating field");
}
// set bottom left corner and width/height of field
float width = position.GetWidth();
float height = position.GetHeight();
float llx = position.GetX();
float lly = position.GetY();
// resize signature image, set position of bottom left corner to x and y coordinates
image.ScaleToFit(width, height);
image.SetFixedPosition(llx, lly);// I want to put the page number in this method override
doc.Add(image);
The image.SetFixedPosition() method has an override that takes in an integer page number, but I am unable to find the page number from the field programmattically. It also forces me to set a width and I'd like to keep the width proportional to the height which is what I am setting.
CodePudding user response:
Determining the Page Number
According to your code you already do have a page object, the PdfPage page
, and are looking for its page number in the document. You can use the PdfDocument
method
public virtual int GetPageNumber(PdfPage page)
for that.
Setting Only the Target Page Number of an Image
Also you have an image object, Image image
, and want to only set the page for it. You can use the Image
method
public virtual Image SetPageNumber(int pageNumber)
for that.