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

Notification

Icon
Error

Post a reply
From:
Message:

Maximum number of characters in each post is: 32767
Bold Italic Underline   Highlight Quote Choose Language for Syntax Highlighting Insert Image Insert an existing Attachment or upload a new File... Create Link   Unordered List Ordered List   Left Justify Center Justify Right Justify   Outdent Indent   More BBCode Tags
Font Color Font Size
Security Image:
Enter The Letters From The Security Image:
  Preview Post Cancel

Last 10 Posts (In reverse order)
Paul Rayman Posted: Monday, September 5, 2016 6:02:32 PM(UTC)
 
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
Paul Rayman Posted: Tuesday, July 26, 2016 8:25:59 PM(UTC)
 
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.
mukesh Posted: Tuesday, July 26, 2016 4:34:34 PM(UTC)
 
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();



mukesh Posted: Monday, July 25, 2016 1:30:19 AM(UTC)
 
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.