我正在寻找一个图像(PNG,JPEG,GIF),以byte[]的形式转换为PDF格式.

I'm currently using this function, which works, but cuts off the bottom of images that over a certain height or specific proportions; for example 500x2000.
Where am I going wrong here?

public byte[] ConvertImageToPDF(byte[] bytes)
{
    byte[] pdfArray;
    using (var memoryStream = new MemoryStream())
    {
        using (var pdfWriter = new PdfWriter(memoryStream))
        {
            var pdf = new PdfDocument(pdfWriter);
            var document = new Document(pdf);
            ImageData imageData = ImageDataFactory.Create(bytes);
            document.Add(new Image(imageData));
            document.Close();
        }
        pdfArray = memoryStream.ToArray();
    }
    return pdfArray;
}

推荐答案

I suppose what you want is the PdfWriter to auto-scale the Image inside the Document.
Optionally, position the Image in the center of the Page.

您可以更改代码设置[Image].SetAutoScale(true)[Image].SetHorizontalAlignment(HorizontalAlignment.CENTER):

Note:我为iText.Layout.Properties(别名:PdfProperties)和iText.Layout.Element.Image(别名:PdfImage)定义了别名,以避免与其他别名冲突.Net程序集,这些程序集的类和枚举数具有相同的确切名称.只要把它们取下来,以防你根本不需要它们.

using iText.IO.Image;
using iText.Kernel.Pdf;
using iText.Layout;
using PdfProperties = iText.Layout.Properties;
using PdfImage = iText.Layout.Element.Image;

public byte[] ConvertImageToPDF(byte[] imageBytes)
{
    using (var ms = new MemoryStream()) {
        using (var pdfWriter = new PdfWriter(ms)) {
            var pdf = new PdfDocument(pdfWriter);
            var document = new Document(pdf);

            var img = new PdfImage(ImageDataFactory.Create(imageBytes))
                .SetAutoScale(true)
                .SetHorizontalAlignment(PdfProperties.HorizontalAlignment.CENTER);

            document.Add(img);
            document.Close();
            pdf.Close();
            return ms.ToArray();
        }
    }
}

You can also specify the size, in floating point units, of the Image and use the [Image].ScaleToFit() method, to scale the Image within those bounds.
Here, using a PageSize set to PageSize.A4. You can of course set different measures.

using iText.Kernel.Geom;

// [...]
var document = new Document(pdf);

var page = document.GetPageEffectiveArea(PageSize.A4);
var img = new PdfImage(ImageDataFactory.Create(imageBytes))
    .ScaleToFit(page.GetWidth(), page.GetHeight())
    .SetHorizontalAlignment(PdfProperties.HorizontalAlignment.CENTER);

// [...]

Csharp相关问答推荐

当前文化和当前文化之间没有区别IgnoreCase,为什么?

需要多次输入的控制台应用程序

C#中的多yield 机制

将实例绑定方法传递到列表foreach

如何将字节数组转换为字符串并返回?

ListaryImportProperty的默认DllImportSearchPathsProperty行为

C#.NET依赖项注入顺序澄清

具有单一导航属性的EF核心一对多关系

有没有办法使.NET 6应用程序在特定的.NET 6运行时版本上运行

HttpConext.Request.Path和HttpConext.GetEndpoint()之间的差异

如何在C#中使用Postman中的本地IP向本地主机上运行的本地API发出请求

.NET SDK包中的官方C#编译器在哪里?

反序列化私有成员

如何防止Visual Studio断点以红色突出显示到整行?

如何返回具有泛型的类?

在C#ASP.NET内核中使用INT AS-1进行控制器场景的单元测试

为什么我的自定义Json.net转换器不工作?

Linq SELECT的多条指令

如何将 colored颜色 转换为KnownColor名称?

无法使用直接URL通过PictureBox.ImageLocation加载图像