简体   繁体   English

如何为 PdfSharp 生成的 Pdf 提供自定义大小

[英]How to give custom size to PdfSharp generated Pdf

I was trying to generate a pdf from HTML which containing a table using PdfSharp and HTMLRenderer.我试图从 HTML 生成一个 pdf,其中包含使用 PdfSharp 和 HTMLRenderer 的表格。 The following shows the code.下面显示了代码。

            pdf = PdfGenerator.GeneratePdf(html, PageSize.A3);

            byte[] fileContents = null;
            using (MemoryStream stream = new MemoryStream())
            {
                pdf.Save(stream, true);
                fileContents = stream.ToArray();
                return new FileStreamResult(new MemoryStream(fileContents.ToArray()), "application/pdf");
            }

Is there any possibility for me to provide a custom size to the PDF and change the orientation of the page.我是否有可能为 PDF 提供自定义大小并更改页面方向。 I am using a memory stream to show the PDF directly on the browser display.我正在使用内存流直接在浏览器显示屏上显示 PDF。

You can use the PdfGenerateConfig parameter of the GeneratePdf method to specify a custom page size using the ManualPageSize property.您可以使用GeneratePdf方法的PdfGenerateConfig参数通过ManualPageSize属性指定自定义页面大小。

Use the PageOrientation to get standard page sizes in landscape.使用PageOrientation获得横向的标准页面大小。

Code from comment:来自评论的代码:

var config = new PdfGenerateConfig();
config.PageOrientation = PageOrientation.Landscape;
config.ManualPageSize = new PdfSharp.Drawing.XSize(1080, 828);

pdf = PdfGenerator.GeneratePdf(html, config);

I just put this way:我只是这样说:

var config = new PdfGenerateConfig()
{
    MarginBottom = 100,
    MarginLeft = 20,
    MarginRight = 20,
    MarginTop = 100,
    PageSize = PageSize.A4
};         

PdfDocument pdf = PdfGenerator.GeneratePdf(html, config); 

If I'm not mistaken, PageSize.A3 is not an enum but Rectangle value.如果我没记错的话,PageSize.A3 不是枚举而是 Rectangle 值。 So instead of passing predefined Rectangle you can provide your own, for example:因此,您可以提供自己的矩形,而不是传递预定义的矩形,例如:

new Rectangle(1191, 842) // for album A3 new Rectangle(1191, 842) // 专辑 A3

new Rectangle(842, 595) // for album A4 new Rectangle(842, 595) // 专辑 A4

and so on...等等...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM