title: How to set format of PDF by PDFsharp, set pagesize to A4 and fit on page (shrink)
body: <p>I would like to convert image to PDF using PDFsharp in ASP.NET MVC project.
  And I have successfully converted them to PDF, but they are in strange format,for example: this image:</p>
  
  <p><a href="https://i.stack.imgur.com/bbS4X.jpg" rel="nofollow noreferrer">Original image</a></p>
  
  <p>After conversion with PDFsharp:</p>
  
  <p><a href="https://i.stack.imgur.com/01Wfl.png" rel="nofollow noreferrer">Partially visible image on page</a></p>
  
  <p>I hope PDF files can adaptive page, fit on page (shrink),and pagesize is A4.</p>
  
  <p>Below is my code:</p>
  
  <pre><code>PdfSharp.Pdf.PdfDocument doc = new PdfSharp.Pdf.PdfDocument();
  doc.PageLayout = PdfPageLayout.OneColumn;
  
  PdfPage pdfPage = new PdfPage();
  XSize size = PageSizeConverter.ToSize(PdfSharp.PageSize.A4);
  
  pdfPage.Orientation = PageOrientation.Portrait;
  
  pdfPage.Width = size.Width;
  pdfPage.Height = size.Height;
  pdfPage.TrimMargins.Top = 0.5;
  pdfPage.TrimMargins.Right = 0.5;
  pdfPage.TrimMargins.Bottom = 0.5;
  pdfPage.TrimMargins.Left = 0.5;
  doc.Pages.Add(new PdfSharp.Pdf.PdfPage());
  XGraphics xgr = XGraphics.FromPdfPage(doc.Pages[0]);
  XImage img;
  try
  {
      img = XImage.FromFile(s);
      //pdfPage.Width = img.Size.Width;
      //pdfPage.Height = img.Size.Height;
      xgr.DrawImage(img, 0, 0);
      img.Dispose();
  }
  catch (Exception ex)
  {
      flag = 3;
      errormassage = ex.Message;
      errorfiles.Add(s);
  }
  
  
  xgr.Dispose();
  FileInfo fi = new FileInfo(s);
  doc.Save(s.Replace(fi.Extension, ".pdf"));
  doc.Close();
  </code></pre>