86756

Mixing text and images causes incorrect vertical positioning

Question:

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.

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.

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

Answer1:

From <a href="http://api.itextpdf.com/itext/com/itextpdf/text/Section.html" rel="nofollow">the documentation for the Java version</a>:

<blockquote>

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

</blockquote>

Further looking at the <a href="http://sourceforge.net/p/itextsharp/code/HEAD/tree/trunk/src/core/iTextSharp/text/Section.cs#l277" rel="nofollow">Add()</a> method in the C# source we see:

<blockquote>

Adds a Paragraph, List, Table or another Section

</blockquote>

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

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

Use this:

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

Or even just this:

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

Recommend

  • VB6: get onreadystate value from browser control
  • how to assign a sequence of computations to a sequence of variables in r
  • Change direction of written text for Hebrew letters iText
  • GetBaseFont() equal null in pdfbox
  • Stackoverflow error in OnstartPage method
  • android filter custom array adapter and bring back old items again
  • List of ignorable characters for string comparison
  • StringSplitOptions.RemoveEmptyEntries equivalent for TextFieldParser
  • ANTLR generating empty conditions
  • Getting random data from a MySQL database but not repeating data
  • itext font UnsupportedCharsetException
  • how to check the textarea content is blank using javascript?
  • changes in jquery 1.4.2 breaking the code?
  • getelement by class name for clicking
  • when does setTimeout start executing in a inline
  • pythonanywhere - How do I use websockets to transmit messages as per the web2py messaging example?
  • how to ignore xml namespaces?
  • Is looping through all style sheets and classes a good idea in JavaScript?
  • Opaque reference instead of PImpl. Is it possible?
  • Making Google Visualization - Annotation Chart to work in GWT
  • Cannot get text from text area
  • Getting error 'Cannot read property 'document' of undefined' while importing exp
  • How can I restyle a word when rendering a pdf with pdf.js?
  • Uncaught TypeError: $(…).select2 is not a function
  • Remove changes from one element when event occurs on another element?
  • Button click event not firing in jQuery
  • Chart.js Multiple dataset
  • How do I get HTML corresponding to current DOM tree?
  • JQuery Internet Explorer and ajaxstop
  • JSON response opens as a file, but I can't access it with JavaScript
  • How to apply VCL Styles to DLL-based forms in Inno Setup?
  • Change an a tag attribute in JavaScript based on screen width
  • Web-crawler for facebook in python
  • Unanticipated behavior
  • Linker errors when using intrinsic function via function pointer
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • trying to dynamically update Highchart column chart but series undefined
  • LevelDB C iterator
  • How can i traverse a binary tree from right to left in java?
  • java string with new operator and a literal