混合文本和图像会导致不正确的垂直定位
Mixing text and images causes incorrect vertical positioning
使用 iTextSharp 版本 5.5.8(5.5.7 中存在相同的错误),当您将图像添加到章节和部分时会出现一个令人不快的错误 - 图像和部分标题开始时正常但很快就会相对于每个部分发生偏移其他.
从以下代码生成的 PDF 开始时正确,上面写着 "Section 1",下面是图像。下一部分 ("Section 2") 有一些图像与部分文本重叠,下一部分更糟,等等。我认为是文本 mal-positioned,而不是图像。
这是一个已知的 iTextSharp 错误吗?
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();
}
来自 the documentation for the Java version:
A Section
is a part of a Document
containing other Section
s, Paragraph
s, List
and/or Table
s.
进一步查看C#源代码中的Add()
方法我们看到:
Adds a Paragraph, List, Table or another Section
基本上,使用 Paragraph
而不是 Chunk
。所以而不是这个
section.Add(new Chunk(" ", m_font));
使用这个:
section.Add(new Paragraph(new Chunk(" ", m_font)));
甚至只是这样:
section.Add(new Paragraph(" ", m_font));
使用 iTextSharp 版本 5.5.8(5.5.7 中存在相同的错误),当您将图像添加到章节和部分时会出现一个令人不快的错误 - 图像和部分标题开始时正常但很快就会相对于每个部分发生偏移其他.
从以下代码生成的 PDF 开始时正确,上面写着 "Section 1",下面是图像。下一部分 ("Section 2") 有一些图像与部分文本重叠,下一部分更糟,等等。我认为是文本 mal-positioned,而不是图像。
这是一个已知的 iTextSharp 错误吗?
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();
}
来自 the documentation for the Java version:
A
Section
is a part of aDocument
containing otherSection
s,Paragraph
s,List
and/orTable
s.
进一步查看C#源代码中的Add()
方法我们看到:
Adds a Paragraph, List, Table or another Section
基本上,使用 Paragraph
而不是 Chunk
。所以而不是这个
section.Add(new Chunk(" ", m_font));
使用这个:
section.Add(new Paragraph(new Chunk(" ", m_font)));
甚至只是这样:
section.Add(new Paragraph(" ", m_font));