You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PDFProfile.java 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. /* $Id$ */
  18. package org.apache.fop.pdf;
  19. import java.text.MessageFormat;
  20. /**
  21. * This class allows tracks the enabled PDF profiles (PDF/A and PDF/X) and provides methods to
  22. * the libarary and its users to enable the generation of PDFs conforming to the enabled PDF
  23. * profiles.
  24. * <p>
  25. * Some profile from PDF/X and PDF/A can be active simultaneously (example: PDF/A-1 and
  26. * PDF/X-3:2003).
  27. */
  28. public class PDFProfile {
  29. /**
  30. * Indicates the PDF/A mode currently active. Defaults to "no restrictions", i.e.
  31. * PDF/A not active.
  32. */
  33. protected PDFAMode pdfAMode = PDFAMode.DISABLED;
  34. /**
  35. * Indicates the PDF/X mode currently active. Defaults to "no restrictions", i.e.
  36. * PDF/X not active.
  37. */
  38. protected PDFXMode pdfXMode = PDFXMode.DISABLED;
  39. private PDFDocument doc;
  40. /**
  41. * Main constructor
  42. * @param doc the PDF document
  43. */
  44. public PDFProfile(PDFDocument doc) {
  45. this.doc = doc;
  46. }
  47. /**
  48. * Validates if the requested profile combination is compatible.
  49. */
  50. protected void validateProfileCombination() {
  51. if (pdfAMode != PDFAMode.DISABLED) {
  52. if (pdfAMode == PDFAMode.PDFA_1B) {
  53. if (pdfXMode != PDFXMode.DISABLED && pdfXMode != PDFXMode.PDFX_3_2003) {
  54. throw new PDFConformanceException(
  55. pdfAMode + " and " + pdfXMode + " are not compatible!");
  56. }
  57. }
  58. }
  59. }
  60. /** @return the PDFDocument this profile is attached to */
  61. public PDFDocument getDocument() {
  62. return this.doc;
  63. }
  64. /** @return the PDF/A mode */
  65. public PDFAMode getPDFAMode() {
  66. return this.pdfAMode;
  67. }
  68. /** @return true if any PDF/A mode is active */
  69. public boolean isPDFAActive() {
  70. return getPDFAMode() != PDFAMode.DISABLED;
  71. }
  72. /**
  73. * Sets the PDF/A mode
  74. * @param mode the PDF/A mode
  75. */
  76. public void setPDFAMode(PDFAMode mode) {
  77. if (mode == null) {
  78. mode = PDFAMode.DISABLED;
  79. }
  80. this.pdfAMode = mode;
  81. validateProfileCombination();
  82. }
  83. /** @return the PDF/X mode */
  84. public PDFXMode getPDFXMode() {
  85. return this.pdfXMode;
  86. }
  87. /** @return true if any PDF/X mode is active */
  88. public boolean isPDFXActive() {
  89. return getPDFXMode() != PDFXMode.DISABLED;
  90. }
  91. /**
  92. * Sets the PDF/X mode
  93. * @param mode the PDF/X mode
  94. */
  95. public void setPDFXMode(PDFXMode mode) {
  96. if (mode == null) {
  97. mode = PDFXMode.DISABLED;
  98. }
  99. this.pdfXMode = mode;
  100. validateProfileCombination();
  101. }
  102. /** {@inheritDoc} */
  103. public String toString() {
  104. StringBuffer sb = new StringBuffer();
  105. if (isPDFAActive() && isPDFXActive()) {
  106. sb.append("[").append(getPDFAMode()).append(",").append(getPDFXMode()).append("]");
  107. } else if (isPDFAActive()) {
  108. sb.append(getPDFAMode());
  109. } else if (isPDFXActive()) {
  110. sb.append(getPDFXMode());
  111. } else {
  112. sb.append(super.toString());
  113. }
  114. return sb.toString();
  115. }
  116. //---------=== Info and validation methods ===---------
  117. private String format(String pattern, Object[] args) {
  118. return MessageFormat.format(pattern, args);
  119. }
  120. private String format(String pattern, Object arg) {
  121. return format(pattern, new Object[] {arg});
  122. }
  123. /** Checks if encryption is allowed. */
  124. public void verifyEncryptionAllowed() {
  125. final String err = "{0} doesn't allow encrypted PDFs";
  126. if (isPDFAActive()) {
  127. throw new PDFConformanceException(format(err, getPDFAMode()));
  128. }
  129. if (isPDFXActive()) {
  130. throw new PDFConformanceException(format(err, getPDFXMode()));
  131. }
  132. }
  133. /** Checks if PostScript XObjects are allowed. */
  134. public void verifyPSXObjectsAllowed() {
  135. final String err = "PostScript XObjects are prohibited when {0}"
  136. + " is active. Convert EPS graphics to another format.";
  137. if (isPDFAActive()) {
  138. throw new PDFConformanceException(format(err, getPDFAMode()));
  139. }
  140. if (isPDFXActive()) {
  141. throw new PDFConformanceException(format(err, getPDFXMode()));
  142. }
  143. }
  144. /**
  145. * Checks if the use of transparency is allowed.
  146. * @param context Context information for the user to identify the problem spot
  147. */
  148. public void verifyTransparencyAllowed(String context) {
  149. final String err = "{0} does not allow the use of transparency. ({1})";
  150. if (isPDFAActive()) {
  151. throw new PDFConformanceException(MessageFormat.format(err,
  152. new Object[] {getPDFAMode(), context}));
  153. }
  154. if (isPDFXActive()) {
  155. throw new PDFConformanceException(MessageFormat.format(err,
  156. new Object[] {getPDFXMode(), context}));
  157. }
  158. }
  159. /** Checks if the right PDF version is set. */
  160. public void verifyPDFVersion() {
  161. final String err = "PDF version must be 1.4 for {0}";
  162. if (getPDFAMode().isPDFA1LevelB()
  163. && !Version.V1_4.equals(getDocument().getPDFVersion())) {
  164. throw new PDFConformanceException(format(err, getPDFAMode()));
  165. }
  166. if (getPDFXMode() == PDFXMode.PDFX_3_2003
  167. && !Version.V1_4.equals(getDocument().getPDFVersion())) {
  168. throw new PDFConformanceException(format(err, getPDFXMode()));
  169. }
  170. }
  171. /**
  172. * Checks a few things required for tagged PDF.
  173. */
  174. public void verifyTaggedPDF() {
  175. if (getPDFAMode().isPDFA1LevelA()) {
  176. final String err = "{0} requires the {1} dictionary entry to be set";
  177. PDFDictionary markInfo = getDocument().getRoot().getMarkInfo();
  178. if (markInfo == null) {
  179. throw new PDFConformanceException(format(
  180. "{0} requires that the accessibility option in the configuration file be enabled",
  181. getPDFAMode()));
  182. }
  183. if (!Boolean.TRUE.equals(markInfo.get("Marked"))) {
  184. throw new PDFConformanceException(format(err,
  185. new Object[] {getPDFAMode(), "Marked"}));
  186. }
  187. if (getDocument().getRoot().getStructTreeRoot() == null) {
  188. throw new PDFConformanceException(format(err,
  189. new Object[] {getPDFAMode(), "StructTreeRoot"}));
  190. }
  191. if (getDocument().getRoot().getLanguage() == null) {
  192. throw new PDFConformanceException(format(err,
  193. new Object[] {getPDFAMode(), "Lang"}));
  194. }
  195. }
  196. }
  197. /** @return true if the ID entry must be present in the trailer. */
  198. public boolean isIDEntryRequired() {
  199. return isPDFAActive() || isPDFXActive();
  200. }
  201. /** @return true if all fonts need to be embedded. */
  202. public boolean isFontEmbeddingRequired() {
  203. return isPDFAActive() || isPDFXActive();
  204. }
  205. /** Checks if a title may be absent. */
  206. public void verifyTitleAbsent() {
  207. if (isPDFXActive()) {
  208. final String err = "{0} requires the title to be set.";
  209. throw new PDFConformanceException(format(err, getPDFXMode()));
  210. }
  211. }
  212. /** @return true if the ModDate Info entry must be present. */
  213. public boolean isModDateRequired() {
  214. return getPDFXMode() == PDFXMode.PDFX_3_2003;
  215. }
  216. /** @return true if the Trapped Info entry must be present. */
  217. public boolean isTrappedEntryRequired() {
  218. return getPDFXMode() == PDFXMode.PDFX_3_2003;
  219. }
  220. /** @return true if annotations are allowed */
  221. public boolean isAnnotationAllowed() {
  222. return !isPDFXActive();
  223. }
  224. /** Checks if annotations are allowed. */
  225. public void verifyAnnotAllowed() {
  226. if (!isAnnotationAllowed()) {
  227. final String err = "{0} does not allow annotations inside the printable area.";
  228. //Note: this rule is simplified. Refer to the standard for details.
  229. throw new PDFConformanceException(format(err, getPDFXMode()));
  230. }
  231. }
  232. /** Checks if Actions are allowed. */
  233. public void verifyActionAllowed() {
  234. if (isPDFXActive()) {
  235. final String err = "{0} does not allow Actions.";
  236. throw new PDFConformanceException(format(err, getPDFXMode()));
  237. }
  238. }
  239. /** Checks if embedded files are allowed. */
  240. public void verifyEmbeddedFilesAllowed() {
  241. final String err = "{0} does not allow embedded files.";
  242. if (isPDFAActive()) {
  243. throw new PDFConformanceException(format(err, getPDFAMode()));
  244. }
  245. if (isPDFXActive()) {
  246. //Implicit since file specs are forbidden
  247. throw new PDFConformanceException(format(err, getPDFXMode()));
  248. }
  249. }
  250. }