Example usage for com.lowagie.text.pdf BaseFont createFont

List of usage examples for com.lowagie.text.pdf BaseFont createFont

Introduction

In this page you can find the example usage for com.lowagie.text.pdf BaseFont createFont.

Prototype

public static BaseFont createFont() throws DocumentException, IOException 

Source Link

Document

Creates a new font.

Usage

From source file:questions.images.CustomizedTitleBar.java

public static Image getTitleBar(PdfWriter writer, Image background, String title)
        throws DocumentException, IOException {
    float width = background.getWidth();
    float height = background.getHeight();
    PdfTemplate tmp = writer.getDirectContent().createTemplate(width, height);
    tmp.addImage(background, width, 0, 0, height, 0, 0);
    BaseFont font = BaseFont.createFont();
    tmp.beginText();/*ww w.  j ava  2  s . c o m*/
    tmp.setGrayFill(1);
    tmp.setFontAndSize(font, 18);
    tmp.showTextAligned(Element.ALIGN_CENTER, title, width / 2, 4, 0);
    tmp.endText();
    return Image.getInstance(tmp);
}

From source file:questions.importpages.ConcatenateWithTOC.java

public static void main(String[] args) {
    try {//from   w w  w. ja va  2 s .  c  om
        // suppose we have some TEST PDF files
        for (int i = 0; i < TEST.length; i++) {
            createTestPdf(i);
        }
        // and we want to concatenate them
        Document document = new Document();
        PdfCopy copy = new PdfCopy(document, new FileOutputStream(RESULT));
        copy.setViewerPreferences(PdfWriter.PageModeUseOutlines);
        document.open();
        // but we want to create an outline tree
        PdfOutline root = copy.getRootOutline();
        // we also want an extra page with the file name
        Document coverpage;
        ByteArrayOutputStream baos;
        PdfReader reader;
        // we want to add page numbers too
        int pagenumber = 0;
        BaseFont bf = BaseFont.createFont();
        for (int i = 0; i < TEST.length; i++) {
            // we create the coverpage in memory
            coverpage = new Document();
            baos = new ByteArrayOutputStream();
            PdfWriter.getInstance(coverpage, baos);
            coverpage.open();
            coverpage.add(new Paragraph("file: " + TEST[i]));
            coverpage.close();
            // we import that page
            reader = new PdfReader(baos.toByteArray());
            pagenumber++;
            copy.addPage(getStampedPage(reader, copy, 1, pagenumber, bf));
            // we create the bookmark to that page
            PdfDestination dest = new PdfDestination(PdfDestination.FIT);
            new PdfOutline(root, PdfAction.gotoLocalPage(pagenumber, dest, copy), TEST[i]);
            // we import the document itself
            reader = new PdfReader(TEST[i]);
            for (int j = 1; j <= reader.getNumberOfPages(); j++) {
                pagenumber++;
                copy.addPage(getStampedPage(reader, copy, j, pagenumber, bf));
            }
        }
        document.close();
    } catch (IOException ioe) {
        ioe.printStackTrace();
    } catch (DocumentException de) {
        de.printStackTrace();
    }
}

From source file:questions.importpages.ConcatenateWithTOC2.java

public static void main(String[] args) {
    try {/*from  ww w.  j  a  va 2 s  .c om*/
        // suppose we have some TEST PDF files
        for (int i = 0; i < TEST.length; i++) {
            createTestPdf(i);
        }
        // and we want to concatenate them
        Document document = new Document();
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        PdfCopy copy = new PdfCopy(document, os);
        copy.setViewerPreferences(PdfWriter.PageModeUseOutlines);
        document.open();
        // but we want to create an outline tree
        PdfOutline root = copy.getRootOutline();
        // we also want an extra page with the file name
        Document coverpage;
        ByteArrayOutputStream baos;
        PdfReader reader;
        // we want keep track of the page numbers
        int pagenumber = 0;
        for (int i = 0; i < TEST.length; i++) {
            // we create the coverpage in memory
            coverpage = new Document();
            baos = new ByteArrayOutputStream();
            PdfWriter.getInstance(coverpage, baos);
            coverpage.open();
            coverpage.add(new Paragraph("file: " + TEST[i]));
            coverpage.close();
            // we import that page
            reader = new PdfReader(baos.toByteArray());
            pagenumber++;
            copy.addPage(copy.getImportedPage(reader, 1));
            // we create the bookmark to that page
            PdfDestination dest = new PdfDestination(PdfDestination.FIT);
            new PdfOutline(root, PdfAction.gotoLocalPage(pagenumber, dest, copy), TEST[i]);
            // we import the document itself
            reader = new PdfReader(TEST[i]);
            for (int j = 1; j <= reader.getNumberOfPages(); j++) {
                pagenumber++;
                copy.addPage(copy.getImportedPage(reader, j));
            }
        }
        document.close();

        // we want to add page X of Y
        reader = new PdfReader(os.toByteArray());
        int n = reader.getNumberOfPages();
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT));
        BaseFont bf = BaseFont.createFont();
        for (int i = 1; i <= n; i++) {
            PdfContentByte canvas = stamper.getOverContent(i);
            canvas.beginText();
            canvas.setFontAndSize(bf, 12);
            canvas.showTextAligned(Element.ALIGN_LEFT, "page " + i + " of " + n, 36, 26, 0);
            canvas.endText();
        }
        stamper.close();
    } catch (IOException ioe) {
        ioe.printStackTrace();
    } catch (DocumentException de) {
        de.printStackTrace();
    }
}

