C#使用itextsharp将图像转换为pdf显示黑色矩形

时间:2019-04-25 12:27:10

标签: c# asp.net itext

我正在使用以下代码将多个图像合并为一个pdf。

https://www.ryadel.com/en/merge-multiple-gif-png-jpg-pdf-image-files-single-pdf-file-asp-net-c-sharp-core-itextsharp/

此代码对除高分辨率图像以外的所有图像均适用。

if (image.Height > iTextSharp.text.PageSize.A4.Height - 25)
{
    image.ScaleToFit(iTextSharp.text.PageSize.A4.Width - 25, iTextSharp.text.PageSize.A4.Height - 25);
}
else if (image.Width > iTextSharp.text.PageSize.A4.Width - 25)
{
    image.ScaleToFit(iTextSharp.text.PageSize.A4.Width - 25, iTextSharp.text.PageSize.A4.Height - 25);
}
else
{
    image.ScaleToFit(doc.PageSize.Width - 10, doc.PageSize.Height - 10);
}

输出:对于高分辨率图像,它显示黑框 enter image description here

注意:当我在Google chrome中打开此文件时,它会显示实际图像。

当我尝试在Adobe Reader X中打开pdf时,显示以下错误。 enter image description here

对于缩放和背景色问题,我尝试使用以下代码,但仍然没有运气。

Image jpg = Image.GetInstance(imagepath + "/Sunset.jpg");
jpg.ScaleToFit(250f, 250f);
jpg.Border = Rectangle.BOX;
jpg.BorderColor = Color.YELLOW;
jpg.BorderWidth = 5f;
doc.Add(jpg);

0 个答案:

没有答案