Porting update.

git-svn-id: svn://svn.code.sf.net/p/itextsharp/code/trunk@28 820d3149-562b-4f88-9aa4-a8e61a3485cf
master
psoares33 2009-06-12 19:50:32 +00:00
parent 404aa891e5
commit 9084839eac
28 changed files with 3606 additions and 3167 deletions

File diff suppressed because it is too large Load Diff

View File

@ -1,218 +1,217 @@
using System;
using System.Text;
using System.Collections;
using System.Net;
using System.IO;
/*
* Copyright 2003 by Paulo Soares.
*
* The contents of this file are subject to the Mozilla Public License Version 1.1
* (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the License.
*
* The Original Code is 'iText, a free JAVA-PDF library'.
*
* The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
* the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
* All Rights Reserved.
* Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
* are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
*
* Contributor(s): all the names of the contributors are added in the source code
* where applicable.
*
* Alternatively, the contents of this file may be used under the terms of the
* LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
* provisions of LGPL are applicable instead of those above. If you wish to
* allow use of your version of this file only under the terms of the LGPL
* License and not to allow others to use your version of this file under
* the MPL, indicate your decision by deleting the provisions above and
* replace them with the notice and other provisions required by the LGPL.
* If you do not delete the provisions above, a recipient may use your version
* of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the MPL as stated above or under the terms of the GNU
* Library General Public License as published by the Free Software Foundation;
* either version 2 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
* details.
*
* If you didn't download this code from the following link, you should check if
* you aren't using an obsolete version:
* http://www.lowagie.com/iText/
*/
namespace iTextSharp.text.pdf {
/** Reads an FDF form and makes the fields available
* @author Paulo Soares (psoares@consiste.pt)
*/
public class FdfReader : PdfReader {
internal Hashtable fields;
internal String fileSpec;
internal PdfName encoding;
/** Reads an FDF form.
* @param filename the file name of the form
* @throws IOException on error
*/
public FdfReader(String filename) : base(filename) {
}
/** Reads an FDF form.
* @param pdfIn the byte array with the form
* @throws IOException on error
*/
public FdfReader(byte[] pdfIn) : base(pdfIn) {
}
/** Reads an FDF form.
* @param url the URL of the document
* @throws IOException on error
*/
public FdfReader(Uri url) : base(url) {
}
/** Reads an FDF form.
* @param is the <CODE>InputStream</CODE> containing the document. The stream is read to the
* end but is not closed
* @throws IOException on error
*/
public FdfReader(Stream isp) : base(isp) {
}
protected internal override void ReadPdf() {
fields = new Hashtable();
try {
tokens.CheckFdfHeader();
RebuildXref();
ReadDocObj();
}
finally {
try {
tokens.Close();
}
catch {
// empty on purpose
}
}
ReadFields();
}
protected virtual void KidNode(PdfDictionary merged, String name) {
PdfArray kids = (PdfArray)GetPdfObject(merged.Get(PdfName.KIDS));
if (kids == null || kids.ArrayList.Count == 0) {
if (name.Length > 0)
name = name.Substring(1);
fields[name] = merged;
}
else {
merged.Remove(PdfName.KIDS);
ArrayList ar = kids.ArrayList;
for (int k = 0; k < ar.Count; ++k) {
PdfDictionary dic = new PdfDictionary();
dic.Merge(merged);
PdfDictionary newDic = (PdfDictionary)GetPdfObject((PdfObject)ar[k]);
PdfString t = (PdfString)GetPdfObject(newDic.Get(PdfName.T));
String newName = name;
if (t != null)
newName += "." + t.ToUnicodeString();
dic.Merge(newDic);
dic.Remove(PdfName.T);
KidNode(dic, newName);
}
}
}
protected virtual void ReadFields() {
catalog = (PdfDictionary)GetPdfObject(trailer.Get(PdfName.ROOT));
PdfDictionary fdf = (PdfDictionary)GetPdfObject(catalog.Get(PdfName.FDF));
if (fdf == null)
return;
PdfString fs = (PdfString)GetPdfObject(fdf.Get(PdfName.F));
if (fs != null)
fileSpec = fs.ToUnicodeString();
PdfArray fld = (PdfArray)GetPdfObject(fdf.Get(PdfName.FIELDS));
if (fld == null)
return;
encoding = (PdfName)GetPdfObject(fdf.Get(PdfName.ENCODING));
PdfDictionary merged = new PdfDictionary();
merged.Put(PdfName.KIDS, fld);
KidNode(merged, "");
}
/** Gets all the fields. The map is keyed by the fully qualified
* field name and the value is a merged <CODE>PdfDictionary</CODE>
* with the field content.
* @return all the fields
*/
public Hashtable Fields {
get {
return fields;
}
}
/** Gets the field dictionary.
* @param name the fully qualified field name
* @return the field dictionary
*/
public PdfDictionary GetField(String name) {
return (PdfDictionary)fields[name];
}
/** Gets the field value or <CODE>null</CODE> if the field does not
* exist or has no value defined.
* @param name the fully qualified field name
* @return the field value or <CODE>null</CODE>
*/
public String GetFieldValue(String name) {
PdfDictionary field = (PdfDictionary)fields[name];
if (field == null)
return null;
PdfObject v = GetPdfObject(field.Get(PdfName.V));
if (v == null)
return null;
if (v.IsName())
return PdfName.DecodeName(((PdfName)v).ToString());
else if (v.IsString()) {
PdfString vs = (PdfString)v;
if (encoding == null || vs.Encoding != null)
return vs.ToUnicodeString();
byte[] b = vs.GetBytes();
if (b.Length >= 2 && b[0] == (byte)254 && b[1] == (byte)255)
return vs.ToUnicodeString();
try {
if (encoding.Equals(PdfName.SHIFT_JIS))
return Encoding.GetEncoding(932).GetString(b);
else if (encoding.Equals(PdfName.UHC))
return Encoding.GetEncoding(949).GetString(b);
else if (encoding.Equals(PdfName.GBK))
return Encoding.GetEncoding(936).GetString(b);
else if (encoding.Equals(PdfName.BIGFIVE))
return Encoding.GetEncoding(950).GetString(b);
}
catch {
}
return vs.ToUnicodeString();
}
return null;
}
/** Gets the PDF file specification contained in the FDF.
* @return the PDF file specification contained in the FDF
*/
public String FileSpec {
get {
return fileSpec;
}
}
}
using System;
using System.Text;
using System.Collections;
using System.Net;
using System.IO;
/*
* Copyright 2003 by Paulo Soares.
*
* The contents of this file are subject to the Mozilla Public License Version 1.1
* (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the License.
*
* The Original Code is 'iText, a free JAVA-PDF library'.
*
* The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
* the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
* All Rights Reserved.
* Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
* are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
*
* Contributor(s): all the names of the contributors are added in the source code
* where applicable.
*
* Alternatively, the contents of this file may be used under the terms of the
* LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
* provisions of LGPL are applicable instead of those above. If you wish to
* allow use of your version of this file only under the terms of the LGPL
* License and not to allow others to use your version of this file under
* the MPL, indicate your decision by deleting the provisions above and
* replace them with the notice and other provisions required by the LGPL.
* If you do not delete the provisions above, a recipient may use your version
* of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the MPL as stated above or under the terms of the GNU
* Library General Public License as published by the Free Software Foundation;
* either version 2 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
* details.
*
* If you didn't download this code from the following link, you should check if
* you aren't using an obsolete version:
* http://www.lowagie.com/iText/
*/
namespace iTextSharp.text.pdf {
/** Reads an FDF form and makes the fields available
* @author Paulo Soares (psoares@consiste.pt)
*/
public class FdfReader : PdfReader {
internal Hashtable fields;
internal String fileSpec;
internal PdfName encoding;
/** Reads an FDF form.
* @param filename the file name of the form
* @throws IOException on error
*/
public FdfReader(String filename) : base(filename) {
}
/** Reads an FDF form.
* @param pdfIn the byte array with the form
* @throws IOException on error
*/
public FdfReader(byte[] pdfIn) : base(pdfIn) {
}
/** Reads an FDF form.
* @param url the URL of the document
* @throws IOException on error
*/
public FdfReader(Uri url) : base(url) {
}
/** Reads an FDF form.
* @param is the <CODE>InputStream</CODE> containing the document. The stream is read to the
* end but is not closed
* @throws IOException on error
*/
public FdfReader(Stream isp) : base(isp) {
}
protected internal override void ReadPdf() {
fields = new Hashtable();
try {
tokens.CheckFdfHeader();
RebuildXref();
ReadDocObj();
}
finally {
try {
tokens.Close();
}
catch {
// empty on purpose
}
}
ReadFields();
}
protected virtual void KidNode(PdfDictionary merged, String name) {
PdfArray kids = merged.GetAsArray(PdfName.KIDS);
if (kids == null || kids.Size == 0) {
if (name.Length > 0)
name = name.Substring(1);
fields[name] = merged;
}
else {
merged.Remove(PdfName.KIDS);
for (int k = 0; k < kids.Size; ++k) {
PdfDictionary dic = new PdfDictionary();
dic.Merge(merged);
PdfDictionary newDic = kids.GetAsDict(k);
PdfString t = newDic.GetAsString(PdfName.T);
String newName = name;
if (t != null)
newName += "." + t.ToUnicodeString();
dic.Merge(newDic);
dic.Remove(PdfName.T);
KidNode(dic, newName);
}
}
}
protected virtual void ReadFields() {
catalog = trailer.GetAsDict(PdfName.ROOT);
PdfDictionary fdf = catalog.GetAsDict(PdfName.FDF);
if (fdf == null)
return;
PdfString fs = fdf.GetAsString(PdfName.F);
if (fs != null)
fileSpec = fs.ToUnicodeString();
PdfArray fld = fdf.GetAsArray(PdfName.FIELDS);
if (fld == null)
return;
encoding = fdf.GetAsName(PdfName.ENCODING);
PdfDictionary merged = new PdfDictionary();
merged.Put(PdfName.KIDS, fld);
KidNode(merged, "");
}
/** Gets all the fields. The map is keyed by the fully qualified
* field name and the value is a merged <CODE>PdfDictionary</CODE>
* with the field content.
* @return all the fields
*/
public Hashtable Fields {
get {
return fields;
}
}
/** Gets the field dictionary.
* @param name the fully qualified field name
* @return the field dictionary
*/
public PdfDictionary GetField(String name) {
return (PdfDictionary)fields[name];
}
/** Gets the field value or <CODE>null</CODE> if the field does not
* exist or has no value defined.
* @param name the fully qualified field name
* @return the field value or <CODE>null</CODE>
*/
public String GetFieldValue(String name) {
PdfDictionary field = (PdfDictionary)fields[name];
if (field == null)
return null;
PdfObject v = GetPdfObject(field.Get(PdfName.V));
if (v == null)
return null;
if (v.IsName())
return PdfName.DecodeName(((PdfName)v).ToString());
else if (v.IsString()) {
PdfString vs = (PdfString)v;
if (encoding == null || vs.Encoding != null)
return vs.ToUnicodeString();
byte[] b = vs.GetBytes();
if (b.Length >= 2 && b[0] == (byte)254 && b[1] == (byte)255)
return vs.ToUnicodeString();
try {
if (encoding.Equals(PdfName.SHIFT_JIS))
return Encoding.GetEncoding(932).GetString(b);
else if (encoding.Equals(PdfName.UHC))
return Encoding.GetEncoding(949).GetString(b);
else if (encoding.Equals(PdfName.GBK))
return Encoding.GetEncoding(936).GetString(b);
else if (encoding.Equals(PdfName.BIGFIVE))
return Encoding.GetEncoding(950).GetString(b);
}
catch {
}
return vs.ToUnicodeString();
}
return null;
}
/** Gets the PDF file specification contained in the FDF.
* @return the PDF file specification contained in the FDF
*/
public String FileSpec {
get {
return fileSpec;
}
}
}
}

View File

@ -1,324 +1,348 @@
using System;
using System.util;
using System.IO;
using System.Collections;
/*
* Copyright 2003 by Paulo Soares.
*
* The contents of this file are subject to the Mozilla Public License Version 1.1
* (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the License.
*
* The Original Code is 'iText, a free JAVA-PDF library'.
*
* The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
* the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
* All Rights Reserved.
* Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
* are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
*
* Contributor(s): all the names of the contributors are added in the source code
* where applicable.
*
* Alternatively, the contents of this file may be used under the terms of the
* LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
* provisions of LGPL are applicable instead of those above. If you wish to
* allow use of your version of this file only under the terms of the LGPL
* License and not to allow others to use your version of this file under
* the MPL, indicate your decision by deleting the provisions above and
* replace them with the notice and other provisions required by the LGPL.
* If you do not delete the provisions above, a recipient may use your version
* of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the MPL as stated above or under the terms of the GNU
* Library General Public License as published by the Free Software Foundation;
* either version 2 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
* details.
*
* If you didn't download this code from the following link, you should check if
* you aren't using an obsolete version:
* http://www.lowagie.com/iText/
*/
namespace iTextSharp.text.pdf {
/** Writes an FDF form.
* @author Paulo Soares (psoares@consiste.pt)
*/
public class FdfWriter {
private static readonly byte[] HEADER_FDF = DocWriter.GetISOBytes("%FDF-1.2\n%\u00e2\u00e3\u00cf\u00d3\n");
Hashtable fields = new Hashtable();
/** The PDF file associated with the FDF. */
private String file;
/** Creates a new FdfWriter. */
public FdfWriter() {
}
/** Writes the content to a stream.
* @param os the stream
* @throws DocumentException on error
* @throws IOException on error
*/
public void WriteTo(Stream os) {
Wrt wrt = new Wrt(os, this);
wrt.WriteTo();
}
internal bool SetField(String field, PdfObject value) {
Hashtable map = fields;
StringTokenizer tk = new StringTokenizer(field, ".");
if (!tk.HasMoreTokens())
return false;
while (true) {
String s = tk.NextToken();
Object obj = map[s];
if (tk.HasMoreTokens()) {
if (obj == null) {
obj = new Hashtable();
map[s] = obj;
map = (Hashtable)obj;
continue;
}
else if (obj is Hashtable)
map = (Hashtable)obj;
else
return false;
}
else {
if (!(obj is Hashtable)) {
map[s] = value;
return true;
}
else
return false;
}
}
}
internal void IterateFields(Hashtable values, Hashtable map, String name) {
foreach (DictionaryEntry entry in map) {
String s = (String)entry.Key;
Object obj = entry.Value;
if (obj is Hashtable)
IterateFields(values, (Hashtable)obj, name + "." + s);
else
values[(name + "." + s).Substring(1)] = obj;
}
}
/** Removes the field value.
* @param field the field name
* @return <CODE>true</CODE> if the field was found and removed,
* <CODE>false</CODE> otherwise
*/
public bool RemoveField(String field) {
Hashtable map = fields;
StringTokenizer tk = new StringTokenizer(field, ".");
if (!tk.HasMoreTokens())
return false;
ArrayList hist = new ArrayList();
while (true) {
String s = tk.NextToken();
Object obj = map[s];
if (obj == null)
return false;
hist.Add(map);
hist.Add(s);
if (tk.HasMoreTokens()) {
if (obj is Hashtable)
map = (Hashtable)obj;
else
return false;
}
else {
if (obj is Hashtable)
return false;
else
break;
}
}
for (int k = hist.Count - 2; k >= 0; k -= 2) {
map = (Hashtable)hist[k];
String s = (String)hist[k + 1];
map.Remove(s);
if (map.Count > 0)
break;
}
return true;
}
/** Gets all the fields. The map is keyed by the fully qualified
* field name and the values are <CODE>PdfObject</CODE>.
* @return a map with all the fields
*/
public Hashtable GetFields() {
Hashtable values = new Hashtable();
IterateFields(values, fields, "");
return values;
}
/** Gets the field value.
* @param field the field name
* @return the field value or <CODE>null</CODE> if not found
*/
public String GetField(String field) {
Hashtable map = fields;
StringTokenizer tk = new StringTokenizer(field, ".");
if (!tk.HasMoreTokens())
return null;
while (true) {
String s = tk.NextToken();
Object obj = map[s];
if (obj == null)
return null;
if (tk.HasMoreTokens()) {
if (obj is Hashtable)
map = (Hashtable)obj;
else
return null;
}
else {
if (obj is Hashtable)
return null;
else {
if (((PdfObject)obj).IsString())
return ((PdfString)obj).ToUnicodeString();
else
return PdfName.DecodeName(obj.ToString());
}
}
}
}
/** Sets the field value as a name.
* @param field the fully qualified field name
* @param value the value
* @return <CODE>true</CODE> if the value was inserted,
* <CODE>false</CODE> if the name is incompatible with
* an existing field
*/
public bool SetFieldAsName(String field, String value) {
return SetField(field, new PdfName(value));
}
/** Sets the field value as a string.
* @param field the fully qualified field name
* @param value the value
* @return <CODE>true</CODE> if the value was inserted,
* <CODE>false</CODE> if the name is incompatible with
* an existing field
*/
public bool SetFieldAsString(String field, String value) {
return SetField(field, new PdfString(value, PdfObject.TEXT_UNICODE));
}
/** Sets all the fields from this <CODE>FdfReader</CODE>
* @param fdf the <CODE>FdfReader</CODE>
*/
public void SetFields(FdfReader fdf) {
Hashtable map = fdf.Fields;
foreach (DictionaryEntry entry in map) {
String key = (String)entry.Key;
PdfDictionary dic = (PdfDictionary)entry.Value;
PdfObject v = dic.Get(PdfName.V);
if (v != null) {
SetField(key, v);
}
}
}
/** Sets all the fields from this <CODE>PdfReader</CODE>
* @param pdf the <CODE>PdfReader</CODE>
*/
public void SetFields(PdfReader pdf) {
SetFields(pdf.AcroFields);
}
/** Sets all the fields from this <CODE>AcroFields</CODE>
* @param acro the <CODE>AcroFields</CODE>
*/
public void SetFields(AcroFields af) {
foreach (DictionaryEntry entry in af.Fields) {
String fn = (String)entry.Key;
AcroFields.Item item = (AcroFields.Item)entry.Value;
PdfDictionary dic = (PdfDictionary)item.merged[0];
PdfObject v = PdfReader.GetPdfObjectRelease(dic.Get(PdfName.V));
if (v == null)
continue;
PdfObject ft = PdfReader.GetPdfObjectRelease(dic.Get(PdfName.FT));
if (ft == null || PdfName.SIG.Equals(ft))
continue;
SetField(fn, v);
}
}
/** Gets the PDF file name associated with the FDF.
* @return the PDF file name associated with the FDF
*/
public String File {
get {
return this.file;
}
set {
file = value;
}
}
internal class Wrt : PdfWriter {
private FdfWriter fdf;
internal Wrt(Stream os, FdfWriter fdf) : base(new PdfDocument(), os) {
this.fdf = fdf;
this.os.Write(HEADER_FDF, 0, HEADER_FDF.Length);
body = new PdfBody(this);
}
internal void WriteTo() {
PdfDictionary dic = new PdfDictionary();
dic.Put(PdfName.FIELDS, Calculate(fdf.fields));
if (fdf.file != null)
dic.Put(PdfName.F, new PdfString(fdf.file, PdfObject.TEXT_UNICODE));
PdfDictionary fd = new PdfDictionary();
fd.Put(PdfName.FDF, dic);
PdfIndirectReference refi = AddToBody(fd).IndirectReference;
byte[] b = GetISOBytes("trailer\n");
os.Write(b, 0, b.Length);
PdfDictionary trailer = new PdfDictionary();
trailer.Put(PdfName.ROOT, refi);
trailer.ToPdf(null, os);
b = GetISOBytes("\n%%EOF\n");
os.Write(b, 0, b.Length);
os.Close();
}
internal PdfArray Calculate(Hashtable map) {
PdfArray ar = new PdfArray();
foreach (DictionaryEntry entry in map) {
String key = (String)entry.Key;
Object v = entry.Value;
PdfDictionary dic = new PdfDictionary();
dic.Put(PdfName.T, new PdfString(key, PdfObject.TEXT_UNICODE));
if (v is Hashtable) {
dic.Put(PdfName.KIDS, Calculate((Hashtable)v));
}
else {
dic.Put(PdfName.V, (PdfObject)v);
}
ar.Add(dic);
}
return ar;
}
}
}
using System;
using System.util;
using System.IO;
using System.Collections;
/*
* Copyright 2003 by Paulo Soares.
*
* The contents of this file are subject to the Mozilla Public License Version 1.1
* (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the License.
*
* The Original Code is 'iText, a free JAVA-PDF library'.
*
* The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
* the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
* All Rights Reserved.
* Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
* are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
*
* Contributor(s): all the names of the contributors are added in the source code
* where applicable.
*
* Alternatively, the contents of this file may be used under the terms of the
* LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
* provisions of LGPL are applicable instead of those above. If you wish to
* allow use of your version of this file only under the terms of the LGPL
* License and not to allow others to use your version of this file under
* the MPL, indicate your decision by deleting the provisions above and
* replace them with the notice and other provisions required by the LGPL.
* If you do not delete the provisions above, a recipient may use your version
* of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the MPL as stated above or under the terms of the GNU
* Library General Public License as published by the Free Software Foundation;
* either version 2 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
* details.
*
* If you didn't download this code from the following link, you should check if
* you aren't using an obsolete version:
* http://www.lowagie.com/iText/
*/
namespace iTextSharp.text.pdf {
/** Writes an FDF form.
* @author Paulo Soares (psoares@consiste.pt)
*/
public class FdfWriter {
private static readonly byte[] HEADER_FDF = DocWriter.GetISOBytes("%FDF-1.2\n%\u00e2\u00e3\u00cf\u00d3\n");
Hashtable fields = new Hashtable();
/** The PDF file associated with the FDF. */
private String file;
/** Creates a new FdfWriter. */
public FdfWriter() {
}
/** Writes the content to a stream.
* @param os the stream
* @throws DocumentException on error
* @throws IOException on error
*/
public void WriteTo(Stream os) {
Wrt wrt = new Wrt(os, this);
wrt.WriteTo();
}
internal bool SetField(String field, PdfObject value) {
Hashtable map = fields;
StringTokenizer tk = new StringTokenizer(field, ".");
if (!tk.HasMoreTokens())
return false;
while (true) {
String s = tk.NextToken();
Object obj = map[s];
if (tk.HasMoreTokens()) {
if (obj == null) {
obj = new Hashtable();
map[s] = obj;
map = (Hashtable)obj;
continue;
}
else if (obj is Hashtable)
map = (Hashtable)obj;
else
return false;
}
else {
if (!(obj is Hashtable)) {
map[s] = value;
return true;
}
else
return false;
}
}
}
internal void IterateFields(Hashtable values, Hashtable map, String name) {
foreach (DictionaryEntry entry in map) {
String s = (String)entry.Key;
Object obj = entry.Value;
if (obj is Hashtable)
IterateFields(values, (Hashtable)obj, name + "." + s);
else
values[(name + "." + s).Substring(1)] = obj;
}
}
/** Removes the field value.
* @param field the field name
* @return <CODE>true</CODE> if the field was found and removed,
* <CODE>false</CODE> otherwise
*/
public bool RemoveField(String field) {
Hashtable map = fields;
StringTokenizer tk = new StringTokenizer(field, ".");
if (!tk.HasMoreTokens())
return false;
ArrayList hist = new ArrayList();
while (true) {
String s = tk.NextToken();
Object obj = map[s];
if (obj == null)
return false;
hist.Add(map);
hist.Add(s);
if (tk.HasMoreTokens()) {
if (obj is Hashtable)
map = (Hashtable)obj;
else
return false;
}
else {
if (obj is Hashtable)
return false;
else
break;
}
}
for (int k = hist.Count - 2; k >= 0; k -= 2) {
map = (Hashtable)hist[k];
String s = (String)hist[k + 1];
map.Remove(s);
if (map.Count > 0)
break;
}
return true;
}
/** Gets all the fields. The map is keyed by the fully qualified
* field name and the values are <CODE>PdfObject</CODE>.
* @return a map with all the fields
*/
public Hashtable GetFields() {
Hashtable values = new Hashtable();
IterateFields(values, fields, "");
return values;
}
/** Gets the field value.
* @param field the field name
* @return the field value or <CODE>null</CODE> if not found
*/
public String GetField(String field) {
Hashtable map = fields;
StringTokenizer tk = new StringTokenizer(field, ".");
if (!tk.HasMoreTokens())
return null;
while (true) {
String s = tk.NextToken();
Object obj = map[s];
if (obj == null)
return null;
if (tk.HasMoreTokens()) {
if (obj is Hashtable)
map = (Hashtable)obj;
else
return null;
}
else {
if (obj is Hashtable)
return null;
else {
if (((PdfObject)obj).IsString())
return ((PdfString)obj).ToUnicodeString();
else
return PdfName.DecodeName(obj.ToString());
}
}
}
}
/** Sets the field value as a name.
* @param field the fully qualified field name
* @param value the value
* @return <CODE>true</CODE> if the value was inserted,
* <CODE>false</CODE> if the name is incompatible with
* an existing field
*/
public bool SetFieldAsName(String field, String value) {
return SetField(field, new PdfName(value));
}
/** Sets the field value as a string.
* @param field the fully qualified field name
* @param value the value
* @return <CODE>true</CODE> if the value was inserted,
* <CODE>false</CODE> if the name is incompatible with
* an existing field
*/
public bool SetFieldAsString(String field, String value) {
return SetField(field, new PdfString(value, PdfObject.TEXT_UNICODE));
}
/**
* Sets the field value as a <CODE>PDFAction</CODE>.
* For example, this method allows setting a form submit button action using {@link PdfAction#createSubmitForm(String, Object[], int)}.
* This method creates an <CODE>A</CODE> entry for the specified field in the underlying FDF file.
* Method contributed by Philippe Laflamme (plaflamme)
* @param field the fully qualified field name
* @param action the field's action
* @return <CODE>true</CODE> if the value was inserted,
* <CODE>false</CODE> if the name is incompatible with
* an existing field
* @since 2.1.5
*/
public bool SetFieldAsAction(String field, PdfAction action) {
return SetField(field, action);
}
/** Sets all the fields from this <CODE>FdfReader</CODE>
* @param fdf the <CODE>FdfReader</CODE>
*/
public void SetFields(FdfReader fdf) {
Hashtable map = fdf.Fields;
foreach (DictionaryEntry entry in map) {
String key = (String)entry.Key;
PdfDictionary dic = (PdfDictionary)entry.Value;
PdfObject v = dic.Get(PdfName.V);
if (v != null) {
SetField(key, v);
}
v = dic.Get(PdfName.A); // (plaflamme)
if (v != null) {
SetField(key, v);
}
}
}
/** Sets all the fields from this <CODE>PdfReader</CODE>
* @param pdf the <CODE>PdfReader</CODE>
*/
public void SetFields(PdfReader pdf) {
SetFields(pdf.AcroFields);
}
/** Sets all the fields from this <CODE>AcroFields</CODE>
* @param acro the <CODE>AcroFields</CODE>
*/
public void SetFields(AcroFields af) {
foreach (DictionaryEntry entry in af.Fields) {
String fn = (String)entry.Key;
AcroFields.Item item = (AcroFields.Item)entry.Value;
PdfDictionary dic = (PdfDictionary)item.merged[0];
//PdfDictionary dic = item.GetMerged(0);
PdfObject v = PdfReader.GetPdfObjectRelease(dic.Get(PdfName.V));
if (v == null)
continue;
PdfObject ft = PdfReader.GetPdfObjectRelease(dic.Get(PdfName.FT));
if (ft == null || PdfName.SIG.Equals(ft))
continue;
SetField(fn, v);
}
}
/** Gets the PDF file name associated with the FDF.
* @return the PDF file name associated with the FDF
*/
public String File {
get {
return this.file;
}
set {
file = value;
}
}
internal class Wrt : PdfWriter {
private FdfWriter fdf;
internal Wrt(Stream os, FdfWriter fdf) : base(new PdfDocument(), os) {
this.fdf = fdf;
this.os.Write(HEADER_FDF, 0, HEADER_FDF.Length);
body = new PdfBody(this);
}
internal void WriteTo() {
PdfDictionary dic = new PdfDictionary();
dic.Put(PdfName.FIELDS, Calculate(fdf.fields));
if (fdf.file != null)
dic.Put(PdfName.F, new PdfString(fdf.file, PdfObject.TEXT_UNICODE));
PdfDictionary fd = new PdfDictionary();
fd.Put(PdfName.FDF, dic);
PdfIndirectReference refi = AddToBody(fd).IndirectReference;
byte[] b = GetISOBytes("trailer\n");
os.Write(b, 0, b.Length);
PdfDictionary trailer = new PdfDictionary();
trailer.Put(PdfName.ROOT, refi);
trailer.ToPdf(null, os);
b = GetISOBytes("\n%%EOF\n");
os.Write(b, 0, b.Length);
os.Close();
}
internal PdfArray Calculate(Hashtable map) {
PdfArray ar = new PdfArray();
foreach (DictionaryEntry entry in map) {
String key = (String)entry.Key;
Object v = entry.Value;
PdfDictionary dic = new PdfDictionary();
dic.Put(PdfName.T, new PdfString(key, PdfObject.TEXT_UNICODE));
if (v is Hashtable) {
dic.Put(PdfName.KIDS, Calculate((Hashtable)v));
}
else if (v is PdfAction) { // (plaflamme)
dic.Put(PdfName.A, (PdfAction)v);
}
else {
dic.Put(PdfName.V, (PdfObject)v);
}
ar.Add(dic);
}
return ar;
}
}
}
}

View File

@ -87,11 +87,6 @@ namespace iTextSharp.text.pdf {
*/
private float top;
/**
* used to store the y position of the bottom of the page
*/
private float pageBottom;
/**
* ColumnText object used to do all the real work. This same object is used for all columns
*/
@ -188,7 +183,7 @@ namespace iTextSharp.text.pdf {
*/
public void AddColumn(float[] left, float[] right) {
ColumnDef nextDef = new ColumnDef(left, right, this);
simple = nextDef.IsSimple();
if (!nextDef.IsSimple()) simple = false;
columnDefs.Add(nextDef);
}
@ -223,6 +218,25 @@ namespace iTextSharp.text.pdf {
}
}
/**
* Adds a <CODE>Phrase</CODE> to the current text array.
* Will not have any effect if addElement() was called before.
* @param phrase the text
* @since 2.1.5
*/
public void AddText(Phrase phrase) {
columnText.AddText(phrase);
}
/**
* Adds a <CODE>Chunk</CODE> to the current text array.
* Will not have any effect if addElement() was called before.
* @param chunk the text
* @since 2.1.5
*/
public void AddText(Chunk chunk) {
columnText.AddText(chunk);
}
/**
* Add an element to be rendered in a column.
* Note that you can only add a <CODE>Phrase</CODE>
@ -262,7 +276,6 @@ namespace iTextSharp.text.pdf {
throw new DocumentException("MultiColumnText has no columns");
}
overflow = false;
pageBottom = document.Bottom;
float currentHeight = 0;
bool done = false;
while (!done) {
@ -421,10 +434,10 @@ namespace iTextSharp.text.pdf {
*/
private float GetColumnBottom() {
if (desiredHeight == AUTOMATIC) {
return pageBottom;
return document.Bottom;
} else {
return Math.Max(top - (desiredHeight - totalHeight), pageBottom);
}
return Math.Max(top - (desiredHeight - totalHeight), document.Bottom);
}
}
/**
@ -580,6 +593,7 @@ namespace iTextSharp.text.pdf {
internal float[] ResolvePositions(float[] positions) {
if (!IsSimple()) {
positions[1] = mc.top;
return positions;
}
if (mc.top == AUTOMATIC) {

View File

@ -1,5 +1,6 @@
using System;
using System.Collections;
using System.Text;
using System.util;
using iTextSharp.text;
/*
@ -812,13 +813,34 @@ namespace iTextSharp.text.pdf {
PdfAnnotation annotation = new PdfAnnotation(writer, new Rectangle(llx, lly, urx, ury));
if (newPage != 0) {
PdfIndirectReference refi = writer.GetPageReference(newPage);
destination.ArrayList[0] = refi;
destination[0] = refi;
}
if (destination != null) annotation.Put(PdfName.DEST, destination);
foreach (object key in parameters.Keys)
annotation.hashMap[key] = parameters[key];
return annotation;
}
/**
* Returns a String representation of the link.
* @return a String representation of the imported link
* @since 2.1.6
*/
public override String ToString() {
StringBuilder buf = new StringBuilder("Imported link: location [");
buf.Append(llx);
buf.Append(' ');
buf.Append(lly);
buf.Append(' ');
buf.Append(urx);
buf.Append(' ');
buf.Append(ury);
buf.Append("] destination ");
buf.Append(destination);
buf.Append(" parameters ");
buf.Append(parameters);
return buf.ToString();
}
}
}
}

