Op 7/05/2011 2:03, Nathan Maves schreef:
I am having issues trying to add an image to a paragraph and have the text in that paragraph wrap around it.I am trying to achieve this https://skitch.com/nmaves/r6dj3/15415-adults-9781611200133-sell.pdf-1-page.
This can only be done with ColumnText. See example in attachment.
import java.io.FileOutputStream; import java.io.IOException; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Element; import com.itextpdf.text.Image; import com.itextpdf.text.Phrase; import com.itextpdf.text.pdf.ColumnText; import com.itextpdf.text.pdf.PdfTemplate; import com.itextpdf.text.pdf.PdfWriter; public class ImageAndText { public static final String KILLINGJPG = "thekilling.jpg"; public static final String KILLINGTXT = "The killing by Stanley Kubrick"; public static final String PATHSOFGLORYJPG = "pathsofglory.jpg"; public static final String PATHSOFGLORYTXT = "Paths of Glory by Stanley Kubrick Paths of Glory by Stanley Kubrick " + "Paths of Glory by Stanley Kubrick Paths of Glory by Stanley Kubrick Paths of Glory by Stanley Kubrick " + "Paths of Glory by Stanley Kubrick Paths of Glory by Stanley Kubrick Paths of Glory by Stanley Kubrick " + "Paths of Glory by Stanley Kubrick Paths of Glory by Stanley Kubrick Paths of Glory by Stanley Kubrick " + "Paths of Glory by Stanley Kubrick Paths of Glory by Stanley Kubrick Paths of Glory by Stanley Kubrick " + "Paths of Glory by Stanley Kubrick Paths of Glory by Stanley Kubrick Paths of Glory by Stanley Kubrick " + "Paths of Glory by Stanley Kubrick Paths of Glory by Stanley Kubrick Paths of Glory by Stanley Kubrick " + "Paths of Glory by Stanley Kubrick Paths of Glory by Stanley Kubrick Paths of Glory by Stanley Kubrick " + "Paths of Glory by Stanley Kubrick Paths of Glory by Stanley Kubrick Paths of Glory by Stanley Kubrick " + "Paths of Glory by Stanley Kubrick Paths of Glory by Stanley Kubrick Paths of Glory by Stanley Kubrick " + "Paths of Glory by Stanley Kubrick Paths of Glory by Stanley Kubrick Paths of Glory by Stanley Kubrick " + "Paths of Glory by Stanley Kubrick Paths of Glory by Stanley Kubrick Paths of Glory by Stanley Kubrick."; public static void main(String[] args) throws IOException, DocumentException { Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("image_text.pdf")); document.open(); document.add(getImageAndText(document, writer, PATHSOFGLORYJPG, PATHSOFGLORYTXT)); document.add(getImageAndText(document, writer, KILLINGJPG, KILLINGTXT)); document.close(); } public static Image getImageAndText(Document document, PdfWriter writer, String jpg, String txt) throws DocumentException, IOException { Image img = Image.getInstance(jpg); Phrase p = new Phrase(txt); float width = document.right() - document.left(); float height = 10000; float gutter = 3; PdfTemplate canvas = writer.getDirectContent().createTemplate(width, height); float x1 = img.getScaledWidth() + gutter; float y = height - img.getScaledHeight() - gutter; float[] LEFT = new float[]{ x1,height, x1,y, 0,y, 0,0 }; float[] RIGHT = new float[]{ width,height, width,0 }; ColumnText ct = new ColumnText(canvas); ct.setColumns(LEFT, RIGHT); ct.addText(p); ct.go(true); height = Math.max(10000 - ct.getYLine(), img.getScaledHeight()); y = height - img.getScaledHeight() - gutter; canvas = writer.getDirectContent().createTemplate(width, height); img.setAbsolutePosition(0, y); canvas.addImage(img); LEFT = new float[]{ x1,height, x1,y, 0,y, 0,0 }; RIGHT = new float[]{ width,height, width,0 }; ct = new ColumnText(canvas); ct.setAlignment(Element.ALIGN_JUSTIFIED); ct.setColumns(LEFT, RIGHT); ct.addText(p); ct.go(); return Image.getInstance(canvas); } }
image_text.pdf
Description: Adobe PDF document
------------------------------------------------------------------------------ WhatsUp Gold - Download Free Network Management Software The most intuitive, comprehensive, and cost-effective network management toolset available today. Delivers lowest initial acquisition cost and overall TCO of any competing solution. http://p.sf.net/sfu/whatsupgold-sd
_______________________________________________ iText-questions mailing list iText-questions@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/itext-questions iText(R) is a registered trademark of 1T3XT BVBA. Many questions posted to this list can (and will) be answered with a reference to the iText book: http://www.itextpdf.com/book/ Please check the keywords list before you ask for examples: http://itextpdf.com/themes/keywords.php