PDFsharp页面大小和设置边距问题c#

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15966672/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 18:38:01  来源:igfitidea点击:

PDFsharp page size and set margin issue c#

c#pdfsharp

提问by Thomas

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.

我正在使用 PDFsharp lib 将图像转换为 pdf。我需要设置边距和页面大小,所以我从这个论坛得到了一个技巧来设置页面大小和边距。从这里我得到了我使用的代码,但在两个区域出现错误。这是我得到的代码。

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

现在我的程序可以编译,但是当我设置边距时,我收到错误,称为 PdfSharp 不包含 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

我正在使用 pdf Sharp 库版本1.0.898.0

So guide me how can I set margin.

所以请指导我如何设置边距。

Here is my full code to generate pdf from image file

这是我从图像文件生成pdf的完整代码

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;
        }

采纳答案by I liked the old Stack Overflow

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

您不能使用 PDFsharp 设置边距 - 绘制项目时在页面上保留边距由您决定。

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.

您复制的代码来自 MigraDoc。MigraDoc 包含在 PDFsharp 中,但在更高级别上工作,您不处理页面,而是处理部分,在这里您可以设置边距。

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.

有关详细信息,请参阅 PDFsharp 和 MigraDoc 的网站:
http://pdfsharp.net/
还有一个 PDFsharp 示例,显示了如何设置页面大小。

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

使用 PDFsharp 时,您可以在页面上的任何位置绘制图像,还可以指定图像的大小。