com.lowagie.text.pdf.PdfGState Java Examples

The following examples show how to use com.lowagie.text.pdf.PdfGState. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PdfContext.java    From geomajas-project-server with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Draws the specified image with the first rectangle's bounds, clipping with the second one.
 *
 * @param img image
 * @param rect rectangle
 * @param clipRect clipping bounds
 * @param opacity opacity of the image (1 = opaque, 0= transparent)
 */
public void drawImage(Image img, Rectangle rect, Rectangle clipRect, float opacity) {
	try {
		template.saveState();
		// opacity
		PdfGState state = new PdfGState();
		state.setFillOpacity(opacity);
		state.setBlendMode(PdfGState.BM_NORMAL);
		template.setGState(state);
		// clipping code
		if (clipRect != null) {
			template.rectangle(clipRect.getLeft() + origX, clipRect.getBottom() + origY, clipRect.getWidth(),
					clipRect.getHeight());
			template.clip();
			template.newPath();
		}
		template.addImage(img, rect.getWidth(), 0, 0, rect.getHeight(), origX + rect.getLeft(), origY
				+ rect.getBottom());
	} catch (DocumentException e) {
		log.warn("could not draw image", e);
	} finally {
		template.restoreState();
	}
}
 
Example #2
Source File: GroupsTest.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Prints 3 circles in different colors that intersect with eachother.
 * @param x
 * @param y
 * @param cb
 * @throws Exception
 */
public static void pictureCircles(float x, float y, PdfContentByte cb) throws Exception {
	PdfGState gs = new PdfGState();
	gs.setBlendMode(PdfGState.BM_SOFTLIGHT);
	gs.setFillOpacity(0.7f);
	cb.setGState(gs);
    cb.setColorFill(Color.gray);
    cb.circle(x + 70, y + 70, 50);
    cb.fill();
    cb.circle(x + 100, y + 130, 50);
    cb.fill();
    cb.circle(x + 130, y + 70, 50);
    cb.fill();
}
 
Example #3
Source File: PdfWatermarkUtils.java    From bamboobsc with Apache License 2.0 5 votes vote down vote up
public static void addWatermark(PdfStamper pdfStamper, BaseFont baseFont, Color fontColor, String waterMarkString) throws Exception {
	if (null == pdfStamper || null == baseFont) {
		throw new java.lang.Exception("PdfStamper or BaseFont is null.");
	}
	if (StringUtils.isBlank(waterMarkString)) {
		return;
	}
	PdfContentByte pdfContentByte = null;
	Rectangle pageRect = null;
	PdfGState pdfGState = new PdfGState();
	// 设置透明度为0.4
	pdfGState.setFillOpacity(0.4f);
	pdfGState.setStrokeOpacity(0.4f);
       int pageNum = pdfStamper.getReader().getNumberOfPages();
       try {
           for (int i = 1; i <= pageNum; i++) {
           	pageRect = pdfStamper.getReader().getPageSizeWithRotation(i);
           	// 计算水印X,Y坐标
           	float x = pageRect.getWidth() / 2;
           	float y = pageRect.getHeight() / 2;
           	//获得PDF最顶层
           	pdfContentByte = pdfStamper.getOverContent(i);
           	pdfContentByte.saveState();
           	// set Transparency
           	pdfContentByte.setGState(pdfGState);
           	pdfContentByte.beginText();
           	pdfContentByte.setColorFill(fontColor);
           	pdfContentByte.setFontAndSize(baseFont, 60);
           	// 水印文字成45度角倾斜
           	pdfContentByte.showTextAligned(Element.ALIGN_CENTER, waterMarkString, x, y, 45);
           	pdfContentByte.endText();             	
           }
       } catch (Exception e) {
       	e.printStackTrace();
       } finally {
       	pdfContentByte = null;
           pageRect = null;        	
       }
}
 
Example #4
Source File: PdfContext.java    From geomajas-project-server with GNU Affero General Public License v3.0 5 votes vote down vote up
private void setStroke(Color color, float linewidth, float[] dashArray) {
	// Color and transparency
	PdfGState state = new PdfGState();
	state.setStrokeOpacity(color.getAlpha());
	state.setBlendMode(PdfGState.BM_NORMAL);
	template.setGState(state);
	template.setColorStroke(color);
	// linewidth
	template.setLineWidth(linewidth);
	if (dashArray != null) {
		template.setLineDash(dashArray, 0f);
	}
}
 
Example #5
Source File: PdfContext.java    From geomajas-project-server with GNU Affero General Public License v3.0 5 votes vote down vote up
private void setFill(Color color) {
	// Color and transparency
	PdfGState state = new PdfGState();
	state.setFillOpacity(color.getAlpha() / 255f);
	state.setBlendMode(PdfGState.BM_NORMAL);
	template.setGState(state);
	template.setColorFill(color);
}
 
