We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: November 16, 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

C# insert an image into a PDF Document

Thread ID:

Created:

Updated:

Platform:

Replies:

142445 Feb 4,2019 11:48 PM UTC Feb 6,2019 11:56 AM UTC WinForms 4
loading
Tags: PDF
Darrin Edwards
Asked On February 4, 2019 11:48 PM UTC

Hi guys,

Using the following code I take an image from the clipboard and insert it into a new PDF Document ...

{code}
            // get the image from the clipboard
            Image img = Clipboard.GetImage();

            // create a blank PDF document  
            PdfDocument doc = new PdfDocument();
            //Add a page to the document.
            PdfPage page = doc.Pages.Add();
            //Create PDF graphics for the page
            PdfGraphics graphics = page.Graphics;
            //Load the image from the object (obtained from clipboard).
            PdfBitmap image = new PdfBitmap(img);
            //Draw the image
            graphics.DrawImage(image, 0, 0);
            //Save the document.
            doc.Save("Output.pdf");
            //Close the document.
            doc.Close(true);

{End Code}

Whilst this works, if the image is bigger than the PDF page, then the image is cropped. Can you demonstrate how you would insert the picture ensuring that it is not cropped?

MTIA

DWE

Darrin Edwards
Replied On February 5, 2019 01:23 AM UTC

Hi All, I managed to resolve my problem and now offer the code for those that would also like an answer to a similar query - if there are improvements perhaps those that make them could post them here ...

// get the image from the clipboard

Image img = Clipboard.GetImage();

 

// create a blank PDF document 

PdfDocument doc = new PdfDocument();

//Add a page to the document.

PdfPage page = doc.Pages.Add();

//Create PDF graphics for the page

PdfGraphics graphics = page.Graphics;

//Load the image from the object (obtained from clipboard).

PdfBitmap image = new PdfBitmap(img);

 

float PageWidth = page.Graphics.ClientSize.Width;

float PageHeight = page.Graphics.ClientSize.Height;

float myWidth = image.Width;

float myHeight = image.Height;

 

float shrinkFactor;

 

if (myWidth > PageWidth)

{

    shrinkFactor = myWidth / PageWidth;

    myWidth = PageWidth;

    myHeight = myHeight / shrinkFactor;

}

 

if (myHeight > PageHeight)

{

    shrinkFactor = myHeight / PageHeight;

    myHeight = PageHeight;

    myWidth = myWidth / shrinkFactor;

}

 

 

// give us a 10mm margin

float XPosition = 10; // (PageWidth - myWidth) / 2;

float YPosition = 10; // (PageHeight - myHeight) / 2;

 

//Draw the image

graphics.DrawImage(image, XPosition, YPosition, myWidth, myHeight);

 

try

{

    SaveFileDialog sd = new SaveFileDialog()

    {

        DefaultExt = "*.pdf",

        Filter = "PDF Files (*.pdf)|*.pdf",

        InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),

        FileName = "copiedImage.pdf",

        Title = "Save screenshot to...",

        OverwritePrompt = true,

        CheckPathExists = true

    };

 

    if (sd.ShowDialog() == DialogResult.OK)

    {

        // save our document stream to our file name

        //Save the document.

        doc.Save(sd.FileName);

    }

 

}

catch (Exception ex)

{

    //sl.Error("Save - Error => " + ex.Message);

    //throw;

}

 

 

 

//Close the document.

doc.Close(true);

 


Karthikeyan Chandrasekar [Syncfusion]
Replied On February 5, 2019 12:11 PM UTC

Hi Darrin, 
Thank you for your interest. Revert us back if you need any further assistance. 
Regards, 
Karthikeyan  


Darrin Edwards
Replied On February 5, 2019 12:27 PM UTC

Karthik - is there any way the code thats posted can be posted so it looks like code rather than poorly formatted text ?

Additionally - do you guys have any suggestions or improvements on the code that you might like to put out there ?

Regards

Darrin

Karthikeyan Chandrasekar [Syncfusion]
Replied On February 6, 2019 11:56 AM UTC

Hi Darrin, 
We have updated your code snippet to look better and readable, you can also do this by copying the code snippet directly from Visual Studio and paste in the forum comments. 
We have also checked your code snippet, and it looks like the best practice to achieve your requirement. 
Regards, 
Karthikeyan  


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

;