PageRenderTime 56ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/thirdparties-extension/org.apache.poi.xwpf.converter/src/main/java/org/apache/poi/xwpf/converter/internal/itext/PDFMapper.java

https://code.google.com/
Java | 638 lines | 483 code | 83 blank | 72 comment | 83 complexity | c57703eb8d21657af4b974b6eb8d10f0 MD5 | raw file
  1. /**
  2. * Copyright (C) 2011 Angelo Zerr <angelo.zerr@gmail.com> and Pascal Leclercq <pascal.leclercq@gmail.com>
  3. *
  4. * All rights reserved.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining
  7. * a copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sublicense, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be
  15. * included in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  21. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  22. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  23. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. */
  25. package org.apache.poi.xwpf.converter.internal.itext;
  26. import static org.apache.poi.xwpf.converter.internal.DxaUtil.dxa2points;
  27. import static org.apache.poi.xwpf.converter.internal.XWPFRunUtils.getFontColor;
  28. import static org.apache.poi.xwpf.converter.internal.XWPFRunUtils.getFontFamily;
  29. import static org.apache.poi.xwpf.converter.internal.XWPFRunUtils.getRStyle;
  30. import static org.apache.poi.xwpf.converter.internal.XWPFRunUtils.isBold;
  31. import static org.apache.poi.xwpf.converter.internal.XWPFRunUtils.isItalic;
  32. import static org.apache.poi.xwpf.converter.internal.XWPFUtils.getRPr;
  33. import static org.apache.poi.xwpf.converter.internal.itext.XWPFTableUtil.setBorder;
  34. import java.awt.Color;
  35. import java.io.OutputStream;
  36. import java.util.List;
  37. import java.util.Stack;
  38. import java.util.logging.Logger;
  39. import org.apache.poi.xwpf.converter.internal.XWPFElementVisitor;
  40. import org.apache.poi.xwpf.converter.internal.itext.stylable.IStylableContainer;
  41. import org.apache.poi.xwpf.converter.internal.itext.stylable.IStylableElement;
  42. import org.apache.poi.xwpf.converter.internal.itext.stylable.StylableDocument;
  43. import org.apache.poi.xwpf.converter.internal.itext.stylable.StylableParagraph;
  44. import org.apache.poi.xwpf.converter.internal.itext.stylable.StylableTable;
  45. import org.apache.poi.xwpf.converter.internal.itext.styles.Style;
  46. import org.apache.poi.xwpf.converter.internal.itext.styles.StyleBorder;
  47. import org.apache.poi.xwpf.converter.itext.PDFViaITextOptions;
  48. import org.apache.poi.xwpf.usermodel.BodyElementType;
  49. import org.apache.poi.xwpf.usermodel.IBodyElement;
  50. import org.apache.poi.xwpf.usermodel.UnderlinePatterns;
  51. import org.apache.poi.xwpf.usermodel.XWPFDocument;
  52. import org.apache.poi.xwpf.usermodel.XWPFFooter;
  53. import org.apache.poi.xwpf.usermodel.XWPFHeader;
  54. import org.apache.poi.xwpf.usermodel.XWPFParagraph;
  55. import org.apache.poi.xwpf.usermodel.XWPFPicture;
  56. import org.apache.poi.xwpf.usermodel.XWPFPictureData;
  57. import org.apache.poi.xwpf.usermodel.XWPFRun;
  58. import org.apache.poi.xwpf.usermodel.XWPFStyle;
  59. import org.apache.poi.xwpf.usermodel.XWPFTable;
  60. import org.apache.poi.xwpf.usermodel.XWPFTableCell;
  61. import org.apache.poi.xwpf.usermodel.XWPFTableRow;
  62. import org.openxmlformats.schemas.drawingml.x2006.main.CTPositiveSize2D;
  63. import org.openxmlformats.schemas.drawingml.x2006.picture.CTPicture;
  64. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBorder;
  65. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBr;
  66. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDecimalNumber;
  67. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTEmpty;
  68. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHdrFtrRef;
  69. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTOnOff;
  70. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPPr;
  71. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPageMar;
  72. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPageSz;
  73. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR;
  74. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRPr;
  75. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSectPr;
  76. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTShd;
  77. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTString;
  78. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcBorders;
  79. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr;
  80. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText;
  81. import org.openxmlformats.schemas.wordprocessingml.x2006.main.STHdrFtr;
  82. import org.openxmlformats.schemas.wordprocessingml.x2006.main.STHexColor;
  83. import org.openxmlformats.schemas.wordprocessingml.x2006.main.STOnOff;
  84. import com.lowagie.text.Chunk;
  85. import com.lowagie.text.Element;
  86. import com.lowagie.text.Font;
  87. import com.lowagie.text.Image;
  88. import com.lowagie.text.Rectangle;
  89. import com.lowagie.text.pdf.PdfPCell;
  90. import fr.opensagres.xdocreport.itext.extension.ExtendedParagraph;
  91. import fr.opensagres.xdocreport.itext.extension.ExtendedPdfPCell;
  92. import fr.opensagres.xdocreport.itext.extension.ExtendedPdfPTable;
  93. import fr.opensagres.xdocreport.itext.extension.IITextContainer;
  94. import fr.opensagres.xdocreport.itext.extension.MasterPage;
  95. import fr.opensagres.xdocreport.itext.extension.MasterPageHeaderFooter;
  96. import fr.opensagres.xdocreport.itext.extension.PageOrientation;
  97. import fr.opensagres.xdocreport.utils.BorderType;
  98. import fr.opensagres.xdocreport.utils.StringUtils;
  99. public class PDFMapper
  100. extends XWPFElementVisitor<IITextContainer>
  101. {
  102. /**
  103. * Logger for this class
  104. */
  105. private static final Logger LOGGER = Logger.getLogger( XWPFElementVisitor.class.getName() );
  106. // Create instance of PDF document
  107. private StylableDocument pdfDocument;
  108. private Stack<CTSectPr> sectPrStack = null;
  109. private StyleEngineForIText styleEngine;
  110. private final PDFViaITextOptions options;
  111. public PDFMapper( XWPFDocument document, PDFViaITextOptions options )
  112. {
  113. super( document );
  114. this.options = options != null ? options : PDFViaITextOptions.create();
  115. }
  116. @Override
  117. protected IITextContainer startVisitDocument( OutputStream out )
  118. throws Exception
  119. {
  120. // Create instance of PDF document
  121. styleEngine = new StyleEngineForIText( document, options );
  122. pdfDocument = new StylableDocument( out, styleEngine );
  123. CTSectPr sectPr = document.getDocument().getBody().getSectPr();
  124. applySectPr( sectPr );
  125. return pdfDocument;
  126. }
  127. private void applySectPr( CTSectPr sectPr )
  128. {
  129. if ( sectPr == null )
  130. {
  131. return;
  132. }
  133. // Set page size
  134. CTPageSz pageSize = sectPr.getPgSz();
  135. Rectangle pdfPageSize = new Rectangle( dxa2points( pageSize.getW() ), dxa2points( pageSize.getH() ) );
  136. pdfDocument.setPageSize( pdfPageSize );
  137. // Orientation
  138. org.openxmlformats.schemas.wordprocessingml.x2006.main.STPageOrientation.Enum orientation =
  139. pageSize.getOrient();
  140. if ( orientation != null )
  141. {
  142. if ( org.openxmlformats.schemas.wordprocessingml.x2006.main.STPageOrientation.LANDSCAPE.equals( orientation ) )
  143. {
  144. pdfDocument.setOrientation( PageOrientation.Landscape );
  145. }
  146. else
  147. {
  148. pdfDocument.setOrientation( PageOrientation.Portrait );
  149. }
  150. }
  151. // Set page margin
  152. CTPageMar pageMar = sectPr.getPgMar();
  153. if ( pageMar != null )
  154. {
  155. pdfDocument.setOriginalMargins( dxa2points( pageMar.getLeft() ), dxa2points( pageMar.getRight() ),
  156. dxa2points( pageMar.getTop() ), dxa2points( pageMar.getBottom() ) );
  157. }
  158. }
  159. @Override
  160. protected void endVisitDocument()
  161. throws Exception
  162. {
  163. pdfDocument.close();
  164. }
  165. @Override
  166. protected void visitHeader( CTHdrFtrRef headerRef )
  167. throws Exception
  168. {
  169. STHdrFtr.Enum type = headerRef.getType();
  170. MasterPage masterPage = getOrCreateMasterPage( type );
  171. MasterPageHeaderFooter pdfHeader = new MasterPageHeaderFooter();
  172. XWPFHeader hdr = getXWPFHeader( headerRef );
  173. visitBodyElements( hdr.getBodyElements(), (ExtendedPdfPCell) pdfHeader.getTableCell() );
  174. pdfHeader.flush();
  175. masterPage.setHeader( pdfHeader );
  176. }
  177. @Override
  178. protected void visitFooter( CTHdrFtrRef footerRef )
  179. throws Exception
  180. {
  181. STHdrFtr.Enum type = footerRef.getType();
  182. MasterPage masterPage = getOrCreateMasterPage( type );
  183. MasterPageHeaderFooter pdfFooter = new MasterPageHeaderFooter();
  184. XWPFFooter hdr = getXWPFFooter( footerRef );
  185. visitBodyElements( hdr.getBodyElements(), (ExtendedPdfPCell) pdfFooter.getTableCell() );
  186. pdfFooter.flush();
  187. masterPage.setFooter( pdfFooter );
  188. }
  189. private MasterPage getOrCreateMasterPage( STHdrFtr.Enum type )
  190. {
  191. String masterPageName = type.toString();
  192. MasterPage masterPage = pdfDocument.getMasterPage( masterPageName );
  193. if ( masterPage == null )
  194. {
  195. masterPage = new MasterPage( masterPageName );
  196. pdfDocument.addMasterPage( masterPage );
  197. }
  198. return masterPage;
  199. }
  200. private Stack<CTSectPr> getSectPrStack()
  201. {
  202. if ( sectPrStack != null )
  203. {
  204. return sectPrStack;
  205. }
  206. sectPrStack = new Stack<CTSectPr>();
  207. for ( IBodyElement bodyElement : document.getBodyElements() )
  208. {
  209. if ( bodyElement.getElementType() == BodyElementType.PARAGRAPH )
  210. {
  211. CTPPr ppr = ( (XWPFParagraph) bodyElement ).getCTP().getPPr();
  212. if ( ppr != null )
  213. {
  214. CTSectPr sectPr = ppr.getSectPr();
  215. if ( sectPr != null )
  216. {
  217. sectPrStack.push( sectPr );
  218. }
  219. }
  220. }
  221. }
  222. return sectPrStack;
  223. }
  224. @Override
  225. protected IITextContainer startVisitPargraph( XWPFParagraph docxParagraph, IITextContainer parentContainer )
  226. throws Exception
  227. {
  228. // 1) Instanciate a pdfParagraph
  229. StylableParagraph pdfParagraph = pdfDocument.createParagraph( (IStylableContainer) null );
  230. // apply style for the title font, color, bold style...
  231. // 2) Create style instance of the paragraph if needed
  232. styleEngine.startVisitPargraph( docxParagraph, pdfParagraph );
  233. pdfParagraph.setITextContainer( parentContainer );
  234. // TODO
  235. String backgroundColor = XWPFParagraphUtils.getBackgroundColor( docxParagraph );
  236. if ( StringUtils.isNotEmpty( backgroundColor ) )
  237. {
  238. pdfParagraph.getPdfPCell().setBackgroundColor( ColorRegistry.getInstance().getColor( "0x" + backgroundColor ) );
  239. }
  240. // finally apply the style to the iText paragraph....
  241. applyStyles( docxParagraph, pdfParagraph );
  242. return pdfParagraph;
  243. }
  244. @Override
  245. protected void endVisitPargraph( XWPFParagraph paragraph, IITextContainer parentContainer,
  246. IITextContainer paragraphContainer )
  247. throws Exception
  248. {
  249. // Page Break
  250. // Cannot use paragraph.isPageBreak() because it throw NPE because
  251. // pageBreak.getVal() can be null.
  252. CTPPr ppr = paragraph.getCTP().getPPr();
  253. if ( ppr.isSetPageBreakBefore() )
  254. {
  255. CTOnOff pageBreak = ppr.getPageBreakBefore();
  256. if ( pageBreak != null
  257. && ( pageBreak.getVal() == null || pageBreak.getVal().intValue() == STOnOff.INT_TRUE ) )
  258. {
  259. pdfDocument.newPage();
  260. }
  261. }
  262. // Paragraph
  263. ExtendedParagraph pdfParagraph = (ExtendedParagraph) paragraphContainer;
  264. parentContainer.addElement( pdfParagraph.getContainer() );
  265. }
  266. @Override
  267. protected void visitEmptyRun( IITextContainer paragraphContainer )
  268. throws Exception
  269. {
  270. ExtendedParagraph pdfParagraph = (ExtendedParagraph) paragraphContainer;
  271. pdfParagraph.add( Chunk.NEWLINE );
  272. }
  273. @Override
  274. protected void visitRun( XWPFRun run, IITextContainer pdfContainer )
  275. throws Exception
  276. {
  277. CTR ctr = run.getCTR();
  278. // Get family name
  279. // Get CTRPr from style+defaults
  280. CTString rStyle = getRStyle( run );
  281. CTRPr runRprStyle = getRPr( super.getXWPFStyle( rStyle != null ? rStyle.getVal() : null ) );
  282. CTRPr rprStyle = getRPr( super.getXWPFStyle( run.getParagraph().getStyleID() ) );
  283. CTRPr rprDefault = getRPr( defaults );
  284. // Font family
  285. String fontFamily = getFontFamily( run, rprStyle, rprDefault );
  286. // Get font size
  287. float fontSize = run.getFontSize();
  288. // Get font style
  289. int fontStyle = Font.NORMAL;
  290. if ( isBold( run, runRprStyle, rprStyle, rprDefault ) )
  291. {
  292. fontStyle |= Font.BOLD;
  293. }
  294. if ( isItalic( run, runRprStyle, rprStyle, rprDefault ) )
  295. {
  296. fontStyle |= Font.ITALIC;
  297. }
  298. // Process color
  299. Color fontColor = null;
  300. String hexColor = getFontColor( run, runRprStyle, rprStyle, rprDefault );
  301. if ( StringUtils.isNotEmpty( hexColor ) )
  302. {
  303. if ( hexColor != null && !"auto".equals( hexColor ) )
  304. {
  305. fontColor = ColorRegistry.getInstance().getColor( "0x" + hexColor );
  306. }
  307. }
  308. // Get font
  309. Font font =
  310. XWPFFontRegistry.getRegistry().getFont( fontFamily, options.getFontEncoding(), fontSize, fontStyle,
  311. fontColor );
  312. UnderlinePatterns underlinePatterns = run.getUnderline();
  313. boolean singleUnderlined = false;
  314. switch ( underlinePatterns )
  315. {
  316. case SINGLE:
  317. singleUnderlined = true;
  318. break;
  319. default:
  320. break;
  321. }
  322. List<CTBr> brs = ctr.getBrList();
  323. for ( @SuppressWarnings( "unused" )
  324. CTBr br : brs )
  325. {
  326. pdfContainer.addElement( Chunk.NEWLINE );
  327. }
  328. List<CTText> texts = run.getCTR().getTList();
  329. for ( CTText ctText : texts )
  330. {
  331. Chunk aChunk = new Chunk( ctText.getStringValue(), font );
  332. if ( singleUnderlined )
  333. aChunk.setUnderline( 1, -2 );
  334. pdfContainer.addElement( aChunk );
  335. }
  336. super.visitPictures( run, pdfContainer );
  337. // <w:lastRenderedPageBreak />
  338. List<CTEmpty> lastRenderedPageBreakList = ctr.getLastRenderedPageBreakList();
  339. if ( lastRenderedPageBreakList != null && lastRenderedPageBreakList.size() > 0 )
  340. {
  341. // IText Document#newPage must be called to generate page break.
  342. // But before that, CTSectPr must be getted to compute pageSize,
  343. // margins...
  344. // The CTSectPr <w:pPr><w:sectPr w:rsidR="00AA33F7"
  345. // w:rsidSect="00607077"><w:pgSz w:w="16838" w:h="11906"
  346. // w:orient="landscape" />...
  347. Stack<CTSectPr> sectPrStack = getSectPrStack();
  348. if ( sectPrStack != null && !sectPrStack.isEmpty() )
  349. {
  350. CTSectPr sectPr = sectPrStack.pop();
  351. applySectPr( sectPr );
  352. }
  353. for ( CTEmpty lastRenderedPageBreak : lastRenderedPageBreakList )
  354. {
  355. pdfDocument.newPage();
  356. }
  357. }
  358. }
  359. // Visit table
  360. protected IITextContainer startVisitTable( XWPFTable table, IITextContainer pdfContainer )
  361. throws Exception
  362. {
  363. styleEngine.startVisitTable( table, pdfContainer );
  364. // 1) Compute colWidth
  365. float[] colWidths = XWPFTableUtil.computeColWidths( table );
  366. // 2) Compute tableWith
  367. TableWidth tableWidth = XWPFTableUtil.getTableWidth( table );
  368. StylableTable pdfPTable = pdfDocument.createTable( (IStylableContainer) null, colWidths.length );
  369. // 3) Create PDF Table.
  370. // ExtendedPdfPTable pdfPTable = new
  371. // ExtendedPdfPTable(colWidths.length);
  372. pdfPTable.setITextContainer( pdfContainer );
  373. pdfPTable.setTotalWidth( colWidths );
  374. if ( tableWidth.width > 0 )
  375. {
  376. if ( tableWidth.percentUnit )
  377. {
  378. pdfPTable.setWidthPercentage( tableWidth.width );
  379. }
  380. else
  381. {
  382. pdfPTable.setTotalWidth( tableWidth.width );
  383. }
  384. }
  385. if ( table.getCTTbl() != null )
  386. {
  387. if ( table.getCTTbl().getTblPr().getTblBorders() != null )
  388. {
  389. CTBorder bottom = table.getCTTbl().getTblPr().getTblBorders().getBottom();
  390. if ( bottom != null )
  391. {
  392. pdfPTable.setBorderBottom( createBorder( bottom, BorderType.BOTTOM ) );
  393. }
  394. CTBorder left = table.getCTTbl().getTblPr().getTblBorders().getLeft();
  395. if ( left != null )
  396. {
  397. pdfPTable.setBorderLeft( createBorder( left, BorderType.LEFT ) );
  398. }
  399. CTBorder top = table.getCTTbl().getTblPr().getTblBorders().getTop();
  400. if ( top != null )
  401. {
  402. pdfPTable.setBorderTop( createBorder( top, BorderType.TOP ) );
  403. }
  404. CTBorder right = table.getCTTbl().getTblPr().getTblBorders().getRight();
  405. if ( right != null )
  406. {
  407. pdfPTable.setBorderRight( createBorder( right, BorderType.RIGHT ) );
  408. }
  409. }
  410. }
  411. pdfPTable.setLockedWidth( true );
  412. // finally apply the style to the iText paragraph....
  413. applyStyles( table, pdfPTable );
  414. return pdfPTable;
  415. }
  416. private StyleBorder createBorder( CTBorder docxBorder, BorderType borderType )
  417. {
  418. if ( docxBorder == null )
  419. {
  420. return null;
  421. }
  422. StyleBorder styleBorder = new StyleBorder( docxBorder.getVal().toString(), borderType );
  423. // XXX semi point ?
  424. styleBorder.setWidth( docxBorder.getSz() );
  425. STHexColor hexColor = docxBorder.xgetColor();
  426. Color bc = ColorRegistry.getInstance().getColor( "0x" + hexColor.getStringValue() );
  427. styleBorder.setColor( bc );
  428. return styleBorder;
  429. }
  430. @Override
  431. protected void endVisitTable( XWPFTable table, IITextContainer parentContainer, IITextContainer tableContainer )
  432. throws Exception
  433. {
  434. parentContainer.addElement( (Element) tableContainer );
  435. }
  436. @Override
  437. protected IITextContainer startVisitTableCell( XWPFTableCell cell, IITextContainer tableContainer )
  438. {
  439. StylableTable pdfPTable = (StylableTable) tableContainer;
  440. XWPFTableRow row = cell.getTableRow();
  441. ExtendedPdfPCell pdfPCell = new ExtendedPdfPCell();
  442. pdfPCell.setITextContainer( pdfPTable );
  443. CTTcPr tcPr = cell.getCTTc().getTcPr();
  444. if ( tcPr != null )
  445. {
  446. // Colspan
  447. Integer colspan = null;
  448. CTDecimalNumber gridSpan = tcPr.getGridSpan();
  449. if ( gridSpan != null )
  450. {
  451. colspan = gridSpan.getVal().intValue();
  452. }
  453. if ( colspan != null )
  454. {
  455. pdfPCell.setColspan( colspan );
  456. }
  457. // Backround Color
  458. CTShd shd = tcPr.getShd();
  459. String hexColor = null;
  460. if ( shd != null )
  461. {
  462. hexColor = shd.xgetFill().getStringValue();
  463. }
  464. if ( hexColor != null && !"auto".equals( hexColor ) )
  465. {
  466. pdfPCell.setBackgroundColor( ColorRegistry.getInstance().getColor( "0x" + hexColor ) );
  467. }
  468. // Borders
  469. // Table Properties on cells
  470. // overridden locally
  471. CTTcBorders borders = tcPr.getTcBorders();
  472. if ( borders != null )
  473. {
  474. // border-left
  475. setBorder( borders.getLeft(), pdfPCell, Rectangle.LEFT );
  476. // border-right
  477. setBorder( borders.getRight(), pdfPCell, Rectangle.RIGHT );
  478. // border-top
  479. setBorder( borders.getTop(), pdfPCell, Rectangle.TOP );
  480. // border-bottom
  481. setBorder( borders.getBottom(), pdfPCell, Rectangle.BOTTOM );
  482. }
  483. }
  484. int height = row.getHeight();
  485. pdfPCell.setMinimumHeight( dxa2points( height ) );
  486. return pdfPCell;
  487. }
  488. @Override
  489. protected void endVisitTableCell( XWPFTableCell cell, IITextContainer tableContainer,
  490. IITextContainer tableCellContainer )
  491. {
  492. ExtendedPdfPTable pdfPTable = (ExtendedPdfPTable) tableContainer;
  493. ExtendedPdfPCell pdfPCell = (ExtendedPdfPCell) tableCellContainer;
  494. pdfPTable.addCell( pdfPCell );
  495. }
  496. @Override
  497. protected void visitPicture( XWPFPicture picture, IITextContainer parentContainer )
  498. throws Exception
  499. {
  500. CTPositiveSize2D ext = picture.getCTPicture().getSpPr().getXfrm().getExt();
  501. long x = ext.getCx();
  502. long y = ext.getCy();
  503. CTPicture ctPic = picture.getCTPicture();
  504. String blipId = ctPic.getBlipFill().getBlip().getEmbed();
  505. XWPFPictureData pictureData = XWPFPictureUtil.getPictureData( document, blipId );
  506. if ( pictureData != null )
  507. {
  508. try
  509. {
  510. Image img = Image.getInstance( pictureData.getData() );
  511. img.scaleAbsolute( dxa2points( x ) / 635, dxa2points( y ) / 635 );
  512. IITextContainer parentOfParentContainer = parentContainer.getITextContainer();
  513. if ( parentOfParentContainer != null && parentOfParentContainer instanceof PdfPCell )
  514. {
  515. ( (PdfPCell) parentOfParentContainer ).setImage( img );
  516. }
  517. else
  518. {
  519. parentContainer.addElement( img );
  520. }
  521. }
  522. catch ( Exception e )
  523. {
  524. LOGGER.severe( e.getMessage() );
  525. }
  526. }
  527. }
  528. private void applyStyles( XWPFParagraph ele, IStylableElement<XWPFParagraph> element )
  529. {
  530. Style style = styleEngine.getStyle( ele.getStyleID() );
  531. element.applyStyles( ele, style );
  532. }
  533. private void applyStyles( XWPFTable ele, IStylableElement<XWPFTable> element )
  534. {
  535. CTString tblStyle = ele.getCTTbl().getTblPr().getTblStyle();
  536. Style style;
  537. if ( tblStyle != null )
  538. {
  539. style = styleEngine.getStyle( tblStyle.getVal() );
  540. }
  541. else
  542. {
  543. style = styleEngine.getDefaultStyle();
  544. }
  545. element.applyStyles( ele, style );
  546. }
  547. protected XWPFStyle getXWPFStyle( XWPFParagraph paragraph )
  548. {
  549. if ( paragraph == null )
  550. {
  551. return null;
  552. }
  553. return getXWPFStyle( paragraph.getStyleID() );
  554. }
  555. }