From source file:questions.javascript.AddJavaScriptToForm.java

public static void createPdf(String filename) throws IOException, DocumentException {
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
    document.open();/*from  w  ww.  j a v a2  s  . co  m*/

    BaseFont bf = BaseFont.createFont();
    PdfContentByte directcontent = writer.getDirectContent();
    directcontent.beginText();
    directcontent.setFontAndSize(bf, 12);
    directcontent.showTextAligned(Element.ALIGN_LEFT, "Married?", 36, 770, 0);
    directcontent.showTextAligned(Element.ALIGN_LEFT, "YES", 58, 750, 0);
    directcontent.showTextAligned(Element.ALIGN_LEFT, "NO", 102, 750, 0);
    directcontent.showTextAligned(Element.ALIGN_LEFT, "Name partner?", 36, 730, 0);
    directcontent.endText();

    PdfFormField married = PdfFormField.createRadioButton(writer, true);
    married.setFieldName("married");
    married.setValueAsName("yes");
    Rectangle rectYes = new Rectangle(40, 766, 56, 744);
    RadioCheckField yes = new RadioCheckField(writer, rectYes, null, "yes");
    yes.setChecked(true);
    married.addKid(yes.getRadioField());
    Rectangle rectNo = new Rectangle(84, 766, 100, 744);
    RadioCheckField no = new RadioCheckField(writer, rectNo, null, "no");
    no.setChecked(false);
    married.addKid(no.getRadioField());
    writer.addAnnotation(married);

    Rectangle rect = new Rectangle(40, 710, 200, 726);
    TextField partner = new TextField(writer, rect, "partner");
    partner.setBorderColor(Color.BLACK);
    partner.setBorderWidth(0.5f);
    writer.addAnnotation(partner.getTextField());

    document.close();
}

From source file:questions.stamppages.PageXofYRightAligned.java

public static void main(String[] args) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {//from   w  w  w .j a  v  a2  s  .co  m
        Document document = new Document();
        PdfWriter.getInstance(document, baos);
        document.open();
        BufferedReader reader = new BufferedReader(new FileReader(RESOURCE));
        String line;
        Paragraph p;
        while ((line = reader.readLine()) != null) {
            p = new Paragraph(line);
            p.setAlignment(Element.ALIGN_JUSTIFIED);
            document.add(p);
            document.newPage();
        }
        reader.close();
        document.close();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (DocumentException e) {
        e.printStackTrace();
    }

    try {
        PdfReader reader = new PdfReader(baos.toByteArray());
        int n = reader.getNumberOfPages();
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT));
        PdfContentByte page;
        Rectangle rect;
        BaseFont bf = BaseFont.createFont();
        for (int i = 1; i < n + 1; i++) {
            page = stamper.getOverContent(i);
            rect = reader.getPageSizeWithRotation(i);
            page.beginText();
            page.setFontAndSize(bf, 12);
            page.showTextAligned(Element.ALIGN_RIGHT, "page " + i + " of " + n, rect.getRight(36),
                    rect.getTop(32), 0);
            page.endText();
        }
        stamper.close();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (DocumentException e) {
        e.printStackTrace();
    }

}

From source file:uk.org.rbc1b.roms.controller.volunteer.VolunteerBadgePdfView.java

License:Open Source License

/**
 * Adds the RBC Region's title (e.g. London and the Home Counties) to the
 * bottom of the badge./*w w  w  . j  a  va  2s. c  om*/
 *
 * @param content to be added
 * @param rbcRegion RBC region
 */
private static void addRBCRegionFooter(PdfContentByte content) throws DocumentException, IOException {
    content.beginText();
    content.moveText(183, 345);

    BaseFont bf = BaseFont.createFont();
    content.setFontAndSize(bf, 9);
    content.setColorFill(Color.DARK_GRAY);
    content.showText(VolunteerBadgePdfView.BADGE_RBC_REGION);
    content.endText();
}