当前位置: 首页>>代码示例>>Java>>正文


Java Document.compress方法代码示例

本文整理汇总了Java中com.lowagie.text.Document.compress方法的典型用法代码示例。如果您正苦于以下问题:Java Document.compress方法的具体用法?Java Document.compress怎么用?Java Document.compress使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在com.lowagie.text.Document的用法示例。


在下文中一共展示了Document.compress方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: PRStream

import com.lowagie.text.Document; //导入方法依赖的package包/类
/**
 * Creates a new PDF stream object that will replace a stream
 * in a existing PDF file.
 * @param	reader	the reader that holds the existing PDF
 * @param	conts	the new content
 * @param	compressionLevel	the compression level for the content
 * @since	2.1.3 (replacing the existing constructor without param compressionLevel)
 */
public PRStream(PdfReader reader, byte[] conts, int compressionLevel) {
    this.reader = reader;
    this.offset = -1;
    if (Document.compress) {
        try {
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            Deflater deflater = new Deflater(compressionLevel);
            DeflaterOutputStream zip = new DeflaterOutputStream(stream, deflater);
            zip.write(conts);
            zip.close();
            deflater.end();
            bytes = stream.toByteArray();
        }
        catch (IOException ioe) {
            throw new ExceptionConverter(ioe);
        }
        put(PdfName.FILTER, PdfName.FLATEDECODE);
    }
    else
        bytes = conts;
    setLength(bytes.length);
}
 
开发者ID:albfernandez,项目名称:itext2,代码行数:31,代码来源:PRStream.java

示例2: setData

import com.lowagie.text.Document; //导入方法依赖的package包/类
/**
 * Sets the data associated with the stream, either compressed or
 * uncompressed. Note that the data will never be compressed if
 * Document.compress is set to false.
 * 
 * @param data raw data, decrypted and uncompressed.
 * @param compress true if you want the stream to be compressed.
 * @param compressionLevel	a value between -1 and 9 (ignored if compress == false)
 * @since	iText 2.1.3
 */
public void setData(byte[] data, boolean compress, int compressionLevel) {
    remove(PdfName.FILTER);
    this.offset = -1;
    if (Document.compress && compress) {
        try {
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            Deflater deflater = new Deflater(compressionLevel);
            DeflaterOutputStream zip = new DeflaterOutputStream(stream, deflater);
            zip.write(data);
            zip.close();
            deflater.end();
            bytes = stream.toByteArray();
            this.compressionLevel = compressionLevel;
        }
        catch (IOException ioe) {
            throw new ExceptionConverter(ioe);
        }
        put(PdfName.FILTER, PdfName.FLATEDECODE);
    }
    else
        bytes = data;
    setLength(bytes.length);
}
 
开发者ID:albfernandez,项目名称:itext2,代码行数:34,代码来源:PRStream.java

示例3: flateCompress

import com.lowagie.text.Document; //导入方法依赖的package包/类
/**
   * Compresses the stream.
* @param compressionLevel the compression level (0 = best speed, 9 = best compression, -1 is default)
* @since	2.1.3
   */
  public void flateCompress(int compressionLevel) {
      if (!Document.compress)
          return;
      // check if the flateCompress-method has already been
      if (compressed) {
          return;
      }
  	this.compressionLevel = compressionLevel;
      if (inputStream != null) {
          compressed = true;
          return;
      }
      // check if a filter already exists
      PdfObject filter = PdfReader.getPdfObject(get(PdfName.FILTER));
      if (filter != null) {
          if (filter.isName()) {
              if (PdfName.FLATEDECODE.equals(filter))
                  return;
          }
          else if (filter.isArray()) {
              if (((PdfArray) filter).contains(PdfName.FLATEDECODE))
                  return;
          }
          else {
              throw new RuntimeException("Stream could not be compressed: filter is not a name or array.");
          }
      }
      try {
          // compress
          ByteArrayOutputStream stream = new ByteArrayOutputStream();
          Deflater deflater = new Deflater(compressionLevel);
          DeflaterOutputStream zip = new DeflaterOutputStream(stream, deflater);
          if (streamBytes != null)
              streamBytes.writeTo(zip);
          else
              zip.write(bytes);
          zip.close();
          deflater.end();
          // update the object
          streamBytes = stream;
          bytes = null;
          put(PdfName.LENGTH, new PdfNumber(streamBytes.size()));
          if (filter == null) {
              put(PdfName.FILTER, PdfName.FLATEDECODE);
          }
          else {
              PdfArray filters = new PdfArray(filter);
              filters.add(PdfName.FLATEDECODE);
              put(PdfName.FILTER, filters);
          }
          compressed = true;
      }
      catch(IOException ioe) {
          throw new ExceptionConverter(ioe);
      }
  }
 
