title: PDFsharp page size and set margin issue c#
body: <p>I am converting an image to pdf using PDFsharp lib. I need to set margin &amp; page size so I got a trick from this forum to set page size and margin. From here I got code which I used but getting error for two area. Here is code which I got.</p>
  
  <pre><code>page = document.AddPage();
  //page.Size = PdfSharp.PageSize.A4;
  XSize size = PageSizeConverter.ToSize(PdfSharp.PageSize.A4);
  if(page.Orientation == PageOrientation.Landscape)
  {
     page.Width  = size.Height;
     page.Height = size.Width;
  }
  else
  {
     page.Width  = size.Width;
     page.Height = size.Height;
  }
  
  // default unit in points 1 inch = 72 points
  page.TrimMargins.Top = 5;
  page.TrimMargins.Right = 5;
  page.TrimMargins.Bottom = 5;
  page.TrimMargins.Left = 5;
  </code></pre>
  
  <p>I got an error for this line </p>
  
  <pre><code>XSize size = PageSizeConverter.ToSize(PdfSharp.PageSize.A4);
  </code></pre>
  
  <p>so i need to change it to </p>
  
  <pre><code>System.Drawing.Size size = PageSizeConverter.ToSize(PdfSharp.PageSize.A4);
  </code></pre>
  
  <p>Now my program compiles but when I set margin then I am getting error called
  <strong>PdfSharp does not contain a definition for TrimMargins</strong></p>
  
  <p>these below line does not compile for setting margin.</p>
  
  <pre><code>    pdfPage.TrimMargins.Top = 5;
      pdfPage.TrimMargins.Right = 5;
      pdfPage.TrimMargins.Bottom = 5;
      pdfPage.TrimMargins.Left = 5;
  </code></pre>
  
  <p>I am using the pdf sharp library version <strong>1.0.898.0</strong></p>
  
  <p>So guide me how can I set margin.</p>
  
  <h2>Here is my full code to generate pdf from image file</h2>
  
  <pre><code>public static string GeneratePdfFromImage(string source)
          {
              string destinaton = source.Replace("gif", "pdf");
              PdfDocument doc = new PdfDocument();
              PdfPage pdfPage = new PdfPage();
              System.Drawing.Size size = PageSizeConverter.ToSize(PdfSharp.PageSize.A4);
              pdfPage.Orientation = PageOrientation.Portrait;
  
              pdfPage.Width = size.Width;
              pdfPage.Height = size.Height;
              pdfPage.TrimMargins.Top = 5;
              pdfPage.TrimMargins.Right = 5;
              pdfPage.TrimMargins.Bottom = 5;
              pdfPage.TrimMargins.Left = 5;
  
              doc.Pages.Add(pdfPage);
  
              XGraphics xgr = XGraphics.FromPdfPage(doc.Pages[0]);
              XImage img = XImage.FromFile(source);
  
              try
              {
                  xgr.DrawImage(img, 0, 0);
                  doc.Save(destinaton);
                  doc.Close();
              }
              catch (Exception ex)
              {
                  destinaton = "";
              }
  
              return destinaton;
          }
  </code></pre>