使用itextsharp将图像html旁边的文本放到pdf中

时间:2012-02-03 14:06:29

标签: html image itext xmlworker

我正在使用itextsharp将html转换为pdf。我必须在图像旁边放置文字而不是图像下方。在html中,我能够在图像旁边放置文本,但在pdf中,文本行在图像

之后开始

请帮忙。

2 个答案:

答案 0 :(得分:10)

由于您提到HTML,您了解内联显示,对吧?通过类比,iTextSharp的默认Image显示是。要内联 Image个对象,您需要:

  1. 将图像添加到Chunk个对象
  2. Phrase个对象
  3. 中添加文字
  4. 然后将这些对象添加到Paragraph对象
  5. 这样的事情:

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

    将上面的imagePath替换为图片的物理路径

答案 1 :(得分:2)

您可以尝试以下代码示例。

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

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

通过Mikesdotnetting