Example usage for org.apache.poi.xwpf.usermodel XWPFRelation HYPERLINK

List of usage examples for org.apache.poi.xwpf.usermodel XWPFRelation HYPERLINK

Introduction

In this page you can find the example usage for org.apache.poi.xwpf.usermodel XWPFRelation HYPERLINK.

Prototype

XWPFRelation HYPERLINK

To view the source code for org.apache.poi.xwpf.usermodel XWPFRelation HYPERLINK.

Click Source Link

Usage

From source file:com.min.word.core.MakeWordFileTest.java

License:Apache License

public static void main(String[] args) throws Exception {
    String fileName = "test.docx";
    System.out.println("---------- Word Create Start ------------");
    //  word ? ?/* www  . ja  va2  s.c  o m*/
    XWPFDocument document = new XWPFDocument();
    FileOutputStream out = new FileOutputStream(new File(fileName));
    System.out.println("---------- Create Blank Success ------------");

    //Paragraph ?
    XWPFParagraph paragraph = document.createParagraph();
    System.out.println("---------- Create Paragraph Success ------------");

    //border ?
    paragraph.setBorderBottom(Borders.BASIC_BLACK_DASHES);
    paragraph.setBorderLeft(Borders.BASIC_BLACK_DASHES);
    paragraph.setBorderRight(Borders.BASIC_BLACK_DASHES);
    paragraph.setBorderTop(Borders.BASIC_BLACK_DASHES);
    System.out.println("---------- Create Border Success ------------");

    XWPFRun run = paragraph.createRun();
    run.setText("At tutorialspoint.com, we strive hard to " + "provide quality tutorials for self-learning "
            + "purpose in the domains of Academics, Information "
            + "Technology, Management and Computer Programming Languages.");
    System.out.println("---------- Text Write to File ------------");

    //Table ?
    XWPFTable table = document.createTable();
    //row
    XWPFTableRow rowOne = table.getRow(0);
    rowOne.getCell(0).setText("Col One, Row One");
    rowOne.addNewTableCell().setText("Col Tow, Row One");
    rowOne.addNewTableCell().setText("Col Three, Row One");
    //row
    XWPFTableRow rowTow = table.createRow();
    rowTow.getCell(0).setText("Col One, Row Tow");
    rowTow.getCell(1).setText("Col Tow, Row Tow");
    rowTow.getCell(2).setText("Col Three, Row Tow");
    //row
    XWPFTableRow rowThree = table.createRow();
    rowThree.getCell(0).setText("Col One, Row Three");
    rowThree.getCell(1).setText("Col Tow, Row Three");
    rowThree.getCell(2).setText("Col Three, Row Three");
    System.out.println("---------- Create Table Success ------------");

    //Add Image
    XWPFParagraph imageParagraph = document.createParagraph();
    XWPFRun imageRun = imageParagraph.createRun();
    imageRun.addPicture(new FileInputStream("test.png"), XWPFDocument.PICTURE_TYPE_PNG, "test.png",
            Units.toEMU(300), Units.toEMU(300));
    System.out.println("---------- Create Image Success ------------");

    //Hyperlink
    XWPFParagraph hyperlink = document.createParagraph();
    String id = hyperlink.getDocument().getPackagePart()
            .addExternalRelationship("http://niee.kr", XWPFRelation.HYPERLINK.getRelation()).getId();
    CTR ctr = CTR.Factory.newInstance();
    CTHyperlink ctHyperlink = hyperlink.getCTP().addNewHyperlink();
    ctHyperlink.setId(id);

    CTText ctText = CTText.Factory.newInstance();
    ctText.setStringValue("Hyper-Link TEST");
    ctr.setTArray(new CTText[] { ctText });

    // ???? ?
    CTColor color = CTColor.Factory.newInstance();
    color.setVal("0000FF");
    CTRPr ctrPr = ctr.addNewRPr();
    ctrPr.setColor(color);
    ctrPr.addNewU().setVal(STUnderline.SINGLE);

    // 
    CTFonts fonts = ctrPr.isSetRFonts() ? ctrPr.getRFonts() : ctrPr.addNewRFonts();
    fonts.setAscii("?? ");
    fonts.setEastAsia("?? ");
    fonts.setHAnsi("?? ");

    // ? 
    CTHpsMeasure sz = ctrPr.isSetSz() ? ctrPr.getSz() : ctrPr.addNewSz();
    sz.setVal(new BigInteger("24"));
    ctHyperlink.setRArray(new CTR[] { ctr });
    hyperlink.setAlignment(ParagraphAlignment.LEFT);
    hyperlink.setVerticalAlignment(TextAlignment.CENTER);
    System.out.println("---------- Create Hyperlink Success ------------");

    //Font style
    XWPFParagraph fontStyle = document.createParagraph();

    //set Bold an Italic
    XWPFRun boldAnItalic = fontStyle.createRun();
    boldAnItalic.setBold(true);
    boldAnItalic.setItalic(true);
    boldAnItalic.setText("Bold an Italic");
    boldAnItalic.addBreak();

    //set Text Position
    XWPFRun textPosition = fontStyle.createRun();
    textPosition.setText("Set Text Position");
    textPosition.setTextPosition(100);

    //Set Strike through and font Size and Subscript
    XWPFRun otherStyle = fontStyle.createRun();
    otherStyle.setStrike(true);
    otherStyle.setFontSize(20);
    otherStyle.setSubscript(VerticalAlign.SUBSCRIPT);
    otherStyle.setText(" Set Strike through and font Size and Subscript");
    System.out.println("---------- Set Font Style ------------");

    //Set Alignment Paragraph
    XWPFParagraph alignment = document.createParagraph();
    //Alignment to Right
    alignment.setAlignment(ParagraphAlignment.RIGHT);

    XWPFRun alignRight = alignment.createRun();
    alignRight.setText(
            "At tutorialspoint.com, we strive hard to " + "provide quality tutorials for self-learning "
                    + "purpose in the domains of Academics, Information "
                    + "Technology, Management and Computer Programming " + "Languages.");

    //Alignment to Center
    alignment = document.createParagraph();
    //Alignment to Right
    alignment.setAlignment(ParagraphAlignment.CENTER);
    XWPFRun alignCenter = alignment.createRun();
    alignCenter.setText("The endeavour started by Mohtashim, an AMU "
            + "alumni, who is the founder and the managing director "
            + "of Tutorials Point (I) Pvt. Ltd. He came up with the "
            + "website tutorialspoint.com in year 2006 with the help"
            + "of handpicked freelancers, with an array of tutorials"
            + " for computer programming languages. ");
    System.out.println("---------- Set Alignment ------------");

    //word ? 
    document.write(out);
    out.close();
    System.out.println("---------- Save File Name : " + fileName + " ------------");
    System.out.println("---------- Word Create End ------------");
}

