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

Notification

Icon
Error

Options
Go to last post Go to first unread
Paul Rayman  
#1 Posted : Sunday, September 4, 2016 1:21:50 AM(UTC)
Paul Rayman

Rank: Administration

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

Thanks: 3 times
Was thanked: 112 time(s) in 109 post(s)
Question:
To insert an image at the mouse click position, I got up to the code found here. 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?

Answer
To achieve your goals you need to
1. call SetMatrix method to positioning the image on the page;
2. Do not use Generatecontent() method to prevent the loss of page content. Use dictionaries modification technique instead.

UPD. Starting with version 3.40.2704 this can be done much easier.
Please look find more details here

Please look at code below (this code uses the features which are available since 3.2.1.2704 version of the Pdfium.Net SDK)
Code:

private void pdfViewer1_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
    //Determine the page number where the mouse was clicked
    int page = pdfViewer1.PointInPage(e.GetPosition(pdfViewer1));
    //Translate the mouse coordinates to the page's coordinate system
    var pointOnPage = pdfViewer1.ClientToPage(page, e.GetPosition(pdfViewer1));
    //Load bitmap from file and insert it into page
    var bmp = System.Drawing.Bitmap.FromFile(@"d:\0\img1.png") as System.Drawing.Bitmap;
    var image = InsertImageToPage(pdfViewer1.Document, pdfViewer1.Document.Pages[page], bmp, pointOnPage);
    pdfViewer1.InvalidateVisual();
    //Insert image into page dictionary
    InsertIntoDictionary(pdfViewer1.Document, pdfViewer1.Document.Pages[page], image);
    //Save document
    pdfViewer1.Document.Save(@"d:\0\6\test.pdf", SaveFlags.NoIncremental);
}

public PdfImageObject InsertImageToPage(PdfDocument doc, PdfPage page, System.Drawing.Bitmap bmp, Point atPoint)
{
    var bi = bmp.LockBits(
        new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height),
        System.Drawing.Imaging.ImageLockMode.ReadOnly,
        System.Drawing.Imaging.PixelFormat.Format32bppArgb);

    //Create PdfBitmap object from .Net bitmap
    var bitmap = new PdfBitmap(
        bmp.Width,
        bmp.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);

    //Scale image object to it's actual width and heihgt
    image.SetMatrix(bmp.Width, 0, 0, bmp.Height, (float)atPoint.X, (float)atPoint.Y);

    page.PageObjects.InsertObject(image);
    return image;
}

public void InsertIntoDictionary(PdfDocument doc, PdfPage page, PdfImageObject image)
{
    //Get page dictionary, list of indirect objects and original page content
    var pageDict = page.Dictionary;
    var list = PdfIndirectList.FromPdfDocument(doc);

    //Convert contents to array. 
    PdfTypeArray array = ConvertContentsToArray(pageDict["Contents"], list, pageDict);

    //Get stream of image.
    IntPtr streamHandle = Pdfium.FPDFImageObj_GenerateStream(image.Handle, page.Handle);
    var stream = PdfTypeStream.Create(streamHandle);

    //Add image's stream into list of indirect objects and then add it to array.
    int num = list.Add(stream);
    array.AddIndirect(list, num);
}

public PdfTypeArray ConvertContentsToArray(PdfTypeBase contents, PdfIndirectList list, PdfTypeDictionary pageDict)
{
    //check the original content whether it's an array
    if (contents is PdfTypeArray)
        return contents as PdfTypeArray;  //if contents is a array just return it
    else if (contents is PdfTypeIndirect)
    {
        if ((contents as PdfTypeIndirect).Direct is PdfTypeArray)
            return (contents as PdfTypeIndirect).Direct as PdfTypeArray; //if contents is a reference to array then return that array
        else if ((contents as PdfTypeIndirect).Direct is PdfTypeStream)
        {
            //if contents is a reference to a stream then create a new array and insert stream as a first element of array
            var array = PdfTypeArray.Create();
            array.AddIndirect(list, (contents as PdfTypeIndirect).Direct);
            //Add array into list of indirect objects
            list.Add(array);
            //And set it as a contents of the page
            pageDict.SetIndirectAt("Contents", list, array);
            return array;
        }
        else
            throw new Exception("Unexcpected content type");
    }
    else
        throw new Exception("Unexcpected content type");
}

