1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.pdfbox.pdmodel.documentinterchange.markedcontent; 18 19 import java.util.ArrayList; 20 import java.util.List; 21 22 import org.apache.pdfbox.cos.COSDictionary; 23 import org.apache.pdfbox.cos.COSName; 24 import org.apache.pdfbox.pdmodel.graphics.xobject.PDXObject; 25 import org.apache.pdfbox.util.TextPosition; 26 27 /** 28 * A marked content. 29 * 30 * @author <a href="mailto:Johannes%20Koch%20%3Ckoch@apache.org%3E">Johannes Koch</a> 31 * @version $Revision: $ 32 */ 33 public class PDMarkedContent 34 { 35 36 private String tag; 37 private COSDictionary properties; 38 private List<Object> contents; 39 40 41 /** 42 * Creates a new marked content object. 43 * 44 * @param tag the tag 45 * @param properties the properties 46 */ 47 public PDMarkedContent(COSName tag, COSDictionary properties) 48 { 49 this.tag = tag == null ? null : tag.getName(); 50 this.properties = properties; 51 this.contents = new ArrayList<Object>(); 52 } 53 54 55 /** 56 * Gets the tag. 57 * 58 * @return the tag 59 */ 60 public String getTag() 61 { 62 return this.tag; 63 } 64 65 /** 66 * Gets the properties. 67 * 68 * @return the properties 69 */ 70 public COSDictionary getProperties() 71 { 72 return this.properties; 73 } 74 75 /** 76 * Gets the marked-content identifier. 77 * 78 * @return the marked-content identifier 79 */ 80 public int getMCID() 81 { 82 return this.getProperties() == null ? null : 83 this.getProperties().getInt(COSName.MCID); 84 } 85 86 /** 87 * Gets the language (Lang). 88 * 89 * @return the language 90 */ 91 public String getLanguage() 92 { 93 return this.getProperties() == null ? null : 94 this.getProperties().getNameAsString(COSName.LANG); 95 } 96 97 /** 98 * Gets the actual text (ActualText). 99 * 100 * @return the actual text 101 */ 102 public String getActualText() 103 { 104 return this.getProperties() == null ? null : 105 this.getProperties().getString(COSName.ACTUAL_TEXT); 106 } 107 108 /** 109 * Gets the alternate description (Alt). 110 * 111 * @return the alternate description 112 */ 113 public String getAlternateDescription() 114 { 115 return this.getProperties() == null ? null : 116 this.getProperties().getString(COSName.ALT); 117 } 118 119 /** 120 * Gets the expanded form (E). 121 * 122 * @return the expanded form 123 */ 124 public String getExpandedForm() 125 { 126 return this.getProperties() == null ? null : 127 this.getProperties().getString(COSName.E); 128 } 129 130 /** 131 * Gets the contents of the marked content sequence. Can be 132 * <ul> 133 * <li>{@link TextPosition},</li> 134 * <li>{@link PDMarkedContent}, or</li> 135 * <li>{@link PDXObject}.</li> 136 * </ul> 137 * 138 * @return the contents of the marked content sequence 139 */ 140 public List<Object> getContents() 141 { 142 return this.contents; 143 } 144 145 /** 146 * Adds a text position to the contents. 147 * 148 * @param text the text position 149 */ 150 public void addText(TextPosition text) 151 { 152 this.getContents().add(text); 153 } 154 155 /** 156 * Adds a marked content to the contents. 157 * 158 * @param markedContent the marked content 159 */ 160 public void addMarkedContent(PDMarkedContent markedContent) 161 { 162 this.getContents().add(markedContent); 163 } 164 165 /** 166 * Adds an XObject to the contents. 167 * 168 * @param xobject the XObject 169 */ 170 public void addXObject(PDXObject xobject) 171 { 172 this.getContents().add(xobject); 173 } 174 175 176 @Override 177 public String toString() 178 { 179 StringBuilder sb = new StringBuilder("tag=").append(this.tag) 180 .append(", properties=").append(this.properties); 181 sb.append(", contents=").append(this.contents); 182 return sb.toString(); 183 } 184 185 }