PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Thu Mar 28, 2024 9:34 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: Wed Apr 22, 2020 7:33 pm 
Offline

Joined: Wed Apr 22, 2020 7:20 pm
Posts: 1
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();
}


Top
 Profile  
Reply with quote  
PostPosted: Thu Apr 23, 2020 7:16 am 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 909
Location: CCAA
Hi!

I assume you have to convert the co-ordinates.

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

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 149 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group