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. Image for the cookie policy date

C# insert an image into a PDF Document

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

4 Replies

DE Darrin Edwards 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);

 



KC Karthikeyan Chandrasekar Syncfusion Team 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  



DE Darrin Edwards 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


KC Karthikeyan Chandrasekar Syncfusion Team 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  


Loader.
Live Chat Icon For mobile
Up arrow icon