开发者ID:albfernandez,项目名称:itext2,代码行数:62,代码来源:PdfStream.java

示例4: main

import com.lowagie.text.Document; //导入方法依赖的package包/类
/**
    * Add an image using different transformation matrices.
    */
@Test
public void main() throws Exception {
       Document.compress = false;
       // step 1: creation of a document-object
       Document document = new Document(PageSize.A4);
       
       try {
           // step 2: creation of the writer
           PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream( "transformimage.pdf"));
           
           // step 3: we open the document
           document.open();
           
           // step 4:
           PdfContentByte cb = writer.getDirectContent();
           Image img = Image.getInstance(PdfTestBase.RESOURCES_DIR + "hitchcock.png");
           cb.addImage(img, 271, -50, -30, 550, 100, 100);
           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();
   }
 
开发者ID:albfernandez,项目名称:itext2,代码行数:34,代码来源:TransformImageTest.java

示例5: main

import com.lowagie.text.Document; //导入方法依赖的package包/类
/**
 * Generates an Acroform with a PushButton
 */
@Test
public void main() throws Exception {

	Document.compress = false;
	// step 1: creation of a document-object
	Document document = new Document(PageSize.A4);

	// step 2:
	PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream("pushbutton.pdf"));

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

	// step 4:
	PdfFormField pushbutton = PdfFormField.createPushButton(writer);
	PdfContentByte cb = writer.getDirectContent();
	cb.moveTo(0, 0);
	PdfAppearance normal = cb.createAppearance(100, 50);
	normal.setColorFill(Color.GRAY);
	normal.rectangle(5, 5, 90, 40);
	normal.fill();
	PdfAppearance rollover = cb.createAppearance(100, 50);
	rollover.setColorFill(Color.RED);
	rollover.rectangle(5, 5, 90, 40);
	rollover.fill();
	PdfAppearance down = cb.createAppearance(100, 50);
	down.setColorFill(Color.BLUE);
	down.rectangle(5, 5, 90, 40);
	down.fill();
	pushbutton.setFieldName("PushMe");
	pushbutton.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, normal);
	pushbutton.setAppearance(PdfAnnotation.APPEARANCE_ROLLOVER, rollover);
	pushbutton.setAppearance(PdfAnnotation.APPEARANCE_DOWN, down);
	pushbutton.setWidget(new Rectangle(100, 700, 200, 750), PdfAnnotation.HIGHLIGHT_PUSH);
	writer.addAnnotation(pushbutton);

	// step 5: we close the document
	document.close();
}
 
开发者ID:albfernandez,项目名称:itext2,代码行数:43,代码来源:FormPushButtonTest.java

示例6: main

import com.lowagie.text.Document; //导入方法依赖的package包/类
@Test
public void main() throws Exception {
       
       Document.compress = false;
       
       // 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( "literal.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();
           String star = "0.3 g\n15.000 27.000 m\n"
               + "7.947 5.292 l\n26.413 18.708 l\n"
               + "3.587 18.708 l\n22.053 5.292 l\nf\n"
               + "45.000 57.000 m\n37.947 35.292 l\n"
               + "56.413 48.708 l\n33.587 48.708 l\n"
               + "52.053 35.292 l\nf\n"
               + "0.7 g\n15.000 57.000 m\n"
               + "7.947 35.292 l\n26.413 48.708 l\n"
               + "3.587 48.708 l\n22.053 35.292 l\nf\n"
               + "45.000 27.000 m\n37.947 5.292 l\n"
               + "56.413 18.708 l\n33.587 18.708 l\n"
               + "52.053 5.292 l\nf";
           cb.setLiteral(star);
           
           // sanityCheck doesn't check literals.
           //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();
   }
 
开发者ID:albfernandez,项目名称:itext2,代码行数:48,代码来源:LiteralTest.java


注:本文中的com.lowagie.text.Document.compress方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。