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();
Originally Posted by: mukesh
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
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 11:46:58 PM(UTC)
|
Reason: Not specified
Important Information:
The Patagames Software Support Forum uses cookies. By continuing to browse this site, you are agreeing to our use of cookies.
More Details
Close