當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。