Example #6
Source File: PdfXConformanceImp.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
/**
* Business logic that checks if a certain object is in conformance with PDF/X.
   * @param writer	the writer that is supposed to write the PDF/X file
   * @param key		the type of PDF/X conformance that has to be checked
   * @param obj1		the object that is checked for conformance
   */
  public static void checkPDFXConformance(PdfWriter writer, int key, Object obj1) {
      if (writer == null || !writer.isPdfX())
          return;
      int conf = writer.getPDFXConformance();
      switch (key) {
          case PDFXKEY_COLOR:
              switch (conf) {
                  case PdfWriter.PDFX1A2001:
                      if (obj1 instanceof ExtendedColor) {
                          ExtendedColor ec = (ExtendedColor)obj1;
                          switch (ec.getType()) {
                              case ExtendedColor.TYPE_CMYK:
                              case ExtendedColor.TYPE_GRAY:
                                  return;
                              case ExtendedColor.TYPE_RGB:
                                  throw new PdfXConformanceException("Colorspace RGB is not allowed.");
                              case ExtendedColor.TYPE_SEPARATION:
                                  SpotColor sc = (SpotColor)ec;
                                  checkPDFXConformance(writer, PDFXKEY_COLOR, sc.getPdfSpotColor().getAlternativeCS());
                                  break;
                              case ExtendedColor.TYPE_SHADING:
                                  ShadingColor xc = (ShadingColor)ec;
                                  checkPDFXConformance(writer, PDFXKEY_COLOR, xc.getPdfShadingPattern().getShading().getColorSpace());
                                  break;
                              case ExtendedColor.TYPE_PATTERN:
                                  PatternColor pc = (PatternColor)ec;
                                  checkPDFXConformance(writer, PDFXKEY_COLOR, pc.getPainter().getDefaultColor());
                                  break;
                          }
                      }
                      else if (obj1 instanceof Color)
                          throw new PdfXConformanceException("Colorspace RGB is not allowed.");
                      break;
              }
              break;
          case PDFXKEY_CMYK:
              break;
          case PDFXKEY_RGB:
              if (conf == PdfWriter.PDFX1A2001)
                  throw new PdfXConformanceException("Colorspace RGB is not allowed.");
              break;
          case PDFXKEY_FONT:
              if (!((BaseFont)obj1).isEmbedded())
                  throw new PdfXConformanceException("All the fonts must be embedded. This one isn't: " + ((BaseFont)obj1).getPostscriptFontName());
              break;
          case PDFXKEY_IMAGE:
              PdfImage image = (PdfImage)obj1;
              if (image.get(PdfName.SMASK) != null)
                  throw new PdfXConformanceException("The /SMask key is not allowed in images.");
              switch (conf) {
                  case PdfWriter.PDFX1A2001:
                      PdfObject cs = image.get(PdfName.COLORSPACE);
                      if (cs == null)
                          return;
                      if (cs.isName()) {
                          if (PdfName.DEVICERGB.equals(cs))
                              throw new PdfXConformanceException("Colorspace RGB is not allowed.");
                      }
                      else if (cs.isArray()) {
                          if (PdfName.CALRGB.equals(((PdfArray)cs).getPdfObject(0)))
                              throw new PdfXConformanceException("Colorspace CalRGB is not allowed.");
                      }
                      break;
              }
              break;
          case PDFXKEY_GSTATE:
              PdfDictionary gs = (PdfDictionary)obj1;
              PdfObject obj = gs.get(PdfName.BM);
              if (obj != null && !PdfGState.BM_NORMAL.equals(obj) && !PdfGState.BM_COMPATIBLE.equals(obj))
                  throw new PdfXConformanceException("Blend mode " + obj.toString() + " not allowed.");
              obj = gs.get(PdfName.CA);
              double v = 0.0;
              if (obj != null && (v = ((PdfNumber)obj).doubleValue()) != 1.0)
                  throw new PdfXConformanceException("Transparency is not allowed: /CA = " + v);
              obj = gs.get(PdfName.ca);
              v = 0.0;
              if (obj != null && (v = ((PdfNumber)obj).doubleValue()) != 1.0)
                  throw new PdfXConformanceException("Transparency is not allowed: /ca = " + v);
              break;
          case PDFXKEY_LAYER:
              throw new PdfXConformanceException("Layers are not allowed.");
      }
  }
 
