PDFsharp & MigraDoc Foundation

Replace text box with an image in the exact location
Page 1 of 1

Author:  prudeesh [ Wed Apr 22, 2020 7:33 pm ]
Post subject:  Replace text box with an image in the exact location

I have a requirement to insert an image on the existing pdf, at a specific location. So, in order to identify the exact position I have inserted a text box. Get the position of the text box using pdfsharp and replace that with an image. The image is not aligning properly when I use the text box rectangle coordinates. To be specific the vertical(y) alignment is not correct, the horizantal(x) seems to be correct. Here is my sample code. Any help would be appreciated.

static readonly string input_path = @"C:\VSProjects\TestFiles\";
static readonly string output_path = @"C:\VSProjects\Willett\Output\";

private static readonly string documentPath = input_path + "Signature-Pages.pdf";
private static readonly string documentOutputPath = output_path + "Signature-Pages-signed.pdf";
private static readonly string signatureImage = input_path + "signature.jpg";

static void Main(string[] args)
var document = PdfReader.Open(documentPath, PdfDocumentOpenMode.Modify);

foreach (var documentPage in document.Pages)
var annotations = documentPage.Annotations;

foreach (var pdfItem in annotations)
var pdfAnnotation = (PdfAnnotation) pdfItem;
Console.WriteLine($"Annotation Type: {pdfAnnotation.Title}; Coordinates: {pdfAnnotation.Rectangle}");

var annotationXRect = pdfAnnotation.Rectangle.ToXRect();

Console.WriteLine($"mod {16 *16 % 100}");

using (XGraphics gfx = XGraphics.FromPdfPage(documentPage))
XImage image = XImage.FromFile(signatureImage);
gfx.DrawImage(image, pdfAnnotation.Rectangle.ToXRect());




Author:  TH-Soft [ Thu Apr 23, 2020 7:16 am ]
Post subject:  Re: Replace text box with an image in the exact location


I assume you have to convert the co-ordinates.

See here:

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group