View File

@ -234,13 +234,13 @@ public class PdfArray : PdfObject {
return true;
}
public bool Add(float[] values) {
public virtual bool Add(float[] values) {
for (int k = 0; k < values.Length; ++k)
arrayList.Add(new PdfNumber(values[k]));
return true;
}
public bool Add(int[] values) {
public virtual bool Add(int[] values) {
for (int k = 0; k < values.Length; ++k)
arrayList.Add(new PdfNumber(values[k]));
return true;
@ -258,7 +258,7 @@ public class PdfArray : PdfObject {
* last position currently set, plus 1.
* @since 2.1.5
*/
public void Add(int index, PdfObject element) {
public virtual void Add(int index, PdfObject element) {
arrayList.Insert(index, element);
}
@ -271,7 +271,7 @@ public class PdfArray : PdfObject {
*
* @param object The <CODE>PdfObject</CODE> to add
*/
public void AddFirst(PdfObject obj) {
public virtual void AddFirst(PdfObject obj) {
arrayList.Insert(0, obj);
}

View File

@ -170,6 +170,7 @@ namespace iTextSharp.text.pdf {
switch (ele.Type) {
case Element.JPEG:
case Element.JPEG2000:
case Element.JBIG2:
case Element.IMGRAW:
case Element.IMGTEMPLATE:
AddImage((Image)ele, left, right, 0.4f * leading, alignment);

View File

@ -361,6 +361,21 @@ namespace iTextSharp.text.pdf {
++currentPageNumber;
}
/**
* Adds a blank page.
* @param rect The page dimension
* @param rotation The rotation angle in degrees
* @since 2.1.5
*/
public void AddPage(Rectangle rect, int rotation) {
PdfRectangle mediabox = new PdfRectangle(rect, rotation);
PageResources resources = new PageResources();
PdfPage page = new PdfPage(mediabox, new Hashtable(), resources.Resources, 0);
page.Put(PdfName.TABS, Tabs);
root.AddPage(page);
++currentPageNumber;
}
/**
* Copy the acroform for an input document. Note that you can only have one,
* we make no effort to merge them.
@ -422,9 +437,7 @@ namespace iTextSharp.text.pdf {
foreach (PdfTemplate template in fieldTemplates.Keys) {
PdfFormField.MergeResources(dr, (PdfDictionary)template.Resources);
}
if (dr.Get(PdfName.ENCODING) == null)
dr.Put(PdfName.ENCODING, PdfName.WIN_ANSI_ENCODING);
PdfDictionary fonts = (PdfDictionary)PdfReader.GetPdfObject(dr.Get(PdfName.FONT));
PdfDictionary fonts = dr.GetAsDict(PdfName.FONT);
if (fonts == null) {
fonts = new PdfDictionary();
dr.Put(PdfName.FONT, fonts);
@ -541,7 +554,7 @@ namespace iTextSharp.text.pdf {
if (under == null) {
if (pageResources == null) {
pageResources = new PageResources();
PdfDictionary resources = (PdfDictionary)PdfReader.GetPdfObject(pageN.Get(PdfName.RESOURCES));
PdfDictionary resources = pageN.GetAsDict(PdfName.RESOURCES);
pageResources.SetOriginalResources(resources, cstp.namePtr);
}
under = new PdfCopy.StampContent(cstp, pageResources);
@ -553,7 +566,7 @@ namespace iTextSharp.text.pdf {
if (over == null) {
if (pageResources == null) {
pageResources = new PageResources();
PdfDictionary resources = (PdfDictionary)PdfReader.GetPdfObject(pageN.Get(PdfName.RESOURCES));
PdfDictionary resources = pageN.GetAsDict(PdfName.RESOURCES);
pageResources.SetOriginalResources(resources, cstp.namePtr);
}
over = new PdfCopy.StampContent(cstp, pageResources);

View File

@ -0,0 +1,134 @@
using System;
/*
* $Id: $
*
* Copyright 2009 by Bruno Lowagie.
*
* The contents of this file are subject to the Mozilla Public License Version 1.1
* (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the License.
*
* The Original Code is 'iText, a free JAVA-PDF library'.
*
* The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
* the Initial Developer are Copyright (C) 1999-2009 by Bruno Lowagie.
* All Rights Reserved.
* Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
* are Copyright (C) 2000-2009 by Paulo Soares. All Rights Reserved.
*
* Contributor(s): all the names of the contributors are added in the source code
* where applicable.
*
* Alternatively, the contents of this file may be used under the terms of the
* LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
* provisions of LGPL are applicable instead of those above. If you wish to
* allow use of your version of this file only under the terms of the LGPL
* License and not to allow others to use your version of this file under
* the MPL, indicate your decision by deleting the provisions above and
* replace them with the notice and other provisions required by the LGPL.
* If you do not delete the provisions above, a recipient may use your version
* of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the MPL as stated above or under the terms of the GNU
* Library General Public License as published by the Free Software Foundation;
* either version 2 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
* details.
*
* If you didn't download this code from the following link, you should check if
* you aren't using an obsolete version:
* http://www.lowagie.com/iText/
*/
namespace iTextSharp.text.pdf {
/**
* Beginning with BaseVersion 1.7, the extensions dictionary lets developers
* designate that a given document contains extensions to PDF. The presence
* of the extension dictionary in a document indicates that it may contain
* developer-specific PDF properties that extend a particular base version
* of the PDF specification.
* The extensions dictionary enables developers to identify their own extensions
* relative to a base version of PDF. Additionally, the convention identifies
* extension levels relative to that base version. The intent of this dictionary
* is to enable developers of PDF-producing applications to identify company-specific
* specifications (such as this one) that PDF-consuming applications use to
* interpret the extensions.
* @since 2.1.6
*/
public class PdfDeveloperExtension {
/** An instance of this class for Adobe 1.7 Extension level 3. */
public static readonly PdfDeveloperExtension ADOBE_1_7_EXTENSIONLEVEL3 =
new PdfDeveloperExtension(PdfName.ADBE, PdfWriter.PDF_VERSION_1_7, 3);
/** The prefix used in the Extensions dictionary added to the Catalog. */
protected PdfName prefix;
/** The base version. */
protected PdfName baseversion;
/** The extension level within the baseversion. */
protected int extensionLevel;
/**
* Creates a PdfDeveloperExtension object.
* @param prefix the prefix referring to the developer
* @param baseversion the number of the base version
* @param extensionLevel the extension level within the baseverion.
*/
public PdfDeveloperExtension(PdfName prefix, PdfName baseversion, int extensionLevel) {
this.prefix = prefix;
this.baseversion = baseversion;
this.extensionLevel = extensionLevel;
}
/**
* Gets the prefix name.
* @return a PdfName
*/
public PdfName Prefix {
get {
return prefix;
}
}
/**
* Gets the baseversion name.
* @return a PdfName
*/
public PdfName Baseversion {
get {
return baseversion;
}
}
/**
* Gets the extension level within the baseversion.
* @return an integer
*/
public int ExtensionLevel {
get {
return extensionLevel;
}
}
/**
* Generations the developer extension dictionary corresponding
* with the prefix.
* @return a PdfDictionary
*/
public PdfDictionary GetDeveloperExtensions() {
PdfDictionary developerextensions = new PdfDictionary();
developerextensions.Put(PdfName.BASEVERSION, baseversion);
developerextensions.Put(PdfName.EXTENSIONLEVEL, new PdfNumber(extensionLevel));
return developerextensions;
}
}
}

View File

@ -233,13 +233,17 @@ namespace iTextSharp.text.pdf {
foreach (DictionaryEntry entry in localDestinations) {
String name = (String)entry.Key;
Object[] obj = (Object[])entry.Value;
if (obj[2] == null) //no destination
continue;
PdfIndirectReference refi = (PdfIndirectReference)obj[1];
ar.Add(new PdfString(name, null));
ar.Add(refi);
}
PdfDictionary dests = new PdfDictionary();
dests.Put(PdfName.NAMES, ar);
names.Put(PdfName.DESTS, writer.AddToBody(dests).IndirectReference);
if (ar.Size > 0) {
PdfDictionary dests = new PdfDictionary();
dests.Put(PdfName.NAMES, ar);
names.Put(PdfName.DESTS, writer.AddToBody(dests).IndirectReference);
}
}
if (documentLevelJS.Count > 0) {
PdfDictionary tree = PdfNameTree.WriteTree(documentLevelJS, writer);
@ -248,7 +252,8 @@ namespace iTextSharp.text.pdf {
if (documentFileAttachment.Count > 0) {
names.Put(PdfName.EMBEDDEDFILES, writer.AddToBody(PdfNameTree.WriteTree(documentFileAttachment, writer)).IndirectReference);
}
Put(PdfName.NAMES, writer.AddToBody(names).IndirectReference);
if (names.Size > 0)
Put(PdfName.NAMES, writer.AddToBody(names).IndirectReference);
}
internal PdfAction OpenAction {
@ -320,6 +325,9 @@ namespace iTextSharp.text.pdf {
get {
return leading;
}
set {
leading = value;
}
}
/** This is the current height of the document. */
protected internal float currentHeight = 0;
@ -683,6 +691,7 @@ namespace iTextSharp.text.pdf {
}
case Element.JPEG:
case Element.JPEG2000:
case Element.JBIG2:
case Element.IMGRAW:
case Element.IMGTEMPLATE: {
//carriageReturn(); suggestion by Marc Campforts
@ -835,6 +844,7 @@ namespace iTextSharp.text.pdf {
// we create the page dictionary
PdfPage page = new PdfPage(new PdfRectangle(pageSize, rotation), thisBoxSize, resources, rotation);
page.Put(PdfName.TABS, writer.Tabs);
// we complete the page dictionary
@ -959,6 +969,16 @@ namespace iTextSharp.text.pdf {
return base.SetMarginMirroring(MarginMirroring);
}
/**
* @see com.lowagie.text.DocListener#setMarginMirroring(boolean)
* @since 2.1.6
*/
public override bool SetMarginMirroringTopBottom(bool MarginMirroringTopBottom) {
if (writer != null && writer.IsPaused()) {
return false;
}
return base.SetMarginMirroringTopBottom(MarginMirroringTopBottom);
}
// [L7] DocListener interface
/**
@ -1697,7 +1717,6 @@ namespace iTextSharp.text.pdf {
int style = f.Style;
style &= ~Font.UNDERLINE;
style &= ~Font.STRIKETHRU;
f.SetStyle(Font.UNDEFINED);
f.SetStyle(style);
}
Chunk space = new Chunk(" ", f);
@ -2160,8 +2179,14 @@ namespace iTextSharp.text.pdf {
marginLeft = nextMarginLeft;
marginRight = nextMarginRight;
}
marginTop = nextMarginTop;
marginBottom = nextMarginBottom;
if (marginMirroringTopBottom && (PageNumber & 1) == 0) {
marginTop = nextMarginBottom;
marginBottom = nextMarginTop;
}
else {
marginTop = nextMarginTop;
marginBottom = nextMarginBottom;
}
}
/**
@ -2364,14 +2389,17 @@ namespace iTextSharp.text.pdf {
*/
internal void AddPTable(PdfPTable ptable) {
ColumnText ct = new ColumnText(writer.DirectContent);
// if the table prefers to be on a single page, and it wouldn't
//fit on the current page, start a new page.
if (ptable.KeepTogether && !FitsPage(ptable, 0f) && currentHeight > 0) {
NewPage();
}
// add dummy paragraph if we aren't at the top of a page, so that
// spacingBefore will be taken into account by ColumnText
if (currentHeight > 0) {
Paragraph p = new Paragraph();
p.Leading = 0;
ct.AddElement(p);
//if the table prefers to be on a single page, and it wouldn't
//fit on the current page, start a new page.
if (ptable.KeepTogether && !FitsPage(ptable, 0f))
NewPage();
}
ct.AddElement(ptable);
bool he = ptable.HeadersInEvent;
@ -2405,7 +2433,8 @@ namespace iTextSharp.text.pdf {
}
// ensuring that a new line has been started.
EnsureNewLine();
return table.TotalHeight <= IndentTop - currentHeight - IndentBottom - margin;
return table.TotalHeight + ((currentHeight > 0) ? table.SpacingBefore : 0f)
<= IndentTop - currentHeight - IndentBottom - margin;
}
// [M4'] Adding a Table

View File

@ -314,7 +314,7 @@ namespace iTextSharp.text.pdf {
PdfName target = null;
for (int k = 0; k < mergeTarget.Length; ++k) {
target = mergeTarget[k];
PdfDictionary pdfDict = (PdfDictionary)PdfReader.GetPdfObject(source.Get(target));
PdfDictionary pdfDict = source.GetAsDict(target);
if ((dic = pdfDict) != null) {
if ((res = (PdfDictionary)PdfReader.GetPdfObject(result.Get(target), result)) == null) {
res = new PdfDictionary();

View File

@ -227,6 +227,18 @@ namespace iTextSharp.text.pdf {
streamBytes = new MemoryStream();
TransferBytes(isp, streamBytes, -1);
break;
case Image.JBIG2:
Put(PdfName.FILTER, PdfName.JBIG2DECODE);
Put(PdfName.COLORSPACE, PdfName.DEVICEGRAY);
Put(PdfName.BITSPERCOMPONENT, new PdfNumber(1));
if (image.RawData != null){
bytes = image.RawData;
Put(PdfName.LENGTH, new PdfNumber(bytes.Length));
return;
}
streamBytes = new MemoryStream();
TransferBytes(isp, streamBytes, -1);
break;
default:
throw new IOException(errorID + " is an unknown Image format.");
}

View File

@ -66,7 +66,7 @@ namespace iTextSharp.text.pdf {
internal PdfImportedPage(PdfReaderInstance readerInstance, PdfWriter writer, int pageNumber) {
this.readerInstance = readerInstance;
this.pageNumber = pageNumber;
thisReference = writer.PdfIndirectReference;
this.writer = writer;
bBox = readerInstance.Reader.GetPageSize(pageNumber);
SetMatrix(1, 0, 0, 1, -bBox.Left, -bBox.Bottom);
type = TYPE_IMPORTED;
@ -155,6 +155,12 @@ namespace iTextSharp.text.pdf {
ThrowError();
}
public override PdfTransparencyGroup Group {
set {
ThrowError();
}
}
internal void ThrowError() {
throw new Exception("Content can not be added to a PdfImportedPage.");
}

View File

@ -136,16 +136,14 @@ namespace iTextSharp.text.pdf {
private static void IterateItems(PdfDictionary dic, Hashtable items) {
PdfArray nn = (PdfArray)PdfReader.GetPdfObjectRelease(dic.Get(PdfName.NAMES));
if (nn != null) {
ArrayList arr = nn.ArrayList;
for (int k = 0; k < arr.Count; ++k) {
PdfString s = (PdfString)PdfReader.GetPdfObjectRelease((PdfObject)arr[k++]);
items[PdfEncodings.ConvertToString(s.GetBytes(), null)] = arr[k];
for (int k = 0; k < nn.Size; ++k) {
PdfString s = (PdfString)PdfReader.GetPdfObjectRelease(nn[k++]);
items[PdfEncodings.ConvertToString(s.GetBytes(), null)] = nn[k];
}
}
else if ((nn = (PdfArray)PdfReader.GetPdfObjectRelease(dic.Get(PdfName.KIDS))) != null) {
ArrayList arr = nn.ArrayList;
for (int k = 0; k < arr.Count; ++k) {
PdfDictionary kid = (PdfDictionary)PdfReader.GetPdfObjectRelease((PdfObject)arr[k]);
for (int k = 0; k < nn.Size; ++k) {
PdfDictionary kid = (PdfDictionary)PdfReader.GetPdfObjectRelease(nn[k]);
IterateItems(kid, items);
}
}

View File

@ -132,16 +132,14 @@ namespace iTextSharp.text.pdf {
private static void IterateItems(PdfDictionary dic, Hashtable items) {
PdfArray nn = (PdfArray)PdfReader.GetPdfObjectRelease(dic.Get(PdfName.NUMS));
if (nn != null) {
ArrayList arr = nn.ArrayList;
for (int k = 0; k < arr.Count; ++k) {
PdfNumber s = (PdfNumber)PdfReader.GetPdfObjectRelease((PdfObject)arr[k++]);
items[s.IntValue] = arr[k];
for (int k = 0; k < nn.Size; ++k) {
PdfNumber s = (PdfNumber)PdfReader.GetPdfObjectRelease(nn[k++]);
items[s.IntValue] = nn[k];
}
}
else if ((nn = (PdfArray)PdfReader.GetPdfObjectRelease(dic.Get(PdfName.KIDS))) != null) {
ArrayList arr = nn.ArrayList;
for (int k = 0; k < arr.Count; ++k) {
PdfDictionary kid = (PdfDictionary)PdfReader.GetPdfObjectRelease((PdfObject)arr[k]);
for (int k = 0; k < nn.Size; ++k) {
PdfDictionary kid = (PdfDictionary)PdfReader.GetPdfObjectRelease(nn[k]);
IterateItems(kid, items);
}
}

View File

@ -190,7 +190,20 @@ namespace iTextSharp.text.pdf {
* @return true if this object can be in an object stream.
*/
public bool CanBeInObjStm() {
return (type >= 1 && type <= 6) || type == 8;
switch (type) {
case NULL:
case BOOLEAN:
case NUMBER:
case STRING:
case NAME:
case ARRAY:
case DICTIONARY:
return true;
case STREAM:
case INDIRECT:
default:
return false;
}
}
/**

View File

@ -135,6 +135,9 @@ namespace iTextSharp.text.pdf {
nextParents.Add(writer.PdfIndirectReference);
top.Put(PdfName.PARENT, (PdfIndirectReference)nextParents[p / leafSize]);
}
else {
top.Put(PdfName.ITXT, new PdfString(Document.Release));
}
writer.AddToBody(top, (PdfIndirectReference)tParents[p]);
}
if (tParents.Count == 1) {

View File

@ -1,143 +1,143 @@
using System;
using iTextSharp.text;
/*
* Copyright 1999, 2000, 2001, 2002 Bruno Lowagie
*
* The contents of this file are subject to the Mozilla Public License Version 1.1
* (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the License.
*
* The Original Code is 'iText, a free JAVA-PDF library'.
*
* The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
* the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
* All Rights Reserved.
* Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
* are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
*
* Contributor(s): all the names of the contributors are added in the source code
* where applicable.
*
* Alternatively, the contents of this file may be used under the terms of the
* LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
* provisions of LGPL are applicable instead of those above. If you wish to
* allow use of your version of this file only under the terms of the LGPL
* License and not to allow others to use your version of this file under
* the MPL, indicate your decision by deleting the provisions above and
* replace them with the notice and other provisions required by the LGPL.
* If you do not delete the provisions above, a recipient may use your version
* of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the MPL as stated above or under the terms of the GNU
* Library General Public License as published by the Free Software Foundation;
* either version 2 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
* details.
*
* If you didn't download this code from the following link, you should check if
* you aren't using an obsolete version:
* http://www.lowagie.com/iText/
*/
namespace iTextSharp.text.pdf {
/**
* <CODE>PdfRectangle</CODE> is the PDF Rectangle object.
* <P>
* Rectangles are used to describe locations on the page and bounding boxes for several
* objects in PDF, such as fonts. A rectangle is represented as an <CODE>array</CODE> of
* four numbers, specifying the lower lef <I>x</I>, lower left <I>y</I>, upper right <I>x</I>,
* and upper right <I>y</I> coordinates of the rectangle, in that order.<BR>
* This object is described in the 'Portable Document Format Reference Manual version 1.3'
* section 7.1 (page 183).
*
* @see iTextSharp.text.Rectangle
* @see PdfArray
*/
public class PdfRectangle : PdfArray {
// membervariables
/** lower left x */
private float llx = 0;
/** lower left y */
private float lly = 0;
/** upper right x */
private float urx = 0;
/** upper right y */
private float ury = 0;
// constructors
/**
* Constructs a <CODE>PdfRectangle</CODE>-object.
*
* @param llx lower left x
* @param lly lower left y
* @param urx upper right x
* @param ury upper right y
*
* @since rugPdf0.10
*/
public PdfRectangle(float llx, float lly, float urx, float ury, int rotation) : base() {
if (rotation == 90 || rotation == 270) {
this.llx = lly;
this.lly = llx;
this.urx = ury;
this.ury = urx;
}
else {
this.llx = llx;
this.lly = lly;
this.urx = urx;
this.ury = ury;
}
base.Add(new PdfNumber(this.llx));
base.Add(new PdfNumber(this.lly));
base.Add(new PdfNumber(this.urx));
base.Add(new PdfNumber(this.ury));
}
public PdfRectangle(float llx, float lly, float urx, float ury) : this(llx, lly, urx, ury, 0) {}
/**
* Constructs a <CODE>PdfRectangle</CODE>-object starting from the origin (0, 0).
*
* @param urx upper right x
* @param ury upper right y
*/
public PdfRectangle(float urx, float ury, int rotation) : this(0, 0, urx, ury, rotation) {}
public PdfRectangle(float urx, float ury) : this(0, 0, urx, ury, 0) {}
/**
* Constructs a <CODE>PdfRectangle</CODE>-object with a <CODE>Rectangle</CODE>-object.
*
* @param rectangle a <CODE>Rectangle</CODE>
*/
public PdfRectangle(Rectangle rectangle, int rotation) : this(rectangle.Left, rectangle.Bottom, rectangle.Right, rectangle.Top, rotation) {}
public PdfRectangle(Rectangle rectangle) : this(rectangle.Left, rectangle.Bottom, rectangle.Right, rectangle.Top, 0) {}
// methods
using System;
using iTextSharp.text;
/*
* Copyright 1999, 2000, 2001, 2002 Bruno Lowagie
*
* The contents of this file are subject to the Mozilla Public License Version 1.1
* (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the License.
*
* The Original Code is 'iText, a free JAVA-PDF library'.
*
* The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
* the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
* All Rights Reserved.
* Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
* are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
*
* Contributor(s): all the names of the contributors are added in the source code
* where applicable.
*
* Alternatively, the contents of this file may be used under the terms of the
* LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
* provisions of LGPL are applicable instead of those above. If you wish to
* allow use of your version of this file only under the terms of the LGPL
* License and not to allow others to use your version of this file under
* the MPL, indicate your decision by deleting the provisions above and
* replace them with the notice and other provisions required by the LGPL.
* If you do not delete the provisions above, a recipient may use your version
* of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the MPL as stated above or under the terms of the GNU
* Library General Public License as published by the Free Software Foundation;
* either version 2 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
* details.
*
* If you didn't download this code from the following link, you should check if
* you aren't using an obsolete version:
* http://www.lowagie.com/iText/
*/
namespace iTextSharp.text.pdf {
/**
* <CODE>PdfRectangle</CODE> is the PDF Rectangle object.
* <P>
* Rectangles are used to describe locations on the page and bounding boxes for several
* objects in PDF, such as fonts. A rectangle is represented as an <CODE>array</CODE> of
* four numbers, specifying the lower lef <I>x</I>, lower left <I>y</I>, upper right <I>x</I>,
* and upper right <I>y</I> coordinates of the rectangle, in that order.<BR>
* This object is described in the 'Portable Document Format Reference Manual version 1.3'
* section 7.1 (page 183).
*
* @see iTextSharp.text.Rectangle
* @see PdfArray
*/
public class PdfRectangle : PdfArray {
// membervariables
/** lower left x */
private float llx = 0;
/** lower left y */
private float lly = 0;
/** upper right x */
private float urx = 0;
/** upper right y */
private float ury = 0;
// constructors
/**
* Constructs a <CODE>PdfRectangle</CODE>-object.
*
* @param llx lower left x
* @param lly lower left y
* @param urx upper right x
* @param ury upper right y
*
* @since rugPdf0.10
*/
public PdfRectangle(float llx, float lly, float urx, float ury, int rotation) : base() {
if (rotation == 90 || rotation == 270) {
this.llx = lly;
this.lly = llx;
this.urx = ury;
this.ury = urx;
}
else {
this.llx = llx;
this.lly = lly;
this.urx = urx;
this.ury = ury;
}
base.Add(new PdfNumber(this.llx));
base.Add(new PdfNumber(this.lly));
base.Add(new PdfNumber(this.urx));
base.Add(new PdfNumber(this.ury));
}
public PdfRectangle(float llx, float lly, float urx, float ury) : this(llx, lly, urx, ury, 0) {}
/**
* Constructs a <CODE>PdfRectangle</CODE>-object starting from the origin (0, 0).
*
* @param urx upper right x
* @param ury upper right y
*/
public PdfRectangle(float urx, float ury, int rotation) : this(0, 0, urx, ury, rotation) {}
public PdfRectangle(float urx, float ury) : this(0, 0, urx, ury, 0) {}
/**
* Constructs a <CODE>PdfRectangle</CODE>-object with a <CODE>Rectangle</CODE>-object.
*
* @param rectangle a <CODE>Rectangle</CODE>
*/
public PdfRectangle(Rectangle rectangle, int rotation) : this(rectangle.Left, rectangle.Bottom, rectangle.Right, rectangle.Top, rotation) {}
public PdfRectangle(Rectangle rectangle) : this(rectangle.Left, rectangle.Bottom, rectangle.Right, rectangle.Top, 0) {}
// methods
/**
* Returns the high level version of this PdfRectangle
* @return this PdfRectangle translated to class Rectangle
@ -148,143 +148,174 @@ namespace iTextSharp.text.pdf {
}
}
/**
* Overrides the <CODE>add</CODE>-method in <CODE>PdfArray</CODE> in order to prevent the adding of extra object to the array.
*
* @param object <CODE>PdfObject</CODE> to add (will not be added here)
* @return <CODE>false</CODE>
*/
public override bool Add(PdfObject obj) {
return false;
}
/**
* Overrides the <CODE>add</CODE>-method in <CODE>PdfArray</CODE> in order to prevent the adding of extra object to the array.
*
* @param object <CODE>PdfObject</CODE> to add (will not be added here)
* @return <CODE>false</CODE>
*/
public override bool Add(PdfObject obj) {
* Block changes to the underlying PdfArray
* @param values stuff we'll ignore. Ha!
* @return false. You can't add anything to a PdfRectangle
* @since 2.1.5
*/
public override bool Add( float[] values ) {
return false;
}
/**
* Returns the lower left x-coordinate.
*
* @return the lower left x-coordinaat
*/
public float Left {
get {
return llx;
}
* Block changes to the underlying PdfArray
* @param values stuff we'll ignore. Ha!
* @return false. You can't add anything to a PdfRectangle
* @since 2.1.5
*/
public override bool Add( int[] values ) {
return false;
}
/**
* Returns the upper right x-coordinate.
*
* @return the upper right x-coordinate
*/
public float Right {
get {
return urx;
}
}
/**
* Returns the upper right y-coordinate.
*
* @return the upper right y-coordinate
*/
public float Top {
get {
return ury;
}
}
/**
* Returns the lower left y-coordinate.
*
* @return the lower left y-coordinate
*/
public float Bottom {
get {
return lly;
}
}
/**
* Returns the lower left x-coordinate, considering a given margin.
*
* @param margin a margin
* @return the lower left x-coordinate
*/
public float GetLeft(int margin) {
return llx + margin;
}
/**
* Returns the upper right x-coordinate, considering a given margin.
*
* @param margin a margin
* @return the upper right x-coordinate
*/
public float GetRight(int margin) {
return urx - margin;
}
/**
* Returns the upper right y-coordinate, considering a given margin.
*
* @param margin a margin
* @return the upper right y-coordinate
*/
public float GetTop(int margin) {
return ury - margin;
}
/**
* Returns the lower left y-coordinate, considering a given margin.
*
* @param margin a margin
* @return the lower left y-coordinate
*/
public float GetBottom(int margin) {
return lly + margin;
}
/**
* Returns the width of the rectangle.
*
* @return a width
*/
public float Width {
get {
return urx - llx;
}
}
/**
* Returns the height of the rectangle.
*
* @return a height
*/
public float Height {
get {
return ury - lly;
}
}
/**
* Swaps the values of urx and ury and of lly and llx in order to rotate the rectangle.
*
* @return a <CODE>PdfRectangle</CODE>
*/
public PdfRectangle Rotate {
get {
return new PdfRectangle(lly, llx, ury, urx, 0);
}
}
}
* Block changes to the underlying PdfArray
* @param object Ignored.
* @since 2.1.5
*/
public override void AddFirst( PdfObject obj ) {
}
/**
* Returns the lower left x-coordinate.
*
* @return the lower left x-coordinaat
*/
public float Left {
get {
return llx;
}
}
/**
* Returns the upper right x-coordinate.
*
* @return the upper right x-coordinate
*/
public float Right {
get {
return urx;
}
}
/**
* Returns the upper right y-coordinate.
*
* @return the upper right y-coordinate
*/
public float Top {
get {
return ury;
}
}
/**
* Returns the lower left y-coordinate.
*
* @return the lower left y-coordinate
*/
public float Bottom {
get {
return lly;
}
}
/**
* Returns the lower left x-coordinate, considering a given margin.
*
* @param margin a margin
* @return the lower left x-coordinate
*/
public float GetLeft(int margin) {
return llx + margin;
}
/**
* Returns the upper right x-coordinate, considering a given margin.
*
* @param margin a margin
* @return the upper right x-coordinate
*/
public float GetRight(int margin) {
return urx - margin;
}
/**
* Returns the upper right y-coordinate, considering a given margin.
*
* @param margin a margin
* @return the upper right y-coordinate
*/
public float GetTop(int margin) {
return ury - margin;
}
/**
* Returns the lower left y-coordinate, considering a given margin.
*
* @param margin a margin
* @return the lower left y-coordinate
*/
public float GetBottom(int margin) {
return lly + margin;
}
/**
* Returns the width of the rectangle.
*
* @return a width
*/
public float Width {
get {
return urx - llx;
}
}
/**
* Returns the height of the rectangle.
*
* @return a height
*/
public float Height {
get {
return ury - lly;
}
}
/**
* Swaps the values of urx and ury and of lly and llx in order to rotate the rectangle.
*
* @return a <CODE>PdfRectangle</CODE>
*/
public PdfRectangle Rotate {
get {
return new PdfRectangle(lly, llx, ury, urx, 0);
}
}
}
}

View File

@ -1,92 +1,92 @@
using System;
/*
* Copyright 1999, 2000, 2001, 2002 Bruno Lowagie
*
* The contents of this file are subject to the Mozilla Public License Version 1.1
* (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the License.
*
* The Original Code is 'iText, a free JAVA-PDF library'.
*
* The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
* the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
* All Rights Reserved.
* Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
* are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
*
* Contributor(s): all the names of the contributors are added in the source code
* where applicable.
*
* Alternatively, the contents of this file may be used under the terms of the
* LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
* provisions of LGPL are applicable instead of those above. If you wish to
* allow use of your version of this file only under the terms of the LGPL
* License and not to allow others to use your version of this file under
* the MPL, indicate your decision by deleting the provisions above and
* replace them with the notice and other provisions required by the LGPL.
* If you do not delete the provisions above, a recipient may use your version
* of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the MPL as stated above or under the terms of the GNU
* Library General Public License as published by the Free Software Foundation;
* either version 2 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
* details.
*
* If you didn't download this code from the following link, you should check if
* you aren't using an obsolete version:
* http://www.lowagie.com/iText/
*/
namespace iTextSharp.text.pdf {
/**
* <CODE>PdfResources</CODE> is the PDF Resources-object.
* <P>
* The marking operations for drawing a page are stored in a stream that is the value of the
* <B>Contents</B> key in the Page object's dictionary. Each marking context includes a list
* of the named resources it uses. This resource list is stored as a dictionary that is the
* value of the context's <B>Resources</B> key, and it serves two functions: it enumerates
* the named resources in the contents stream, and it established the mapping from the names
* to the objects used by the marking operations.<BR>
* This object is described in the 'Portable Document Format Reference Manual version 1.3'
* section 7.5 (page 195-197).
*
* @see PdfResource
* @see PdfProcSet
* @see PdfFontDictionary
* @see PdfPage
*/
internal class PdfResources : PdfDictionary {
// constructor
/**
* Constructs a PDF ResourcesDictionary.
*/
internal PdfResources() : base() {}
// methods
internal void Add(PdfName key, PdfDictionary resource) {
if (resource.Size == 0)
return;
PdfDictionary dic = (PdfDictionary)PdfReader.GetPdfObject(Get(key));
if (dic == null)
Put(key, resource);
else
dic.Merge(resource);
}
}
using System;
/*
* Copyright 1999, 2000, 2001, 2002 Bruno Lowagie
*
* The contents of this file are subject to the Mozilla Public License Version 1.1
* (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the License.
*
* The Original Code is 'iText, a free JAVA-PDF library'.
*
* The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
* the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
* All Rights Reserved.
* Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
* are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
*
* Contributor(s): all the names of the contributors are added in the source code
* where applicable.
*
* Alternatively, the contents of this file may be used under the terms of the
* LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
* provisions of LGPL are applicable instead of those above. If you wish to
* allow use of your version of this file only under the terms of the LGPL
* License and not to allow others to use your version of this file under
* the MPL, indicate your decision by deleting the provisions above and
* replace them with the notice and other provisions required by the LGPL.
* If you do not delete the provisions above, a recipient may use your version
* of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the MPL as stated above or under the terms of the GNU
* Library General Public License as published by the Free Software Foundation;
* either version 2 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
* details.
*
* If you didn't download this code from the following link, you should check if
* you aren't using an obsolete version:
* http://www.lowagie.com/iText/
*/
namespace iTextSharp.text.pdf {
/**
* <CODE>PdfResources</CODE> is the PDF Resources-object.
* <P>
* The marking operations for drawing a page are stored in a stream that is the value of the
* <B>Contents</B> key in the Page object's dictionary. Each marking context includes a list
* of the named resources it uses. This resource list is stored as a dictionary that is the
* value of the context's <B>Resources</B> key, and it serves two functions: it enumerates
* the named resources in the contents stream, and it established the mapping from the names
* to the objects used by the marking operations.<BR>
* This object is described in the 'Portable Document Format Reference Manual version 1.3'
* section 7.5 (page 195-197).
*
* @see PdfResource
* @see PdfProcSet
* @see PdfFontDictionary
* @see PdfPage
*/
internal class PdfResources : PdfDictionary {
// constructor
/**
* Constructs a PDF ResourcesDictionary.
*/
internal PdfResources() : base() {}
// methods
internal void Add(PdfName key, PdfDictionary resource) {
if (resource.Size == 0)
return;
PdfDictionary dic = GetAsDict(key);
if (dic == null)
Put(key, resource);
else
dic.Merge(resource);
}
}
}

View File

@ -183,9 +183,8 @@ namespace iTextSharp.text.pdf {
bb.Append("$A");
if (level <= 0)
return;
ArrayList ar = array.ArrayList;
for (int k = 0; k < ar.Count; ++k) {
SerObject((PdfObject)ar[k], level, bb);
for (int k = 0; k < array.Size; ++k) {
SerObject(array[k], level, bb);
}
}

View File

@ -273,8 +273,8 @@ namespace iTextSharp.text.pdf {
if (PdfName.CRYPT.Equals(filter))
crypto = null;
else if (filter.IsArray()) {
ArrayList af = ((PdfArray)filter).ArrayList;
if (af.Count > 0 && PdfName.CRYPT.Equals(af[0]))
PdfArray a = ((PdfArray)filter);
if (a.Size > 0 && PdfName.CRYPT.Equals(a[0]))
crypto = null;
}
}

View File

@ -272,7 +272,7 @@ namespace iTextSharp.text.pdf {
}
}
public PdfTransparencyGroup Group {
public virtual PdfTransparencyGroup Group {
get {
return this.group;
}

View File

@ -80,7 +80,13 @@ namespace iTextSharp.text.pdf {
IPdfRunDirection,
IPdfAnnotations {
// INNER CLASSES
/**
* The highest generation number possible.
* @since iText 2.1.6
*/
public const int GENERATION_MAX = 65535;
// INNER CLASSES
/**
* This class generates the structure of a PDF document.
@ -165,7 +171,7 @@ namespace iTextSharp.text.pdf {
String s1 = offset.ToString().PadLeft(10, '0');
String s2 = generation.ToString().PadLeft(5, '0');
ByteBuffer buf = new ByteBuffer(40);
if (generation == 65535) {
if (generation == GENERATION_MAX) {
buf.Append(s1).Append(' ').Append(s2).Append(" f \n");
}
else {
@ -237,7 +243,7 @@ namespace iTextSharp.text.pdf {
*/
internal PdfBody(PdfWriter writer) {
xrefs = new k_Tree();
xrefs[new PdfCrossReference(0, 0, 65535)] = null;
xrefs[new PdfCrossReference(0, 0, GENERATION_MAX)] = null;
position = writer.Os.Counter;
refnum = 1;
this.writer = writer;
@ -323,7 +329,7 @@ namespace iTextSharp.text.pdf {
internal int IndirectReferenceNumber {
get {
int n = refnum++;
xrefs[new PdfCrossReference(n, 0, 65536)] = null;
xrefs[new PdfCrossReference(n, 0, GENERATION_MAX)] = null;
return n;
}
}
@ -909,6 +915,11 @@ namespace iTextSharp.text.pdf {
protected ArrayList pageReferences = new ArrayList();
/** The current page number. */
protected int currentPageNumber = 1;
/**
* The value of the Tabs entry in the page dictionary.
* @since 2.1.5
*/
protected PdfName tabs = null;
/**
* Use this method to make sure the page tree has a lineair structure
@ -988,6 +999,22 @@ namespace iTextSharp.text.pdf {
}
/**
* Sets the value for the Tabs entry in the page tree.
* @param tabs Can be PdfName.R, PdfName.C or PdfName.S.
* Since the Adobe Extensions Level 3, it can also be PdfName.A
* or PdfName.W
* @since 2.1.5
*/
public PdfName Tabs {
get{
return tabs;
}
set {
tabs = value;
}
}
/**
* Adds some <CODE>PdfContents</CODE> to this Writer.
* <P>
* The document has to be open before you can begin to add content
@ -1325,6 +1352,14 @@ namespace iTextSharp.text.pdf {
pdf_version.SetPdfVersion(version);
}
/**
* @see com.lowagie.text.pdf.interfaces.PdfVersion#addDeveloperExtension(com.lowagie.text.pdf.PdfDeveloperExtension)
* @since 2.1.6
*/
public void AddDeveloperExtension(PdfDeveloperExtension de) {
pdf_version.AddDeveloperExtension(de);
}
/**
* Returns the version information.
*/
@ -1733,7 +1768,7 @@ namespace iTextSharp.text.pdf {
* @param destOutputProfile a value
* @throws IOException on error
*/
public void SetOutputIntents(String outputConditionIdentifier, String outputCondition, String registryName, String info, byte[] destOutputProfile) {
public void SetOutputIntents(String outputConditionIdentifier, String outputCondition, String registryName, String info, ICC_Profile colorProfile) {
PdfDictionary outa = ExtraCatalog; //force the creation
outa = new PdfDictionary(PdfName.OUTPUTINTENT);
if (outputCondition != null)
@ -1744,15 +1779,42 @@ namespace iTextSharp.text.pdf {
outa.Put(PdfName.REGISTRYNAME, new PdfString(registryName, PdfObject.TEXT_UNICODE));
if (info != null)
outa.Put(PdfName.INFO, new PdfString(info, PdfObject.TEXT_UNICODE));
if (destOutputProfile != null) {
PdfStream stream = new PdfStream(destOutputProfile);
stream.FlateCompress(compressionLevel);
if (colorProfile != null) {
PdfStream stream = new PdfICCBased(colorProfile, compressionLevel);
outa.Put(PdfName.DESTOUTPUTPROFILE, AddToBody(stream).IndirectReference);
}
outa.Put(PdfName.S, PdfName.GTS_PDFX);
PdfName intentSubtype;
if (pdfxConformance.IsPdfA1() || "PDFA/1".Equals(outputCondition)) {
intentSubtype = PdfName.GTS_PDFA1;
}
else {
intentSubtype = PdfName.GTS_PDFX;
}
outa.Put(PdfName.S, intentSubtype);
extraCatalog.Put(PdfName.OUTPUTINTENTS, new PdfArray(outa));
}
/**
* Sets the values of the output intent dictionary. Null values are allowed to
* suppress any key.
*
* Prefer the <CODE>ICC_Profile</CODE>-based version of this method.
* @param outputConditionIdentifier a value
* @param outputCondition a value, "PDFA/A" to force GTS_PDFA1, otherwise cued by pdfxConformance.
* @param registryName a value
* @param info a value
* @param destOutputProfile a value
* @since 1.x
*
* @throws IOException
*/
public void SetOutputIntents(String outputConditionIdentifier, String outputCondition, String registryName, String info, byte[] destOutputProfile) {
ICC_Profile colorProfile = (destOutputProfile == null) ? null : ICC_Profile.GetInstance(destOutputProfile);
SetOutputIntents(outputConditionIdentifier, outputCondition, registryName, info, colorProfile);
}
/**
* Copies the output intent dictionary from other document to this one.
* @param reader the other document
@ -1764,13 +1826,13 @@ namespace iTextSharp.text.pdf {
*/
public bool SetOutputIntents(PdfReader reader, bool checkExistence) {
PdfDictionary catalog = reader.Catalog;
PdfArray outs = (PdfArray)PdfReader.GetPdfObject(catalog.Get(PdfName.OUTPUTINTENTS));
PdfArray outs = catalog.GetAsArray(PdfName.OUTPUTINTENTS);
if (outs == null)
return false;
ArrayList arr = outs.ArrayList;
if (arr.Count == 0)
if (outs.Size == 0)
return false;
PdfDictionary outa = (PdfDictionary)PdfReader.GetPdfObject((PdfObject)arr[0]);
PdfDictionary outa = outs.GetAsDict(0);
PdfObject obj = PdfReader.GetPdfObject(outa.Get(PdfName.S));
if (obj == null || !PdfName.GTS_PDFX.Equals(obj))
return false;
@ -2868,17 +2930,24 @@ namespace iTextSharp.text.pdf {
maskRef = GetImageReference(mname);
}
PdfImage i = new PdfImage(image, "img" + images.Count, maskRef);
if (image is ImgJBIG2) {
byte[] globals = ((ImgJBIG2) image).GlobalBytes;
if (globals != null) {
PdfDictionary decodeparms = new PdfDictionary();
decodeparms.Put(PdfName.JBIG2GLOBALS, GetReferenceJBIG2Globals(globals));
i.Put(PdfName.DECODEPARMS, decodeparms);
}
}
if (image.HasICCProfile()) {
PdfICCBased icc = new PdfICCBased(image.TagICC, image.CompressionLevel);
PdfIndirectReference iccRef = Add(icc);
PdfArray iccArray = new PdfArray();
iccArray.Add(PdfName.ICCBASED);
iccArray.Add(iccRef);
PdfObject colorspace = i.Get(PdfName.COLORSPACE);
if (colorspace != null && colorspace.IsArray()) {
ArrayList ar = ((PdfArray)colorspace).ArrayList;
if (ar.Count > 1 && PdfName.INDEXED.Equals(ar[0]))
ar[1] = iccArray;
PdfArray colorspace = i.GetAsArray(PdfName.COLORSPACE);
if (colorspace != null) {
if (colorspace.Size > 1 && PdfName.INDEXED.Equals(colorspace[0]))
colorspace[1] = iccArray;
else
i.Put(PdfName.COLORSPACE, iccArray);
}
@ -2933,6 +3002,35 @@ namespace iTextSharp.text.pdf {
return objecta.IndirectReference;
}
/**
* A Hashtable with Stream objects containing JBIG2 Globals
* @since 2.1.5
*/
protected Hashtable JBIG2Globals = new Hashtable();
/**
* Gets an indirect reference to a JBIG2 Globals stream.
* Adds the stream if it hasn't already been added to the writer.
* @param content a byte array that may already been added to the writer inside a stream object.
* @since 2.1.5
*/
protected PdfIndirectReference GetReferenceJBIG2Globals(byte[] content) {
if (content == null) return null;
foreach (PdfStream str in JBIG2Globals.Keys) {
if (Org.BouncyCastle.Utilities.Arrays.AreEqual(content, str.GetBytes())) {
return (PdfIndirectReference) JBIG2Globals[str];
}
}
PdfStream stream = new PdfStream(content);
PdfIndirectObject refi;
try {
refi = AddToBody(stream);
} catch (IOException) {
return null;
}
JBIG2Globals[stream] = refi.IndirectReference;
return refi.IndirectReference;
}
// [M4] Old table functionality; do we still need it?
/**

View File

@ -1,94 +1,101 @@
using System;
using iTextSharp.text;
using iTextSharp.text.pdf;
/*
* $Id: IPdfVersion.cs,v 1.1 2007/02/09 15:34:40 psoares33 Exp $
*
* Copyright 2006 Bruno Lowagie
*
* The contents of this file are subject to the Mozilla Public License Version 1.1
* (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the License.
*
* The Original Code is 'iText, a free JAVA-PDF library'.
*
* The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
* the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
* All Rights Reserved.
* Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
* are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
*
* Contributor(s): all the names of the contributors are added in the source code
* where applicable.
*
* Alternatively, the contents of this file may be used under the terms of the
* LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
* provisions of LGPL are applicable instead of those above. If you wish to
* allow use of your version of this file only under the terms of the LGPL
* License and not to allow others to use your version of this file under
* the MPL, indicate your decision by deleting the provisions above and
* replace them with the notice and other provisions required by the LGPL.
* If you do not delete the provisions above, a recipient may use your version
* of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the MPL as stated above or under the terms of the GNU
* Library General Public License as published by the Free Software Foundation;
* either version 2 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
* details.
*
* If you didn't download this code from the following link, you should check if
* you aren't using an obsolete version:
* http://www.lowagie.com/iText/
*/
namespace iTextSharp.text.pdf.interfaces {
/**
* The PDF version is described in the PDF Reference 1.7 p92
* (about the PDF Header) and page 139 (the version entry in
* the Catalog). You'll also find info about setting the version
* in the book 'iText in Action' sections 2.1.3 (PDF Header)
* and 3.3 (Version history).
*/
public interface IPdfVersion {
using System;
using iTextSharp.text;
using iTextSharp.text.pdf;
/*
* $Id: IPdfVersion.cs,v 1.1 2007/02/09 15:34:40 psoares33 Exp $
*
* Copyright 2006 Bruno Lowagie
*
* The contents of this file are subject to the Mozilla Public License Version 1.1
* (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the License.
*
* The Original Code is 'iText, a free JAVA-PDF library'.
*
* The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
* the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
* All Rights Reserved.
* Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
* are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
*
* Contributor(s): all the names of the contributors are added in the source code
* where applicable.
*
* Alternatively, the contents of this file may be used under the terms of the
* LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
* provisions of LGPL are applicable instead of those above. If you wish to
* allow use of your version of this file only under the terms of the LGPL
* License and not to allow others to use your version of this file under
* the MPL, indicate your decision by deleting the provisions above and
* replace them with the notice and other provisions required by the LGPL.
* If you do not delete the provisions above, a recipient may use your version
* of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the MPL as stated above or under the terms of the GNU
* Library General Public License as published by the Free Software Foundation;
* either version 2 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
* details.
*
* If you didn't download this code from the following link, you should check if
* you aren't using an obsolete version:
* http://www.lowagie.com/iText/
*/
namespace iTextSharp.text.pdf.interfaces {
/**
* The PDF version is described in the PDF Reference 1.7 p92
* (about the PDF Header) and page 139 (the version entry in
* the Catalog). You'll also find info about setting the version
* in the book 'iText in Action' sections 2.1.3 (PDF Header)
* and 3.3 (Version history).
*/
public interface IPdfVersion {
/**
* If the PDF Header hasn't been written yet,
* this changes the version as it will appear in the PDF Header.
* If the PDF header was already written to the Stream,
* this changes the version as it will appear in the Catalog.
* @param version a character representing the PDF version
*/
char PdfVersion {
set;
}
/**
* If the PDF Header hasn't been written yet,
* this changes the version as it will appear in the PDF Header,
* but only if param refers to a higher version.
* If the PDF header was already written to the Stream,
* this changes the version as it will appear in the Catalog.
* @param version a character representing the PDF version
*/
void SetAtLeastPdfVersion(char version);
/**
* Sets the PDF version as it will appear in the Catalog.
* Note that this only has effect if you use a later version
* than the one that appears in the header; this method
* ignores the parameter if you try to set a lower version.
* @param version the PDF name that will be used for the Version key in the catalog
*/
void SetPdfVersion(PdfName version);
/**
* If the PDF Header hasn't been written yet,
* this changes the version as it will appear in the PDF Header.
* If the PDF header was already written to the Stream,
* this changes the version as it will appear in the Catalog.
* @param version a character representing the PDF version
* Adds a developer extension to the Extensions dictionary
* in the Catalog.
* @param de an object that contains the extensions prefix and dictionary
* @since 2.1.6
*/
char PdfVersion {
set;
}
/**
* If the PDF Header hasn't been written yet,
* this changes the version as it will appear in the PDF Header,
* but only if param refers to a higher version.
* If the PDF header was already written to the Stream,
* this changes the version as it will appear in the Catalog.
* @param version a character representing the PDF version
*/
void SetAtLeastPdfVersion(char version);
/**
* Sets the PDF version as it will appear in the Catalog.
* Note that this only has effect if you use a later version
* than the one that appears in the header; this method
* ignores the parameter if you try to set a lower version.
* @param version the PDF name that will be used for the Version key in the catalog
*/
void SetPdfVersion(PdfName version);
}
void AddDeveloperExtension(PdfDeveloperExtension de);
}
}

View File

@ -1,172 +1,202 @@
using System;
using iTextSharp.text.pdf;
using iTextSharp.text;
using iTextSharp.text.pdf.interfaces;
/*
* $Id: PdfVersionImp.cs,v 1.1 2007/02/09 15:34:40 psoares33 Exp $
*
* Copyright 2006 Bruno Lowagie
*
* The contents of this file are subject to the Mozilla Public License Version 1.1
* (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the License.
*
* The Original Code is 'iText, a free JAVA-PDF library'.
*
* The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
* the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
* All Rights Reserved.
* Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
* are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
*
* Contributor(s): all the names of the contributors are added in the source code
* where applicable.
*
* Alternatively, the contents of this file may be used under the terms of the
* LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
* provisions of LGPL are applicable instead of those above. If you wish to
* allow use of your version of this file only under the terms of the LGPL
* License and not to allow others to use your version of this file under
* the MPL, indicate your decision by deleting the provisions above and
* replace them with the notice and other provisions required by the LGPL.
* If you do not delete the provisions above, a recipient may use your version
* of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the MPL as stated above or under the terms of the GNU
* Library General Public License as published by the Free Software Foundation;
* either version 2 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
* details.
*
* If you didn't download this code from the following link, you should check if
* you aren't using an obsolete version:
* http://www.lowagie.com/iText/
*/
namespace iTextSharp.text.pdf.intern {
/**
* Stores the PDF version information,
* knows how to write a PDF Header,
* and how to add the version to the catalog (if necessary).
*/
public class PdfVersionImp : IPdfVersion {
/** Contains different strings that are part of the header. */
public static readonly byte[][] HEADER = {
DocWriter.GetISOBytes("\n"),
DocWriter.GetISOBytes("%PDF-"),
DocWriter.GetISOBytes("\n%\u00e2\u00e3\u00cf\u00d3\n")
};
/** Indicates if the header was already written. */
protected bool headerWasWritten = false;
/** Indicates if we are working in append mode. */
protected bool appendmode = false;
/** The version that was or will be written to the header. */
protected char header_version = PdfWriter.VERSION_1_4;
/** The version that will be written to the catalog. */
protected PdfName catalog_version = null;
using System;
using iTextSharp.text.pdf;
using iTextSharp.text;
using iTextSharp.text.pdf.interfaces;
/*
* $Id: PdfVersionImp.cs,v 1.1 2007/02/09 15:34:40 psoares33 Exp $
*
* Copyright 2006 Bruno Lowagie
*
* The contents of this file are subject to the Mozilla Public License Version 1.1
* (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the License.
*
* The Original Code is 'iText, a free JAVA-PDF library'.
*
* The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
* the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
* All Rights Reserved.
* Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
* are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
*
* Contributor(s): all the names of the contributors are added in the source code
* where applicable.
*
* Alternatively, the contents of this file may be used under the terms of the
* LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
* provisions of LGPL are applicable instead of those above. If you wish to
* allow use of your version of this file only under the terms of the LGPL
* License and not to allow others to use your version of this file under
* the MPL, indicate your decision by deleting the provisions above and
* replace them with the notice and other provisions required by the LGPL.
* If you do not delete the provisions above, a recipient may use your version
* of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the MPL as stated above or under the terms of the GNU
* Library General Public License as published by the Free Software Foundation;
* either version 2 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
* details.
*
* If you didn't download this code from the following link, you should check if
* you aren't using an obsolete version:
* http://www.lowagie.com/iText/
*/
namespace iTextSharp.text.pdf.intern {
/**
* Stores the PDF version information,
* knows how to write a PDF Header,
* and how to add the version to the catalog (if necessary).
*/
public class PdfVersionImp : IPdfVersion {
/** Contains different strings that are part of the header. */
public static readonly byte[][] HEADER = {
DocWriter.GetISOBytes("\n"),
DocWriter.GetISOBytes("%PDF-"),
DocWriter.GetISOBytes("\n%\u00e2\u00e3\u00cf\u00d3\n")
};
/** Indicates if the header was already written. */
protected bool headerWasWritten = false;
/** Indicates if we are working in append mode. */
protected bool appendmode = false;
/** The version that was or will be written to the header. */
protected char header_version = PdfWriter.VERSION_1_4;
/** The version that will be written to the catalog. */
protected PdfName catalog_version = null;
/**
* @see com.lowagie.text.pdf.interfaces.PdfVersion#setPdfVersion(char)
*/
public char PdfVersion {
set {
if (headerWasWritten || appendmode) {
SetPdfVersion(GetVersionAsName(value));
}
else {
this.header_version = value;
}
* The extensions dictionary.
* @since 2.1.6
*/
protected PdfDictionary extensions = null;
/**
* @see com.lowagie.text.pdf.interfaces.PdfVersion#setPdfVersion(char)
*/
public char PdfVersion {
set {
if (headerWasWritten || appendmode) {
SetPdfVersion(GetVersionAsName(value));
}
else {
this.header_version = value;
}
}
}
/**
* @see com.lowagie.text.pdf.interfaces.PdfVersion#setAtLeastPdfVersion(char)
*/
public void SetAtLeastPdfVersion(char version) {
if (version > header_version) {
PdfVersion = version;
}
}
/**
* @see com.lowagie.text.pdf.interfaces.PdfVersion#setPdfVersion(com.lowagie.text.pdf.PdfName)
*/
public void SetPdfVersion(PdfName version) {
if (catalog_version == null || catalog_version.CompareTo(version) < 0) {
this.catalog_version = version;
}
}
/**
* Sets the append mode.
*/
public void SetAppendmode(bool appendmode) {
this.appendmode = appendmode;
}
/**
* Writes the header to the OutputStreamCounter.
* @throws IOException
*/
public void WriteHeader(OutputStreamCounter os) {
if (appendmode) {
os.Write(HEADER[0], 0, HEADER[0].Length);
}
else {
os.Write(HEADER[1], 0, HEADER[1].Length);
os.Write(GetVersionAsByteArray(header_version), 0, GetVersionAsByteArray(header_version).Length);
os.Write(HEADER[2], 0, HEADER[2].Length);
headerWasWritten = true;
}
}
/**
* Returns the PDF version as a name.
* @param version the version character.
*/
public PdfName GetVersionAsName(char version) {
switch (version) {
case PdfWriter.VERSION_1_2:
return PdfWriter.PDF_VERSION_1_2;
case PdfWriter.VERSION_1_3:
return PdfWriter.PDF_VERSION_1_3;
case PdfWriter.VERSION_1_4:
return PdfWriter.PDF_VERSION_1_4;
case PdfWriter.VERSION_1_5:
return PdfWriter.PDF_VERSION_1_5;
case PdfWriter.VERSION_1_6:
return PdfWriter.PDF_VERSION_1_6;
case PdfWriter.VERSION_1_7:
return PdfWriter.PDF_VERSION_1_7;
default:
return PdfWriter.PDF_VERSION_1_4;
}
}
/**
* Returns the version as a byte[].
* @param version the version character
*/
public byte[] GetVersionAsByteArray(char version) {
return DocWriter.GetISOBytes(GetVersionAsName(version).ToString().Substring(1));
}
/** Adds the version to the Catalog dictionary. */
public void AddToCatalog(PdfDictionary catalog) {
if(catalog_version != null) {
catalog.Put(PdfName.VERSION, catalog_version);
}
if (extensions != null) {
catalog.Put(PdfName.EXTENSIONS, extensions);
}
}
/**
* @see com.lowagie.text.pdf.interfaces.PdfVersion#setAtLeastPdfVersion(char)
* @see com.lowagie.text.pdf.interfaces.PdfVersion#addDeveloperExtension(com.lowagie.text.pdf.PdfDeveloperExtension)
* @since 2.1.6
*/
public void SetAtLeastPdfVersion(char version) {
if (version > header_version) {
PdfVersion = version;
}
}
/**
* @see com.lowagie.text.pdf.interfaces.PdfVersion#setPdfVersion(com.lowagie.text.pdf.PdfName)
*/
public void SetPdfVersion(PdfName version) {
if (catalog_version == null || catalog_version.CompareTo(version) < 0) {
this.catalog_version = version;
}
}
/**
* Sets the append mode.
*/
public void SetAppendmode(bool appendmode) {
this.appendmode = appendmode;
}
/**
* Writes the header to the OutputStreamCounter.
* @throws IOException
*/
public void WriteHeader(OutputStreamCounter os) {
if (appendmode) {
os.Write(HEADER[0], 0, HEADER[0].Length);
public void AddDeveloperExtension(PdfDeveloperExtension de) {
if (extensions == null) {
extensions = new PdfDictionary();
}
else {
os.Write(HEADER[1], 0, HEADER[1].Length);
os.Write(GetVersionAsByteArray(header_version), 0, GetVersionAsByteArray(header_version).Length);
os.Write(HEADER[2], 0, HEADER[2].Length);
headerWasWritten = true;
PdfDictionary extension = extensions.GetAsDict(de.Prefix);
if (extension != null) {
int diff = de.Baseversion.CompareTo(extension.GetAsName(PdfName.BASEVERSION));
if (diff < 0)
return;
diff = de.ExtensionLevel - extension.GetAsNumber(PdfName.EXTENSIONLEVEL).IntValue;
if (diff <= 0)
return;
}
}
}
/**
* Returns the PDF version as a name.
* @param version the version character.
*/
public PdfName GetVersionAsName(char version) {
switch (version) {
case PdfWriter.VERSION_1_2:
return PdfWriter.PDF_VERSION_1_2;
case PdfWriter.VERSION_1_3:
return PdfWriter.PDF_VERSION_1_3;
case PdfWriter.VERSION_1_4:
return PdfWriter.PDF_VERSION_1_4;
case PdfWriter.VERSION_1_5:
return PdfWriter.PDF_VERSION_1_5;
case PdfWriter.VERSION_1_6:
return PdfWriter.PDF_VERSION_1_6;
case PdfWriter.VERSION_1_7:
return PdfWriter.PDF_VERSION_1_7;
default:
return PdfWriter.PDF_VERSION_1_4;
}
}
/**
* Returns the version as a byte[].
* @param version the version character
*/
public byte[] GetVersionAsByteArray(char version) {
return DocWriter.GetISOBytes(GetVersionAsName(version).ToString().Substring(1));
}
/** Adds the version to the Catalog dictionary. */
public void AddToCatalog(PdfDictionary catalog) {
if(catalog_version != null) {
catalog.Put(PdfName.VERSION, catalog_version);
}
}
}
extensions.Put(de.Prefix, de.GetDeveloperExtensions());
}
}
}

View File

@ -1,359 +1,361 @@
using System;
using iTextSharp.text.pdf;
using iTextSharp.text.pdf.interfaces;
/*
* $Id: PdfViewerPreferencesImp.cs,v 1.7 2008/05/04 10:49:46 psoares33 Exp $
*
* Copyright 2006 Bruno Lowagie
*
* The contents of this file are subject to the Mozilla Public License Version 1.1
* (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the License.
*
* The Original Code is 'iText, a free JAVA-PDF library'.
*
* The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
* the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
* All Rights Reserved.
* Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
* are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
*
* Contributor(s): all the names of the contributors are added in the source code
* where applicable.
*
* Alternatively, the contents of this file may be used under the terms of the
* LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
* provisions of LGPL are applicable instead of those above. If you wish to
* allow use of your version of this file only under the terms of the LGPL
* License and not to allow others to use your version of this file under
* the MPL, indicate your decision by deleting the provisions above and
* replace them with the notice and other provisions required by the LGPL.
* If you do not delete the provisions above, a recipient may use your version
* of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the MPL as stated above or under the terms of the GNU
* Library General Public License as published by the Free Software Foundation;
* either version 2 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
* details.
*
* If you didn't download this code from the following link, you should check if
* you aren't using an obsolete version:
* http://www.lowagie.com/iText/
*/
namespace iTextSharp.text.pdf.intern {
/**
* Stores the information concerning viewer preferences,
* and contains the business logic that allows you to set viewer preferences.
*/
public class PdfViewerPreferencesImp : IPdfViewerPreferences {
public static readonly PdfName[] VIEWER_PREFERENCES = {
PdfName.HIDETOOLBAR, // 0
PdfName.HIDEMENUBAR, // 1
PdfName.HIDEWINDOWUI, // 2
PdfName.FITWINDOW, // 3
PdfName.CENTERWINDOW, // 4
PdfName.DISPLAYDOCTITLE, // 5
PdfName.NONFULLSCREENPAGEMODE, // 6
PdfName.DIRECTION, // 7
PdfName.VIEWAREA, // 8
PdfName.VIEWCLIP, // 9
PdfName.PRINTAREA, // 10
PdfName.PRINTCLIP, // 11
PdfName.PRINTSCALING, // 12
PdfName.DUPLEX, // 13
PdfName.PICKTRAYBYPDFSIZE, // 14
PdfName.PRINTPAGERANGE, // 15
PdfName.NUMCOPIES // 16
};
/** A series of viewer preferences. */
public static readonly PdfName[] NONFULLSCREENPAGEMODE_PREFERENCES = {
PdfName.USENONE, PdfName.USEOUTLINES, PdfName.USETHUMBS, PdfName.USEOC
};
/** A series of viewer preferences. */
public static readonly PdfName[] DIRECTION_PREFERENCES = {
PdfName.L2R, PdfName.R2L
};
/** A series of viewer preferences. */
public static readonly PdfName[] PAGE_BOUNDARIES = {
PdfName.MEDIABOX, PdfName.CROPBOX, PdfName.BLEEDBOX, PdfName.TRIMBOX, PdfName.ARTBOX
};
/** A series of viewer preferences */
public static readonly PdfName[] PRINTSCALING_PREFERENCES = {
PdfName.APPDEFAULT, PdfName.NONE
};
/** A series of viewer preferences. */
public static readonly PdfName[] DUPLEX_PREFERENCES = {
PdfName.SIMPLEX, PdfName.DUPLEXFLIPSHORTEDGE, PdfName.DUPLEXFLIPLONGEDGE
};
/** This value will hold the viewer preferences for the page layout and page mode. */
private int pageLayoutAndMode = 0;
/** This dictionary holds the viewer preferences (other than page layout and page mode). */
private PdfDictionary viewerPreferences = new PdfDictionary();
/** The mask to decide if a ViewerPreferences dictionary is needed */
private const int viewerPreferencesMask = 0xfff000;
/**
* Returns the page layout and page mode value.
*/
public int PageLayoutAndMode {
get {
return pageLayoutAndMode;
}
}
/**
* Returns the viewer preferences.
*/
public PdfDictionary GetViewerPreferences() {
return viewerPreferences;
}
/**
* Sets the viewer preferences as the sum of several constants.
*
* @param preferences
* the viewer preferences
* @see PdfWriter#setViewerPreferences
*/
public int ViewerPreferences {
set {
int preferences = value;
this.pageLayoutAndMode |= preferences;
// for backwards compatibility, it is also possible
// to set the following viewer preferences with this method:
if ((preferences & viewerPreferencesMask) != 0) {
pageLayoutAndMode = ~viewerPreferencesMask & pageLayoutAndMode;
if ((preferences & PdfWriter.HideToolbar) != 0)
viewerPreferences.Put(PdfName.HIDETOOLBAR, PdfBoolean.PDFTRUE);
if ((preferences & PdfWriter.HideMenubar) != 0)
viewerPreferences.Put(PdfName.HIDEMENUBAR, PdfBoolean.PDFTRUE);
if ((preferences & PdfWriter.HideWindowUI) != 0)
viewerPreferences.Put(PdfName.HIDEWINDOWUI, PdfBoolean.PDFTRUE);
if ((preferences & PdfWriter.FitWindow) != 0)
viewerPreferences.Put(PdfName.FITWINDOW, PdfBoolean.PDFTRUE);
if ((preferences & PdfWriter.CenterWindow) != 0)
viewerPreferences.Put(PdfName.CENTERWINDOW, PdfBoolean.PDFTRUE);
if ((preferences & PdfWriter.DisplayDocTitle) != 0)
viewerPreferences.Put(PdfName.DISPLAYDOCTITLE, PdfBoolean.PDFTRUE);
if ((preferences & PdfWriter.NonFullScreenPageModeUseNone) != 0)
viewerPreferences.Put(PdfName.NONFULLSCREENPAGEMODE, PdfName.USENONE);
else if ((preferences & PdfWriter.NonFullScreenPageModeUseOutlines) != 0)
viewerPreferences.Put(PdfName.NONFULLSCREENPAGEMODE, PdfName.USEOUTLINES);
else if ((preferences & PdfWriter.NonFullScreenPageModeUseThumbs) != 0)
viewerPreferences.Put(PdfName.NONFULLSCREENPAGEMODE, PdfName.USETHUMBS);
else if ((preferences & PdfWriter.NonFullScreenPageModeUseOC) != 0)
viewerPreferences.Put(PdfName.NONFULLSCREENPAGEMODE, PdfName.USEOC);
if ((preferences & PdfWriter.DirectionL2R) != 0)
viewerPreferences.Put(PdfName.DIRECTION, PdfName.L2R);
else if ((preferences & PdfWriter.DirectionR2L) != 0)
viewerPreferences.Put(PdfName.DIRECTION, PdfName.R2L);
if ((preferences & PdfWriter.PrintScalingNone) != 0)
viewerPreferences.Put(PdfName.PRINTSCALING, PdfName.NONE);
}
}
}
/**
* Given a key for a viewer preference (a PdfName object),
* this method returns the index in the VIEWER_PREFERENCES array.
* @param key a PdfName referring to a viewer preference
* @return an index in the VIEWER_PREFERENCES array
*/
private int GetIndex(PdfName key) {
for (int i = 0; i < VIEWER_PREFERENCES.Length; i++) {
if (VIEWER_PREFERENCES[i].Equals(key))
return i;
}
return -1;
}
/**
* Checks if some value is valid for a certain key.
*/
private bool IsPossibleValue(PdfName value, PdfName[] accepted) {
for (int i = 0; i < accepted.Length; i++) {
if (accepted[i].Equals(value)) {
return true;
}
}
return false;
}
/**
* Sets the viewer preferences for printing.
*/
public virtual void AddViewerPreference(PdfName key, PdfObject value) {
switch (GetIndex(key)) {
case 0: // HIDETOOLBAR
case 1: // HIDEMENUBAR
case 2: // HIDEWINDOWUI
case 3: // FITWINDOW
case 4: // CENTERWINDOW
case 5: // DISPLAYDOCTITLE
case 14: // PICKTRAYBYPDFSIZE
if (value is PdfBoolean) {
viewerPreferences.Put(key, value);
}
break;
case 6: // NONFULLSCREENPAGEMODE
if (value is PdfName
&& IsPossibleValue((PdfName)value, NONFULLSCREENPAGEMODE_PREFERENCES)) {
viewerPreferences.Put(key, value);
}
break;
case 7: // DIRECTION
if (value is PdfName
&& IsPossibleValue((PdfName)value, DIRECTION_PREFERENCES)) {
viewerPreferences.Put(key, value);
}
break;
case 8: // VIEWAREA
case 9: // VIEWCLIP
case 10: // PRINTAREA
case 11: // PRINTCLIP
if (value is PdfName
&& IsPossibleValue((PdfName)value, PAGE_BOUNDARIES)) {
viewerPreferences.Put(key, value);
}
break;
case 12: // PRINTSCALING
if (value is PdfName
&& IsPossibleValue((PdfName)value, PRINTSCALING_PREFERENCES)) {
viewerPreferences.Put(key, value);
}
break;
case 13: // DUPLEX
if (value is PdfName
&& IsPossibleValue((PdfName)value, DUPLEX_PREFERENCES)) {
viewerPreferences.Put(key, value);
}
break;
case 15: // PRINTPAGERANGE
if (value is PdfArray) {
viewerPreferences.Put(key, value);
}
break;
case 16: // NUMCOPIES
if (value is PdfNumber) {
viewerPreferences.Put(key, value);
}
break;
}
}
/**
* Adds the viewer preferences defined in the preferences parameter to a
* PdfDictionary (more specifically the root or catalog of a PDF file).
*
* @param catalog
*/
public void AddToCatalog(PdfDictionary catalog) {
// Page Layout
catalog.Remove(PdfName.PAGELAYOUT);
if ((pageLayoutAndMode & PdfWriter.PageLayoutSinglePage) != 0)
catalog.Put(PdfName.PAGELAYOUT, PdfName.SINGLEPAGE);
else if ((pageLayoutAndMode & PdfWriter.PageLayoutOneColumn) != 0)
catalog.Put(PdfName.PAGELAYOUT, PdfName.ONECOLUMN);
else if ((pageLayoutAndMode & PdfWriter.PageLayoutTwoColumnLeft) != 0)
catalog.Put(PdfName.PAGELAYOUT, PdfName.TWOCOLUMNLEFT);
else if ((pageLayoutAndMode & PdfWriter.PageLayoutTwoColumnRight) != 0)
catalog.Put(PdfName.PAGELAYOUT, PdfName.TWOCOLUMNRIGHT);
else if ((pageLayoutAndMode & PdfWriter.PageLayoutTwoPageLeft) != 0)
catalog.Put(PdfName.PAGELAYOUT, PdfName.TWOPAGELEFT);
else if ((pageLayoutAndMode & PdfWriter.PageLayoutTwoPageRight) != 0)
catalog.Put(PdfName.PAGELAYOUT, PdfName.TWOPAGERIGHT);
// Page Mode
catalog.Remove(PdfName.PAGEMODE);
if ((pageLayoutAndMode & PdfWriter.PageModeUseNone) != 0)
catalog.Put(PdfName.PAGEMODE, PdfName.USENONE);
else if ((pageLayoutAndMode & PdfWriter.PageModeUseOutlines) != 0)
catalog.Put(PdfName.PAGEMODE, PdfName.USEOUTLINES);
else if ((pageLayoutAndMode & PdfWriter.PageModeUseThumbs) != 0)
catalog.Put(PdfName.PAGEMODE, PdfName.USETHUMBS);
else if ((pageLayoutAndMode & PdfWriter.PageModeFullScreen) != 0)
catalog.Put(PdfName.PAGEMODE, PdfName.FULLSCREEN);
else if ((pageLayoutAndMode & PdfWriter.PageModeUseOC) != 0)
catalog.Put(PdfName.PAGEMODE, PdfName.USEOC);
else if ((pageLayoutAndMode & PdfWriter.PageModeUseAttachments) != 0)
catalog.Put(PdfName.PAGEMODE, PdfName.USEATTACHMENTS);
// viewer preferences (Table 8.1 of the PDF Reference)
catalog.Remove(PdfName.VIEWERPREFERENCES);
if (viewerPreferences.Size > 0) {
catalog.Put(PdfName.VIEWERPREFERENCES, viewerPreferences);
}
}
public static PdfViewerPreferencesImp GetViewerPreferences(PdfDictionary catalog) {
PdfViewerPreferencesImp preferences = new PdfViewerPreferencesImp();
int prefs = 0;
PdfName name = null;
// page layout
PdfObject obj = PdfReader.GetPdfObjectRelease(catalog.Get(PdfName.PAGELAYOUT));
if (obj != null && obj.IsName()) {
name = (PdfName) obj;
if (name.Equals(PdfName.SINGLEPAGE))
prefs |= PdfWriter.PageLayoutSinglePage;
else if (name.Equals(PdfName.ONECOLUMN))
prefs |= PdfWriter.PageLayoutOneColumn;
else if (name.Equals(PdfName.TWOCOLUMNLEFT))
prefs |= PdfWriter.PageLayoutTwoColumnLeft;
else if (name.Equals(PdfName.TWOCOLUMNRIGHT))
prefs |= PdfWriter.PageLayoutTwoColumnRight;
else if (name.Equals(PdfName.TWOPAGELEFT))
prefs |= PdfWriter.PageLayoutTwoPageLeft;
else if (name.Equals(PdfName.TWOPAGERIGHT))
prefs |= PdfWriter.PageLayoutTwoPageRight;
}
// page mode
obj = PdfReader.GetPdfObjectRelease(catalog.Get(PdfName.PAGEMODE));
if (obj != null && obj.IsName()) {
name = (PdfName) obj;
if (name.Equals(PdfName.USENONE))
prefs |= PdfWriter.PageModeUseNone;
else if (name.Equals(PdfName.USEOUTLINES))
prefs |= PdfWriter.PageModeUseOutlines;
else if (name.Equals(PdfName.USETHUMBS))
prefs |= PdfWriter.PageModeUseThumbs;
else if (name.Equals(PdfName.USEOC))
prefs |= PdfWriter.PageModeUseOC;
else if (name.Equals(PdfName.USEATTACHMENTS))
prefs |= PdfWriter.PageModeUseAttachments;
}
// set page layout and page mode preferences
preferences.ViewerPreferences = prefs;
// other preferences
obj = PdfReader.GetPdfObjectRelease(catalog
.Get(PdfName.VIEWERPREFERENCES));
if (obj != null && obj.IsDictionary()) {
PdfDictionary vp = (PdfDictionary) obj;
for (int i = 0; i < VIEWER_PREFERENCES.Length; i++) {
obj = PdfReader.GetPdfObjectRelease(vp.Get(VIEWER_PREFERENCES[i]));
preferences.AddViewerPreference(VIEWER_PREFERENCES[i], obj);
}
}
return preferences;
}
}
using System;
using iTextSharp.text.pdf;
using iTextSharp.text.pdf.interfaces;
/*
* $Id: PdfViewerPreferencesImp.cs,v 1.7 2008/05/04 10:49:46 psoares33 Exp $
*
* Copyright 2006 Bruno Lowagie
*
* The contents of this file are subject to the Mozilla Public License Version 1.1
* (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the License.
*
* The Original Code is 'iText, a free JAVA-PDF library'.
*
* The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
* the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
* All Rights Reserved.
* Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
* are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
*
* Contributor(s): all the names of the contributors are added in the source code
* where applicable.
*
* Alternatively, the contents of this file may be used under the terms of the
* LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
* provisions of LGPL are applicable instead of those above. If you wish to
* allow use of your version of this file only under the terms of the LGPL
* License and not to allow others to use your version of this file under
* the MPL, indicate your decision by deleting the provisions above and
* replace them with the notice and other provisions required by the LGPL.
* If you do not delete the provisions above, a recipient may use your version
* of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the MPL as stated above or under the terms of the GNU
* Library General Public License as published by the Free Software Foundation;
* either version 2 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
* details.
*
* If you didn't download this code from the following link, you should check if
* you aren't using an obsolete version:
* http://www.lowagie.com/iText/
*/
namespace iTextSharp.text.pdf.intern {
/**
* Stores the information concerning viewer preferences,
* and contains the business logic that allows you to set viewer preferences.
*/
public class PdfViewerPreferencesImp : IPdfViewerPreferences {
public static readonly PdfName[] VIEWER_PREFERENCES = {
PdfName.HIDETOOLBAR, // 0
PdfName.HIDEMENUBAR, // 1
PdfName.HIDEWINDOWUI, // 2
PdfName.FITWINDOW, // 3
PdfName.CENTERWINDOW, // 4
PdfName.DISPLAYDOCTITLE, // 5
PdfName.NONFULLSCREENPAGEMODE, // 6
PdfName.DIRECTION, // 7
PdfName.VIEWAREA, // 8
PdfName.VIEWCLIP, // 9
PdfName.PRINTAREA, // 10
PdfName.PRINTCLIP, // 11
PdfName.PRINTSCALING, // 12
PdfName.DUPLEX, // 13
PdfName.PICKTRAYBYPDFSIZE, // 14
PdfName.PRINTPAGERANGE, // 15
PdfName.NUMCOPIES // 16
};
/** A series of viewer preferences. */
public static readonly PdfName[] NONFULLSCREENPAGEMODE_PREFERENCES = {
PdfName.USENONE, PdfName.USEOUTLINES, PdfName.USETHUMBS, PdfName.USEOC
};
/** A series of viewer preferences. */
public static readonly PdfName[] DIRECTION_PREFERENCES = {
PdfName.L2R, PdfName.R2L
};
/** A series of viewer preferences. */
public static readonly PdfName[] PAGE_BOUNDARIES = {
PdfName.MEDIABOX, PdfName.CROPBOX, PdfName.BLEEDBOX, PdfName.TRIMBOX, PdfName.ARTBOX
};
/** A series of viewer preferences */
public static readonly PdfName[] PRINTSCALING_PREFERENCES = {
PdfName.APPDEFAULT, PdfName.NONE
};
/** A series of viewer preferences. */
public static readonly PdfName[] DUPLEX_PREFERENCES = {
PdfName.SIMPLEX, PdfName.DUPLEXFLIPSHORTEDGE, PdfName.DUPLEXFLIPLONGEDGE
};
/** This value will hold the viewer preferences for the page layout and page mode. */
private int pageLayoutAndMode = 0;
/** This dictionary holds the viewer preferences (other than page layout and page mode). */
private PdfDictionary viewerPreferences = new PdfDictionary();
/** The mask to decide if a ViewerPreferences dictionary is needed */
private const int viewerPreferencesMask = 0xfff000;
/**
* Returns the page layout and page mode value.
*/
public int PageLayoutAndMode {
get {
return pageLayoutAndMode;
}
}
/**
* Returns the viewer preferences.
*/
public PdfDictionary GetViewerPreferences() {
return viewerPreferences;
}
/**
* Sets the viewer preferences as the sum of several constants.
*
* @param preferences
* the viewer preferences
* @see PdfWriter#setViewerPreferences
*/
public int ViewerPreferences {
set {
int preferences = value;
this.pageLayoutAndMode |= preferences;
// for backwards compatibility, it is also possible
// to set the following viewer preferences with this method:
if ((preferences & viewerPreferencesMask) != 0) {
pageLayoutAndMode = ~viewerPreferencesMask & pageLayoutAndMode;
if ((preferences & PdfWriter.HideToolbar) != 0)
viewerPreferences.Put(PdfName.HIDETOOLBAR, PdfBoolean.PDFTRUE);
if ((preferences & PdfWriter.HideMenubar) != 0)
viewerPreferences.Put(PdfName.HIDEMENUBAR, PdfBoolean.PDFTRUE);
if ((preferences & PdfWriter.HideWindowUI) != 0)
viewerPreferences.Put(PdfName.HIDEWINDOWUI, PdfBoolean.PDFTRUE);
if ((preferences & PdfWriter.FitWindow) != 0)
viewerPreferences.Put(PdfName.FITWINDOW, PdfBoolean.PDFTRUE);
if ((preferences & PdfWriter.CenterWindow) != 0)
viewerPreferences.Put(PdfName.CENTERWINDOW, PdfBoolean.PDFTRUE);
if ((preferences & PdfWriter.DisplayDocTitle) != 0)
viewerPreferences.Put(PdfName.DISPLAYDOCTITLE, PdfBoolean.PDFTRUE);
if ((preferences & PdfWriter.NonFullScreenPageModeUseNone) != 0)
viewerPreferences.Put(PdfName.NONFULLSCREENPAGEMODE, PdfName.USENONE);
else if ((preferences & PdfWriter.NonFullScreenPageModeUseOutlines) != 0)
viewerPreferences.Put(PdfName.NONFULLSCREENPAGEMODE, PdfName.USEOUTLINES);
else if ((preferences & PdfWriter.NonFullScreenPageModeUseThumbs) != 0)
viewerPreferences.Put(PdfName.NONFULLSCREENPAGEMODE, PdfName.USETHUMBS);
else if ((preferences & PdfWriter.NonFullScreenPageModeUseOC) != 0)
viewerPreferences.Put(PdfName.NONFULLSCREENPAGEMODE, PdfName.USEOC);
if ((preferences & PdfWriter.DirectionL2R) != 0)
viewerPreferences.Put(PdfName.DIRECTION, PdfName.L2R);
else if ((preferences & PdfWriter.DirectionR2L) != 0)
viewerPreferences.Put(PdfName.DIRECTION, PdfName.R2L);
if ((preferences & PdfWriter.PrintScalingNone) != 0)
viewerPreferences.Put(PdfName.PRINTSCALING, PdfName.NONE);
}
}
}
/**
* Given a key for a viewer preference (a PdfName object),
* this method returns the index in the VIEWER_PREFERENCES array.
* @param key a PdfName referring to a viewer preference
* @return an index in the VIEWER_PREFERENCES array
*/
private int GetIndex(PdfName key) {
for (int i = 0; i < VIEWER_PREFERENCES.Length; i++) {
if (VIEWER_PREFERENCES[i].Equals(key))
return i;
}
return -1;
}
/**
* Checks if some value is valid for a certain key.
*/
private bool IsPossibleValue(PdfName value, PdfName[] accepted) {
for (int i = 0; i < accepted.Length; i++) {
if (accepted[i].Equals(value)) {
return true;
}
}
return false;
}
/**
* Sets the viewer preferences for printing.
*/
public virtual void AddViewerPreference(PdfName key, PdfObject value) {
switch (GetIndex(key)) {
case 0: // HIDETOOLBAR
case 1: // HIDEMENUBAR
case 2: // HIDEWINDOWUI
case 3: // FITWINDOW
case 4: // CENTERWINDOW
case 5: // DISPLAYDOCTITLE
case 14: // PICKTRAYBYPDFSIZE
if (value is PdfBoolean) {
viewerPreferences.Put(key, value);
}
break;
case 6: // NONFULLSCREENPAGEMODE
if (value is PdfName
&& IsPossibleValue((PdfName)value, NONFULLSCREENPAGEMODE_PREFERENCES)) {
viewerPreferences.Put(key, value);
}
break;
case 7: // DIRECTION
if (value is PdfName
&& IsPossibleValue((PdfName)value, DIRECTION_PREFERENCES)) {
viewerPreferences.Put(key, value);
}
break;
case 8: // VIEWAREA
case 9: // VIEWCLIP
case 10: // PRINTAREA
case 11: // PRINTCLIP
if (value is PdfName
&& IsPossibleValue((PdfName)value, PAGE_BOUNDARIES)) {
viewerPreferences.Put(key, value);
}
break;
case 12: // PRINTSCALING
if (value is PdfName
&& IsPossibleValue((PdfName)value, PRINTSCALING_PREFERENCES)) {
viewerPreferences.Put(key, value);
}
break;
case 13: // DUPLEX
if (value is PdfName
&& IsPossibleValue((PdfName)value, DUPLEX_PREFERENCES)) {
viewerPreferences.Put(key, value);
}
break;
case 15: // PRINTPAGERANGE
if (value is PdfArray) {
viewerPreferences.Put(key, value);
}
break;
case 16: // NUMCOPIES
if (value is PdfNumber) {
viewerPreferences.Put(key, value);
}
break;
}
}
/**
* Adds the viewer preferences defined in the preferences parameter to a
* PdfDictionary (more specifically the root or catalog of a PDF file).
*
* @param catalog
*/
public void AddToCatalog(PdfDictionary catalog) {
// Page Layout
catalog.Remove(PdfName.PAGELAYOUT);
if ((pageLayoutAndMode & PdfWriter.PageLayoutSinglePage) != 0)
catalog.Put(PdfName.PAGELAYOUT, PdfName.SINGLEPAGE);
else if ((pageLayoutAndMode & PdfWriter.PageLayoutOneColumn) != 0)
catalog.Put(PdfName.PAGELAYOUT, PdfName.ONECOLUMN);
else if ((pageLayoutAndMode & PdfWriter.PageLayoutTwoColumnLeft) != 0)
catalog.Put(PdfName.PAGELAYOUT, PdfName.TWOCOLUMNLEFT);
else if ((pageLayoutAndMode & PdfWriter.PageLayoutTwoColumnRight) != 0)
catalog.Put(PdfName.PAGELAYOUT, PdfName.TWOCOLUMNRIGHT);
else if ((pageLayoutAndMode & PdfWriter.PageLayoutTwoPageLeft) != 0)
catalog.Put(PdfName.PAGELAYOUT, PdfName.TWOPAGELEFT);
else if ((pageLayoutAndMode & PdfWriter.PageLayoutTwoPageRight) != 0)
catalog.Put(PdfName.PAGELAYOUT, PdfName.TWOPAGERIGHT);
// Page Mode
catalog.Remove(PdfName.PAGEMODE);
if ((pageLayoutAndMode & PdfWriter.PageModeUseNone) != 0)
catalog.Put(PdfName.PAGEMODE, PdfName.USENONE);
else if ((pageLayoutAndMode & PdfWriter.PageModeUseOutlines) != 0)
catalog.Put(PdfName.PAGEMODE, PdfName.USEOUTLINES);
else if ((pageLayoutAndMode & PdfWriter.PageModeUseThumbs) != 0)
catalog.Put(PdfName.PAGEMODE, PdfName.USETHUMBS);
else if ((pageLayoutAndMode & PdfWriter.PageModeFullScreen) != 0)
catalog.Put(PdfName.PAGEMODE, PdfName.FULLSCREEN);
else if ((pageLayoutAndMode & PdfWriter.PageModeUseOC) != 0)
catalog.Put(PdfName.PAGEMODE, PdfName.USEOC);
else if ((pageLayoutAndMode & PdfWriter.PageModeUseAttachments) != 0)
catalog.Put(PdfName.PAGEMODE, PdfName.USEATTACHMENTS);
// viewer preferences (Table 8.1 of the PDF Reference)
catalog.Remove(PdfName.VIEWERPREFERENCES);
if (viewerPreferences.Size > 0) {
catalog.Put(PdfName.VIEWERPREFERENCES, viewerPreferences);
}
}
public static PdfViewerPreferencesImp GetViewerPreferences(PdfDictionary catalog) {
PdfViewerPreferencesImp preferences = new PdfViewerPreferencesImp();
int prefs = 0;
PdfName name = null;
// page layout
PdfObject obj = PdfReader.GetPdfObjectRelease(catalog.Get(PdfName.PAGELAYOUT));
if (obj != null && obj.IsName()) {
name = (PdfName) obj;
if (name.Equals(PdfName.SINGLEPAGE))
prefs |= PdfWriter.PageLayoutSinglePage;
else if (name.Equals(PdfName.ONECOLUMN))
prefs |= PdfWriter.PageLayoutOneColumn;
else if (name.Equals(PdfName.TWOCOLUMNLEFT))
prefs |= PdfWriter.PageLayoutTwoColumnLeft;
else if (name.Equals(PdfName.TWOCOLUMNRIGHT))
prefs |= PdfWriter.PageLayoutTwoColumnRight;
else if (name.Equals(PdfName.TWOPAGELEFT))
prefs |= PdfWriter.PageLayoutTwoPageLeft;
else if (name.Equals(PdfName.TWOPAGERIGHT))
prefs |= PdfWriter.PageLayoutTwoPageRight;
}
// page mode
obj = PdfReader.GetPdfObjectRelease(catalog.Get(PdfName.PAGEMODE));
if (obj != null && obj.IsName()) {
name = (PdfName) obj;
if (name.Equals(PdfName.USENONE))
prefs |= PdfWriter.PageModeUseNone;
else if (name.Equals(PdfName.USEOUTLINES))
prefs |= PdfWriter.PageModeUseOutlines;
else if (name.Equals(PdfName.USETHUMBS))
prefs |= PdfWriter.PageModeUseThumbs;
else if (name.Equals(PdfName.FULLSCREEN))
prefs |= PdfWriter.PageModeFullScreen;
else if (name.Equals(PdfName.USEOC))
prefs |= PdfWriter.PageModeUseOC;
else if (name.Equals(PdfName.USEATTACHMENTS))
prefs |= PdfWriter.PageModeUseAttachments;
}
// set page layout and page mode preferences
preferences.ViewerPreferences = prefs;
// other preferences
obj = PdfReader.GetPdfObjectRelease(catalog
.Get(PdfName.VIEWERPREFERENCES));
if (obj != null && obj.IsDictionary()) {
PdfDictionary vp = (PdfDictionary) obj;
for (int i = 0; i < VIEWER_PREFERENCES.Length; i++) {
obj = PdfReader.GetPdfObjectRelease(vp.Get(VIEWER_PREFERENCES[i]));
preferences.AddViewerPreference(VIEWER_PREFERENCES[i], obj);
}
}
return preferences;
}
}
}

View File

@ -1,256 +1,256 @@
using System;
using System.Collections;
using iTextSharp.text.pdf;
using iTextSharp.text;
using iTextSharp.text.pdf.interfaces;
/*
* $Id: PdfXConformanceImp.cs,v 1.3 2007/06/05 15:00:44 psoares33 Exp $
*
* Copyright 2006 Bruno Lowagie (based on code by Paulo Soares)
*
* The contents of this file are subject to the Mozilla Public License Version 1.1
* (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the License.
*
* The Original Code is 'iText, a free JAVA-PDF library'.
*
* The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
* the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
* All Rights Reserved.
* Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
* are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
*
* Contributor(s): all the names of the contributors are added in the source code
* where applicable.
*
* Alternatively, the contents of this file may be used under the terms of the
* LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
* provisions of LGPL are applicable instead of those above. If you wish to
* allow use of your version of this file only under the terms of the LGPL
* License and not to allow others to use your version of this file under
* the MPL, indicate your decision by deleting the provisions above and
* replace them with the notice and other provisions required by the LGPL.
* If you do not delete the provisions above, a recipient may use your version
* of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the MPL as stated above or under the terms of the GNU
* Library General Public License as published by the Free Software Foundation;
* either version 2 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
* details.
*
* If you didn't download this code from the following link, you should check if
* you aren't using an obsolete version:
* http://www.lowagie.com/iText/
*/
namespace iTextSharp.text.pdf.intern {
public class PdfXConformanceImp : IPdfXConformance {
/** A key for an aspect that can be checked for PDF/X Conformance. */
public const int PDFXKEY_COLOR = 1;
/** A key for an aspect that can be checked for PDF/X Conformance. */
public const int PDFXKEY_CMYK = 2;
/** A key for an aspect that can be checked for PDF/X Conformance. */
public const int PDFXKEY_RGB = 3;
/** A key for an aspect that can be checked for PDF/X Conformance. */
public const int PDFXKEY_FONT = 4;
/** A key for an aspect that can be checked for PDF/X Conformance. */
public const int PDFXKEY_IMAGE = 5;
/** A key for an aspect that can be checked for PDF/X Conformance. */
public const int PDFXKEY_GSTATE = 6;
/** A key for an aspect that can be checked for PDF/X Conformance. */
public const int PDFXKEY_LAYER = 7;
/**
* The value indicating if the PDF has to be in conformance with PDF/X.
*/
protected internal int pdfxConformance = PdfWriter.PDFXNONE;
/**
* @see com.lowagie.text.pdf.interfaces.PdfXConformance#setPDFXConformance(int)
*/
public int PDFXConformance {
set {
this.pdfxConformance = value;
}
get {
return pdfxConformance;
}
}
/**
* Checks if the PDF/X Conformance is necessary.
* @return true if the PDF has to be in conformance with any of the PDF/X specifications
*/
public bool IsPdfX() {
return pdfxConformance != PdfWriter.PDFXNONE;
}
/**
* Checks if the PDF has to be in conformance with PDF/X-1a:2001
* @return true of the PDF has to be in conformance with PDF/X-1a:2001
*/
public bool IsPdfX1A2001() {
return pdfxConformance == PdfWriter.PDFX1A2001;
}
/**
* Checks if the PDF has to be in conformance with PDF/X-3:2002
* @return true of the PDF has to be in conformance with PDF/X-3:2002
*/
public bool IsPdfX32002() {
return pdfxConformance == PdfWriter.PDFX32002;
}
/**
* Checks if the PDF has to be in conformance with PDFA1
* @return true of the PDF has to be in conformance with PDFA1
*/
public bool IsPdfA1() {
return pdfxConformance == PdfWriter.PDFA1A || pdfxConformance == PdfWriter.PDFA1B;
}
/**
* Checks if the PDF has to be in conformance with PDFA1A
* @return true of the PDF has to be in conformance with PDFA1A
*/
public bool IsPdfA1A() {
return pdfxConformance == PdfWriter.PDFA1A;
}
public void CompleteInfoDictionary(PdfDictionary info) {
if (IsPdfX() && !IsPdfA1()) {
if (info.Get(PdfName.GTS_PDFXVERSION) == null) {
if (IsPdfX1A2001()) {
info.Put(PdfName.GTS_PDFXVERSION, new PdfString("PDF/X-1:2001"));
info.Put(new PdfName("GTS_PDFXConformance"), new PdfString("PDF/X-1a:2001"));
}
else if (IsPdfX32002())
info.Put(PdfName.GTS_PDFXVERSION, new PdfString("PDF/X-3:2002"));
}
if (info.Get(PdfName.TITLE) == null) {
info.Put(PdfName.TITLE, new PdfString("Pdf document"));
}
if (info.Get(PdfName.CREATOR) == null) {
info.Put(PdfName.CREATOR, new PdfString("Unknown"));
}
if (info.Get(PdfName.TRAPPED) == null) {
info.Put(PdfName.TRAPPED, new PdfName("False"));
}
}
}
public void CompleteExtraCatalog(PdfDictionary extraCatalog) {
if (IsPdfX() && !IsPdfA1()) {
if (extraCatalog.Get(PdfName.OUTPUTINTENTS) == null) {
PdfDictionary outp = new PdfDictionary(PdfName.OUTPUTINTENT);
outp.Put(PdfName.OUTPUTCONDITION, new PdfString("SWOP CGATS TR 001-1995"));
outp.Put(PdfName.OUTPUTCONDITIONIDENTIFIER, new PdfString("CGATS TR 001"));
outp.Put(PdfName.REGISTRYNAME, new PdfString("http://www.color.org"));
outp.Put(PdfName.INFO, new PdfString(""));
outp.Put(PdfName.S, PdfName.GTS_PDFX);
extraCatalog.Put(PdfName.OUTPUTINTENTS, new PdfArray(outp));
}
}
}
/**
* 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.PDFXConformance;
switch (key) {
case PDFXKEY_COLOR:
switch (conf) {
case PdfWriter.PDFX1A2001:
if (obj1 is ExtendedColor) {
ExtendedColor ec = (ExtendedColor)obj1;
switch (ec.Type) {
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.PdfSpotColor.AlternativeCS);
break;
case ExtendedColor.TYPE_SHADING:
ShadingColor xc = (ShadingColor)ec;
CheckPDFXConformance(writer, PDFXKEY_COLOR, xc.PdfShadingPattern.Shading.ColorSpace);
break;
case ExtendedColor.TYPE_PATTERN:
PatternColor pc = (PatternColor)ec;
CheckPDFXConformance(writer, PDFXKEY_COLOR, pc.Painter.DefaultColor);
break;
}
}
else if (obj1 is 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.");
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).ArrayList[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.");
}
}
}
using System;
using System.Collections;
using iTextSharp.text.pdf;
using iTextSharp.text;
using iTextSharp.text.pdf.interfaces;
/*
* $Id: PdfXConformanceImp.cs,v 1.3 2007/06/05 15:00:44 psoares33 Exp $
*
* Copyright 2006 Bruno Lowagie (based on code by Paulo Soares)
*
* The contents of this file are subject to the Mozilla Public License Version 1.1
* (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the License.
*
* The Original Code is 'iText, a free JAVA-PDF library'.
*
* The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
* the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
* All Rights Reserved.
* Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
* are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
*
* Contributor(s): all the names of the contributors are added in the source code
* where applicable.
*
* Alternatively, the contents of this file may be used under the terms of the
* LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
* provisions of LGPL are applicable instead of those above. If you wish to
* allow use of your version of this file only under the terms of the LGPL
* License and not to allow others to use your version of this file under
* the MPL, indicate your decision by deleting the provisions above and
* replace them with the notice and other provisions required by the LGPL.
* If you do not delete the provisions above, a recipient may use your version
* of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the MPL as stated above or under the terms of the GNU
* Library General Public License as published by the Free Software Foundation;
* either version 2 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
* details.
*
* If you didn't download this code from the following link, you should check if
* you aren't using an obsolete version:
* http://www.lowagie.com/iText/
*/
namespace iTextSharp.text.pdf.intern {
public class PdfXConformanceImp : IPdfXConformance {
/** A key for an aspect that can be checked for PDF/X Conformance. */
public const int PDFXKEY_COLOR = 1;
/** A key for an aspect that can be checked for PDF/X Conformance. */
public const int PDFXKEY_CMYK = 2;
/** A key for an aspect that can be checked for PDF/X Conformance. */
public const int PDFXKEY_RGB = 3;
/** A key for an aspect that can be checked for PDF/X Conformance. */
public const int PDFXKEY_FONT = 4;
/** A key for an aspect that can be checked for PDF/X Conformance. */
public const int PDFXKEY_IMAGE = 5;
/** A key for an aspect that can be checked for PDF/X Conformance. */
public const int PDFXKEY_GSTATE = 6;
/** A key for an aspect that can be checked for PDF/X Conformance. */
public const int PDFXKEY_LAYER = 7;
/**
* The value indicating if the PDF has to be in conformance with PDF/X.
*/
protected internal int pdfxConformance = PdfWriter.PDFXNONE;
/**
* @see com.lowagie.text.pdf.interfaces.PdfXConformance#setPDFXConformance(int)
*/
public int PDFXConformance {
set {
this.pdfxConformance = value;
}
get {
return pdfxConformance;
}
}
/**
* Checks if the PDF/X Conformance is necessary.
* @return true if the PDF has to be in conformance with any of the PDF/X specifications
*/
public bool IsPdfX() {
return pdfxConformance != PdfWriter.PDFXNONE;
}
/**
* Checks if the PDF has to be in conformance with PDF/X-1a:2001
* @return true of the PDF has to be in conformance with PDF/X-1a:2001
*/
public bool IsPdfX1A2001() {
return pdfxConformance == PdfWriter.PDFX1A2001;
}
/**
* Checks if the PDF has to be in conformance with PDF/X-3:2002
* @return true of the PDF has to be in conformance with PDF/X-3:2002
*/
public bool IsPdfX32002() {
return pdfxConformance == PdfWriter.PDFX32002;
}
/**
* Checks if the PDF has to be in conformance with PDFA1
* @return true of the PDF has to be in conformance with PDFA1
*/
public bool IsPdfA1() {
return pdfxConformance == PdfWriter.PDFA1A || pdfxConformance == PdfWriter.PDFA1B;
}
/**
* Checks if the PDF has to be in conformance with PDFA1A
* @return true of the PDF has to be in conformance with PDFA1A
*/
public bool IsPdfA1A() {
return pdfxConformance == PdfWriter.PDFA1A;
}
public void CompleteInfoDictionary(PdfDictionary info) {
if (IsPdfX() && !IsPdfA1()) {
if (info.Get(PdfName.GTS_PDFXVERSION) == null) {
if (IsPdfX1A2001()) {
info.Put(PdfName.GTS_PDFXVERSION, new PdfString("PDF/X-1:2001"));
info.Put(new PdfName("GTS_PDFXConformance"), new PdfString("PDF/X-1a:2001"));
}
else if (IsPdfX32002())
info.Put(PdfName.GTS_PDFXVERSION, new PdfString("PDF/X-3:2002"));
}
if (info.Get(PdfName.TITLE) == null) {
info.Put(PdfName.TITLE, new PdfString("Pdf document"));
}
if (info.Get(PdfName.CREATOR) == null) {
info.Put(PdfName.CREATOR, new PdfString("Unknown"));
}
if (info.Get(PdfName.TRAPPED) == null) {
info.Put(PdfName.TRAPPED, new PdfName("False"));
}
}
}
public void CompleteExtraCatalog(PdfDictionary extraCatalog) {
if (IsPdfX() && !IsPdfA1()) {
if (extraCatalog.Get(PdfName.OUTPUTINTENTS) == null) {
PdfDictionary outp = new PdfDictionary(PdfName.OUTPUTINTENT);
outp.Put(PdfName.OUTPUTCONDITION, new PdfString("SWOP CGATS TR 001-1995"));
outp.Put(PdfName.OUTPUTCONDITIONIDENTIFIER, new PdfString("CGATS TR 001"));
outp.Put(PdfName.REGISTRYNAME, new PdfString("http://www.color.org"));
outp.Put(PdfName.INFO, new PdfString(""));
outp.Put(PdfName.S, PdfName.GTS_PDFX);
extraCatalog.Put(PdfName.OUTPUTINTENTS, new PdfArray(outp));
}
}
}
/**
* 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.PDFXConformance;
switch (key) {
case PDFXKEY_COLOR:
switch (conf) {
case PdfWriter.PDFX1A2001:
if (obj1 is ExtendedColor) {
ExtendedColor ec = (ExtendedColor)obj1;
switch (ec.Type) {
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.PdfSpotColor.AlternativeCS);
break;
case ExtendedColor.TYPE_SHADING:
ShadingColor xc = (ShadingColor)ec;
CheckPDFXConformance(writer, PDFXKEY_COLOR, xc.PdfShadingPattern.Shading.ColorSpace);
break;
case ExtendedColor.TYPE_PATTERN:
PatternColor pc = (PatternColor)ec;
CheckPDFXConformance(writer, PDFXKEY_COLOR, pc.Painter.DefaultColor);
break;
}
}
else if (obj1 is 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).PostscriptFontName);
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)[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.");
}
}
}
}

View File

@ -857,6 +857,11 @@
SubType = "Code"
BuildAction = "Compile"
/>
<File
RelPath = "iTextSharp\text\pdf\PdfDeveloperExtension.cs"
SubType = "Code"
BuildAction = "Compile"
/>
<File
RelPath = "iTextSharp\text\pdf\PdfDictionary.cs"
SubType = "Code"