c# - 使用 itextsharp 创建的 pdf 中的错误尺寸图像

标签 c# itext

我在使用 itextsharp 从 .tiff 中的图像创建 pdf 时遇到问题。 这是一些代码:

        iTextSharp.text.Document d = new iTextSharp.text.Document();
        PdfWriter pw = PdfWriter.GetInstance(d, new FileStream(filename, FileMode.Create));
        d.Open();

        PdfContentByte cb = pw.DirectContent;
        foreach (Image img in imgs)
        {
            d.NewPage();
            d.SetPageSize(new iTextSharp.text.Rectangle(0, 0, img.Width, img.Height));
            iTextSharp.text.Image timg = iTextSharp.text.Image.GetInstance(img, iTextSharp.text.BaseColor.WHITE);
            timg.SetAbsolutePosition(0, 0);
            cb.AddImage(timg);
            cb.Stroke();
        }
        d.Close();

它创建了两页的 pdf,但第一页上的图像太大了。
该页面具有图像的大小,但它缩放了图像的左下角。 它只对 tiff 图像执行此操作,如果我使用 png,它工作正常。

有什么解决办法吗?

最佳答案

感谢mkl的评论,我找到了。 在新页面命令(NewPage)之前设置页面大小(SetPageSize)

关于c# - 使用 itextsharp 创建的 pdf 中的错误尺寸图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13046711/

相关文章:

c# - 如何在 memcached 中存储 protobuf 对象?

c# - 异步等待 Task<T> 超时完成

c# - 在最近的工具更新失败并出现依赖错误后添加 Controller

java - 使用 iText 创建 10000 多页 PDF

c# - 使用 itextsharp 突出显示 PDF 文件时出现数字超出范围错误

c# - 如何在 iTextSharp\iText 中的两个元素之间添加空格\边距?

Java:使用 apache POI 如何将 ms word 文件转换为 pdf?

c# - 为什么在ToLookup之前多放一个ToArray会更快?

javascript - 过滤、排序和分页方法的最快方法

android - 如何更改 itext 中线条的颜色?