I am converting an image to pdf using the PDFsharp lib. I need to set the margin and page size so that I get a trick from this forum to set the page size and size. From here I got the code that I used, but I get an error for two areas. Here is the code I received.
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;
I got an error for this line
XSize size = PageSizeConverter.ToSize(PdfSharp.PageSize.A4);
so i need to change it to
System.Drawing.Size size = PageSizeConverter.ToSize(PdfSharp.PageSize.A4);
Now my program compiles, but when I set the field, I raise an error
PdfSharp does not contain a definition for TrimMargins
these lines below are not compiled to specify the field.
pdfPage.TrimMargins.Top = 5;
pdfPage.TrimMargins.Right = 5;
pdfPage.TrimMargins.Bottom = 5;
pdfPage.TrimMargins.Left = 5;
I am using pdf sharp library version 1.0.898.0
So tell me how I can set the margin.
Here is my complete code to create a pdf file from a file
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;
}