Thread ID: |
Created: |
Updated: |
Platform: |
Replies: |
142445 | Feb 4,2019 11:48 PM UTC | Feb 6,2019 11:56 AM UTC | WinForms | 4 |
![]() |
Tags: PDF |
// 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); |
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.
This page will automatically be redirected to the sign-in page in 10 seconds.