Support for setting the compression level (zlib).

Works for streams created by PdfWriter, for the image types where FlateDecode is done by iText, and for font streams.


git-svn-id: svn://svn.code.sf.net/p/itextsharp/code/trunk@10 820d3149-562b-4f88-9aa4-a8e61a3485cf
master
psoares33 2008-07-27 15:43:33 +00:00
parent 61a66b3167
commit c4e9a9ce5f
26 changed files with 1229 additions and 991 deletions

View File

@ -177,6 +177,12 @@ namespace iTextSharp.text {
/// <summary> This is the original height of the image taking rotation into account. </summary>
protected float scaledHeight;
/**
* The compression level of the content streams.
* @since 2.1.3
*/
protected int compressionLevel = PdfStream.DEFAULT_COMPRESSION;
/// <summary> This is the rotation of the image. </summary>
protected float rotationRadians;
@ -1479,5 +1485,22 @@ namespace iTextSharp.text {
return directReference;
}
}
/**
* Sets the compression level to be used if the image is written as a compressed stream.
* @param compressionLevel a value between 0 (best speed) and 9 (best compression)
* @since 2.1.3
*/
public int CompressionLevel {
set {
if (compressionLevel < PdfStream.NO_COMPRESSION || compressionLevel > PdfStream.BEST_COMPRESSION)
compressionLevel = PdfStream.DEFAULT_COMPRESSION;
else
compressionLevel = compressionLevel;
}
get {
return compressionLevel;
}
}
}
}

View File

@ -9,7 +9,7 @@ using System.util;
using iTextSharp.text.xml.simpleparser;
/*
* $Id: BaseFont.cs,v 1.18 2008/06/01 13:17:07 psoares33 Exp $
* $Id: BaseFont.cs,v 1.17 2008/05/13 11:25:17 psoares33 Exp $
*
*
* Copyright 2000-2006 by Paulo Soares.
@ -258,6 +258,12 @@ namespace iTextSharp.text.pdf {
/** true if the font is to be embedded in the PDF */
protected bool embedded;
/**
* The compression level for the font stream.
* @since 2.1.3
*/
protected int compressionLevel = PdfStream.DEFAULT_COMPRESSION;
/**
* true if the font must use it's built in encoding. In that case the
* <CODE>encoding</CODE> is only used to map a char to the position inside
@ -324,22 +330,33 @@ namespace iTextSharp.text.pdf {
* a PdfStream.
* @param contents the content of the stream
* @param lengths an array of int that describes the several lengths of each part of the font
* @param compressionLevel the compression level of the Stream
* @throws DocumentException error in the stream compression
* @since 2.1.3 (replaces the constructor without param compressionLevel)
*/
internal StreamFont(byte[] contents, int[] lengths) {
internal StreamFont(byte[] contents, int[] lengths, int compressionLevel) {
bytes = contents;
Put(PdfName.LENGTH, new PdfNumber(bytes.Length));
for (int k = 0; k < lengths.Length; ++k) {
Put(new PdfName("Length" + (k + 1)), new PdfNumber(lengths[k]));
}
FlateCompress();
FlateCompress(compressionLevel);
}
internal StreamFont(byte[] contents, string subType) {
/**
* Generates the PDF stream for a font.
* @param contents the content of a stream
* @param subType the subtype of the font.
* @param compressionLevel the compression level of the Stream
* @throws DocumentException error in the stream compression
* @since 2.1.3 (replaces the constructor without param compressionLevel)
*/
internal StreamFont(byte[] contents, string subType, int compressionLevel) {
bytes = contents;
Put(PdfName.LENGTH, new PdfNumber(bytes.Length));
if (subType != null)
Put(PdfName.SUBTYPE, new PdfName(subType));
FlateCompress();
FlateCompress(compressionLevel);
}
}
@ -1438,5 +1455,22 @@ namespace iTextSharp.text.pdf {
subsetRanges = new ArrayList();
subsetRanges.Add(range);
}
/**
* Sets the compression level to be used for the font streams.
* @param compressionLevel a value between 0 (best speed) and 9 (best compression)
* @since 2.1.3
*/
public int CompressionLevel {
set {
if (compressionLevel < PdfStream.NO_COMPRESSION || compressionLevel > PdfStream.BEST_COMPRESSION)
compressionLevel = PdfStream.DEFAULT_COMPRESSION;
else
compressionLevel = compressionLevel;
}
get {
return compressionLevel;
}
}
}
}

View File

