混合文本和图像会导致垂直定位不正确 [英] Mixing text and images causes incorrect vertical positioning

查看:127
本文介绍了混合文本和图像会导致垂直定位不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用iTextSharp版本5.5.8(5.5.7中存在同样的错误),当您向章节和章节添加图像时会出现一个令人不快的错误 - 图像和章节标题开始正常但很快就会相对于彼此偏移。

Using iTextSharp version 5.5.8 (same bug existed in 5.5.7), there's an unpleasant bug when you add images to Chapters and Sections - the images and the section headings start out OK but quickly become offset relative to each other.

从以下代码生成的PDF正确地开始,它显示第1部分,下面是图像。下一部分(第2部分)有一些图像与部分文本重叠,下一部分甚至更糟,等等。我认为这是错误定位的文本,而不是图像。

The PDF generated from the following code starts out correctly, it says "Section 1" and below it is the image. The next section ("Section 2") has a little of the image overlapping the section text, the next section is even worse, etc. I think it's the text that's mal-positioned, not the image.

这是一个已知的iTextSharp错误吗?

Is this a known iTextSharp bug?

    static Document m_doc = null;
    static BaseFont m_helvetica = null;
    static Font m_font = null;
    static PdfWriter m_writer = null;

    static Image m_image = null;

    static void Main(string[] args)
    {
        m_doc = new Document(PageSize.LETTER);

        m_helvetica = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
        m_font = new Font(m_helvetica, 10.0f);

        m_writer = PdfWriter.GetInstance(m_doc, new FileStream("Output.pdf", FileMode.Create));

        m_writer.StrictImageSequence = true;

        m_doc.Open();

        m_doc.Add(new Chunk("Created by iTextSharp version " + new iTextSharp.text.Version().GetVersion, m_font));

        Chapter chapter = new Chapter("Chapter 1", 1);
        chapter.TriggerNewPage = false;

        if (m_image == null)
        {
            m_image = Image.GetInstance(new Uri("https://pbs.twimg.com/profile_images/2002307628/Captura_de_pantalla_2012-03-17_a_la_s__22.14.48.png"));
            m_image.ScaleAbsolute(100, 100);
        }

        for (int i = 0; i < 5; i++)
        {
            Section section = chapter.AddSection(18, "Section " + (i + 1));
            section.Add(new Chunk("   ", m_font));
            section.Add(m_image);
        }

        m_doc.Add(chapter);

        m_doc.Close();
    }


推荐答案

来自 Java版本的文档


A 部分是包含其他<$ c的文档的一部分$ c>部分 s,段落 s,列表和/或 s。

A Section is a part of a Document containing other Sections, Paragraphs, List and/or Tables.

进一步查看 Add()我们在C#源代码中看到方法:

Further looking at the Add() method in the C# source we see:


添加段落,列表,表格或其他部分

Adds a Paragraph, List, Table or another Section

基本上,不是使用段落。所以代替这个

Basically, instead of a Chunk use a Paragraph. So instead of this

section.Add(new Chunk("   ", m_font));

使用此:

section.Add(new Paragraph(new Chunk("   ", m_font)));

或者就是这样:

section.Add(new Paragraph("   ", m_font));

这篇关于混合文本和图像会导致垂直定位不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