Example #7
Source File: PdfXConformanceImp.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
* Business logic that checks if a certain object is in conformance with PDF/X.
   * @param writer	the writer that is supposed to write the PDF/X file
   * @param key		the type of PDF/X conformance that has to be checked
   * @param obj1		the object that is checked for conformance
   */
  public static void checkPDFXConformance(PdfWriter writer, int key, Object obj1) {
      if (writer == null || !writer.isPdfX())
          return;
      int conf = writer.getPDFXConformance();
      switch (key) {
          case PDFXKEY_COLOR:
              switch (conf) {
                  case PdfWriter.PDFX1A2001:
                      if (obj1 instanceof ExtendedColor) {
                          ExtendedColor ec = (ExtendedColor)obj1;
                          switch (ec.getType()) {
                              case ExtendedColor.TYPE_CMYK:
                              case ExtendedColor.TYPE_GRAY:
                                  return;
                              case ExtendedColor.TYPE_RGB:
                                  throw new PdfXConformanceException("Colorspace RGB is not allowed.");
                              case ExtendedColor.TYPE_SEPARATION:
                                  SpotColor sc = (SpotColor)ec;
                                  checkPDFXConformance(writer, PDFXKEY_COLOR, sc.getPdfSpotColor().getAlternativeCS());
                                  break;
                              case ExtendedColor.TYPE_SHADING:
                                  ShadingColor xc = (ShadingColor)ec;
                                  checkPDFXConformance(writer, PDFXKEY_COLOR, xc.getPdfShadingPattern().getShading().getColorSpace());
                                  break;
                              case ExtendedColor.TYPE_PATTERN:
                                  PatternColor pc = (PatternColor)ec;
                                  checkPDFXConformance(writer, PDFXKEY_COLOR, pc.getPainter().getDefaultColor());
                                  break;
                          }
                      }
                      else if (obj1 instanceof Color)
                          throw new PdfXConformanceException("Colorspace RGB is not allowed.");
                      break;
              }
              break;
          case PDFXKEY_CMYK:
              break;
          case PDFXKEY_RGB:
              if (conf == PdfWriter.PDFX1A2001)
                  throw new PdfXConformanceException("Colorspace RGB is not allowed.");
              break;
          case PDFXKEY_FONT:
              if (!((BaseFont)obj1).isEmbedded())
                  throw new PdfXConformanceException("All the fonts must be embedded. This one isn't: " + ((BaseFont)obj1).getPostscriptFontName());
              break;
          case PDFXKEY_IMAGE:
              PdfImage image = (PdfImage)obj1;
              if (image.get(PdfName.SMASK) != null)
                  throw new PdfXConformanceException("The /SMask key is not allowed in images.");
              switch (conf) {
                  case PdfWriter.PDFX1A2001:
                      PdfObject cs = image.get(PdfName.COLORSPACE);
                      if (cs == null)
                          return;
                      if (cs.isName()) {
                          if (PdfName.DEVICERGB.equals(cs))
                              throw new PdfXConformanceException("Colorspace RGB is not allowed.");
                      }
                      else if (cs.isArray()) {
                          if (PdfName.CALRGB.equals(((PdfArray)cs).getPdfObject(0)))
                              throw new PdfXConformanceException("Colorspace CalRGB is not allowed.");
                      }
                      break;
              }
              break;
          case PDFXKEY_GSTATE:
              PdfDictionary gs = (PdfDictionary)obj1;
              PdfObject obj = gs.get(PdfName.BM);
              if (obj != null && !PdfGState.BM_NORMAL.equals(obj) && !PdfGState.BM_COMPATIBLE.equals(obj))
                  throw new PdfXConformanceException("Blend mode " + obj.toString() + " not allowed.");
              obj = gs.get(PdfName.CA);
              double v = 0.0;
              if (obj != null && (v = ((PdfNumber)obj).doubleValue()) != 1.0)
                  throw new PdfXConformanceException("Transparency is not allowed: /CA = " + v);
              obj = gs.get(PdfName.ca);
              v = 0.0;
              if (obj != null && (v = ((PdfNumber)obj).doubleValue()) != 1.0)
                  throw new PdfXConformanceException("Transparency is not allowed: /ca = " + v);
              break;
          case PDFXKEY_LAYER:
              throw new PdfXConformanceException("Layers are not allowed.");
      }
  }
 
Example #8
Source File: GStateTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Changing the Graphics State with PdfGState.
 * 
 */
@Test
public void main() throws Exception {

	// step 1: creation of a document-object
	Document document = new Document();

	try {

		// step 2:
		// we create a writer that listens to the document
		// and directs a PDF-stream to a file
		PdfWriter writer = PdfWriter.getInstance(document,
				PdfTestBase.getOutputStream( "gstate.pdf"));

		// step 3: we open the document
		document.open();

		// step 4: we grab the ContentByte and do some stuff with it
		PdfContentByte cb = writer.getDirectContent();

           PdfGState gs = new PdfGState();
           gs.setFillOpacity(0.5f);
           cb.setGState(gs);
		cb.setColorFill(Color.red);
		cb.circle(260.0f, 500.0f, 250.0f);
		cb.fill();
		cb.circle(260.0f, 500.0f, 200.0f);
		cb.fill();
		cb.circle(260.0f, 500.0f, 150.0f);
		cb.fill();
		gs.setFillOpacity(0.2f);
		cb.setGState(gs);
		cb.setColorFill(Color.blue);
		cb.circle(260.0f, 500.0f, 100.0f);
		cb.fill();
		cb.circle(260.0f, 500.0f, 50.0f);
		cb.fill();
		
		cb.sanityCheck();
	} catch (DocumentException de) {
		System.err.println(de.getMessage());
	} catch (IOException ioe) {
		System.err.println(ioe.getMessage());
	}

	// step 5: we close the document
	document.close();
}