PDFsharp & MigraDoc Foundation
http://forum.pdfsharp.de/

Replace text box with an image in the exact location
http://forum.pdfsharp.de/viewtopic.php?f=2&t=4127
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());
}

}
}

document.Save(documentOutputPath);
Process.Start(documentOutputPath);

Console.ReadLine();
}

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

Hi!

I assume you have to convert the co-ordinates.

See here:
viewtopic.php?p=12400#p12400

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/