JasperReports页面上的水印

时间:2012-07-26 08:51:11

标签: jasper-reports reporting ireport

我们在网络应用程序中使用jasperReports和iReports来生成报告。

当我探索jasper报告时,我能够轻松地在报告中插入水印..

然而,我的目的是将水印字符串说(迈克尔杰克逊)放在页面上。

通过这些属性,我只能找到rotation option of left/right and upside down ...

是否可以在整个页面的报告中添加水印..

我正在使用ireport设计报告......

3 个答案:

答案 0 :(得分:5)

JasperReports本身不支持将文本旋转到90度,180度或270度以外的任何角度。解决方案是生成旋转文本的图像,然后在报告上呈现该图像。

this jasperforge thread的最后一篇文章中,用户“artduc”共享一个报告scriptlet,用于完成该操作。

答案 1 :(得分:2)

我只是在解释GenericJon建议的内容......

将图像元素添加到背景区域(设置见截图)。

实现Renderable并通过参数map将其传递给Jasper Reports:

InputStream jasperReportInputStream = getClass().getResourceAsStream("/reports/Test.jasper");  
JRBeanCollectionDataSource dataSource = ...    
Map parameters = new HashMap();
parameters.put("watermark", new WaterMarkRenderer(true);    
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReportInputStream, parameters, dataSource);

The WatermarkRenderer:

@SuppressWarnings("deprecation")
public class WaterMarkRenderer extends JRAbstractRenderer
{
  private boolean m_licenseTrial = false;

  public WaterMarkRenderer(boolean isLicenseTrial)
  {
    m_licenseTrial = isLicenseTrial;
  }

  @Override
  public byte getType()
  {
    // no idea what this does
    return RenderableTypeEnum.SVG.getValue();
  }

  @Override
  public byte getImageType()
  {
    // no idea what this does
    return ImageTypeEnum.UNKNOWN.getValue();
  }

  @Override
  public Dimension2D getDimension() throws JRException
  {
    // A4 in pixel: 595x842
    // this seems to override whatever is configured in jasperreports studio
    return new Dimension(595 - 2 * 40, 700);
  }

  @Override
  public byte[] getImageData() throws JRException
  {
    // no idea what this does
    return new byte[0];
  }

  @Override
  public void render(Graphics2D g2, Rectangle2D rectangle) throws JRException
  {
    if(m_licenseTrial)
    {
      AffineTransform originalTransform = g2.getTransform();

      // just for debugging
      g2.setColor(Color.BLUE);
      g2.draw(rectangle);

      g2.translate(rectangle.getX() + 100, rectangle.getMaxY());
      g2.rotate(-55 * Math.PI / 180);

      Font font = new Font("Arial", Font.PLAIN, 120);
      Shape shape = font.createGlyphVector(g2.getFontRenderContext(), "Trial License").getOutline();
      g2.setColor(new Color(255, 0, 0, 100));
      g2.setStroke(new BasicStroke(1));
      g2.draw(shape);

      g2.setTransform(originalTransform);
    }
  }
}

结果:

答案 2 :(得分:0)

您可以使用iText阅读使用jasper报告创建的PDF。要么将pdf读作byteArray。然后使用itext PdfReader

使用它
byte [] dataArray = JasperExportManager.exportReportToPdf(jasperPrint);
PdfReader pdfReader = new PdfReader(dataArray);

或从某个位置读取pdf(如果已经写好)

JasperExportManager.exportReportToPdfFile(jasperPrint,pdfFileLocation); 
PdfReader pdfReader = new PdfReader(pdfFileLocation);

我希望以下代码段可以帮助我读取文件并添加水印,删除现有文件并编写新文件。

ByteArrayOutputStream baos = new ByteArrayOutputStream();
BaseFont bf = null;
PdfBoolean pdfBoolean_YES = new PdfBoolean(true);
PdfReader pdfReader = new PdfReader(pdfFileLocation);
PdfStamper pdfStamper = new PdfStamper(pdfReader, baos);  
PdfContentByte contentunder = pdfStamper.getUnderContent(1);

contentunder.saveState();                   
contentunder.setColorFill(new Color(200, 200, 200));
contentunder.beginText();
bf = BaseFont.createFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
contentunder.setFontAndSize(bf, 90);
contentunder.showTextAligned(Element.ALIGN_MIDDLE, "  WaterMark Content", 200, 400, 45);
contentunder.endText();    
contentunder.restoreState();

// We could stack those ViewerPreferences using '|' ... :)
pdfStamper.addViewerPreference(PdfName.HIDETOOLBAR, pdfBoolean_YES);
pdfStamper.addViewerPreference(PdfName.HIDEMENUBAR, pdfBoolean_YES);
//pdfStamper.addViewerPreference(PdfName.HIDEWINDOWUI, pdfBoolean_YES);                   

pdfReader.close();
pdfStamper.close();
//deleting existing file                 
FileUtil.delete(pdfFileLocation);

FileOutputStream fos = new FileOutputStream(pdfFileLocation);
baos.writeTo(fos);                    
fos.flush();
//close streams                
baos.close();
fos.close();