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  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-itOriginally 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 8:46:58 PM(UTC)
| Reason: Not specified
|