【问题标题】:Unable to add margins in iTextSharp document having images无法在具有图像的 iTextSharp 文档中添加边距
【发布时间】:2016-08-23 14:32:44
【问题描述】:

要求: 大图像(动态)需要拆分并显示在 PDF 页面中。如果图像不能容纳在一个页面中,那么我们需要添加另一个页面并尝试适应剩余部分等等。

到目前为止,我可以将图像拆分为多个页面,但是它们似乎完全忽略了边距值,因此显示的图像没有任何边距。

请看下面的代码:

string fileStringReplace = imageByteArray.Replace("data:image/jpeg;base64,", "");
        Byte[] imageByte = Convert.FromBase64String(fileStringReplace);
        iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(imageByte);
        float w = image.ScaledWidth;
        float h = image.ScaledHeight;
        float cropHeight = 1500f;

        iTextSharp.text.Rectangle page = new iTextSharp.text.Rectangle(1150f, cropHeight);
        var x = page.Height;
        Byte[] created;

        iTextSharp.text.Document document = new iTextSharp.text.Document(page, 20f, 20f, 20f, 40f); --This has no impact

        using (var outputMemoryStream = new MemoryStream())
        {
            PdfWriter writer = PdfWriter.GetInstance(document, outputMemoryStream);
            writer.CloseStream = false;

            document.Open();
            PdfContentByte canvas = writer.DirectContentUnder;

            float usedHeights = h;

            while (usedHeights >= 0)
            {
                usedHeights -= cropHeight;
                document.SetPageSize(new iTextSharp.text.Rectangle(1150f, cropHeight));
                canvas.AddImage(image, w, 0, 0, h, 0, -usedHeights);

                document.NewPage();
            }

            document.Close();

            created = outputMemoryStream.ToArray();

            outputMemoryStream.Write(created, 0, created.Length);
            outputMemoryStream.Position = 0;
        }
        return created;

我还尝试通过 document.SetMargins() 在循环中设置边距 - 但这不起作用。

【问题讨论】:

    标签: pdf itext


    【解决方案1】:

    你正在混合不同的东西。

    当您创建边距时,无论是在构造 Document 实例时还是使用 setMargins() 方法时,您都会为让 iText(Sharp) 决定布局时创建边距。也就是说:当您执行document.Add(image) 之类的操作时,将尊重边距。

    但是,您不允许 iText 创建布局。您创建了一个名为 canvasPdfContentByte,并决定使用转换矩阵将图像添加到该画布。这意味着您将计算 AddImage() 方法所需的 abcdef 值。

    你应该做那个数学。如果你想看到一个边距,那么值 w00h0-usedHeights 是错误的,你不应该责怪 iTextSharp,你应该责怪你的对解析几何缺乏洞察力(这是你 16 岁在高中时学到的东西)。

    这对你来说可能更容易:

    iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(imageByte);
    float w = image.ScaledWidth;
    float h = image.ScaledHeight;
    // For the sake of simplicity, I don't crop the image, I just add 20 user units
    iTextSharp.text.Rectangle page = new iTextSharp.text.Rectangle(w + 20, h + 20);
    iTextSharp.text.Document document = new iTextSharp.text.Document(page);
    PdfWriter writer = PdfWriter.GetInstance(document, outputMemoryStream);
    // Please drop the line that prevents closing the output stream!
    // Why are so many people making this mistake?
    // Who told you you shouldn't close the output stream???
    document.Open();
    // We define an absolute position for the image
    // it will leave a margin of 10 to the left and to the bottom
    // as we created a page that is 20 user units to wide and to high,
    // we will also have a margin of 10 to the right and to the top
    img.SetAbsolutePosition(10, 10);
    document.Add(Image);
    document.Close();
    

    请注意,SetAbsolutePosition() 还可以让您控制,而不管边距如何,作为替代方案,您可以使用:

    iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(imageByte);
    float w = image.ScaledWidth;
    float h = image.ScaledHeight;
    // For the sake of simplicity, I don't crop the image, I just add 20 user units
    iTextSharp.text.Rectangle page = new iTextSharp.text.Rectangle(w + 20, h + 20);
    iTextSharp.text.Document document = new iTextSharp.text.Document(page, 10, 10, 10, 10);
    PdfWriter writer = PdfWriter.GetInstance(document, outputMemoryStream);
    // Please drop the line that prevents closing the output stream!
    // Why are so many people making this mistake?
    // Who told you you shouldn't close the output stream???
    document.Open();
    // We add the image to the document, and we let iTextSharp decide where to put it
    // As there is just sufficient space to fit the image inside the page, it should fit,
    // But be aware of the existence of a leading; that could create side-effects
    // such as forwarding the image to the next page because it doesn't fit vertically
    document.Add(Image);
    document.Close();
    

    【讨论】:

    • 我们来看看第一个解决方案:有一行“iTextSharp.text.Rectangle page = new iTextSharp.text.Rectangle(w + 20, h + 20);” - 但是,我的图像(大部分)将是大尺寸的。如果我们添加 20,20,那么它就不会放在一页中。页面外的图像部分会发生什么?
    • 另外,您可以指导我在哪里可以找到 PDFContentByte.AddImage 方法的文档
    • 解决方案 1 不起作用。正如我在我的问题中提到的。我只是不想添加图像,它总是很大,所以我必须做一些工作来适应它。我正在这样做,但由于保证金被忽略了。我需要找到真正聪明的解决方案。
    • 图片尺寸为w,尺寸为h。如果您创建一个以w + 20 为单位h + 20 的矩形,则图像将始终适合该矩形。 AddImage() 方法所需的数学在我写的关于 iText 的不同书籍中进行了解释。其中一些是免费的。您声称解决方案 1 不起作用,但您可能没有尝试过,因为如果您使用 SetAbsolutePosition(10, 10)会有余量。我是 iText 的原始开发者,我认为你是一个试图通过说 它不起作用来激怒我的巨魔。如果你不是认真地寻找答案,请走开。跨度>
    • 我确实尝试过,但它可以将整个图像放在一页中。此外,我指的是developers.itextpdf.com/question/… 提供的您自己的解决方案 - 唯一的区别是我需要边距。现在很混乱。
    猜你喜欢
    • 2015-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-01
    • 2013-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多