From source file:org.apache.tika.parser.microsoft.ooxml.xwpf.BodyContentHandler.java

License:Apache License

@Override
public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
    if (uri.equals(MC_NS)) {
        if (localName.equals("AlternateContent")) {
            inAlternateContent = true;//www .j a  v a  2s.  c o  m
        } else if (localName.equals("Choice")) {
            inACChoice = true;
        } else if (localName.equals("Fallback")) {
            inACFallback = true;
        }
    }
    if (inACFallback) {
        return;
    }

    if (uri.equals(W_NS)) {
        if (localName.equals("p")) {
            handler.startElement("p");
        } else if (localName.equals("r")) {
            inR = true;
        } else if (localName.equals("t")) {
            inT = true;
        } else if (localName.equals("tab")) {
            handler.characters(TAB, 0, 1);
        } else if (localName.equals("tbl")) {
            handler.startElement("table");
        } else if (localName.equals("tc")) {
            handler.startElement("td");
        } else if (localName.equals("tr")) {
            handler.startElement("tr");
        } else if (localName.equals("rPr")) {
            inRPr = true;
        } else if (inR && inRPr && localName.equals("i")) {
            //rprs don't have to be inR; ignore those that aren't
            currFormat.italics = true;
        } else if (inR && inRPr && localName.equals("b")) {
            currFormat.bold = true;
        } else if (localName.equals("delText")) {
            inDelText = true;
        } else if (localName.equals("ins")) {
            editAuthor = atts.getValue(W_NS, "author");
            editDate = atts.getValue(W_NS, "date");
            editType = EditType.INSERT;
        } else if (localName.equals("del")) {
            editAuthor = atts.getValue(W_NS, "author");
            editDate = atts.getValue(W_NS, "date");
            editType = EditType.DELETE;
        } else if (localName.equals("hyperlink")) {
            String hyperlinkId = atts.getValue(OFFICE_DOC_RELATIONSHIP_NS, "id");
            if (hyperlinkId != null) {
                Relationship relationship = relationshipsManager.getRelationship(getName(), hyperlinkId);
                if (relationship != null
                        && XWPFRelation.HYPERLINK.getRelation().equals(relationship.getContentType())) {
                    hyperlink = relationship.getTarget();
                    handler.startElement("a", "href", hyperlink);
                    hasWrittenAHref = true;
                }
            }
        }
    }
}

