无法在包含图像的iTextSharp文档中添加边距

时间:2016-08-22 21:20:08

标签: pdf itext

要求: 需要拆分大图像(动态)并以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()在循环中设置边距 - 但这不起作用。

1 个答案:

答案 0 :(得分:0)

你正在混合不同的东西。

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

但是,您不允许iText创建布局。您创建名为PdfContentByte的{​​{1}},然后决定使用转换矩阵将图像添加到该画布。这意味着您将计算{{1}所需的canvasabcde值方法。

你应该做那个数学。如果您想查看保证金,则值fAddImage()w00h是错误的,你不应该责怪iTextSharp,你应该归咎于你缺乏对分析几何学的洞察力(这是你在16岁时在高中学到的东西)。

这对您来说可能更容易:

0

请注意-usedHeights也可以让您控制,无论边距如何,作为替代方案,您可以使用:

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();