PDFsharp page size and set margin issue c#
Asked Answered
S

1

9

I am converting an image to pdf using PDFsharp lib. I need to set margin & 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.

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 margin then I am getting error called PdfSharp does not contain a definition for TrimMargins

these below line does not compile for setting margin.

    pdfPage.TrimMargins.Top = 5;
    pdfPage.TrimMargins.Right = 5;
    pdfPage.TrimMargins.Bottom = 5;
    pdfPage.TrimMargins.Left = 5;

I am using the pdf sharp library version 1.0.898.0

So guide me how can I set margin.

Here is my full code to generate pdf from image 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;
        }
Silvester answered 12/4, 2013 at 8:35 Comment(0)
D
7

You cannot set margins with PDFsharp - it's up to you to reserve margins on the page when you draw items.

The code you copied is from MigraDoc. MigraDoc is included with PDFsharp, but works on a higher level where you do not deal with pages, instead you deal with sections and here you can set margins.

See the website for PDFsharp and MigraDoc for further information:
http://pdfsharp.net/
There also is a PDFsharp sample that shows how to set the page size.

When you use PDFsharp, you can draw images anywhere on the page and you also specify the size of the image.

Datestamp answered 12/4, 2013 at 12:47 Comment(3)
What are you even talking about? pdfPage.TrimMargins.All = 5; is working like it should. You then can work 0 (like 0,0) based and the margin is working.Allophone
@sLw Some things have changed between 2013 and 2018 and between PDFsharp 1.0 and PDFsharp 1.50.Datestamp
oh, I didn't think about that. Sorry ;)Allophone

© 2022 - 2024 — McMap. All rights reserved.