Java Code Examples for org.docx4j.relationships.Relationship#setTarget()

The following examples show how to use org.docx4j.relationships.Relationship#setTarget() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UrlVisitor.java    From yarg with Apache License 2.0 6 votes vote down vote up
@Override
public List<Object> apply(Object o) {
    if (o instanceof P.Hyperlink) {
        P.Hyperlink hyperlink = (P.Hyperlink) o;
        try {
            Relationships contents = mainDocumentPart.getRelationshipsPart().getContents();
            List<Relationship> relationships = contents.getRelationship();
            for (Relationship relationship : relationships) {
                if (relationship.getId().equals(hyperlink.getId())) {
                    relationship.setTarget(docxFormatter.handleStringWithAliases(URLDecoder.decode(relationship.getTarget(), "UTF-8")));
                }
            }
        } catch (Exception e) {
            throw new RuntimeException("An error occurred while processing URL with aliases",e);
        }
    }
    return null;
}
 
Example 2
Source File: AbstractInliner.java    From yarg with Apache License 2.0 5 votes vote down vote up
private void putImage(WorksheetPart worksheetPart, SpreadsheetMLPackage pkg, BinaryPartAbstractImage imagePart, CTOneCellAnchor anchor) throws Docx4JException {
    PartName drawingPart = new PartName(StringUtils.replaceIgnoreCase(worksheetPart.getPartName().getName(),
            "worksheets/sheet", "drawings/drawing"));
    String imagePartName = imagePart.getPartName().getName();
    Part part = pkg.getParts().get(drawingPart);
    if (part != null && !(part instanceof Drawing))
        throw new ReportFormattingException("Wrong Class: not a Drawing");
    Drawing drawing = (Drawing) part;
    int currentId = 0;
    if (drawing == null) {
        drawing = new Drawing(drawingPart);
        drawing.setContents(new org.docx4j.dml.spreadsheetdrawing.CTDrawing());
        Relationship relationship = worksheetPart.addTargetPart(drawing);
        org.xlsx4j.sml.CTDrawing smlDrawing = new org.xlsx4j.sml.CTDrawing();
        smlDrawing.setId(relationship.getId());
        smlDrawing.setParent(worksheetPart.getContents());
        worksheetPart.getContents().setDrawing(smlDrawing);
    } else {
        currentId = drawing.getContents().getEGAnchor().size();
    }

    CTPicture picture = new CTPicture();

    CTBlipFillProperties blipFillProperties = new CTBlipFillProperties();
    blipFillProperties.setStretch(new CTStretchInfoProperties());
    blipFillProperties.getStretch().setFillRect(new CTRelativeRect());
    blipFillProperties.setBlip(new CTBlip());
    blipFillProperties.getBlip().setEmbed("rId" + (currentId + 1));
    blipFillProperties.getBlip().setCstate(STBlipCompression.PRINT);

    picture.setBlipFill(blipFillProperties);

    CTNonVisualDrawingProps nonVisualDrawingProps = new CTNonVisualDrawingProps();
    nonVisualDrawingProps.setId(currentId + 2);
    nonVisualDrawingProps.setName(imagePartName.substring(imagePartName.lastIndexOf("/") + 1));
    nonVisualDrawingProps.setDescr(nonVisualDrawingProps.getName());

    CTNonVisualPictureProperties nonVisualPictureProperties = new CTNonVisualPictureProperties();
    nonVisualPictureProperties.setPicLocks(new CTPictureLocking());
    nonVisualPictureProperties.getPicLocks().setNoChangeAspect(true);
    CTPictureNonVisual nonVisualPicture = new CTPictureNonVisual();

    nonVisualPicture.setCNvPr(nonVisualDrawingProps);
    nonVisualPicture.setCNvPicPr(nonVisualPictureProperties);

    picture.setNvPicPr(nonVisualPicture);

    CTShapeProperties shapeProperties = new CTShapeProperties();
    CTTransform2D transform2D = new CTTransform2D();
    transform2D.setOff(new CTPoint2D());
    transform2D.setExt(new CTPositiveSize2D());
    shapeProperties.setXfrm(transform2D);
    shapeProperties.setPrstGeom(new CTPresetGeometry2D());
    shapeProperties.getPrstGeom().setPrst(STShapeType.RECT);
    shapeProperties.getPrstGeom().setAvLst(new CTGeomGuideList());

    picture.setSpPr(shapeProperties);

    anchor.setPic(picture);
    anchor.setClientData(new CTAnchorClientData());

    drawing.getContents().getEGAnchor().add(anchor);

    Relationship rel = new Relationship();
    rel.setId("rId" + (currentId + 1));
    rel.setType(Namespaces.IMAGE);
    rel.setTarget(imagePartName);

    drawing.getRelationshipsPart().addRelationship(rel);
    RelationshipsPart relPart = drawing.getRelationshipsPart();
    pkg.getParts().remove(relPart.getPartName());
    pkg.getParts().put(relPart);
    pkg.getParts().remove(drawing.getPartName());
    pkg.getParts().put(drawing);
}