我正在使用 itextsharp 将 html 转换为 pdf。我必须将文本放在图像旁边而不是图像下方。在 html 中,我可以在图像旁边放置文本,但在 pdf 中,文本行在图像之后开始
请帮忙。
既然你提到了 HTML,你就理解块显示和内联显示,对吧?以此类推,iTextSharp 的默认Image
显示是block。要内联 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
上面替换为图像的物理路径
您可以尝试以下代码示例。
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