logo
Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

New Topic Post Reply
Options
Go to last post Go to first unread
mukesh  
#1 Posted : Monday, July 25, 2016 1:30:19 AM(UTC)
Quote
mukesh

Rank: Member

Groups: Registered
Joined: 7/14/2016(UTC)
Posts: 20
United States

Thanks: 4 times
How can I insert an image at the location where the user clicked on PDFViewer in WPF C# app? I want the user to be able to see the image that got inserted before he can choose to save the doc.
mukesh  
#2 Posted : Tuesday, July 26, 2016 4:34:34 PM(UTC)
Quote
mukesh

Rank: Member

Groups: Registered
Joined: 7/14/2016(UTC)
Posts: 20
United States

Thanks: 4 times
To insert an image at the mouse click position, I got up to the following code in WPF app. It puts the image on the PDF but the following things happen
1. Puts the signature at the end of the document, not where the user clicked. How can I position the image to be inserted at the mouse clicked position?
2. When the document is saved, most of the text on the document is lost and only the Acro fields and the inserted image at the end of the doc is saved. How do I prevent loss of other parts of the document?

//Load document
var doc = pdfViewer1.Document;

//Create .Net bitmap from file and lock bits for copy into pdf image
var bi = bmpSign.LockBits(
new System.Drawing.Rectangle(0, 0, bmpSign.Width, bmpSign.Height),
System.Drawing.Imaging.ImageLockMode.ReadOnly,
System.Drawing.Imaging.PixelFormat.Format32bppArgb);

//Create PdfBitmap object from .Net bitmap
var bitmap = new PdfBitmap(
bmpSign.Width,
bmpSign.Height,
BitmapFormats.FXDIB_Argb,
bi.Scan0,
bi.Stride);

//Create pdf image object and then set PdfBitmap object into it.
var image = PdfImageObject.Create(doc);
image.SetBitmap(bitmap);

//Move image object to 0,0 and set it's size to 1px;
image.SetMatrix(bmpSign.Width, 0, 0, bmpSign.Height, 0, 0);

var page = doc.Pages.CurrentPage;
page.PageObjects.InsertObject(image,-1);

page.GenerateContent();

pdfViewer1.ClearRenderBuffer();



Paul Rayman  
#3 Posted : Tuesday, July 26, 2016 8:25:59 PM(UTC)
Quote
Paul Rayman

Rank: Administration

Groups: Administrators
Joined: 1/5/2016(UTC)
Posts: 1,138

Thanks: 10 times
Was thanked: 133 time(s) in 130 post(s)
Originally Posted by: mukesh Go to Quoted Post
1. How can I position the image to be inserted at the mouse clicked position?


Please look at following example.
Code:

private void pdfViewer1_MouseDown(object sender, MouseEventArgs e)
{
	var doc = pdfViewer1.Document;
	if (doc == null)
		return;

	using (var testBmp = Bitmap.FromFile(@"d:\0\test.png") as Bitmap)
	{
		var locationOnPage = pdfViewer1.ClientToPage(pdfViewer1.CurrentIndex, e.Location);
		InsertImageAt(doc.Pages.CurrentPage, testBmp, locationOnPage);
	};
	pdfViewer1.ClearRenderBuffer();
}

private void InsertImageAt(PdfPage page, Bitmap bmp, PointF location)
{
			
	var bi = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
	using (var bitmap = new PdfBitmap(bmp.Width, bmp.Height, BitmapFormats.FXDIB_Argb, bi.Scan0, bi.Stride))
	{
		using (var image = PdfImageObject.Create(page.Document))
		{
			image.SetBitmap(bitmap);
			image.SetMatrix(bmp.Width / bmp.HorizontalResolution * 72, 0, 0, bmp.Height / bmp.VerticalResolution * 72, location.X, location.Y);
			page.PageObjects.InsertObject(image, -1);
		}
	}
	bmp.UnlockBits(bi);
}


The explaining how to work with matrix you can found here: https://blog.patagames.c...matrix-and-how-to-use-it

Originally Posted by: mukesh Go to Quoted Post

2. When the document is saved, most of the text on the document is lost and only the Acro fields and the inserted image at the end of the doc is saved. How do I prevent loss of other parts of the document?


Unfortunately Pdfium can't save the modified documents.
It is a viewer and it is able to save the images and forms only.

3. There is another problem stems from the previous one.
When a page is not visible, the PdfViewer is automaticaly dispose it. Since saving is not possible, then all changes will be lost.

Unfortunately, if the page will not be disposed then the free memory very quickly ends. Chiefly for large documents.
I can suggest the following solution: You can subscribe to the PageCollection.PageLoaded event, so everytime when the page is loaded into memory you inserts an image again, if necessary. Of course you need track which pages should have the image and which - no.

Please look at code what illustrates the idea below

Code:

...
pdfViewer1.DocumentLoaded += PdfViewer1_DocumentLoaded;


private void PdfViewer1_DocumentLoaded(object sender, EventArgs e)
{
    pdfViewer1.Document.Pages.PageLoaded += Pages_PageLoaded;
}

private void Pages_PageLoaded(object sender, Patagames.Pdf.Net.EventArguments.PdfPageEventArgs e)
{
    //your code to insert images if need here
    e.Page.PageObjects.InsertObject(...)
}


And of course, if you need to save your changes to PDF, you should use a third-party library for generate PDF. For example GhostScript.

Edited by user Tuesday, July 26, 2016 8:46:58 PM(UTC)  | Reason: Not specified

Paul Rayman  
#4 Posted : Monday, September 5, 2016 6:02:32 PM(UTC)
Quote
Paul Rayman

Rank: Administration

Groups: Administrators
Joined: 1/5/2016(UTC)
Posts: 1,138

Thanks: 10 times
Was thanked: 133 time(s) in 130 post(s)
Originally Posted by: mukesh Go to Quoted Post
2. When the document is saved, most of the text on the document is lost and only the Acro fields and the inserted image at the end of the doc is saved. How do I prevent loss of other parts of the document?


We have a solution for this issue.
Please find more details here: http://forum.patagames.com/posts/t225-How-to-insert-image-at-the-mouse-click-position-in-WPF-Pdf-Viewer
Quick Reply Show Quick Reply
Users browsing this topic
Guest
Similar Topics
How to insert image at the mouse click position in WPF Pdf Viewer? (FAQ)
by Paul Rayman 11/7/2018 8:31:11 PM(UTC)
[obsolete]How to insert image at the mouse click position in WPF Pdf Viewer? (FAQ)
by Paul Rayman 9/3/2016 10:21:50 PM(UTC)
New Topic Post Reply
Forum Jump  
You can post new topics in this forum.
You can reply to topics in this forum.
You can delete your posts in this forum.
You can edit your posts in this forum.
You cannot create polls in this forum.
You can vote in polls in this forum.