Edited by user Wednesday, November 7, 2018 11:37:38 PM(UTC)  | Reason: Not specified

vmchacon  
#2 Posted : Tuesday, May 23, 2017 5:35:34 PM(UTC)
vmchacon

Rank: Newbie

Groups: Registered
Joined: 5/17/2017(UTC)
Posts: 1
Location: CDMX

Hi,

I've made slightly changes to the code to test release 3.7.7.2704.

The bitmap is added correctly in the screen, but it's not saved in the new pdf.

What do I need to do?
Is it a matter of a paid license?
Can I use this SDK to build a UI canvas designer? Like a drawing tool?
Is it possible to select programmatically a bitmap already added to the pdf?
Even more, can I add a bitmap, save the document and after re-openning the pdf, select the added bitmap?
(to rotate it perhaps, or changing its position?)

Thanks in advance,
vmchacon

Code:

using Patagames.Pdf.Enums;
using Patagames.Pdf.Net;
using Patagames.Pdf;
using Patagames.Pdf.Net.BasicTypes;

private void pdfViewer1_MouseDoubleClick(object sender, MouseEventArgs e)
{
    //Determine the page number where the mouse was clicked
    int page = pdfViewer1.PointInPage(e.Location);
    //( e.GetPosition(pdfViewer1));
    //Translate the mouse coordinates to the page's coordinate system
    var pointOnPage = pdfViewer1.ClientToPage(page, e.Location);
        //GetPosition(pdfViewer1));
    //Load bitmap from file and insert it into page
    var bmp = System.Drawing.Bitmap.FromFile(@"C:\test.png") as System.Drawing.Bitmap;
    var image = InsertImageToPage(pdfViewer1.Document, pdfViewer1.Document.Pages[page], bmp, pointOnPage);
    pdfViewer1.ClearRenderBuffer();
    pdfViewer1.Invalidate();
            
    //InvalidateVisual();
    //Insert image into page dictionary
    InsertIntoDictionary(pdfViewer1.Document, pdfViewer1.Document.Pages[page], image);
    //Save document
    pdfViewer1.Document.Save(@"C:\test2.pdf", SaveFlags.NoIncremental);

}

public PdfImageObject InsertImageToPage(PdfDocument doc, PdfPage page, System.Drawing.Bitmap bmp, PointF atPoint)
{
    var bi = bmp.LockBits(
        new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height),
        System.Drawing.Imaging.ImageLockMode.ReadOnly,
        System.Drawing.Imaging.PixelFormat.Format32bppArgb);

    //Create PdfBitmap object from .Net bitmap
    var bitmap = new PdfBitmap(
        bmp.Width,
        bmp.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);

    //Scale image object to it's actual width and heihgt
    image.SetMatrix(bmp.Width, 0, 0, bmp.Height, (float)atPoint.X, (float)atPoint.Y);

    page.PageObjects.InsertObject(image);
    return image;
}

public void InsertIntoDictionary(PdfDocument doc, PdfPage page, PdfImageObject image)
{
    //Get page dictionary, list of indirect objects and original page content
    var pageDict = page.Dictionary;
    var list = PdfIndirectList.FromPdfDocument(doc);

    //Convert contents to array. 
    PdfTypeArray array = ConvertContentsToArray(pageDict["Contents"], list, pageDict);

    //Get stream of image.
    IntPtr streamHandle = Pdfium.FPDFImageObj_GenerateStream(image.Handle, page.Handle);
    var stream = PdfTypeStream.Create(streamHandle);

    //Add image's stream into list of indirect objects and then add it to array.
    int num = list.Add(stream);
    array.AddIndirect(list, num);
}

