Search in sources :

Example 16 with Image

use of com.itextpdf.layout.element.Image in project i7js-highlevel by itext.

the class C04E02_DivExample2 method createPdf.

public void createPdf(String dest) throws IOException {
    PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
    Document document = new Document(pdf);
    List<List<String>> resultSet = CsvTo2DList.convert(SRC, "|");
    resultSet.remove(0);
    for (List<String> record : resultSet) {
        Div div = new Div().setKeepTogether(true).setBorderLeft(new SolidBorder(2)).setPaddingLeft(3).setMarginBottom(10);
        String url = String.format("http://www.imdb.com/title/tt%s", record.get(0));
        Link movie = new Link(record.get(2), PdfAction.createURI(url));
        div.add(new Paragraph(movie.setFontSize(14))).add(new Paragraph(String.format("Directed by %s (%s, %s)", record.get(3), record.get(4), record.get(1))));
        File file = new File(String.format("src/main/resources/img/%s.jpg", record.get(0)));
        if (file.exists()) {
            Image img = new Image(ImageDataFactory.create(file.getPath()));
            img.scaleToFit(10000, 120);
            div.add(img);
        }
        document.add(div);
    }
    document.close();
}
Also used : PdfWriter(com.itextpdf.kernel.pdf.PdfWriter) PdfDocument(com.itextpdf.kernel.pdf.PdfDocument) Document(com.itextpdf.layout.Document) Image(com.itextpdf.layout.element.Image) PdfDocument(com.itextpdf.kernel.pdf.PdfDocument) SolidBorder(com.itextpdf.layout.borders.SolidBorder) Paragraph(com.itextpdf.layout.element.Paragraph) Div(com.itextpdf.layout.element.Div) CsvTo2DList(com.itextpdf.highlevel.util.CsvTo2DList) List(java.util.List) File(java.io.File) Link(com.itextpdf.layout.element.Link)

Example 17 with Image

use of com.itextpdf.layout.element.Image in project i7js-highlevel by itext.

the class C07E04_ImageWatermark method createPdf.

public void createPdf(String dest) throws IOException {
    // Initialize PDF document
    PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
    Image img = new Image(ImageDataFactory.create(IMG));
    IEventHandler handler = new TransparentImage(img);
    pdf.addEventHandler(PdfDocumentEvent.START_PAGE, handler);
    // Initialize document
    Document document = new Document(pdf);
    PdfFont bold = PdfFontFactory.createFont(StandardFonts.HELVETICA_BOLD);
    document.setTextAlignment(TextAlignment.JUSTIFIED).setHyphenation(new HyphenationConfig("en", "uk", 3, 3));
    BufferedReader br = new BufferedReader(new FileReader(SRC));
    String name, line;
    Paragraph p;
    boolean title = true;
    int counter = 0;
    List<SimpleEntry<String, SimpleEntry<String, Integer>>> toc = new ArrayList<>();
    while ((line = br.readLine()) != null) {
        p = new Paragraph(line);
        p.setKeepTogether(true);
        if (title) {
            name = String.format("title%02d", counter++);
            SimpleEntry<String, Integer> titlePage = new SimpleEntry(line, pdf.getNumberOfPages());
            p.setFont(bold).setFontSize(12).setKeepWithNext(true).setDestination(name).setNextRenderer(new UpdatePageRenderer(p, titlePage));
            title = false;
            document.add(p);
            toc.add(new SimpleEntry(name, titlePage));
        } else {
            p.setFirstLineIndent(36);
            if (line.isEmpty()) {
                p.setMarginBottom(12);
                title = true;
            } else {
                p.setMarginBottom(0);
            }
            document.add(p);
        }
    }
    pdf.removeEventHandler(PdfDocumentEvent.START_PAGE, handler);
    document.add(new AreaBreak(AreaBreakType.NEXT_PAGE));
    p = new Paragraph().setFont(bold).add("Table of Contents").setDestination("toc");
    document.add(p);
    toc.remove(0);
    List<TabStop> tabstops = new ArrayList();
    tabstops.add(new TabStop(580, TabAlignment.RIGHT, new DottedLine()));
    for (SimpleEntry<String, SimpleEntry<String, Integer>> entry : toc) {
        SimpleEntry<String, Integer> text = entry.getValue();
        p = new Paragraph().addTabStops(tabstops).add(text.getKey()).add(new Tab()).add(String.valueOf(text.getValue())).setAction(PdfAction.createGoTo(entry.getKey()));
        document.add(p);
    }
    // Close document
    document.close();
}
Also used : ArrayList(java.util.ArrayList) HyphenationConfig(com.itextpdf.layout.hyphenation.HyphenationConfig) TabStop(com.itextpdf.layout.element.TabStop) AreaBreak(com.itextpdf.layout.element.AreaBreak) Image(com.itextpdf.layout.element.Image) Document(com.itextpdf.layout.Document) PdfDocument(com.itextpdf.kernel.pdf.PdfDocument) DottedLine(com.itextpdf.kernel.pdf.canvas.draw.DottedLine) PdfFont(com.itextpdf.kernel.font.PdfFont) FileReader(java.io.FileReader) PdfWriter(com.itextpdf.kernel.pdf.PdfWriter) SimpleEntry(java.util.AbstractMap.SimpleEntry) PdfDocument(com.itextpdf.kernel.pdf.PdfDocument) IEventHandler(com.itextpdf.kernel.events.IEventHandler) Paragraph(com.itextpdf.layout.element.Paragraph) Tab(com.itextpdf.layout.element.Tab) BufferedReader(java.io.BufferedReader)

