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 : Tuesday, January 5, 2016 3:55:36 AM(UTC)
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)
Question:
I have my viewer working great. I can't seem to figure out how to add a new page and insert a PNG file into that page.

Answer:
The following code example demonstrates how to insert .Net Image into newly added PDF page:

Code:

public void InsertImageToPage()
{
    //Initialize library
    PdfCommon.Initialize();

    //Load document
    pdfViewer1.LoadDocument(@"c:\test002.pdf");
    var doc = pdfViewer1.Document;

    //Create .Net bitmap from file and lock bits for copy into pdf image
    Bitmap bmp = (Bitmap)Bitmap.FromFile(@"c:\sample_image.jpg");
    var bi = bmp.LockBits(
        new Rectangle(0, 0, bmp.Width, bmp.Height),
        ImageLockMode.ReadOnly,
        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);

    //Move image object to 0,0 and set it's size to 1px;
    image.SetMatrix(1, 0, 0, 1, 0, 0);
    //Scale image object to it's actual width and heihgt
    image.Transform(bmp.Width, 0, 0, bmp.Height, 0, 0);

    //Insert blank page into document
    //Get the newly added page and insert image object into page
    doc.Pages.InsertPageAt(doc.Pages.Count, 900, 900);
    var page = doc.Pages[doc.Pages.Count - 1];
    page.PageObjects.InsertObject(image);

    //Save PDF document to the file
    page.GenerateContent();
    doc.Save(@"c:\test002_modified.pdf", Patagames.Pdf.Enums.SaveFlags.NoIncremental);
}
Elm  
#2 Posted : Monday, May 30, 2016 1:27:05 AM(UTC)
Elm

Rank: Member

Groups: Registered
Joined: 5/30/2016(UTC)
Posts: 14
France

Thanks: 4 times
Hi Paul,

implemented this code on a test Winform app in order to add an image to an existing PDF.
When called from the Form ctor, this works great and the image appears but when called for an event (ex : mouse click) the image does not appear.
To make it appear, I need to maximise the form (this must generate an event that make the control refresh ilself).
I've tried control.refresh and control.invalidate but it does not make the image appear.


Any idea ?

Regards,
Elm
Paul Rayman  
#3 Posted : Tuesday, May 31, 2016 3:12:44 AM(UTC)
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)
Hi,

Please try the following:
Code:

pdfViewer1.ClearRenderBuffer();
pdfViewer1.Invalidate();
thanks 1 user thanked Paul Rayman for this useful post.
Elm on 5/31/2016(UTC)
Elm  
#4 Posted : Tuesday, May 31, 2016 9:59:08 AM(UTC)
Elm

Rank: Member

Groups: Registered
Joined: 5/30/2016(UTC)
Posts: 14
France

Thanks: 4 times
Thanks Paul for the reply.

I'm testing your API to append QRCodes to a PDF document.
I can add a QRcode on a page exactly where I want it (ie under the mouse position), that's great.

I then encounter 2 issues :

Issue 1 :

When I try to save with "SaveFlags.Incremental", my QR Code is not saved.
When I try to save with "SaveFlags.NoIncremental", my QR Code is saved but the existing text has gone.
There is certainly something I don't get here. Maybe GenerateContent() which I don't understand the usage.

Issue 2 :

With multi-pages PDF, when I scroll to another page my previous QRCodes in other pages are gone and the PageObjects collection is Disposed.
Is there a way to persist my changes while the PDF is not saved and still have acces to the PageObjects collection ?

Thanks for the help.
My test source-code is available if needed

Edited by user Tuesday, May 31, 2016 10:00:14 AM(UTC)  | Reason: Not specified

Users browsing this topic
Guest (2)
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.