您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

PDFRenderer.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * $Id$
  3. * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
  4. * For details on use and redistribution please refer to the
  5. * LICENSE file included with these sources.
  6. */
  7. package org.apache.fop.render.pdf;
  8. // FOP
  9. import org.apache.fop.render.PrintRenderer;
  10. import org.apache.fop.image.ImageArea;
  11. import org.apache.fop.image.FopImage;
  12. import org.apache.fop.apps.FOPException;
  13. import org.apache.fop.fo.properties.*;
  14. import org.apache.fop.datatypes.*;
  15. import org.apache.fop.pdf.*;
  16. import org.apache.fop.image.*;
  17. import org.apache.fop.extensions.*;
  18. import org.apache.fop.datatypes.IDReferences;
  19. import org.apache.fop.render.pdf.fonts.LazyFont;
  20. import org.apache.fop.area.*;
  21. import org.apache.fop.area.inline.*;
  22. import org.w3c.dom.Document;
  23. // Java
  24. import java.io.IOException;
  25. import java.io.OutputStream;
  26. import java.awt.geom.Rectangle2D;
  27. /*
  28. TODO:
  29. viewport clipping
  30. word rendering and optimistion
  31. pdf state optimistation
  32. line and border
  33. leader
  34. background pattern
  35. user agent xml (svg) rendering
  36. orientation
  37. writing mode
  38. text decoration
  39. */
  40. /**
  41. * Renderer that renders areas to PDF
  42. *
  43. */
  44. public class PDFRenderer extends PrintRenderer {
  45. /**
  46. * the PDF Document being created
  47. */
  48. protected PDFDocument pdfDoc;
  49. protected String producer;
  50. protected OutputStream ostream;
  51. /**
  52. * the /Resources object of the PDF document being created
  53. */
  54. protected PDFResources pdfResources;
  55. /**
  56. * the current stream to add PDF commands to
  57. */
  58. PDFStream currentStream;
  59. /**
  60. * the current annotation list to add annotations to
  61. */
  62. PDFAnnotList currentAnnotList;
  63. /**
  64. * the current page to add annotations to
  65. */
  66. PDFPage currentPage;
  67. PDFColor currentColor;
  68. /**
  69. * true if a TJ command is left to be written
  70. */
  71. boolean textOpen = false;
  72. /**
  73. * the previous Y coordinate of the last word written.
  74. * Used to decide if we can draw the next word on the same line.
  75. */
  76. int prevWordY = 0;
  77. /**
  78. * the previous X coordinate of the last word written.
  79. * used to calculate how much space between two words
  80. */
  81. int prevWordX = 0;
  82. /**
  83. * The width of the previous word. Used to calculate space between
  84. */
  85. int prevWordWidth = 0;
  86. /**
  87. * reusable word area string buffer to reduce memory usage
  88. */
  89. private StringBuffer _wordAreaPDF = new StringBuffer();
  90. /**
  91. * create the PDF renderer
  92. */
  93. public PDFRenderer() {
  94. }
  95. /**
  96. * set the PDF document's producer
  97. *
  98. * @param producer string indicating application producing PDF
  99. */
  100. public void setProducer(String prod) {
  101. producer = prod;
  102. }
  103. public void startRenderer(OutputStream stream) throws IOException {
  104. ostream = stream;
  105. this.pdfDoc = new PDFDocument();
  106. this.pdfDoc.setProducer(producer);
  107. pdfDoc.outputHeader(stream);
  108. }
  109. public void stopRenderer() throws IOException {
  110. FontSetup.addToResources(this.pdfDoc, fontInfo);
  111. pdfDoc.outputTrailer(ostream);
  112. this.pdfDoc = null;
  113. ostream = null;
  114. }
  115. /**
  116. * This method creates a pdf stream for the current page
  117. * uses it as the contents of a new page. The page is wriiten
  118. * immediately to the output stream.
  119. */
  120. public void renderPage(PageViewport page) throws IOException,
  121. FOPException {
  122. this.pdfResources = this.pdfDoc.getResources();
  123. currentStream = this.pdfDoc.makeStream();
  124. currentStream.add("BT\n");
  125. Page p = page.getPage();
  126. renderPageAreas(p);
  127. currentStream.add("ET\n");
  128. Rectangle2D bounds = page.getViewArea();
  129. double w = bounds.getWidth();
  130. double h = bounds.getHeight();
  131. currentPage = this.pdfDoc.makePage(this.pdfResources, currentStream,
  132. (int) Math.round(w / 1000), (int) Math.round(h / 1000));
  133. this.pdfDoc.output(ostream);
  134. }
  135. }