Example 18 with Image

use of com.itextpdf.layout.element.Image in project i7js-highlevel by itext.

the class ImageProperties method createPdf.

public void createPdf(String dest) throws IOException {
    // Initialize PDF document
    PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
    Document document = new Document(pdf);
    Image img1 = new Image(ImageDataFactory.create(TEST1));
    img1.scaleToFit(100, 100).setDestination("Top");
    document.add(img1);
    Image img2 = new Image(ImageDataFactory.create(TEST2));
    img2.setHeight(300);
    document.add(img2);
    Image img3 = new Image(ImageDataFactory.create(TEST3));
    img3.scaleToFit(100, 100);
    img3.setBackgroundColor(ColorConstants.BLUE);
    document.add(img3);
    Image img4 = new Image(ImageDataFactory.create(TEST4));
    img4.scaleToFit(100, 100);
    img4.setBackgroundColor(ColorConstants.RED);
    document.add(img4);
    Image img5 = new Image(ImageDataFactory.create(TEST5));
    img5.scaleToFit(50, 50);
    Style style = new Style();
    style.setBorderRight(new SolidBorder(2));
    img5.addStyle(style);
    document.add(img5);
    Image img6 = new Image(ImageDataFactory.create(TEST6));
    PdfAction top = PdfAction.createGoTo("Top");
    img6.scaleToFit(100, 100).setAction(top);
    document.add(img6);
    document.close();
}
Also used : PdfAction(com.itextpdf.kernel.pdf.action.PdfAction) PdfWriter(com.itextpdf.kernel.pdf.PdfWriter) Style(com.itextpdf.layout.Style) PdfDocument(com.itextpdf.kernel.pdf.PdfDocument) Document(com.itextpdf.layout.Document) Image(com.itextpdf.layout.element.Image) PdfDocument(com.itextpdf.kernel.pdf.PdfDocument) SolidBorder(com.itextpdf.layout.borders.SolidBorder)

Example 19 with Image

use of com.itextpdf.layout.element.Image in project pdf-editor by Pdf-Creator.

the class PDFDocument method addRectangleWithFormulaItem.