@ -70,6 +70,7 @@ public class PRStream : PdfStream {
offset = stream.offset;
length = stream.Length;
compressed = stream.compressed;
compressionLevel = stream.compressionLevel;
streamBytes = stream.streamBytes;
bytes = stream.bytes;
objNum = stream.objNum;
@ -89,12 +90,23 @@ public class PRStream : PdfStream {
this.offset = offset;
}
public PRStream(PdfReader reader, byte[] conts) {
public PRStream(PdfReader reader, byte[] conts) : this(reader, conts, DEFAULT_COMPRESSION) {
}
/**
* 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) {
MemoryStream stream = new MemoryStream();
ZDeflaterOutputStream zip = new ZDeflaterOutputStream(stream);
ZDeflaterOutputStream zip = new ZDeflaterOutputStream(stream, compressionLevel);
zip.Write(conts, 0, conts.Length);
zip.Close();
bytes = stream.ToArray();
@ -115,14 +127,29 @@ public class PRStream : PdfStream {
* @since iText 2.1.1
*/
public void SetData(byte[] data, bool compress) {
SetData(data, compress, DEFAULT_COMPRESSION);
}
/**
* 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 compresssed.
* @param compressionLevel a value between -1 and 9 (ignored if compress == false)
* @since iText 2.1.3
*/
public void SetData(byte[] data, bool compress, int compressionLevel) {
Remove(PdfName.FILTER);
this.offset = -1;
if (Document.Compress && compress) {
MemoryStream stream = new MemoryStream();
ZDeflaterOutputStream zip = new ZDeflaterOutputStream(stream);
ZDeflaterOutputStream zip = new ZDeflaterOutputStream(stream, compressionLevel);
zip.Write(data, 0, data.Length);
zip.Close();
bytes = stream.ToArray();
this.compressionLevel = compressionLevel;
Put(PdfName.FILTER, PdfName.FLATEDECODE);
}
else
@ -195,7 +222,7 @@ public class PRStream : PdfStream {
Put(PdfName.LENGTH, objLen);
os.Write(STARTSTREAM, 0, STARTSTREAM.Length);
if (length > 0) {
if (crypto != null)
if (crypto != null && !crypto.IsEmbeddedFilesOnly())
b = crypto.EncryptByteArray(b);
os.Write(b, 0, b.Length);
}

View File

@ -276,7 +276,7 @@ namespace iTextSharp.text.pdf {
else {
byte[] b = PdfEncodings.ConvertToBytes(code, unicode ? PdfObject.TEXT_UNICODE : PdfObject.TEXT_PDFDOCENCODING);
PdfStream stream = new PdfStream(b);
stream.FlateCompress();
stream.FlateCompress(writer.CompressionLevel);
js.Put(PdfName.JS, writer.AddToBody(stream).IndirectReference);
}
return js;

View File

@ -86,7 +86,7 @@ namespace iTextSharp.text.pdf {
streamBytes = new MemoryStream();
if (Document.Compress) {
compressed = true;
ostr = new ZDeflaterOutputStream(streamBytes);
ostr = new ZDeflaterOutputStream(streamBytes, text.PdfWriter.CompressionLevel);
}
else
ostr = streamBytes;

View File

@ -593,7 +593,7 @@ namespace iTextSharp.text.pdf {
if (over != null)
out_p.Append(PdfContents.SAVESTATE);
PdfStream stream = new PdfStream(out_p.ToByteArray());
stream.FlateCompress();
stream.FlateCompress(cstp.CompressionLevel);
PdfIndirectReference ref1 = cstp.AddToBody(stream).IndirectReference;
ar.AddFirst(ref1);
out_p.Reset();
@ -605,7 +605,7 @@ namespace iTextSharp.text.pdf {
out_p.Append(over.InternalBuffer);
out_p.Append(PdfContents.RESTORESTATE);
stream = new PdfStream(out_p.ToByteArray());
stream.FlateCompress();
stream.FlateCompress(cstp.CompressionLevel);
ar.Add(cstp.AddToBody(stream).IndirectReference);
}
pageN.Put(PdfName.RESOURCES, pageResources.Resources);

View File

@ -101,6 +101,12 @@ public class PdfEncryption {
/** The generic key length. It may be 40 or 128. */
private int keyLength;
private bool encryptMetadata;
/**
* Indicates if the encryption is only necessary for embedded files.
* @since 2.1.3
*/
private bool embeddedFilesOnly;
private int cryptoMode;
public PdfEncryption() {
@ -118,20 +124,24 @@ public class PdfEncryption {
revision = enc.revision;
keyLength = enc.keyLength;
encryptMetadata = enc.encryptMetadata;
embeddedFilesOnly = enc.embeddedFilesOnly;
publicKeyHandler = enc.publicKeyHandler;
}
public void SetCryptoMode(int mode, int kl) {
cryptoMode = mode;
encryptMetadata = (mode & PdfWriter.DO_NOT_ENCRYPT_METADATA) == 0;
embeddedFilesOnly = (mode & PdfWriter.EMBEDDED_FILES_ONLY) != 0;
mode &= PdfWriter.ENCRYPTION_MASK;
switch (mode) {
case PdfWriter.STANDARD_ENCRYPTION_40:
encryptMetadata = true;
embeddedFilesOnly = false;
keyLength = 40;
revision = STANDARD_ENCRYPTION_40;
break;
case PdfWriter.STANDARD_ENCRYPTION_128:
embeddedFilesOnly = false;
if (kl > 0)
keyLength = kl;
else
@ -155,6 +165,15 @@ public class PdfEncryption {
return encryptMetadata;
}
/**
* Indicates if only the embedded files have to be encrypted.
* @return if true only the embedded files will be encrypted
* @since 2.1.3
*/
public bool IsEmbeddedFilesOnly() {
return embeddedFilesOnly;
}
private byte[] PadPassword(byte[] userPassword) {
byte[] userPad = new byte[32];
if (userPassword == null) {
@ -390,9 +409,16 @@ public class PdfEncryption {
PdfDictionary cf = new PdfDictionary();
cf.Put(PdfName.DEFAULTCRYPTFILER, stdcf);
dic.Put(PdfName.CF, cf);
if (embeddedFilesOnly) {
dic.Put(PdfName.EFF, PdfName.DEFAULTCRYPTFILER);
dic.Put(PdfName.STRF, PdfName.IDENTITY);
dic.Put(PdfName.STMF, PdfName.IDENTITY);
}
else {
dic.Put(PdfName.STRF, PdfName.DEFAULTCRYPTFILER);
dic.Put(PdfName.STMF, PdfName.DEFAULTCRYPTFILER);
}
}
SHA1 sh = new SHA1CryptoServiceProvider();
byte[] encodedRecipient = null;
@ -431,7 +457,17 @@ public class PdfEncryption {
dic.Put(PdfName.LENGTH, new PdfNumber(128));
PdfDictionary stdcf = new PdfDictionary();
stdcf.Put(PdfName.LENGTH, new PdfNumber(16));
if (embeddedFilesOnly) {
stdcf.Put(PdfName.AUTHEVENT, PdfName.EFOPEN);
dic.Put(PdfName.EFF, PdfName.STDCF);
dic.Put(PdfName.STRF, PdfName.IDENTITY);
dic.Put(PdfName.STMF, PdfName.IDENTITY);
}
else {
stdcf.Put(PdfName.AUTHEVENT, PdfName.DOCOPEN);
dic.Put(PdfName.STRF, PdfName.STDCF);
dic.Put(PdfName.STMF, PdfName.STDCF);
}
if (revision == AES_128)
stdcf.Put(PdfName.CFM, PdfName.AESV2);
else
@ -439,8 +475,6 @@ public class PdfEncryption {
PdfDictionary cf = new PdfDictionary();
cf.Put(PdfName.STDCF, stdcf);
dic.Put(PdfName.CF, cf);
dic.Put(PdfName.STRF, PdfName.STDCF);
dic.Put(PdfName.STMF, PdfName.STDCF);
}
}
return dic;

View File

@ -69,18 +69,15 @@ namespace iTextSharp.text.pdf {
/** This is the 1 - matrix. */
public static PdfLiteral MATRIX = new PdfLiteral("[1 0 0 1 0 0]");
// membervariables
// constructor
/**
* Constructs a <CODE>PdfFormXObject</CODE>-object.
*
* @param template the template
* @param compressionLevel the compression level for the stream
* @since 2.1.3 (Replacing the existing constructor with param compressionLevel)
*/
internal PdfFormXObject(PdfTemplate template) : base() {
internal PdfFormXObject(PdfTemplate template, int compressionLevel) : base() {
Put(PdfName.TYPE, PdfName.XOBJECT);
Put(PdfName.SUBTYPE, PdfName.FORM);
Put(PdfName.RESOURCES, template.Resources);
@ -97,7 +94,7 @@ namespace iTextSharp.text.pdf {
Put(PdfName.MATRIX, matrix);
bytes = template.ToPdf(null);
Put(PdfName.LENGTH, new PdfNumber(bytes.Length));
FlateCompress();
FlateCompress(compressionLevel);
}
}

View File

@ -79,7 +79,7 @@ namespace iTextSharp.text.pdf {
int bitsPerSample, int order, float[] encode, float[] decode, byte[] stream) {
PdfFunction func = new PdfFunction(writer);
func.dictionary = new PdfStream(stream);
((PdfStream)func.dictionary).FlateCompress();
((PdfStream)func.dictionary).FlateCompress(writer.CompressionLevel);
func.dictionary.Put(PdfName.FUNCTIONTYPE, new PdfNumber(0));
func.dictionary.Put(PdfName.DOMAIN, new PdfArray(domain));
func.dictionary.Put(PdfName.RANGE, new PdfArray(range));
@ -131,7 +131,7 @@ namespace iTextSharp.text.pdf {
b[k] = (byte)postscript[k];
PdfFunction func = new PdfFunction(writer);
func.dictionary = new PdfStream(b);
((PdfStream)func.dictionary).FlateCompress();
((PdfStream)func.dictionary).FlateCompress(writer.CompressionLevel);
func.dictionary.Put(PdfName.FUNCTIONTYPE, new PdfNumber(4));
func.dictionary.Put(PdfName.DOMAIN, new PdfArray(domain));
func.dictionary.Put(PdfName.RANGE, new PdfArray(range));

View File

@ -10,7 +10,23 @@ namespace iTextSharp.text.pdf {
public class PdfICCBased : PdfStream {
public PdfICCBased(ICC_Profile profile) {
/**
* Creates an ICC stream.
* @param profile an ICC profile
*/
public PdfICCBased(ICC_Profile profile) : this(profile, DEFAULT_COMPRESSION) {
;
}
/**
* Creates an ICC stream.
*
* @param compressionLevel the compressionLevel
*
* @param profile an ICC profile
* @since 2.1.3 (replacing the constructor without param compressionLevel)
*/
public PdfICCBased(ICC_Profile profile, int compressionLevel) {
int numberOfComponents = profile.NumComponents;
switch (numberOfComponents) {
case 1:
@ -27,7 +43,7 @@ namespace iTextSharp.text.pdf {
}
Put(PdfName.N, new PdfNumber(numberOfComponents));
bytes = profile.Data;
FlateCompress();
FlateCompress(compressionLevel);
}
}
}

View File

@ -161,7 +161,7 @@ namespace iTextSharp.text.pdf {
if (image.Deflated)
Put(PdfName.FILTER, PdfName.FLATEDECODE);
else {
FlateCompress();
FlateCompress(image.CompressionLevel);
}
}
return;
@ -273,6 +273,7 @@ namespace iTextSharp.text.pdf {
protected void ImportAll(PdfImage dup) {
name = dup.name;
compressed = dup.compressed;
compressionLevel = dup.compressionLevel;
streamBytes = dup.streamBytes;
bytes = dup.bytes;
hashMap = dup.hashMap;

View File

@ -3,7 +3,7 @@ using System;
using iTextSharp.text;
/*
* $Id: PdfImportedPage.cs,v 1.4 2008/07/05 09:34:43 psoares33 Exp $
* $Id: PdfImportedPage.cs,v 1.3 2008/05/13 11:25:21 psoares33 Exp $
*
*
* Copyright 2001, 2002 Paulo Soares
@ -123,10 +123,15 @@ namespace iTextSharp.text.pdf {
}
}
internal override PdfStream FormXObject {
get {
return readerInstance.GetFormXObject(pageNumber);
}
/**
* Gets the stream representing this page.
*
* @param compressionLevel the compressionLevel
* @return the stream representing this page
* @since 2.1.3 (replacing the method without param compressionLevel)
*/
internal override PdfStream GetFormXObject(int compressionLevel) {
return readerInstance.GetFormXObject(pageNumber, compressionLevel);
}
public override void SetColorFill(PdfSpotColor sp, float tint) {

View File

@ -357,6 +357,12 @@ namespace iTextSharp.text.pdf {
public static readonly PdfName EARLYCHANGE = new PdfName("EarlyChange");
/** A name */
public static readonly PdfName EF = new PdfName("EF");
/** A name
* @since 2.1.3 */
public static readonly PdfName EFF = new PdfName("EFF");
/** A name
* @since 2.1.3 */
public static readonly PdfName EFOPEN = new PdfName("EFOpen");
/** A name */
public static readonly PdfName EMBEDDEDFILE = new PdfName("EmbeddedFile");
/** A name */

View File

@ -66,19 +66,18 @@ namespace iTextSharp.text.pdf {
/**
* Gets the stream representing this object.
*
* @return the stream representing this object
* @param compressionLevel the compressionLevel
* @return the stream representing this template
* @since 2.1.3 (replacing the method without param compressionLevel)
* @throws IOException
*/
internal override PdfStream FormXObject {
get {
internal override PdfStream GetFormXObject(int compressionLevel) {
PdfStream s = new PdfStream(content.ToByteArray());
s.Put(PdfName.TYPE, PdfName.XOBJECT);
s.Put(PdfName.SUBTYPE, PdfName.PS);
s.FlateCompress();
s.FlateCompress(compressionLevel);
return s;
}
}
/**
* Gets a duplicate of this <CODE>PdfPSXObject</CODE>. All

View File

@ -54,7 +54,20 @@ namespace iTextSharp.text.pdf {
public class PdfPattern : PdfStream {
internal PdfPattern(PdfPatternPainter painter) : base() {
/**
* Creates a PdfPattern object.
* @param painter a pattern painter instance
*/
internal PdfPattern(PdfPatternPainter painter) : this(painter, DEFAULT_COMPRESSION) {
}
/**
* Creates a PdfPattern object.
* @param painter a pattern painter instance
* @param compressionLevel the compressionLevel for the stream
* @since 2.1.3
*/
internal PdfPattern(PdfPatternPainter painter, int compressionLevel) : base() {
PdfNumber one = new PdfNumber(1);
PdfArray matrix = painter.Matrix;
if ( matrix != null ) {
@ -73,7 +86,7 @@ namespace iTextSharp.text.pdf {
Put(PdfName.YSTEP, new PdfNumber(painter.YStep));
bytes = painter.ToPdf(null);
Put(PdfName.LENGTH, new PdfNumber(bytes.Length));
FlateCompress();
FlateCompress(compressionLevel);
}
}
}

View File

@ -110,16 +110,23 @@ namespace iTextSharp.text.pdf {
public void SetPatternMatrix(float a, float b, float c, float d, float e, float f) {
SetMatrix(a, b, c, d, e, f);
}
/**
* Gets the stream representing this pattern
*
* @return the stream representing this pattern
*/
internal PdfPattern Pattern {
get {
internal PdfPattern GetPattern() {
return new PdfPattern(this);
}
/**
* Gets the stream representing this pattern
* @param compressionLevel the compression level of the stream
* @return the stream representing this pattern
* @since 2.1.3
*/
internal PdfPattern GetPattern(int compressionLevel) {
return new PdfPattern(this, compressionLevel);
}
/**

View File

@ -1957,6 +1957,15 @@ namespace iTextSharp.text.pdf {
* @throws IOException on error
*/
public void SetPageContent(int pageNum, byte[] content) {
SetPageContent(pageNum, content, PdfStream.DEFAULT_COMPRESSION);
}
/** Sets the contents of the page.
* @param content the new page content
* @param pageNum the page number. 1 is the first
* @since 2.1.3 (the method already existed without param compressionLevel)
*/
public void SetPageContent(int pageNum, byte[] content, int compressionLevel) {
PdfDictionary page = GetPageN(pageNum);
if (page == null)
return;
@ -1968,7 +1977,7 @@ namespace iTextSharp.text.pdf {
freeXref = xrefObj.Count - 1;
}
page.Put(PdfName.CONTENTS, new PRIndirectReference(this, freeXref));
xrefObj[freeXref] = new PRStream(this, content);
xrefObj[freeXref] = new PRStream(this, content, compressionLevel);
}
/** Get the content from a stream applying the required filters.

View File

@ -113,8 +113,14 @@ namespace iTextSharp.text.pdf {
return obj;
}
internal PdfStream GetFormXObject(int pageNumber) {
/**
* Gets the content stream of a page as a PdfStream object.
* @param pageNumber the page of which you want the stream
* @param compressionLevel the compression level you want to apply to the stream
* @return a PdfStream object
* @since 2.1.3 (the method already existed without param compressionLevel)
*/
internal PdfStream GetFormXObject(int pageNumber, int compressionLevel) {
PdfDictionary page = reader.GetPageNRelease(pageNumber);
PdfObject contents = PdfReader.GetPdfObjectRelease(page.Get(PdfName.CONTENTS));
PdfDictionary dic = new PdfDictionary();
@ -167,7 +173,7 @@ namespace iTextSharp.text.pdf {
try {
file.ReOpen();
foreach (PdfImportedPage ip in importedPages.Values) {
writer.AddToBody(ip.FormXObject, ip.IndirectReference);
writer.AddToBody(ip.GetFormXObject(writer.CompressionLevel), ip.IndirectReference);
}
WriteAllVisited();
}

View File

@ -376,7 +376,7 @@ namespace iTextSharp.text.pdf {
if (ps.over != null)
out_p.Append(PdfContents.SAVESTATE);
PdfStream stream = new PdfStream(out_p.ToByteArray());
stream.FlateCompress();
stream.FlateCompress(compressionLevel);
ar.AddFirst(AddToBody(stream).IndirectReference);
out_p.Reset();
if (ps.over != null) {
@ -389,7 +389,7 @@ namespace iTextSharp.text.pdf {
out_p.Append(buf.Buffer, ps.replacePoint, buf.Size - ps.replacePoint);
out_p.Append(PdfContents.RESTORESTATE);
stream = new PdfStream(out_p.ToByteArray());
stream.FlateCompress();
stream.FlateCompress(compressionLevel);
ar.Add(AddToBody(stream).IndirectReference);
}
AlterResources(ps);

View File

@ -80,9 +80,36 @@ namespace iTextSharp.text.pdf {
// membervariables
/**
* A possible compression level.
* @since 2.1.3
*/
public const int DEFAULT_COMPRESSION = -1;
/**
* A possible compression level.
* @since 2.1.3
*/
public const int NO_COMPRESSION = 0;
/**
* A possible compression level.
* @since 2.1.3
*/
public const int BEST_SPEED = 1;
/**
* A possible compression level.
* @since 2.1.3
*/
public const int BEST_COMPRESSION = 9;
/** is the stream compressed? */
protected bool compressed = false;
/**
* The level of compression.
* @since 2.1.3
*/
protected int compressionLevel = NO_COMPRESSION;
protected MemoryStream streamBytes = null;
protected Stream inputStream;
@ -169,17 +196,24 @@ namespace iTextSharp.text.pdf {
/**
* Compresses the stream.
*
* @throws PdfException if a filter is allready defined
*/
public void FlateCompress() {
FlateCompress(DEFAULT_COMPRESSION);
}
/**
* 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 allready been
if (compressed) {
return;
}
this.compressionLevel = compressionLevel;
if (inputStream != null) {
compressed = true;
return;
@ -201,7 +235,7 @@ namespace iTextSharp.text.pdf {
}
// compress
MemoryStream stream = new MemoryStream();
ZDeflaterOutputStream zip = new ZDeflaterOutputStream(stream);
ZDeflaterOutputStream zip = new ZDeflaterOutputStream(stream, compressionLevel);
if (streamBytes != null)
streamBytes.WriteTo(zip);
else
@ -261,10 +295,10 @@ namespace iTextSharp.text.pdf {
OutputStreamCounter osc = new OutputStreamCounter(os);
OutputStreamEncryption ose = null;
Stream fout = osc;
if (crypto != null)
if (crypto != null && !crypto.IsEmbeddedFilesOnly())
fout = ose = crypto.GetEncryptionStream(fout);
if (compressed)
fout = def = new ZDeflaterOutputStream(fout);
fout = def = new ZDeflaterOutputStream(fout, compressionLevel);
byte[] buf = new byte[4192];
while (true) {
@ -281,13 +315,7 @@ namespace iTextSharp.text.pdf {
inputStreamLength = osc.Counter;
}
else {
if (crypto == null) {
if (streamBytes != null)
streamBytes.WriteTo(os);
else
os.Write(bytes, 0, bytes.Length);
}
else {
if (crypto != null && !crypto.IsEmbeddedFilesOnly()) {
byte[] b;
if (streamBytes != null) {
b = crypto.EncryptByteArray(streamBytes.ToArray());
@ -297,6 +325,12 @@ namespace iTextSharp.text.pdf {
}
os.Write(b, 0, b.Length);
}
else {
if (streamBytes != null)
streamBytes.WriteTo(os);
else
os.Write(bytes, 0, bytes.Length);
}
}
os.Write(ENDSTREAM, 0, ENDSTREAM.Length);
}

View File

@ -224,13 +224,12 @@ namespace iTextSharp.text.pdf {
/**
* Gets the stream representing this template.
*
* @param compressionLevel the compressionLevel
* @return the stream representing this template
* @since 2.1.3 (replacing the method without param compressionLevel)
*/
internal virtual PdfStream FormXObject {
get {
return new PdfFormXObject(this);
}
internal virtual PdfStream GetFormXObject(int compressionLevel) {
return new PdfFormXObject(this, compressionLevel);
}
/**

View File

@ -277,7 +277,7 @@ namespace iTextSharp.text.pdf {
int first = index.Size;
index.Append(streamObjects);
PdfStream stream = new PdfStream(index.ToByteArray());
stream.FlateCompress();
stream.FlateCompress(writer.CompressionLevel);
stream.Put(PdfName.TYPE, PdfName.OBJSTM);
stream.Put(PdfName.N, new PdfNumber(numObj));
stream.Put(PdfName.FIRST, new PdfNumber(first));
@ -452,7 +452,7 @@ namespace iTextSharp.text.pdf {
}
PdfStream xr = new PdfStream(buf.ToByteArray());
buf = null;
xr.FlateCompress();
xr.FlateCompress(writer.CompressionLevel);
xr.Put(PdfName.SIZE, new PdfNumber(Size));
xr.Put(PdfName.ROOT, root);
if (info != null) {
@ -1182,7 +1182,7 @@ namespace iTextSharp.text.pdf {
if (template != null && template.IndirectReference is PRIndirectReference)
continue;
if (template != null && template.Type == PdfTemplate.TYPE_TEMPLATE) {
AddToBody(template.FormXObject, template.IndirectReference);
AddToBody(template.GetFormXObject(compressionLevel), template.IndirectReference);
}
}
// add all the dependencies in the imported pages
@ -1197,7 +1197,7 @@ namespace iTextSharp.text.pdf {
}
// add the pattern
foreach (PdfPatternPainter pat in documentPatterns.Keys) {
AddToBody(pat.Pattern, pat.IndirectReference);
AddToBody(pat.GetPattern(compressionLevel), pat.IndirectReference);
}
// add the shading patterns
foreach (PdfShadingPattern shadingPattern in documentShadingPatterns.Keys) {
@ -1746,7 +1746,7 @@ namespace iTextSharp.text.pdf {
outa.Put(PdfName.INFO, new PdfString(info, PdfObject.TEXT_UNICODE));
if (destOutputProfile != null) {
PdfStream stream = new PdfStream(destOutputProfile);
stream.FlateCompress();
stream.FlateCompress(compressionLevel);
outa.Put(PdfName.DESTOUTPUTPROFILE, AddToBody(stream).IndirectReference);
}
outa.Put(PdfName.S, PdfName.GTS_PDFX);
@ -1809,6 +1809,11 @@ namespace iTextSharp.text.pdf {
internal const int ENCRYPTION_MASK = 7;
/** Add this to the mode to keep the metadata in clear text */
public const int DO_NOT_ENCRYPT_METADATA = 8;
/**
* Add this to the mode to keep encrypt only the embedded files.
* @since 2.1.3
*/
public const int EMBEDDED_FILES_ONLY = 24;
// permissions
@ -2018,6 +2023,29 @@ namespace iTextSharp.text.pdf {
SetAtLeastPdfVersion(VERSION_1_5);
}
/**
* The compression level of the content streams.
* @since 2.1.3
*/
protected internal int compressionLevel = PdfStream.DEFAULT_COMPRESSION;
/**
* Sets the compression level to be used for streams written by this writer.
* @param compressionLevel a value between 0 (best speed) and 9 (best compression)
* @since 2.1.3
*/
public int CompressionLevel {
set {
if (compressionLevel < PdfStream.NO_COMPRESSION || compressionLevel > PdfStream.BEST_COMPRESSION)
compressionLevel = PdfStream.DEFAULT_COMPRESSION;
else
compressionLevel = value;
}
get {
return compressionLevel;
}
}
// [F3] adding fonts
/** The fonts of this document */
@ -2111,7 +2139,7 @@ namespace iTextSharp.text.pdf {
if (template.IndirectReference is PRIndirectReference)
return;
if (template.Type == PdfTemplate.TYPE_TEMPLATE) {
AddToBody(template.FormXObject, template.IndirectReference);
AddToBody(template.GetFormXObject(compressionLevel), template.IndirectReference);
objs[1] = null;
}
}
@ -2841,7 +2869,7 @@ namespace iTextSharp.text.pdf {
}
PdfImage i = new PdfImage(image, "img" + images.Count, maskRef);
if (image.HasICCProfile()) {
PdfICCBased icc = new PdfICCBased(image.TagICC);
PdfICCBased icc = new PdfICCBased(image.TagICC, image.CompressionLevel);
PdfIndirectReference iccRef = Add(icc);
PdfArray iccArray = new PdfArray();
iccArray.Add(PdfName.ICCBASED);

View File

@ -6,7 +6,7 @@ using System.Collections;
using iTextSharp.text;
/*
* $Id: TrueTypeFont.cs,v 1.13 2008/06/01 13:17:07 psoares33 Exp $
* $Id: TrueTypeFont.cs,v 1.12 2008/05/13 11:25:23 psoares33 Exp $
*
*
* Copyright 2001, 2002 Paulo Soares
@ -1258,7 +1258,7 @@ namespace iTextSharp.text.pdf {
string subsetPrefix = "";
if (embedded) {
if (cff) {
pobj = new StreamFont(ReadCffFont(), "Type1C");
pobj = new StreamFont(ReadCffFont(), "Type1C", compressionLevel);
obj = writer.AddToBody(pobj);
ind_font = obj.IndirectReference;
}
@ -1294,7 +1294,7 @@ namespace iTextSharp.text.pdf {
b = GetFullFont();
}
int[] lengths = new int[]{b.Length};
pobj = new StreamFont(b, lengths);
pobj = new StreamFont(b, lengths, compressionLevel);
obj = writer.AddToBody(pobj);
ind_font = obj.IndirectReference;
}
@ -1341,12 +1341,12 @@ namespace iTextSharp.text.pdf {
*/
public override PdfStream GetFullFontStream() {
if (cff) {
return new StreamFont(ReadCffFont(), "Type1C");
return new StreamFont(ReadCffFont(), "Type1C", compressionLevel);
}
else {
byte[] b = GetFullFont();
int[] lengths = new int[]{b.Length};
return new StreamFont(b, lengths);
return new StreamFont(b, lengths, compressionLevel);
}
}

View File

@ -4,7 +4,7 @@ using System.Text;
using System.Collections;
/*
* $Id: TrueTypeFontUnicode.cs,v 1.13 2008/06/01 13:17:07 psoares33 Exp $
* $Id: TrueTypeFontUnicode.cs,v 1.12 2008/05/13 11:25:23 psoares33 Exp $
*
*
* Copyright 2001, 2002 Paulo Soares
@ -207,7 +207,7 @@ namespace iTextSharp.text.pdf {
"end end\n");
string s = buf.ToString();
PdfStream stream = new PdfStream(PdfEncodings.ConvertToBytes(s, null));
stream.FlateCompress();
stream.FlateCompress(compressionLevel);
return stream;
}
@ -351,7 +351,7 @@ namespace iTextSharp.text.pdf {
bt[v / 8] |= rotbits[v % 8];
}
stream = new PdfStream(bt);
stream.FlateCompress();
stream.FlateCompress(compressionLevel);
}
cidset = writer.AddToBody(stream).IndirectReference;
}
@ -363,7 +363,7 @@ namespace iTextSharp.text.pdf {
b = cffs.Process((cffs.GetNames())[0] );
}
pobj = new StreamFont(b, "CIDFontType0C");
pobj = new StreamFont(b, "CIDFontType0C", compressionLevel);
obj = writer.AddToBody(pobj);
ind_font = obj.IndirectReference;
} else {
@ -376,7 +376,7 @@ namespace iTextSharp.text.pdf {
b = GetFullFont();
}
int[] lengths = new int[]{b.Length};
pobj = new StreamFont(b, lengths);
pobj = new StreamFont(b, lengths, compressionLevel);
obj = writer.AddToBody(pobj);
ind_font = obj.IndirectReference;
}
@ -409,7 +409,7 @@ namespace iTextSharp.text.pdf {
*/
public override PdfStream GetFullFontStream() {
if (cff) {
return new StreamFont(ReadCffFont(), "CIDFontType0C");
return new StreamFont(ReadCffFont(), "CIDFontType0C", compressionLevel);
}
return base.GetFullFontStream();
}

View File

@ -4,7 +4,7 @@ using System.Collections;
using System.util;
/*
* $Id: Type1Font.cs,v 1.14 2008/06/01 13:17:07 psoares33 Exp $
* $Id: Type1Font.cs,v 1.13 2008/05/13 11:25:23 psoares33 Exp $
*
*
* Copyright 2001, 2002 Paulo Soares
@ -503,7 +503,7 @@ namespace iTextSharp.text.pdf {
size -= got;
}
}
return new StreamFont(st, lengths);
return new StreamFont(st, lengths, compressionLevel);
}
finally {
if (rf != null) {

View File

@ -238,7 +238,7 @@ namespace iTextSharp.text.pdf {
diffs.Add(n);
Type3Glyph glyph = (Type3Glyph)char2glyph[(char)c2];
PdfStream stream = new PdfStream(glyph.ToPdf(null));
stream.FlateCompress();
stream.FlateCompress(compressionLevel);
PdfIndirectReference refp = writer.AddToBody(stream).IndirectReference;
charprocs.Put(n, refp);
}