我正在使用itextsharp将html转换为pdf。我必须在图像旁边放置文字而不是图像下方。在html中,我能够在图像旁边放置文本,但在pdf中,文本行在图像
之后开始请帮忙。
答案 0 :(得分:10)
由于您提到HTML,您了解块和内联显示,对吧?通过类比,iTextSharp的默认Image
显示是块。要内联 Image
个对象,您需要:
Chunk
个对象Phrase
个对象Paragraph
对象这样的事情:
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