public void addRectangleWithFormulaItem(FormulaItem formulaItem) throws IOException {
    PdfCanvas canvas = new PdfCanvas(currentPage);
    Rectangle rect = new Rectangle(formulaItem.getX(), formulaItem.getY(), formulaItem.getW(), formulaItem.getW());
    canvas.setStrokeColor(formulaItem.getRectangleStrokeColor()).setFillColor(formulaItem.getRectangleStrokeColor()).rectangle(rect).fill().stroke();
    // very strange method
    TeXFormula tf = new TeXFormula(formulaItem.getFormula());
    TeXIcon ti = tf.createTeXIcon(TeXConstants.STYLE_DISPLAY, formulaItem.getFontSize());
    BufferedImage bimg = new BufferedImage(ti.getIconWidth(), ti.getIconHeight(), BufferedImage.TYPE_4BYTE_ABGR);
    // -- painting the formula --
    Graphics2D g2d = bimg.createGraphics();
    g2d.setColor(Color.white);
    g2d.fillRect(0, 0, ti.getIconWidth(), ti.getIconHeight());
    JLabel jl = new JLabel();
    jl.setForeground(new Color(0, 0, 0));
    ti.paintIcon(jl, g2d, 0, 0);
    // --------------------------
    // -- adding it to pdf --
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    ImageIO.write(bimg, "png", byteArrayOutputStream);
    ImageData data = ImageDataFactory.create(byteArrayOutputStream.toByteArray());
    Image image = new Image(data);
    image.setFixedPosition(formulaItem.getX(), formulaItem.getY());
    image.scaleAbsolute(formulaItem.getW(), formulaItem.getH());
    new Canvas(canvas, rect).add(image);
}
Also used : ImageData(com.itextpdf.io.image.ImageData) TeXFormula(org.scilab.forge.jlatexmath.TeXFormula) Canvas(com.itextpdf.layout.Canvas) PdfCanvas(com.itextpdf.kernel.pdf.canvas.PdfCanvas) Rectangle(com.itextpdf.kernel.geom.Rectangle) PdfCanvas(com.itextpdf.kernel.pdf.canvas.PdfCanvas) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BufferedImage(java.awt.image.BufferedImage) Image(com.itextpdf.layout.element.Image) BufferedImage(java.awt.image.BufferedImage) TeXIcon(org.scilab.forge.jlatexmath.TeXIcon)

Example 20 with Image

use of com.itextpdf.layout.element.Image in project digilib by robcast.

the class PDFStreamWorker method addImage.

/**
 * adds an image to the document.
 *
 * @param doc
 * @param iji
 * @return
 * @throws InterruptedException
 * @throws ExecutionException
 * @throws IOException
 * @throws DocumentException
 */
public Document addImage(Document doc, ImageJobDescription iji) throws InterruptedException, ExecutionException, IOException {
    // create image worker
    ImageWorker job = new ImageWorker(dlConfig, iji);
    // submit
    Future<DocuImage> jobTicket = imageJobCenter.submit(job);
    // wait for result
    DocuImage img = jobTicket.get();
    Image pdfimg = new Image(ImageDataFactory.create(img.getAwtImage(), null));
    // fit the image to the page
    pdfimg.setAutoScale(true);
    // add to PDF
    doc.add(pdfimg);
    return doc;
}
Also used : ImageWorker(digilib.image.ImageWorker) DocuImage(digilib.image.DocuImage) Image(com.itextpdf.layout.element.Image) DocuImage(digilib.image.DocuImage)

Aggregations

Image (com.itextpdf.layout.element.Image)37 PdfDocument (com.itextpdf.kernel.pdf.PdfDocument)25 PdfWriter (com.itextpdf.kernel.pdf.PdfWriter)25 Document (com.itextpdf.layout.Document)25 Paragraph (com.itextpdf.layout.element.Paragraph)20 CsvTo2DList (com.itextpdf.highlevel.util.CsvTo2DList)6 File (java.io.File)6 List (java.util.List)6 ImageData (com.itextpdf.io.image.ImageData)5 SolidBorder (com.itextpdf.layout.borders.SolidBorder)4 AreaBreak (com.itextpdf.layout.element.AreaBreak)4 Cell (com.itextpdf.layout.element.Cell)4 Link (com.itextpdf.layout.element.Link)4 IOException (java.io.IOException)4 PdfFont (com.itextpdf.kernel.font.PdfFont)3 Rectangle (com.itextpdf.kernel.geom.Rectangle)3 PdfReader (com.itextpdf.kernel.pdf.PdfReader)3 ListItem (com.itextpdf.layout.element.ListItem)3 Barcode128 (com.itextpdf.barcodes.Barcode128)2 BarcodeQRCode (com.itextpdf.barcodes.BarcodeQRCode)2