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.

PDFFactory.java 70KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797
  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. // Java
  20. import java.awt.Color;
  21. import java.awt.geom.Point2D;
  22. import java.awt.geom.Rectangle2D;
  23. import java.io.IOException;
  24. import java.io.InputStream;
  25. import java.text.DecimalFormat;
  26. import java.util.ArrayList;
  27. import java.util.Arrays;
  28. import java.util.BitSet;
  29. import java.util.Iterator;
  30. import java.util.List;
  31. import java.util.Map;
  32. import org.apache.commons.io.IOUtils;
  33. import org.apache.commons.io.output.ByteArrayOutputStream;
  34. import org.apache.commons.logging.Log;
  35. import org.apache.commons.logging.LogFactory;
  36. import org.apache.xmlgraphics.java2d.color.ColorUtil;
  37. import org.apache.xmlgraphics.java2d.color.NamedColorSpace;
  38. import org.apache.xmlgraphics.xmp.Metadata;
  39. import org.apache.fop.fonts.CIDFont;
  40. import org.apache.fop.fonts.CodePointMapping;
  41. import org.apache.fop.fonts.CustomFont;
  42. import org.apache.fop.fonts.EmbeddingMode;
  43. import org.apache.fop.fonts.FontDescriptor;
  44. import org.apache.fop.fonts.FontMetrics;
  45. import org.apache.fop.fonts.FontType;
  46. import org.apache.fop.fonts.LazyFont;
  47. import org.apache.fop.fonts.MultiByteFont;
  48. import org.apache.fop.fonts.SimpleSingleByteEncoding;
  49. import org.apache.fop.fonts.SingleByteEncoding;
  50. import org.apache.fop.fonts.SingleByteFont;
  51. import org.apache.fop.fonts.Typeface;
  52. import org.apache.fop.fonts.truetype.FontFileReader;
  53. import org.apache.fop.fonts.truetype.TTFSubSetFile;
  54. import org.apache.fop.fonts.type1.PFBData;
  55. import org.apache.fop.fonts.type1.PFBParser;
  56. /**
  57. * This class provides method to create and register PDF objects.
  58. */
  59. public class PDFFactory {
  60. /** Resolution of the User Space coordinate system (72dpi). */
  61. public static final int DEFAULT_PDF_RESOLUTION = 72;
  62. private PDFDocument document;
  63. private Log log = LogFactory.getLog(PDFFactory.class);
  64. private int subsetFontCounter = -1;
  65. /**
  66. * Creates a new PDFFactory.
  67. * @param document the parent PDFDocument needed to register the generated
  68. * objects
  69. */
  70. public PDFFactory(PDFDocument document) {
  71. this.document = document;
  72. }
  73. /**
  74. * Returns the parent PDFDocument associated with this factory.
  75. * @return PDFDocument the parent PDFDocument
  76. */
  77. public final PDFDocument getDocument() {
  78. return this.document;
  79. }
  80. /* ========================= structure objects ========================= */
  81. /**
  82. * Make a /Catalog (Root) object. This object is written in
  83. * the trailer.
  84. *
  85. * @param pages the pages pdf object that the root points to
  86. * @return the new pdf root object for this document
  87. */
  88. public PDFRoot makeRoot(PDFPages pages) {
  89. //Make a /Pages object. This object is written in the trailer.
  90. PDFRoot pdfRoot = new PDFRoot(++this.document.objectcount, pages);
  91. pdfRoot.setDocument(getDocument());
  92. getDocument().addTrailerObject(pdfRoot);
  93. return pdfRoot;
  94. }
  95. /**
  96. * Make a /Pages object. This object is written in the trailer.
  97. *
  98. * @return a new PDF Pages object for adding pages to
  99. */
  100. public PDFPages makePages() {
  101. PDFPages pdfPages = new PDFPages(++(this.document.objectcount));
  102. pdfPages.setDocument(getDocument());
  103. getDocument().addTrailerObject(pdfPages);
  104. return pdfPages;
  105. }
  106. /**
  107. * Make a /Resources object. This object is written in the trailer.
  108. *
  109. * @return a new PDF resources object
  110. */
  111. public PDFResources makeResources() {
  112. PDFResources pdfResources = new PDFResources(++this.document.objectcount);
  113. pdfResources.setDocument(getDocument());
  114. getDocument().addTrailerObject(pdfResources);
  115. return pdfResources;
  116. }
  117. /**
  118. * make an /Info object
  119. *
  120. * @param prod string indicating application producing the PDF
  121. * @return the created /Info object
  122. */
  123. protected PDFInfo makeInfo(String prod) {
  124. /*
  125. * create a PDFInfo with the next object number and add to
  126. * list of objects
  127. */
  128. PDFInfo pdfInfo = new PDFInfo();
  129. // set the default producer
  130. pdfInfo.setProducer(prod);
  131. getDocument().registerObject(pdfInfo);
  132. return pdfInfo;
  133. }
  134. /**
  135. * Make a Metadata object.
  136. * @param meta the DOM Document containing the XMP metadata.
  137. * @param readOnly true if the metadata packet should be marked read-only
  138. * @return the newly created Metadata object
  139. */
  140. public PDFMetadata makeMetadata(Metadata meta, boolean readOnly) {
  141. PDFMetadata pdfMetadata = new PDFMetadata(meta, readOnly);
  142. getDocument().registerObject(pdfMetadata);
  143. return pdfMetadata;
  144. }
  145. /**
  146. * Make a OutputIntent dictionary.
  147. * @return the newly created OutputIntent dictionary
  148. */
  149. public PDFOutputIntent makeOutputIntent() {
  150. PDFOutputIntent outputIntent = new PDFOutputIntent();
  151. getDocument().registerObject(outputIntent);
  152. return outputIntent;
  153. }
  154. /**
  155. * Make a /Page object. The page is assigned an object number immediately
  156. * so references can already be made. The page must be added to the
  157. * PDFDocument later using addObject().
  158. *
  159. * @param resources resources object to use
  160. * @param pageIndex index of the page (zero-based)
  161. * @param mediaBox the MediaBox area
  162. * @param cropBox the CropBox area
  163. * @param bleedBox the BleedBox area
  164. * @param trimBox the TrimBox area
  165. *
  166. * @return the created /Page object
  167. */
  168. public PDFPage makePage(PDFResources resources, int pageIndex,
  169. Rectangle2D mediaBox, Rectangle2D cropBox,
  170. Rectangle2D bleedBox, Rectangle2D trimBox) {
  171. /*
  172. * create a PDFPage with the next object number, the given
  173. * resources, contents and dimensions
  174. */
  175. PDFPage page = new PDFPage(resources, pageIndex, mediaBox, cropBox, bleedBox, trimBox);
  176. getDocument().assignObjectNumber(page);
  177. getDocument().getPages().addPage(page);
  178. return page;
  179. }
  180. /**
  181. * Make a /Page object. The page is assigned an object number immediately
  182. * so references can already be made. The page must be added to the
  183. * PDFDocument later using addObject().
  184. *
  185. * @param resources resources object to use
  186. * @param pageWidth width of the page in points
  187. * @param pageHeight height of the page in points
  188. * @param pageIndex index of the page (zero-based)
  189. *
  190. * @return the created /Page object
  191. */
  192. public PDFPage makePage(PDFResources resources,
  193. int pageWidth, int pageHeight, int pageIndex) {
  194. Rectangle2D mediaBox = new Rectangle2D.Double(0, 0, pageWidth, pageHeight);
  195. return makePage(resources, pageIndex, mediaBox, mediaBox, mediaBox, mediaBox);
  196. }
  197. /**
  198. * Make a /Page object. The page is assigned an object number immediately
  199. * so references can already be made. The page must be added to the
  200. * PDFDocument later using addObject().
  201. *
  202. * @param resources resources object to use
  203. * @param pageWidth width of the page in points
  204. * @param pageHeight height of the page in points
  205. *
  206. * @return the created /Page object
  207. */
  208. public PDFPage makePage(PDFResources resources,
  209. int pageWidth, int pageHeight) {
  210. return makePage(resources, pageWidth, pageHeight, -1);
  211. }
  212. /* ========================= functions ================================= */
  213. /**
  214. * Make a Type 0 sampled function
  215. *
  216. * @param theDomain List objects of Double objects.
  217. * This is the domain of the function.
  218. * See page 264 of the PDF 1.3 Spec.
  219. * @param theRange List objects of Double objects.
  220. * This is the Range of the function.
  221. * See page 264 of the PDF 1.3 Spec.
  222. * @param theSize A List object of Integer objects.
  223. * This is the number of samples in each input dimension.
  224. * I can't imagine there being more or less than two input dimensions,
  225. * so maybe this should be an array of length 2.
  226. *
  227. * See page 265 of the PDF 1.3 Spec.
  228. * @param theBitsPerSample An int specifying the number of bits user
  229. * to represent each sample value.
  230. * Limited to 1,2,4,8,12,16,24 or 32.
  231. * See page 265 of the 1.3 PDF Spec.
  232. * @param theOrder The order of interpolation between samples.
  233. * Default is 1 (one). Limited
  234. * to 1 (one) or 3, which means linear or cubic-spline interpolation.
  235. *
  236. * This attribute is optional.
  237. *
  238. * See page 265 in the PDF 1.3 spec.
  239. * @param theEncode List objects of Double objects.
  240. * This is the linear mapping of input values intop the domain
  241. * of the function's sample table. Default is hard to represent in
  242. * ascii, but basically [0 (Size0 1) 0 (Size1 1)...].
  243. * This attribute is optional.
  244. *
  245. * See page 265 in the PDF 1.3 spec.
  246. * @param theDecode List objects of Double objects.
  247. * This is a linear mapping of sample values into the range.
  248. * The default is just the range.
  249. *
  250. * This attribute is optional.
  251. * Read about it on page 265 of the PDF 1.3 spec.
  252. * @param theFunctionDataStream The sample values that specify
  253. * the function are provided in a stream.
  254. *
  255. * This is optional, but is almost always used.
  256. *
  257. * Page 265 of the PDF 1.3 spec has more.
  258. * @param theFilter This is a vector of String objects which
  259. * are the various filters that have are to be
  260. * applied to the stream to make sense of it.
  261. * Order matters, so watch out.
  262. *
  263. * This is not documented in the Function section of the PDF 1.3 spec,
  264. * it was deduced from samples that this is sometimes used, even if we may never
  265. * use it in FOP. It is added for completeness sake.
  266. * @param theFunctionType This is the type of function (0,2,3, or 4).
  267. * It should be 0 as this is the constructor for sampled functions.
  268. * @return the PDF function that was created
  269. */
  270. public PDFFunction makeFunction( // CSOK: ParameterNumber
  271. int theFunctionType, List theDomain,
  272. List theRange, List theSize,
  273. int theBitsPerSample, int theOrder,
  274. List theEncode, List theDecode,
  275. StringBuffer theFunctionDataStream,
  276. List theFilter) {
  277. // Type 0 function
  278. PDFFunction function = new PDFFunction(theFunctionType, theDomain,
  279. theRange, theSize,
  280. theBitsPerSample, theOrder,
  281. theEncode, theDecode,
  282. theFunctionDataStream,
  283. theFilter);
  284. PDFFunction oldfunc = getDocument().findFunction(function);
  285. if (oldfunc == null) {
  286. getDocument().registerObject(function);
  287. } else {
  288. function = oldfunc;
  289. }
  290. return (function);
  291. }
  292. /**
  293. * make a type Exponential interpolation function
  294. * (for shading usually)
  295. *
  296. * @param theDomain List objects of Double objects.
  297. * This is the domain of the function.
  298. * See page 264 of the PDF 1.3 Spec.
  299. * @param theRange List of Doubles that is the Range of the function.
  300. * See page 264 of the PDF 1.3 Spec.
  301. * @param theCZero This is a vector of Double objects which defines the function result
  302. * when x=0.
  303. *
  304. * This attribute is optional.
  305. * It's described on page 268 of the PDF 1.3 spec.
  306. * @param theCOne This is a vector of Double objects which defines the function result
  307. * when x=1.
  308. *
  309. * This attribute is optional.
  310. * It's described on page 268 of the PDF 1.3 spec.
  311. * @param theInterpolationExponentN This is the inerpolation exponent.
  312. *
  313. * This attribute is required.
  314. * PDF Spec page 268
  315. * @param theFunctionType The type of the function, which should be 2.
  316. * @return the PDF function that was created
  317. */
  318. public PDFFunction makeFunction(int theFunctionType, List theDomain,
  319. List theRange, List theCZero,
  320. List theCOne,
  321. double theInterpolationExponentN) { // type 2
  322. PDFFunction function = new PDFFunction(theFunctionType, theDomain,
  323. theRange, theCZero, theCOne,
  324. theInterpolationExponentN);
  325. PDFFunction oldfunc = getDocument().findFunction(function);
  326. if (oldfunc == null) {
  327. getDocument().registerObject(function);
  328. } else {
  329. function = oldfunc;
  330. }
  331. return (function);
  332. }
  333. /**
  334. * Make a Type 3 Stitching function
  335. *
  336. * @param theDomain List objects of Double objects.
  337. * This is the domain of the function.
  338. * See page 264 of the PDF 1.3 Spec.
  339. * @param theRange List objects of Double objects.
  340. * This is the Range of the function.
  341. * See page 264 of the PDF 1.3 Spec.
  342. * @param theFunctions An List of the PDFFunction objects
  343. * that the stitching function stitches.
  344. *
  345. * This attributed is required.
  346. * It is described on page 269 of the PDF spec.
  347. * @param theBounds This is a vector of Doubles representing
  348. * the numbers that, in conjunction with Domain
  349. * define the intervals to which each function from
  350. * the 'functions' object applies. It must be in
  351. * order of increasing magnitude, and each must be
  352. * within Domain.
  353. *
  354. * It basically sets how much of the gradient each function handles.
  355. *
  356. * This attributed is required.
  357. * It's described on page 269 of the PDF 1.3 spec.
  358. * @param theEncode List objects of Double objects.
  359. * This is the linear mapping of input values intop the domain
  360. * of the function's sample table. Default is hard to represent in
  361. * ascii, but basically [0 (Size0 1) 0 (Size1 1)...].
  362. * This attribute is required.
  363. *
  364. * See page 270 in the PDF 1.3 spec.
  365. * @param theFunctionType This is the function type. It should be 3,
  366. * for a stitching function.
  367. * @return the PDF function that was created
  368. */
  369. public PDFFunction makeFunction(int theFunctionType, List theDomain,
  370. List theRange, List theFunctions,
  371. List theBounds,
  372. List theEncode) {
  373. // Type 3
  374. PDFFunction function = new PDFFunction(theFunctionType, theDomain,
  375. theRange, theFunctions,
  376. theBounds, theEncode);
  377. PDFFunction oldfunc = getDocument().findFunction(function);
  378. if (oldfunc == null) {
  379. getDocument().registerObject(function);
  380. } else {
  381. function = oldfunc;
  382. }
  383. return (function);
  384. }
  385. /**
  386. * make a postscript calculator function
  387. *
  388. * @param theNumber the PDF object number
  389. * @param theFunctionType the type of function to make
  390. * @param theDomain the domain values
  391. * @param theRange the range values of the function
  392. * @param theFunctionDataStream a string containing the pdf drawing
  393. * @return the PDF function that was created
  394. */
  395. public PDFFunction makeFunction(int theNumber, int theFunctionType,
  396. List theDomain, List theRange,
  397. StringBuffer theFunctionDataStream) {
  398. // Type 4
  399. PDFFunction function = new PDFFunction(theFunctionType, theDomain,
  400. theRange,
  401. theFunctionDataStream);
  402. PDFFunction oldfunc = getDocument().findFunction(function);
  403. if (oldfunc == null) {
  404. getDocument().registerObject(function);
  405. } else {
  406. function = oldfunc;
  407. }
  408. return (function);
  409. }
  410. /* ========================= shadings ================================== */
  411. /**
  412. * make a function based shading object
  413. *
  414. * @param res the PDF resource context to add the shading, may be null
  415. * @param theShadingType The type of shading object, which should be 1 for function
  416. * based shading.
  417. * @param theColorSpace The colorspace is 'DeviceRGB' or something similar.
  418. * @param theBackground An array of color components appropriate to the
  419. * colorspace key specifying a single color value.
  420. * This key is used by the f operator buy ignored by the sh operator.
  421. * @param theBBox List of double's representing a rectangle
  422. * in the coordinate space that is current at the
  423. * time of shading is imaged. Temporary clipping
  424. * boundary.
  425. * @param theAntiAlias Whether or not to anti-alias.
  426. * @param theDomain Optional vector of Doubles specifying the domain.
  427. * @param theMatrix List of Doubles specifying the matrix.
  428. * If it's a pattern, then the matrix maps it to pattern space.
  429. * If it's a shading, then it maps it to current user space.
  430. * It's optional, the default is the identity matrix
  431. * @param theFunction The PDF Function that maps an (x,y) location to a color
  432. * @return the PDF shading that was created
  433. */
  434. public PDFShading makeShading( // CSOK: ParameterNumber
  435. PDFResourceContext res, int theShadingType,
  436. PDFDeviceColorSpace theColorSpace,
  437. List theBackground, List theBBox,
  438. boolean theAntiAlias, List theDomain,
  439. List theMatrix,
  440. PDFFunction theFunction) {
  441. // make Shading of Type 1
  442. PDFShading shading = new PDFShading(theShadingType,
  443. theColorSpace, theBackground,
  444. theBBox, theAntiAlias, theDomain,
  445. theMatrix, theFunction);
  446. PDFShading oldshad = getDocument().findShading(shading);
  447. if (oldshad == null) {
  448. getDocument().registerObject(shading);
  449. } else {
  450. shading = oldshad;
  451. }
  452. // add this shading to resources
  453. if (res != null) {
  454. res.getPDFResources().addShading(shading);
  455. } else {
  456. getDocument().getResources().addShading(shading);
  457. }
  458. return (shading);
  459. }
  460. /**
  461. * Make an axial or radial shading object.
  462. *
  463. * @param res the PDF resource context to add the shading, may be null
  464. * @param theShadingType 2 or 3 for axial or radial shading
  465. * @param theColorSpace "DeviceRGB" or similar.
  466. * @param theBackground theBackground An array of color components appropriate to the
  467. * colorspace key specifying a single color value.
  468. * This key is used by the f operator buy ignored by the sh operator.
  469. * @param theBBox List of double's representing a rectangle
  470. * in the coordinate space that is current at the
  471. * time of shading is imaged. Temporary clipping
  472. * boundary.
  473. * @param theAntiAlias Default is false
  474. * @param theCoords List of four (type 2) or 6 (type 3) Double
  475. * @param theDomain List of Doubles specifying the domain
  476. * @param theFunction the Stitching (PDFfunction type 3) function,
  477. * even if it's stitching a single function
  478. * @param theExtend List of Booleans of whether to extend the
  479. * start and end colors past the start and end points
  480. * The default is [false, false]
  481. * @return the PDF shading that was created
  482. */
  483. public PDFShading makeShading( // CSOK: ParameterNumber
  484. PDFResourceContext res, int theShadingType,
  485. PDFDeviceColorSpace theColorSpace,
  486. List theBackground, List theBBox,
  487. boolean theAntiAlias, List theCoords,
  488. List theDomain, PDFFunction theFunction,
  489. List theExtend) {
  490. // make Shading of Type 2 or 3
  491. PDFShading shading = new PDFShading(theShadingType,
  492. theColorSpace, theBackground,
  493. theBBox, theAntiAlias, theCoords,
  494. theDomain, theFunction,
  495. theExtend);
  496. PDFShading oldshad = getDocument().findShading(shading);
  497. if (oldshad == null) {
  498. getDocument().registerObject(shading);
  499. } else {
  500. shading = oldshad;
  501. }
  502. if (res != null) {
  503. res.getPDFResources().addShading(shading);
  504. } else {
  505. getDocument().getResources().addShading(shading);
  506. }
  507. return (shading);
  508. }
  509. /**
  510. * Make a free-form gouraud shaded triangle mesh, coons patch mesh, or tensor patch mesh
  511. * shading object
  512. *
  513. * @param res the PDF resource context to add the shading, may be null
  514. * @param theShadingType 4, 6, or 7 depending on whether it's
  515. * Free-form gouraud-shaded triangle meshes, coons patch meshes,
  516. * or tensor product patch meshes, respectively.
  517. * @param theColorSpace "DeviceRGB" or similar.
  518. * @param theBackground theBackground An array of color components appropriate to the
  519. * colorspace key specifying a single color value.
  520. * This key is used by the f operator buy ignored by the sh operator.
  521. * @param theBBox List of double's representing a rectangle
  522. * in the coordinate space that is current at the
  523. * time of shading is imaged. Temporary clipping
  524. * boundary.
  525. * @param theAntiAlias Default is false
  526. * @param theBitsPerCoordinate 1,2,4,8,12,16,24 or 32.
  527. * @param theBitsPerComponent 1,2,4,8,12, and 16
  528. * @param theBitsPerFlag 2,4,8.
  529. * @param theDecode List of Doubles see PDF 1.3 spec pages 303 to 312.
  530. * @param theFunction the PDFFunction
  531. * @return the PDF shading that was created
  532. */
  533. public PDFShading makeShading( // CSOK: ParameterNumber
  534. PDFResourceContext res, int theShadingType,
  535. PDFDeviceColorSpace theColorSpace,
  536. List theBackground, List theBBox,
  537. boolean theAntiAlias,
  538. int theBitsPerCoordinate,
  539. int theBitsPerComponent,
  540. int theBitsPerFlag, List theDecode,
  541. PDFFunction theFunction) {
  542. // make Shading of type 4,6 or 7
  543. PDFShading shading = new PDFShading(theShadingType,
  544. theColorSpace, theBackground,
  545. theBBox, theAntiAlias,
  546. theBitsPerCoordinate,
  547. theBitsPerComponent,
  548. theBitsPerFlag, theDecode,
  549. theFunction);
  550. PDFShading oldshad = getDocument().findShading(shading);
  551. if (oldshad == null) {
  552. getDocument().registerObject(shading);
  553. } else {
  554. shading = oldshad;
  555. }
  556. if (res != null) {
  557. res.getPDFResources().addShading(shading);
  558. } else {
  559. getDocument().getResources().addShading(shading);
  560. }
  561. return (shading);
  562. }
  563. /**
  564. * make a Lattice-Form Gouraud mesh shading object
  565. *
  566. * @param res the PDF resource context to add the shading, may be null
  567. * @param theShadingType 5 for lattice-Form Gouraud shaded-triangle mesh
  568. * without spaces. "Shading1" or "Sh1" are good examples.
  569. * @param theColorSpace "DeviceRGB" or similar.
  570. * @param theBackground theBackground An array of color components appropriate to the
  571. * colorspace key specifying a single color value.
  572. * This key is used by the f operator buy ignored by the sh operator.
  573. * @param theBBox List of double's representing a rectangle
  574. * in the coordinate space that is current at the
  575. * time of shading is imaged. Temporary clipping
  576. * boundary.
  577. * @param theAntiAlias Default is false
  578. * @param theBitsPerCoordinate 1,2,4,8,12,16, 24, or 32
  579. * @param theBitsPerComponent 1,2,4,8,12,24,32
  580. * @param theDecode List of Doubles. See page 305 in PDF 1.3 spec.
  581. * @param theVerticesPerRow number of vertices in each "row" of the lattice.
  582. * @param theFunction The PDFFunction that's mapped on to this shape
  583. * @return the PDF shading that was created
  584. */
  585. public PDFShading makeShading( // CSOK: ParameterNumber
  586. PDFResourceContext res, int theShadingType,
  587. PDFDeviceColorSpace theColorSpace,
  588. List theBackground, List theBBox,
  589. boolean theAntiAlias,
  590. int theBitsPerCoordinate,
  591. int theBitsPerComponent, List theDecode,
  592. int theVerticesPerRow,
  593. PDFFunction theFunction) {
  594. // make shading of Type 5
  595. PDFShading shading = new PDFShading(theShadingType,
  596. theColorSpace, theBackground,
  597. theBBox, theAntiAlias,
  598. theBitsPerCoordinate,
  599. theBitsPerComponent, theDecode,
  600. theVerticesPerRow, theFunction);
  601. PDFShading oldshad = getDocument().findShading(shading);
  602. if (oldshad == null) {
  603. getDocument().registerObject(shading);
  604. } else {
  605. shading = oldshad;
  606. }
  607. if (res != null) {
  608. res.getPDFResources().addShading(shading);
  609. } else {
  610. getDocument().getResources().addShading(shading);
  611. }
  612. return (shading);
  613. }
  614. /* ========================= patterns ================================== */
  615. /**
  616. * Make a tiling pattern
  617. *
  618. * @param res the PDF resource context to add the shading, may be null
  619. * @param thePatternType the type of pattern, which is 1 for tiling.
  620. * @param theResources the resources associated with this pattern
  621. * @param thePaintType 1 or 2, colored or uncolored.
  622. * @param theTilingType 1, 2, or 3, constant spacing, no distortion, or faster tiling
  623. * @param theBBox List of Doubles: The pattern cell bounding box
  624. * @param theXStep horizontal spacing
  625. * @param theYStep vertical spacing
  626. * @param theMatrix Optional List of Doubles transformation matrix
  627. * @param theXUID Optional vector of Integers that uniquely identify the pattern
  628. * @param thePatternDataStream The stream of pattern data to be tiled.
  629. * @return the PDF pattern that was created
  630. */
  631. public PDFPattern makePattern( // CSOK: ParameterNumber
  632. PDFResourceContext res, int thePatternType, // 1
  633. PDFResources theResources, int thePaintType, int theTilingType,
  634. List theBBox, double theXStep,
  635. double theYStep, List theMatrix,
  636. List theXUID, StringBuffer thePatternDataStream) {
  637. // PDFResources theResources
  638. PDFPattern pattern = new PDFPattern(theResources, 1,
  639. thePaintType, theTilingType,
  640. theBBox, theXStep, theYStep,
  641. theMatrix, theXUID,
  642. thePatternDataStream);
  643. PDFPattern oldpatt = getDocument().findPattern(pattern);
  644. if (oldpatt == null) {
  645. getDocument().registerObject(pattern);
  646. } else {
  647. pattern = oldpatt;
  648. }
  649. if (res != null) {
  650. res.getPDFResources().addPattern(pattern);
  651. } else {
  652. getDocument().getResources().addPattern(pattern);
  653. }
  654. return (pattern);
  655. }
  656. /**
  657. * Make a smooth shading pattern
  658. *
  659. * @param res the PDF resource context to add the shading, may be null
  660. * @param thePatternType the type of the pattern, which is 2, smooth shading
  661. * @param theShading the PDF Shading object that comprises this pattern
  662. * @param theXUID optional:the extended unique Identifier if used.
  663. * @param theExtGState optional: the extended graphics state, if used.
  664. * @param theMatrix Optional:List of Doubles that specify the matrix.
  665. * @return the PDF pattern that was created
  666. */
  667. public PDFPattern makePattern(PDFResourceContext res,
  668. int thePatternType, PDFShading theShading,
  669. List theXUID, StringBuffer theExtGState,
  670. List theMatrix) {
  671. PDFPattern pattern = new PDFPattern(2, theShading,
  672. theXUID, theExtGState, theMatrix);
  673. PDFPattern oldpatt = getDocument().findPattern(pattern);
  674. if (oldpatt == null) {
  675. getDocument().registerObject(pattern);
  676. } else {
  677. pattern = oldpatt;
  678. }
  679. if (res != null) {
  680. res.getPDFResources().addPattern(pattern);
  681. } else {
  682. getDocument().getResources().addPattern(pattern);
  683. }
  684. return (pattern);
  685. }
  686. /**
  687. * Make a gradient
  688. *
  689. * @param res the PDF resource context to add the shading, may be null
  690. * @param radial if true a radial gradient will be created
  691. * @param theColorspace the colorspace of the gradient
  692. * @param theColors the list of colors for the gradient
  693. * @param theBounds the list of bounds associated with the colors
  694. * @param theCoords the coordinates for the gradient
  695. * @param theMatrix the coordinate-transformation matrix
  696. * @return the PDF pattern that was created
  697. */
  698. public PDFPattern makeGradient(PDFResourceContext res, boolean radial,
  699. PDFDeviceColorSpace theColorspace,
  700. List theColors, List theBounds,
  701. List theCoords, List theMatrix) {
  702. PDFShading myShad;
  703. PDFFunction myfunky;
  704. PDFFunction myfunc;
  705. List theCzero;
  706. List theCone;
  707. PDFPattern myPattern;
  708. //PDFColorSpace theColorSpace;
  709. double interpolation = 1.000;
  710. List theFunctions = new ArrayList();
  711. int currentPosition;
  712. int lastPosition = theColors.size() - 1;
  713. // if 5 elements, the penultimate element is 3.
  714. // do not go beyond that, because you always need
  715. // to have a next color when creating the function.
  716. for (currentPosition = 0; currentPosition < lastPosition;
  717. currentPosition++) { // for every consecutive color pair
  718. Color currentColor = (Color)theColors.get(currentPosition);
  719. Color nextColor = (Color)theColors.get(currentPosition + 1);
  720. // colorspace must be consistent, so we simply convert to sRGB where necessary
  721. if (!currentColor.getColorSpace().isCS_sRGB()) {
  722. //Convert to sRGB
  723. currentColor = ColorUtil.toSRGBColor(currentColor);
  724. theColors.set(currentPosition, currentColor);
  725. }
  726. if (!nextColor.getColorSpace().isCS_sRGB()) {
  727. //Convert to sRGB
  728. nextColor = ColorUtil.toSRGBColor(nextColor);
  729. theColors.set(currentPosition + 1, nextColor);
  730. }
  731. theCzero = toColorVector(currentColor);
  732. theCone = toColorVector(nextColor);
  733. myfunc = makeFunction(2, null, null, theCzero, theCone,
  734. interpolation);
  735. theFunctions.add(myfunc);
  736. } // end of for every consecutive color pair
  737. myfunky = makeFunction(3, null, null, theFunctions, theBounds,
  738. null);
  739. if (radial) {
  740. if (theCoords.size() == 6) {
  741. myShad = makeShading(res, 3, getDocument().getPDFColorSpace(),
  742. null, null,
  743. false, theCoords, null, myfunky,
  744. null);
  745. } else { // if the center x, center y, and radius specifiy
  746. // the gradient, then assume the same center x, center y,
  747. // and radius of zero for the other necessary component
  748. List newCoords = new ArrayList();
  749. newCoords.add(theCoords.get(0));
  750. newCoords.add(theCoords.get(1));
  751. newCoords.add(theCoords.get(2));
  752. newCoords.add(theCoords.get(0));
  753. newCoords.add(theCoords.get(1));
  754. newCoords.add(new Double(0.0));
  755. myShad = makeShading(res, 3, getDocument().getPDFColorSpace(),
  756. null, null,
  757. false, newCoords, null, myfunky,
  758. null);
  759. }
  760. } else {
  761. myShad = makeShading(res, 2, getDocument().getPDFColorSpace(),
  762. null, null,
  763. false, theCoords, null, myfunky,
  764. null);
  765. }
  766. myPattern = makePattern(res, 2, myShad, null, null, theMatrix);
  767. return (myPattern);
  768. }
  769. private List toColorVector(Color nextColor) {
  770. List vector = new java.util.ArrayList();
  771. float[] comps = nextColor.getColorComponents(null);
  772. for (int i = 0, c = comps.length; i < c; i++) {
  773. vector.add(new Double(comps[i]));
  774. }
  775. return vector;
  776. }
  777. /* ============= named destinations and the name dictionary ============ */
  778. /**
  779. * Registers and returns newdest if it is unique. Otherwise, returns
  780. * the equal destination already present in the document.
  781. *
  782. * @param newdest a new, as yet unregistered destination
  783. * @return newdest if unique, else the already registered instance
  784. */
  785. protected PDFDestination getUniqueDestination(PDFDestination newdest) {
  786. PDFDestination existing = getDocument().findDestination(newdest);
  787. if (existing != null) {
  788. return existing;
  789. } else {
  790. getDocument().addDestination(newdest);
  791. return newdest;
  792. }
  793. }
  794. /**
  795. * Make a named destination.
  796. *
  797. * @param idRef ID Reference for this destination (the name of the destination)
  798. * @param goToRef Object reference to the GoTo Action
  799. * @return the newly created destrination
  800. */
  801. public PDFDestination makeDestination(String idRef, Object goToRef) {
  802. PDFDestination destination = new PDFDestination(idRef, goToRef);
  803. return getUniqueDestination(destination);
  804. }
  805. /**
  806. * Make a names dictionary (the /Names object).
  807. * @return the new PDFNames object
  808. */
  809. public PDFNames makeNames() {
  810. PDFNames names = new PDFNames();
  811. getDocument().assignObjectNumber(names);
  812. getDocument().addTrailerObject(names);
  813. return names;
  814. }
  815. /**
  816. * Make a names dictionary (the /PageLabels object).
  817. * @return the new PDFPageLabels object
  818. */
  819. public PDFPageLabels makePageLabels() {
  820. PDFPageLabels pageLabels = new PDFPageLabels();
  821. getDocument().assignObjectNumber(pageLabels);
  822. getDocument().addTrailerObject(pageLabels);
  823. return pageLabels;
  824. }
  825. /**
  826. * Make a the head object of the name dictionary (the /Dests object).
  827. *
  828. * @param destinationList a list of PDFDestination instances
  829. * @return the new PDFDests object
  830. */
  831. public PDFDests makeDests(List destinationList) {
  832. PDFDests dests;
  833. //TODO: Check why the below conditional branch is needed. Condition is always true...
  834. final boolean deep = true;
  835. //true for a "deep" structure (one node per entry), true for a "flat" structure
  836. if (deep) {
  837. dests = new PDFDests();
  838. PDFArray kids = new PDFArray(dests);
  839. Iterator iter = destinationList.iterator();
  840. while (iter.hasNext()) {
  841. PDFDestination dest = (PDFDestination)iter.next();
  842. PDFNameTreeNode node = new PDFNameTreeNode();
  843. getDocument().registerObject(node);
  844. node.setLowerLimit(dest.getIDRef());
  845. node.setUpperLimit(dest.getIDRef());
  846. node.setNames(new PDFArray(node));
  847. PDFArray names = node.getNames();
  848. names.add(dest);
  849. kids.add(node);
  850. }
  851. dests.setLowerLimit(((PDFNameTreeNode)kids.get(0)).getLowerLimit());
  852. dests.setUpperLimit(((PDFNameTreeNode)kids.get(kids.length() - 1)).getUpperLimit());
  853. dests.setKids(kids);
  854. } else {
  855. dests = new PDFDests(destinationList);
  856. }
  857. getDocument().registerObject(dests);
  858. return dests;
  859. }
  860. /**
  861. * Make a name tree node.
  862. *
  863. * @return the new name tree node
  864. */
  865. public PDFNameTreeNode makeNameTreeNode() {
  866. PDFNameTreeNode node = new PDFNameTreeNode();
  867. getDocument().registerObject(node);
  868. return node;
  869. }
  870. /* ========================= links ===================================== */
  871. // Some of the "yoffset-only" functions in this part are obsolete and can
  872. // possibly be removed or deprecated. Some are still called by PDFGraphics2D
  873. // (although that could be changed, they don't need the yOffset param anyway).
  874. /**
  875. * Create a PDF link to an existing PDFAction object
  876. *
  877. * @param rect the hotspot position in absolute coordinates
  878. * @param pdfAction the PDFAction that this link refers to
  879. * @return the new PDFLink object, or null if either rect or pdfAction is null
  880. */
  881. public PDFLink makeLink(Rectangle2D rect, PDFAction pdfAction) {
  882. if (rect == null || pdfAction == null) {
  883. return null;
  884. } else {
  885. PDFLink link = new PDFLink(rect);
  886. link.setAction(pdfAction);
  887. getDocument().registerObject(link);
  888. return link;
  889. // does findLink make sense? I mean, how often will it happen that several
  890. // links have the same target *and* the same hot rect? And findLink has to
  891. // walk and compare the entire link list everytime you call it...
  892. }
  893. }
  894. /**
  895. * Make an internal link.
  896. *
  897. * @param rect the hotspot position in absolute coordinates
  898. * @param page the target page reference value
  899. * @param dest the position destination
  900. * @return the new PDF link object
  901. */
  902. public PDFLink makeLink(Rectangle2D rect, String page, String dest) {
  903. PDFLink link = new PDFLink(rect);
  904. getDocument().registerObject(link);
  905. PDFGoTo gt = new PDFGoTo(page);
  906. gt.setDestination(dest);
  907. getDocument().registerObject(gt);
  908. PDFInternalLink internalLink = new PDFInternalLink(gt.referencePDF());
  909. link.setAction(internalLink);
  910. return link;
  911. }
  912. /**
  913. * Make a {@link PDFLink} object
  914. *
  915. * @param rect the clickable rectangle
  916. * @param destination the destination file
  917. * @param linkType the link type
  918. * @param yoffset the yoffset on the page for an internal link
  919. * @return the PDFLink object created
  920. */
  921. public PDFLink makeLink(Rectangle2D rect, String destination,
  922. int linkType, float yoffset) {
  923. //PDFLink linkObject;
  924. PDFLink link = new PDFLink(rect);
  925. if (linkType == PDFLink.EXTERNAL) {
  926. link.setAction(getExternalAction(destination, false));
  927. } else {
  928. // linkType is internal
  929. String goToReference = getGoToReference(destination, yoffset);
  930. PDFInternalLink internalLink = new PDFInternalLink(goToReference);
  931. link.setAction(internalLink);
  932. }
  933. PDFLink oldlink = getDocument().findLink(link);
  934. if (oldlink == null) {
  935. getDocument().registerObject(link);
  936. } else {
  937. link = oldlink;
  938. }
  939. return link;
  940. }
  941. private static final String EMBEDDED_FILE = "embedded-file:";
  942. /**
  943. * Create/find and return the appropriate external PDFAction according to the target
  944. *
  945. * @param target The external target. This may be a PDF file name
  946. * (optionally with internal page number or destination) or any type of URI.
  947. * @param newWindow boolean indicating whether the target should be
  948. * displayed in a new window
  949. * @return the PDFAction thus created or found
  950. */
  951. public PDFAction getExternalAction(String target, boolean newWindow) {
  952. int index;
  953. String targetLo = target.toLowerCase();
  954. if (target.startsWith(EMBEDDED_FILE)) {
  955. // File Attachments (Embedded Files)
  956. String filename = target.substring(EMBEDDED_FILE.length());
  957. return getActionForEmbeddedFile(filename, newWindow);
  958. } else if (targetLo.startsWith("http://")) {
  959. // HTTP URL?
  960. return new PDFUri(target);
  961. } else if (targetLo.startsWith("https://")) {
  962. // HTTPS URL?
  963. return new PDFUri(target);
  964. } else if (targetLo.startsWith("file://")) {
  965. // Non PDF files. Try to /Launch them.
  966. target = target.substring("file://".length());
  967. return getLaunchAction(target);
  968. } else if (targetLo.endsWith(".pdf")) {
  969. // Bare PDF file name?
  970. return getGoToPDFAction(target, null, -1, newWindow);
  971. } else if ((index = targetLo.indexOf(".pdf#page=")) > 0) { // CSOK: InnerAssignment
  972. // PDF file + page?
  973. String filename = target.substring(0, index + 4);
  974. int page = Integer.parseInt(target.substring(index + 10));
  975. return getGoToPDFAction(filename, null, page, newWindow);
  976. } else if ((index = targetLo.indexOf(".pdf#dest=")) > 0) { // CSOK: InnerAssignment
  977. // PDF file + destination?
  978. String filename = target.substring(0, index + 4);
  979. String dest = target.substring(index + 10);
  980. return getGoToPDFAction(filename, dest, -1, newWindow);
  981. } else {
  982. // None of the above? Default to URI:
  983. return new PDFUri(target);
  984. }
  985. }
  986. private PDFAction getActionForEmbeddedFile(String filename, boolean newWindow) {
  987. PDFNames names = getDocument().getRoot().getNames();
  988. if (names == null) {
  989. throw new IllegalStateException(
  990. "No Names dictionary present."
  991. + " Cannot create Launch Action for embedded file: " + filename);
  992. }
  993. PDFNameTreeNode embeddedFiles = names.getEmbeddedFiles();
  994. if (embeddedFiles == null) {
  995. throw new IllegalStateException(
  996. "No /EmbeddedFiles name tree present."
  997. + " Cannot create Launch Action for embedded file: " + filename);
  998. }
  999. //Find filespec reference for the embedded file
  1000. filename = PDFText.toPDFString(filename, '_');
  1001. PDFArray files = embeddedFiles.getNames();
  1002. PDFReference embeddedFileRef = null;
  1003. int i = 0;
  1004. while (i < files.length()) {
  1005. String name = (String)files.get(i);
  1006. i++;
  1007. PDFReference ref = (PDFReference)files.get(i);
  1008. if (name.equals(filename)) {
  1009. embeddedFileRef = ref;
  1010. break;
  1011. }
  1012. i++;
  1013. }
  1014. if (embeddedFileRef == null) {
  1015. throw new IllegalStateException(
  1016. "No embedded file with name " + filename + " present.");
  1017. }
  1018. //Finally create the action
  1019. //PDFLaunch action = new PDFLaunch(embeddedFileRef);
  1020. //This works with Acrobat 8 but not with Acrobat 9
  1021. //The following two options didn't seem to have any effect.
  1022. //PDFGoToEmbedded action = new PDFGoToEmbedded(embeddedFileRef, 0, newWindow);
  1023. //PDFGoToRemote action = new PDFGoToRemote(embeddedFileRef, 0, newWindow);
  1024. //This finally seems to work:
  1025. StringBuffer scriptBuffer = new StringBuffer();
  1026. scriptBuffer.append("this.exportDataObject({cName:\"");
  1027. scriptBuffer.append(filename);
  1028. scriptBuffer.append("\", nLaunch:2});");
  1029. PDFJavaScriptLaunchAction action = new PDFJavaScriptLaunchAction(scriptBuffer.toString());
  1030. return action;
  1031. }
  1032. /**
  1033. * Create or find a PDF GoTo with the given page reference string and Y offset,
  1034. * and return its PDF object reference
  1035. *
  1036. * @param pdfPageRef the PDF page reference, e.g. "23 0 R"
  1037. * @param yoffset the distance from the bottom of the page in points
  1038. * @return the GoTo's object reference
  1039. */
  1040. public String getGoToReference(String pdfPageRef, float yoffset) {
  1041. return getPDFGoTo(pdfPageRef, new Point2D.Float(0.0f, yoffset)).referencePDF();
  1042. }
  1043. /**
  1044. * Finds and returns a PDFGoTo to the given page and position.
  1045. * Creates the PDFGoTo if not found.
  1046. *
  1047. * @param pdfPageRef the PDF page reference
  1048. * @param position the (X,Y) position in points
  1049. *
  1050. * @return the new or existing PDFGoTo object
  1051. */
  1052. public PDFGoTo getPDFGoTo(String pdfPageRef, Point2D position) {
  1053. getDocument().getProfile().verifyActionAllowed();
  1054. PDFGoTo gt = new PDFGoTo(pdfPageRef, position);
  1055. PDFGoTo oldgt = getDocument().findGoTo(gt);
  1056. if (oldgt == null) {
  1057. getDocument().assignObjectNumber(gt);
  1058. getDocument().addTrailerObject(gt);
  1059. } else {
  1060. gt = oldgt;
  1061. }
  1062. return gt;
  1063. }
  1064. /**
  1065. * Create and return a goto pdf document action.
  1066. * This creates a pdf files spec and pdf goto remote action.
  1067. * It also checks available pdf objects so it will not create an
  1068. * object if it already exists.
  1069. *
  1070. * @param file the pdf file name
  1071. * @param dest the remote name destination, may be null
  1072. * @param page the remote page number, -1 means not specified
  1073. * @param newWindow boolean indicating whether the target should be
  1074. * displayed in a new window
  1075. * @return the pdf goto remote object
  1076. */
  1077. private PDFGoToRemote getGoToPDFAction(String file, String dest, int page, boolean newWindow) {
  1078. getDocument().getProfile().verifyActionAllowed();
  1079. PDFFileSpec fileSpec = new PDFFileSpec(file);
  1080. PDFFileSpec oldspec = getDocument().findFileSpec(fileSpec);
  1081. if (oldspec == null) {
  1082. getDocument().registerObject(fileSpec);
  1083. } else {
  1084. fileSpec = oldspec;
  1085. }
  1086. PDFGoToRemote remote;
  1087. if (dest == null && page == -1) {
  1088. remote = new PDFGoToRemote(fileSpec, newWindow);
  1089. } else if (dest != null) {
  1090. remote = new PDFGoToRemote(fileSpec, dest, newWindow);
  1091. } else {
  1092. remote = new PDFGoToRemote(fileSpec, page, newWindow);
  1093. }
  1094. PDFGoToRemote oldremote = getDocument().findGoToRemote(remote);
  1095. if (oldremote == null) {
  1096. getDocument().registerObject(remote);
  1097. } else {
  1098. remote = oldremote;
  1099. }
  1100. return remote;
  1101. }
  1102. /**
  1103. * Creates and returns a launch pdf document action using
  1104. * <code>file</code> to create a file spcifiaciton for
  1105. * the document/file to be opened with an external application.
  1106. *
  1107. * @param file the pdf file name
  1108. * @return the pdf launch object
  1109. */
  1110. private PDFLaunch getLaunchAction(String file) {
  1111. getDocument().getProfile().verifyActionAllowed();
  1112. PDFFileSpec fileSpec = new PDFFileSpec(file);
  1113. PDFFileSpec oldSpec = getDocument().findFileSpec(fileSpec);
  1114. if (oldSpec == null) {
  1115. getDocument().registerObject(fileSpec);
  1116. } else {
  1117. fileSpec = oldSpec;
  1118. }
  1119. PDFLaunch launch = new PDFLaunch(fileSpec);
  1120. PDFLaunch oldLaunch = getDocument().findLaunch(launch);
  1121. if (oldLaunch == null) {
  1122. getDocument().registerObject(launch);
  1123. } else {
  1124. launch = oldLaunch;
  1125. }
  1126. return launch;
  1127. }
  1128. /**
  1129. * Make an outline object and add it to the given parent
  1130. *
  1131. * @param parent the parent PDFOutline object (may be null)
  1132. * @param label the title for the new outline object
  1133. * @param actionRef the action reference string to be placed after the /A
  1134. * @param showSubItems whether to initially display child outline items
  1135. * @return the new PDF outline object
  1136. */
  1137. public PDFOutline makeOutline(PDFOutline parent, String label,
  1138. String actionRef, boolean showSubItems) {
  1139. PDFOutline pdfOutline = new PDFOutline(label, actionRef, showSubItems);
  1140. if (parent != null) {
  1141. parent.addOutline(pdfOutline);
  1142. }
  1143. getDocument().registerObject(pdfOutline);
  1144. return pdfOutline;
  1145. }
  1146. /**
  1147. * Make an outline object and add it to the given parent
  1148. *
  1149. * @param parent the parent PDFOutline object (may be null)
  1150. * @param label the title for the new outline object
  1151. * @param pdfAction the action that this outline item points to - must not be null!
  1152. * @param showSubItems whether to initially display child outline items
  1153. * @return the new PDFOutline object, or null if pdfAction is null
  1154. */
  1155. public PDFOutline makeOutline(PDFOutline parent, String label,
  1156. PDFAction pdfAction, boolean showSubItems) {
  1157. return pdfAction == null
  1158. ? null
  1159. : makeOutline(parent, label, pdfAction.getAction(), showSubItems);
  1160. }
  1161. // This one is obsolete now, at least it isn't called from anywhere inside FOP
  1162. /**
  1163. * Make an outline object and add it to the given outline
  1164. *
  1165. * @param parent parent PDFOutline object which may be null
  1166. * @param label the title for the new outline object
  1167. * @param destination the reference string for the action to go to
  1168. * @param yoffset the yoffset on the destination page
  1169. * @param showSubItems whether to initially display child outline items
  1170. * @return the new PDF outline object
  1171. */
  1172. public PDFOutline makeOutline(PDFOutline parent, String label,
  1173. String destination, float yoffset,
  1174. boolean showSubItems) {
  1175. String goToRef = getGoToReference(destination, yoffset);
  1176. return makeOutline(parent, label, goToRef, showSubItems);
  1177. }
  1178. /* ========================= fonts ===================================== */
  1179. /**
  1180. * make a /Encoding object
  1181. *
  1182. * @param encodingName character encoding scheme name
  1183. * @return the created /Encoding object
  1184. */
  1185. public PDFEncoding makeEncoding(String encodingName) {
  1186. PDFEncoding encoding = new PDFEncoding(encodingName);
  1187. getDocument().registerObject(encoding);
  1188. return encoding;
  1189. }
  1190. /**
  1191. * Make a Type1 /Font object.
  1192. *
  1193. * @param fontname internal name to use for this font (eg "F1")
  1194. * @param basefont name of the base font (eg "Helvetica")
  1195. * @param encoding character encoding scheme used by the font
  1196. * @param metrics additional information about the font
  1197. * @param descriptor additional information about the font
  1198. * @return the created /Font object
  1199. */
  1200. public PDFFont makeFont(String fontname, String basefont,
  1201. String encoding, FontMetrics metrics,
  1202. FontDescriptor descriptor) {
  1203. PDFFont preRegisteredfont = getDocument().findFont(fontname);
  1204. if (preRegisteredfont != null) {
  1205. return preRegisteredfont;
  1206. }
  1207. boolean forceToUnicode = true;
  1208. if (descriptor == null) {
  1209. //Usually Base 14 fonts
  1210. PDFFont font = new PDFFont(fontname, FontType.TYPE1, basefont, encoding);
  1211. getDocument().registerObject(font);
  1212. if (forceToUnicode && !PDFEncoding.isPredefinedEncoding(encoding)) {
  1213. SingleByteEncoding mapping;
  1214. if (encoding != null) {
  1215. mapping = CodePointMapping.getMapping(encoding);
  1216. } else {
  1217. //for Symbol and ZapfDingbats where encoding must be null in PDF
  1218. Typeface tf = (Typeface)metrics;
  1219. mapping = CodePointMapping.getMapping(tf.getEncodingName());
  1220. }
  1221. generateToUnicodeCmap(font, mapping);
  1222. }
  1223. return font;
  1224. } else {
  1225. FontType fonttype = metrics.getFontType();
  1226. String fontPrefix = descriptor.isSubsetEmbedded() ? createSubsetFontPrefix() : "";
  1227. String subsetFontName = fontPrefix + basefont;
  1228. PDFFontDescriptor pdfdesc = makeFontDescriptor(descriptor, fontPrefix);
  1229. PDFFont font = null;
  1230. font = PDFFont.createFont(fontname, fonttype, subsetFontName, null);
  1231. getDocument().registerObject(font);
  1232. if (fonttype == FontType.TYPE0) {
  1233. font.setEncoding(encoding);
  1234. CIDFont cidMetrics;
  1235. if (metrics instanceof LazyFont) {
  1236. cidMetrics = (CIDFont)((LazyFont) metrics).getRealFont();
  1237. } else {
  1238. cidMetrics = (CIDFont)metrics;
  1239. }
  1240. PDFCIDSystemInfo sysInfo = new PDFCIDSystemInfo(cidMetrics.getRegistry(),
  1241. cidMetrics.getOrdering(), cidMetrics.getSupplement());
  1242. PDFCIDFont cidFont = new PDFCIDFont(subsetFontName, cidMetrics.getCIDType(),
  1243. cidMetrics.getDefaultWidth(), getFontWidths(cidMetrics), sysInfo,
  1244. (PDFCIDFontDescriptor) pdfdesc);
  1245. getDocument().registerObject(cidFont);
  1246. PDFCMap cmap = new PDFToUnicodeCMap(cidMetrics.getCIDSet().getChars(), "fop-ucs-H",
  1247. new PDFCIDSystemInfo("Adobe", "Identity", 0), false);
  1248. getDocument().registerObject(cmap);
  1249. ((PDFFontType0)font).setCMAP(cmap);
  1250. ((PDFFontType0)font).setDescendantFonts(cidFont);
  1251. } else {
  1252. PDFFontNonBase14 nonBase14 = (PDFFontNonBase14)font;
  1253. nonBase14.setDescriptor(pdfdesc);
  1254. SingleByteFont singleByteFont;
  1255. if (metrics instanceof LazyFont) {
  1256. singleByteFont = (SingleByteFont)((LazyFont)metrics).getRealFont();
  1257. } else {
  1258. singleByteFont = (SingleByteFont)metrics;
  1259. }
  1260. int firstChar = singleByteFont.getFirstChar();
  1261. int lastChar = singleByteFont.getLastChar();
  1262. nonBase14.setWidthMetrics(firstChar,
  1263. lastChar,
  1264. new PDFArray(null, metrics.getWidths()));
  1265. //Handle encoding
  1266. SingleByteEncoding mapping = singleByteFont.getEncoding();
  1267. if (singleByteFont.isSymbolicFont()) {
  1268. //no encoding, use the font's encoding
  1269. if (forceToUnicode) {
  1270. generateToUnicodeCmap(nonBase14, mapping);
  1271. }
  1272. } else if (PDFEncoding.isPredefinedEncoding(mapping.getName())) {
  1273. font.setEncoding(mapping.getName());
  1274. //No ToUnicode CMap necessary if PDF 1.4, chapter 5.9 (page 368) is to be
  1275. //believed.
  1276. } else {
  1277. Object pdfEncoding = createPDFEncoding(mapping,
  1278. singleByteFont.getFontName());
  1279. if (pdfEncoding instanceof PDFEncoding) {
  1280. font.setEncoding((PDFEncoding)pdfEncoding);
  1281. } else {
  1282. font.setEncoding((String)pdfEncoding);
  1283. }
  1284. if (forceToUnicode) {
  1285. generateToUnicodeCmap(nonBase14, mapping);
  1286. }
  1287. }
  1288. //Handle additional encodings (characters outside the primary encoding)
  1289. if (singleByteFont.hasAdditionalEncodings()) {
  1290. for (int i = 0, c = singleByteFont.getAdditionalEncodingCount(); i < c; i++) {
  1291. SimpleSingleByteEncoding addEncoding
  1292. = singleByteFont.getAdditionalEncoding(i);
  1293. String name = fontname + "_" + (i + 1);
  1294. Object pdfenc = createPDFEncoding(addEncoding,
  1295. singleByteFont.getFontName());
  1296. PDFFontNonBase14 addFont = (PDFFontNonBase14)PDFFont.createFont(
  1297. name, fonttype,
  1298. basefont, pdfenc);
  1299. addFont.setDescriptor(pdfdesc);
  1300. addFont.setWidthMetrics(
  1301. addEncoding.getFirstChar(),
  1302. addEncoding.getLastChar(),
  1303. new PDFArray(null, singleByteFont.getAdditionalWidths(i)));
  1304. getDocument().registerObject(addFont);
  1305. getDocument().getResources().addFont(addFont);
  1306. if (forceToUnicode) {
  1307. generateToUnicodeCmap(addFont, addEncoding);
  1308. }
  1309. }
  1310. }
  1311. }
  1312. return font;
  1313. }
  1314. }
  1315. private void generateToUnicodeCmap(PDFFont font, SingleByteEncoding encoding) {
  1316. PDFCMap cmap = new PDFToUnicodeCMap(encoding.getUnicodeCharMap(),
  1317. "fop-ucs-H",
  1318. new PDFCIDSystemInfo("Adobe", "Identity", 0), true);
  1319. getDocument().registerObject(cmap);
  1320. font.setToUnicode(cmap);
  1321. }
  1322. /**
  1323. * Creates a PDFEncoding instance from a CodePointMapping instance.
  1324. * @param encoding the code point mapping (encoding)
  1325. * @param fontName ...
  1326. * @return the PDF Encoding dictionary (or a String with the predefined encoding)
  1327. */
  1328. public Object createPDFEncoding(SingleByteEncoding encoding, String fontName) {
  1329. return PDFEncoding.createPDFEncoding(encoding, fontName);
  1330. }
  1331. private PDFWArray getFontWidths(CIDFont cidFont) {
  1332. // Create widths for reencoded chars
  1333. PDFWArray warray = new PDFWArray();
  1334. int[] widths = cidFont.getCIDSet().getWidths();
  1335. warray.addEntry(0, widths);
  1336. return warray;
  1337. }
  1338. private String createSubsetFontPrefix() {
  1339. subsetFontCounter++;
  1340. DecimalFormat counterFormat = new DecimalFormat("00000");
  1341. String counterString = counterFormat.format(subsetFontCounter);
  1342. // Subset prefix as described in chapter 5.5.3 of PDF 1.4
  1343. StringBuffer sb = new StringBuffer("E");
  1344. for (char c : counterString.toCharArray()) {
  1345. // translate numbers to uppercase characters
  1346. sb.append((char) (c + ('A' - '0')));
  1347. }
  1348. sb.append("+");
  1349. return sb.toString();
  1350. }
  1351. /**
  1352. * make a /FontDescriptor object
  1353. *
  1354. * @param desc the font descriptor
  1355. * @param fontPrefix the String with which to prefix the font name
  1356. * @return the new PDF font descriptor
  1357. */
  1358. private PDFFontDescriptor makeFontDescriptor(FontDescriptor desc, String fontPrefix) {
  1359. PDFFontDescriptor descriptor = null;
  1360. if (desc.getFontType() == FontType.TYPE0) {
  1361. // CID Font
  1362. descriptor = new PDFCIDFontDescriptor(fontPrefix + desc.getEmbedFontName(),
  1363. desc.getFontBBox(),
  1364. desc.getCapHeight(),
  1365. desc.getFlags(),
  1366. desc.getItalicAngle(),
  1367. desc.getStemV(), null);
  1368. } else {
  1369. // Create normal FontDescriptor
  1370. descriptor = new PDFFontDescriptor(desc.getEmbedFontName(),
  1371. desc.getAscender(),
  1372. desc.getDescender(),
  1373. desc.getCapHeight(),
  1374. desc.getFlags(),
  1375. new PDFRectangle(desc.getFontBBox()),
  1376. desc.getItalicAngle(),
  1377. desc.getStemV());
  1378. }
  1379. getDocument().registerObject(descriptor);
  1380. // Check if the font is embeddable
  1381. if (desc.isEmbeddable()) {
  1382. AbstractPDFStream stream = makeFontFile(desc);
  1383. if (stream != null) {
  1384. descriptor.setFontFile(desc.getFontType(), stream);
  1385. getDocument().registerObject(stream);
  1386. }
  1387. CustomFont font = getCustomFont(desc);
  1388. if (font instanceof CIDFont) {
  1389. CIDFont cidFont = (CIDFont)font;
  1390. buildCIDSet(descriptor, cidFont);
  1391. }
  1392. }
  1393. return descriptor;
  1394. }
  1395. private void buildCIDSet(PDFFontDescriptor descriptor, CIDFont cidFont) {
  1396. BitSet cidSet = cidFont.getCIDSet().getGlyphIndices();
  1397. PDFStream pdfStream = makeStream(null, true);
  1398. ByteArrayOutputStream baout = new ByteArrayOutputStream(cidSet.length() / 8 + 1);
  1399. int value = 0;
  1400. for (int i = 0, c = cidSet.length(); i < c; i++) {
  1401. int shift = i % 8;
  1402. boolean b = cidSet.get(i);
  1403. if (b) {
  1404. value |= 1 << 7 - shift;
  1405. }
  1406. if (shift == 7) {
  1407. baout.write(value);
  1408. value = 0;
  1409. }
  1410. }
  1411. baout.write(value);
  1412. try {
  1413. pdfStream.setData(baout.toByteArray());
  1414. descriptor.setCIDSet(pdfStream);
  1415. } catch (IOException ioe) {
  1416. log.error(
  1417. "Failed to write CIDSet [" + cidFont + "] "
  1418. + cidFont.getEmbedFontName(), ioe);
  1419. }
  1420. }
  1421. /**
  1422. * Embeds a font.
  1423. * @param desc FontDescriptor of the font.
  1424. * @return PDFStream The embedded font file
  1425. */
  1426. public AbstractPDFStream makeFontFile(FontDescriptor desc) {
  1427. if (desc.getFontType() == FontType.OTHER) {
  1428. throw new IllegalArgumentException("Trying to embed unsupported font type: "
  1429. + desc.getFontType());
  1430. }
  1431. CustomFont font = getCustomFont(desc);
  1432. InputStream in = null;
  1433. try {
  1434. in = font.getInputStream();
  1435. if (in == null) {
  1436. return null;
  1437. } else {
  1438. AbstractPDFStream embeddedFont;
  1439. if (desc.getFontType() == FontType.TYPE0) {
  1440. MultiByteFont mbfont = (MultiByteFont) font;
  1441. FontFileReader reader = new FontFileReader(in);
  1442. byte[] fontBytes;
  1443. if (font.getEmbeddingMode() == EmbeddingMode.FULL) {
  1444. fontBytes = reader.getAllBytes();
  1445. } else {
  1446. TTFSubSetFile ttfFile = new TTFSubSetFile();
  1447. ttfFile.readFont(reader, mbfont.getTTCName(), mbfont.getUsedGlyphs());
  1448. fontBytes = ttfFile.getFontSubset();
  1449. }
  1450. embeddedFont = new PDFTTFStream(fontBytes.length);
  1451. ((PDFTTFStream) embeddedFont).setData(fontBytes, fontBytes.length);
  1452. } else if (desc.getFontType() == FontType.TYPE1) {
  1453. PFBParser parser = new PFBParser();
  1454. PFBData pfb = parser.parsePFB(in);
  1455. embeddedFont = new PDFT1Stream();
  1456. ((PDFT1Stream) embeddedFont).setData(pfb);
  1457. } else {
  1458. byte[] file = IOUtils.toByteArray(in);
  1459. embeddedFont = new PDFTTFStream(file.length);
  1460. ((PDFTTFStream) embeddedFont).setData(file, file.length);
  1461. }
  1462. /*
  1463. embeddedFont.getFilterList().addFilter("flate");
  1464. if (getDocument().isEncryptionActive()) {
  1465. getDocument().applyEncryption(embeddedFont);
  1466. } else {
  1467. embeddedFont.getFilterList().addFilter("ascii-85");
  1468. }*/
  1469. return embeddedFont;
  1470. }
  1471. } catch (IOException ioe) {
  1472. log.error("Failed to embed font [" + desc + "] " + desc.getEmbedFontName(), ioe);
  1473. return null;
  1474. } finally {
  1475. IOUtils.closeQuietly(in);
  1476. }
  1477. }
  1478. private CustomFont getCustomFont(FontDescriptor desc) {
  1479. Typeface tempFont;
  1480. if (desc instanceof LazyFont) {
  1481. tempFont = ((LazyFont)desc).getRealFont();
  1482. } else {
  1483. tempFont = (Typeface)desc;
  1484. }
  1485. if (!(tempFont instanceof CustomFont)) {
  1486. throw new IllegalArgumentException(
  1487. "FontDescriptor must be instance of CustomFont, but is a "
  1488. + desc.getClass().getName());
  1489. }
  1490. return (CustomFont)tempFont;
  1491. }
  1492. /* ========================= streams =================================== */
  1493. /**
  1494. * Make a stream object
  1495. *
  1496. * @param type the type of stream to be created
  1497. * @param add if true then the stream will be added immediately
  1498. * @return the stream object created
  1499. */
  1500. public PDFStream makeStream(String type, boolean add) {
  1501. // create a PDFStream with the next object number
  1502. // and add it to the list of objects
  1503. PDFStream obj = new PDFStream();
  1504. obj.setDocument(getDocument());
  1505. obj.getFilterList().addDefaultFilters(
  1506. getDocument().getFilterMap(),
  1507. type);
  1508. if (add) {
  1509. getDocument().registerObject(obj);
  1510. }
  1511. //getDocument().applyEncryption(obj);
  1512. return obj;
  1513. }
  1514. /**
  1515. * Create a PDFICCStream
  1516. * @see PDFImageXObject
  1517. * @see org.apache.fop.pdf.PDFDeviceColorSpace
  1518. * @return the new PDF ICC stream object
  1519. */
  1520. public PDFICCStream makePDFICCStream() {
  1521. PDFICCStream iccStream = new PDFICCStream();
  1522. getDocument().registerObject(iccStream);
  1523. //getDocument().applyEncryption(iccStream);
  1524. return iccStream;
  1525. }
  1526. /* ========================= misc. objects ============================= */
  1527. /**
  1528. * Makes a new ICCBased color space and registers it in the resource context.
  1529. * @param res the PDF resource context to add the shading, may be null
  1530. * @param explicitName the explicit name for the color space, may be null
  1531. * @param iccStream the ICC stream to associate with this color space
  1532. * @return the newly instantiated color space
  1533. */
  1534. public PDFICCBasedColorSpace makeICCBasedColorSpace(PDFResourceContext res,
  1535. String explicitName, PDFICCStream iccStream) {
  1536. PDFICCBasedColorSpace cs = new PDFICCBasedColorSpace(explicitName, iccStream);
  1537. getDocument().registerObject(cs);
  1538. if (res != null) {
  1539. res.getPDFResources().addColorSpace(cs);
  1540. } else {
  1541. getDocument().getResources().addColorSpace(cs);
  1542. }
  1543. return cs;
  1544. }
  1545. /**
  1546. * Create a new Separation color space.
  1547. * @param res the resource context (may be null)
  1548. * @param ncs the named color space to map to a separation color space
  1549. * @return the newly created Separation color space
  1550. */
  1551. public PDFSeparationColorSpace makeSeparationColorSpace(PDFResourceContext res,
  1552. NamedColorSpace ncs) {
  1553. String colorName = ncs.getColorName();
  1554. final Double zero = new Double(0d);
  1555. final Double one = new Double(1d);
  1556. List theDomain = Arrays.asList(new Double[] {zero, one});
  1557. List theRange = Arrays.asList(new Double[] {zero, one, zero, one, zero, one});
  1558. List theCZero = Arrays.asList(new Double[] {one, one, one});
  1559. List theCOne = new ArrayList();
  1560. float[] comps = ncs.getRGBColor().getColorComponents(null);
  1561. for (int i = 0, c = comps.length; i < c; i++) {
  1562. theCOne.add(new Double(comps[i]));
  1563. }
  1564. PDFFunction tintFunction = makeFunction(2, theDomain, theRange,
  1565. theCZero, theCOne, 1.0d);
  1566. PDFSeparationColorSpace cs = new PDFSeparationColorSpace(colorName, tintFunction);
  1567. getDocument().registerObject(cs);
  1568. if (res != null) {
  1569. res.getPDFResources().addColorSpace(cs);
  1570. } else {
  1571. getDocument().getResources().addColorSpace(cs);
  1572. }
  1573. return cs;
  1574. }
  1575. /**
  1576. * Make an Array object (ex. Widths array for a font).
  1577. *
  1578. * @param values the int array values
  1579. * @return the PDF Array with the int values
  1580. */
  1581. public PDFArray makeArray(int[] values) {
  1582. PDFArray array = new PDFArray(null, values);
  1583. getDocument().registerObject(array);
  1584. return array;
  1585. }
  1586. /**
  1587. * make an ExtGState for extra graphics options
  1588. * This tries to find a GState that will setup the correct values
  1589. * for the current context. If there is no suitable GState it will
  1590. * create a new one.
  1591. *
  1592. * @param settings the settings required by the caller
  1593. * @param current the current GState of the current PDF context
  1594. * @return a PDF GState, either an existing GState or a new one
  1595. */
  1596. public PDFGState makeGState(Map settings, PDFGState current) {
  1597. // try to locate a gstate that has all the settings
  1598. // or will inherit from the current gstate
  1599. // compare "DEFAULT + settings" with "current + each gstate"
  1600. PDFGState wanted = new PDFGState();
  1601. wanted.addValues(PDFGState.DEFAULT);
  1602. wanted.addValues(settings);
  1603. PDFGState existing = getDocument().findGState(wanted, current);
  1604. if (existing != null) {
  1605. return existing;
  1606. }
  1607. PDFGState gstate = new PDFGState();
  1608. gstate.addValues(settings);
  1609. getDocument().registerObject(gstate);
  1610. return gstate;
  1611. }
  1612. /**
  1613. * Make an annotation list object
  1614. *
  1615. * @return the annotation list object created
  1616. */
  1617. public PDFAnnotList makeAnnotList() {
  1618. PDFAnnotList obj = new PDFAnnotList();
  1619. getDocument().assignObjectNumber(obj);
  1620. return obj;
  1621. }
  1622. }