From source file:org.apache.tika.parser.microsoft.ooxml.xwpf.XWPFEventBasedWordExtractor.java

License:Apache License

private Map<String, String> loadHyperlinkRelationships(PackagePart bodyPart) {
    Map<String, String> hyperlinks = new HashMap<>();
    try {/*from w ww  . j a  v  a  2s  .c  om*/
        PackageRelationshipCollection prc = bodyPart
                .getRelationshipsByType(XWPFRelation.HYPERLINK.getRelation());
        for (int i = 0; i < prc.size(); i++) {
            PackageRelationship pr = prc.getRelationship(i);
            if (pr == null) {
                continue;
            }
            String id = pr.getId();
            String url = (pr.getTargetURI() == null) ? null : pr.getTargetURI().toString();
            if (id != null && url != null) {
                hyperlinks.put(id, url);
            }
        }
    } catch (InvalidFormatException e) {
    }
    return hyperlinks;
}

From source file:org.cgiar.ccafs.marlo.utils.POISummary.java

License:Open Source License

public void textHyperlink(String url, String text, XWPFParagraph paragraph) {

    // Add the link as External relationship
    String id = paragraph.getDocument().getPackagePart()
            .addExternalRelationship(url, XWPFRelation.HYPERLINK.getRelation()).getId();

    // Append the link and bind it to the relationship
    CTHyperlink cLink = paragraph.getCTP().addNewHyperlink();
    cLink.setId(id);//from  w  ww .  j  a  v a 2s.c  o  m

    // // Create the linked text
    CTText ctText = CTText.Factory.newInstance();
    ctText.setStringValue(text);

    CTR ctr = CTR.Factory.newInstance();
    ctr.setTArray(new CTText[] { ctText });
    ctr.addNewRPr().addNewColor().setVal("0000FF");
    ctr.addNewRPr().addNewU().setVal(STUnderline.SINGLE);
    ctr.addNewRPr().addNewRFonts().setAscii(FONT_TYPE);
    // Insert the linked text into the link
    cLink.setRArray(new CTR[] { ctr });

}

From source file:org.obeonetwork.m2doc.generator.M2DocEvaluator.java

License:Open Source License

/**
 * Inserts the given {@link MHyperLink}.
 * //from  w  ww  .jav  a 2  s  .com
 * @param run
 *            the {@link XWPFRun}
 * @param hyperLink
 *            the {@link MHyperLink}
 */
private void insertMHyperLink(XWPFRun run, MHyperLink hyperLink) {
    final String id = currentGeneratedParagraph.getDocument().getPackagePart()
            .addExternalRelationship(hyperLink.getUrl(), XWPFRelation.HYPERLINK.getRelation()).getId();
    final CTHyperlink cLink = currentGeneratedParagraph.getCTP().addNewHyperlink();
    cLink.setId(id);
    CTText ctText = CTText.Factory.newInstance();
    ctText.setStringValue(hyperLink.getText());

    CTR ctr = CTR.Factory.newInstance();
    ctr.setRPr((CTRPr) run.getCTR().getRPr().copy());
    ctr.setTArray(new CTText[] { ctText });
    cLink.setRArray(new CTR[] { ctr });
}