public PdfTypeArray ConvertContentsToArray(PdfTypeBase contents, PdfIndirectList list, PdfTypeDictionary pageDict)
{
    //check the original content whether it's an array
    if (contents is PdfTypeArray)
        return contents as PdfTypeArray;  //if contents is a array just return it
    else if (contents is PdfTypeIndirect)
    {
        if ((contents as PdfTypeIndirect).Direct is PdfTypeArray)
            return (contents as PdfTypeIndirect).Direct as PdfTypeArray; //if contents is a reference to array then return that array
        else if ((contents as PdfTypeIndirect).Direct is PdfTypeStream)
        {
            //if contents is a reference to a stream then create a new array and insert stream as a first element of array
            var array = PdfTypeArray.Create();
            array.AddIndirect(list, (contents as PdfTypeIndirect).Direct);
            //Add array into list of indirect objects
            list.Add(array);
            //And set it as a contents of the page
            pageDict.SetIndirectAt("Contents", list, array);
            return array;
        }
        else
            throw new Exception("Unexcpected content type");
    }
    else
        throw new Exception("Unexcpected content type");
}

Edited by moderator Wednesday, May 24, 2017 9:43:32 AM(UTC)  | Reason: Not specified

Paul Rayman  
#3 Posted : Wednesday, May 24, 2017 9:42:30 AM(UTC)
Paul Rayman

Rank: Administration

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

Thanks: 3 times
Was thanked: 112 time(s) in 109 post(s)
​1. What do I need to do to make the example working at a 100%?
Is it a matter that a need a paid license?

​​You can use the trial version of SDK for 30 days without any paid.
It has only one restrictions - the size of PDF documents which size between 1024 Kb and 10 Mb can't be opened. Other documents can be loaded without any restriction.
The SDK does not have any other limitations except described above.
So you can test it on documents wich meets these conditions.

2. Can I use this SDK to build a UI canvas designer? Like a drawing tool?
Yes, you can. It is a complex task, but it can be implemented. Also you should know the PdfViewer does not have such functionality. You should implement your own control.

​3. Is it possible to select programmatically a bitmap already added to the pdf?
​You can find the image under mouse pointer and then drawn an selection by self. PdfViewer does not have such functionality, but you can create a derived class, override DrawPage method (or other most suitable of follofing
​DrawPageBackColor
DrawFillForms
DrawPageBorder
DrawFillFormsSelection
DrawTextHighlight
DrawTextSelection
DrawCurrentPageHighlight
DrawPageSeparators
OnPaint
​)
​and draw the selection by self.
​How to find an image under mouse pointer is shown here:
http://forum.patagames.com/posts/t137-Is-it-possible-to-extract-image-under-mouse-pointer


4. Even more, can I add a bitmap, save the document and after re-openning the pdf, select the added bitmap?
Well, there is a many options.
​If you need to save the document as a tiff file, then you can render each page anto image and the save that image as tiff file or as a page inside a tiff file. Please find an example here:
http://forum.patagames.com/posts/t257-Save-Pdf-as-Tiff
​If you want to save it as a PDF the there is some problems. Pdfium can do it with losing any other content on the page. The images only can be saved. If this behavior is right for you then just call GenerateContent method before saving
​http://pdfium.patagames.com/help/html/3df4308b-e96a-ba47-c65a-bd6beccac211.htm
​Otherwise, please look a workaround at the first post on this topic to know how to save the imageswithout losing other content

​5. (to rotate it perhaps, or changing its position?)
You can apply transformation matrix to found image object to transfrom it.
https://pdfium.patagames.com/help/html/1946efb7-8ace-1e73-1e28-c5edc3586df7.htm
Please read more about matrices​ here
http://blog.patagames.com/post/what-is-transformation-matrix-and-how-to-use

Edited by user Thursday, May 25, 2017 8:30:31 PM(UTC)  | Reason: Not specified

Users browsing this topic
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.