place text next to image html to pdf using itextsharp

北慕城南 提交于 2019-12-18 07:03:40

问题


I am converting html to pdf using itextsharp. I have to place text next to the image not below the image. In html I am able to place text next to image but in pdf the text line starts after image

Please help.


回答1:


Since you mention HTML, you understand block and inline display, right? By analogy, iTextSharp's default Image display is block. To inline Image objects you need to:

  1. Add images to Chunk object(s)
  2. Add text in Phrase object(s)
  3. Then add those object to a Paragraph object

Something like this:

Image image = Image.GetInstance(imagePath);  
Paragraph p = new Paragraph();
p.Add(new Phrase("Text next to the image "));
p.Add(new Chunk(image, 0, 0));
p.Add(new Phrase(" and text after the image.")); 
document.Add(p);

Replace imagePath above with the physical path to your image




回答2:


You can try these following code sample.

Image jpg = Image.GetInstance(imagepath + "/Sunset.jpg");
Paragraph paragraph = new Paragraph(@"Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Suspendisse blandit blandit turpis. Nam in lectus ut dolor consectetuer bibendum. Morbi neque ipsum, laoreet id; dignissim et, viverra id, mauris. Nulla mauris elit, consectetuer sit amet, accumsan eget, congue ac, libero. Vivamus suscipit. Nunc dignissim consectetuer lectus. Fusce elit nisi; commodo non, facilisis quis, hendrerit eu, dolor? Suspendisse eleifend nisi ut magna. Phasellus id lectus! Vivamus laoreet enim et dolor. Integer arcu mauris, ultricies vel, porta quis, venenatis at, libero. Donec nibh est, adipiscing et, ullamcorper vitae, placerat at, diam. Integer ac turpis vel ligula rutrum auctor! Morbi egestas erat sit amet diam. Ut ut ipsum? Aliquam non sem. Nulla risus eros, mollis quis, blandit ut; luctus eget, urna. Vestibulum vestibulum dapibus erat. Proin egestas leo a metus?");
paragraph.Alignment = Element.ALIGN_JUSTIFIED;
jpg.ScaleToFit(250f, 250f);
jpg.Alignment = Image.TEXTWRAP | Image.ALIGN_RIGHT;
jpg.IndentationLeft = 9f;
jpg.SpacingAfter = 9f;
jpg.BorderWidthTop = 36f;
jpg.BorderColorTop = Color.WHITE;
doc.Add(jpg);
doc.Add(paragraph);

source : http://www.mikesdotnetting.com/Article/87/iTextSharp-Working-with-images

By Mikesdotnetting



来源:https://stackoverflow.com/questions/9129866/place-text-next-to-image-html-to-pdf-using-itextsharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!