I created a C# application to edit a page and print it, also save it as a pdf. I m facing issue in displaying image on a page. Following is the screenshot of controls of my application:
Application
Following is my code:
using System;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;
namespace PDFCreator
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void close_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void preview_Click(object sender, EventArgs e)
{
printPreviewDialog.Document = printDocument;
printPreviewDialog.Document.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("PaperA4", 840, 1180);
printPreviewDialog.ShowDialog();
}
private void AddLogo(PrintPageEventArgs e)
{
// Add Logo
Point ulCorner = new Point(60, 75);
var img = new Bitmap("C:\Users\admin\Desktop\temp\PDFCreator\Resources\SampleLogo.png");
e.Graphics.DrawImage(img, ulCorner);
}
private void printDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
// Add logo
AddLogo(e);
}
private void print_Click(object sender, EventArgs e)
{
printDocument.Print();
}
}
}
Following is the sample image I m trying to display:
Sample Logo
Issue I m facing:
I m loading the image on on the page without cropping/resizing as shown in code. When I m clicking on Print Preview
button I m getting correct page preview as shown below:
Print preview
But after I m saving it as pdf I m seeing cropped image, as shown below:
Saved pdf
What can be the issue and resolution?
I have tried image after resizing it, I have also tried different image but the behavior is same.
Rupali Desai is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.