问题描述
使用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();
}
推荐答案
来自:
进一步查看方法:
Further looking at the Add()
method in the C# source we see:
基本上,不是块
使用段落
。所以代替这个
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));
这篇关于混合文本和图像会导致垂直定位不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!