diff --git a/src/core/iTextSharp/text/pdf/PdfFileSpecification.cs b/src/core/iTextSharp/text/pdf/PdfFileSpecification.cs index 17b0c2b..63244c5 100644 --- a/src/core/iTextSharp/text/pdf/PdfFileSpecification.cs +++ b/src/core/iTextSharp/text/pdf/PdfFileSpecification.cs @@ -1,79 +1,94 @@ -using System; -using System.IO; -using System.Net; -using iTextSharp.text.pdf.collection; -/* - * Copyright 2003 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 inp 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 inp 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"), inp 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 inp 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 { - /** Specifies a file or an URL. The file can be extern or embedded. - * - * @author Paulo Soares (psoares@consiste.pt) - */ - public class PdfFileSpecification : PdfDictionary { - protected PdfWriter writer; - protected PdfIndirectReference refi; - - /** Creates a new instance of PdfFileSpecification. The static methods are preferred. */ - public PdfFileSpecification() : base(PdfName.FILESPEC) { - } - - /** - * Creates a file specification of type URL. - * @param writer the PdfWriter - * @param url the URL - * @return the file specification - */ - public static PdfFileSpecification Url(PdfWriter writer, String url) { - PdfFileSpecification fs = new PdfFileSpecification(); - fs.writer = writer; - fs.Put(PdfName.FS, PdfName.URL); - fs.Put(PdfName.F, new PdfString(url)); - return fs; +using System; +using System.IO; +using System.Net; +using iTextSharp.text.pdf.collection; +/* + * Copyright 2003 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 inp 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 inp 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"), inp 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 inp 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 { + /** Specifies a file or an URL. The file can be extern or embedded. + * + * @author Paulo Soares (psoares@consiste.pt) + */ + public class PdfFileSpecification : PdfDictionary { + protected PdfWriter writer; + protected PdfIndirectReference refi; + + /** Creates a new instance of PdfFileSpecification. The static methods are preferred. */ + public PdfFileSpecification() : base(PdfName.FILESPEC) { + } + + /** + * Creates a file specification of type URL. + * @param writer the PdfWriter + * @param url the URL + * @return the file specification + */ + public static PdfFileSpecification Url(PdfWriter writer, String url) { + PdfFileSpecification fs = new PdfFileSpecification(); + fs.writer = writer; + fs.Put(PdfName.FS, PdfName.URL); + fs.Put(PdfName.F, new PdfString(url)); + return fs; + } + + /** + * Creates a file specification with the file embedded. The file may + * come from the file system or from a byte array. The data is flate compressed. + * @param writer the PdfWriter + * @param filePath the file path + * @param fileDisplay the file information that is presented to the user + * @param fileStore the byte array with the file. If it is not null + * it takes precedence over filePath + * @throws IOException on error + * @return the file specification + */ + public static PdfFileSpecification FileEmbedded(PdfWriter writer, String filePath, String fileDisplay, byte[] fileStore) { + return FileEmbedded(writer, filePath, fileDisplay, fileStore, PdfStream.BEST_COMPRESSION); } /** @@ -84,29 +99,51 @@ namespace iTextSharp.text.pdf { * @param fileDisplay the file information that is presented to the user * @param fileStore the byte array with the file. If it is not null * it takes precedence over filePath - * @throws IOException on error - * @return the file specification - */ - public static PdfFileSpecification FileEmbedded(PdfWriter writer, String filePath, String fileDisplay, byte[] fileStore) { - return FileEmbedded(writer, filePath, fileDisplay, fileStore, true); - } - - - /** - * Creates a file specification with the file embedded. The file may - * come from the file system or from a byte array. - * @param writer the PdfWriter - * @param filePath the file path - * @param fileDisplay the file information that is presented to the user - * @param fileStore the byte array with the file. If it is not null + * @param compressionLevel the compression level to be used for compressing the file * it takes precedence over filePath - * @param compress sets the compression on the data. Multimedia content will benefit little - * from compression * @throws IOException on error * @return the file specification + * @since 2.1.3 */ - public static PdfFileSpecification FileEmbedded(PdfWriter writer, String filePath, String fileDisplay, byte[] fileStore, bool compress) { - return FileEmbedded(writer, filePath, fileDisplay, fileStore, compress, null, null); + public static PdfFileSpecification FileEmbedded(PdfWriter writer, String filePath, String fileDisplay, byte[] fileStore, int compressionLevel) { + return FileEmbedded(writer, filePath, fileDisplay, fileStore, null, null, compressionLevel); + } + + + /** + * Creates a file specification with the file embedded. The file may + * come from the file system or from a byte array. + * @param writer the PdfWriter + * @param filePath the file path + * @param fileDisplay the file information that is presented to the user + * @param fileStore the byte array with the file. If it is not null + * it takes precedence over filePath + * @param compress sets the compression on the data. Multimedia content will benefit little + * from compression + * @throws IOException on error + * @return the file specification + */ + public static PdfFileSpecification FileEmbedded(PdfWriter writer, String filePath, String fileDisplay, byte[] fileStore, bool compress) { + return FileEmbedded(writer, filePath, fileDisplay, fileStore, null, null, compress ? PdfStream.BEST_COMPRESSION : PdfStream.NO_COMPRESSION); + } + + /** + * Creates a file specification with the file embedded. The file may + * come from the file system or from a byte array. + * @param writer the PdfWriter + * @param filePath the file path + * @param fileDisplay the file information that is presented to the user + * @param fileStore the byte array with the file. If it is not null + * it takes precedence over filePath + * @param compress sets the compression on the data. Multimedia content will benefit little + * from compression + * @param mimeType the optional mimeType + * @param fileParameter the optional extra file parameters such as the creation or modification date + * @throws IOException on error + * @return the file specification + */ + public static PdfFileSpecification FileEmbedded(PdfWriter writer, String filePath, String fileDisplay, byte[] fileStore, bool compress, String mimeType, PdfDictionary fileParameter) { + return FileEmbedded(writer, filePath, fileDisplay, fileStore, null, null, compress ? PdfStream.BEST_COMPRESSION : PdfStream.NO_COMPRESSION); } /** @@ -117,145 +154,144 @@ namespace iTextSharp.text.pdf { * @param fileDisplay the file information that is presented to the user * @param fileStore the byte array with the file. If it is not null * it takes precedence over filePath - * @param compress sets the compression on the data. Multimedia content will benefit little - * from compression * @param mimeType the optional mimeType * @param fileParameter the optional extra file parameters such as the creation or modification date + * @param compressionLevel the level of compression * @throws IOException on error * @return the file specification + * @since 2.1.3 */ - public static PdfFileSpecification FileEmbedded(PdfWriter writer, String filePath, String fileDisplay, byte[] fileStore, bool compress, String mimeType, PdfDictionary fileParameter) { - PdfFileSpecification fs = new PdfFileSpecification(); - fs.writer = writer; - fs.Put(PdfName.F, new PdfString(fileDisplay)); - PdfStream stream; - Stream inp = null; - PdfIndirectReference refi; - PdfIndirectReference refFileLength; - try { - refFileLength = writer.PdfIndirectReference; - if (fileStore == null) { - if (File.Exists(filePath)) { - inp = new FileStream(filePath, FileMode.Open, FileAccess.Read); - } - else { - if (filePath.StartsWith("file:/") || filePath.StartsWith("http://") || filePath.StartsWith("https://")) { - WebRequest w = WebRequest.Create(filePath); - inp = w.GetResponse().GetResponseStream(); - } - else { - inp = BaseFont.GetResourceStream(filePath); - if (inp == null) - throw new IOException(filePath + " not found as file or resource."); - } - } - stream = new PdfStream(inp, writer); - } - else - stream = new PdfStream(fileStore); - stream.Put(PdfName.TYPE, PdfName.EMBEDDEDFILE); - if (compress) - stream.FlateCompress(); - stream.Put(PdfName.PARAMS, refFileLength); - if (mimeType != null) - stream.Put(PdfName.SUBTYPE, new PdfName(mimeType)); - refi = writer.AddToBody(stream).IndirectReference; - if (fileStore == null) { - stream.WriteLength(); - } - PdfDictionary param = new PdfDictionary(); - if (fileParameter != null) - param.Merge(fileParameter); - param.Put(PdfName.SIZE, new PdfNumber(stream.RawLength)); - writer.AddToBody(param, refFileLength); - } - finally { - if (inp != null) - try{inp.Close();}catch{} - } - PdfDictionary f = new PdfDictionary(); - f.Put(PdfName.F, refi); - fs.Put(PdfName.EF, f); - return fs; - } - - /** - * Creates a file specification for an external file. - * @param writer the PdfWriter - * @param filePath the file path - * @return the file specification - */ - public static PdfFileSpecification FileExtern(PdfWriter writer, String filePath) { - PdfFileSpecification fs = new PdfFileSpecification(); - fs.writer = writer; - fs.Put(PdfName.F, new PdfString(filePath)); - return fs; - } - - /** - * Gets the indirect reference to this file specification. - * Multiple invocations will retrieve the same value. - * @throws IOException on error - * @return the indirect reference - */ - public PdfIndirectReference Reference { - get { - if (refi != null) - return refi; - refi = writer.AddToBody(this).IndirectReference; - return refi; - } - } - - /** - * Sets the file name (the key /F) string as an hex representation - * to support multi byte file names. The name must have the slash and - * backslash escaped according to the file specification rules - * @param fileName the file name as a byte array - */ - public byte[] MultiByteFileName { - set { - Put(PdfName.F, new PdfString(value).SetHexWriting(true)); - } - } - - /** - * Adds the unicode file name (the key /UF). This entry was introduced - * in PDF 1.7. The filename must have the slash and backslash escaped - * according to the file specification rules. - * @param filename the filename - * @param unicode if true, the filename is UTF-16BE encoded; otherwise PDFDocEncoding is used; - */ - public void SetUnicodeFileName(String filename, bool unicode) { - Put(PdfName.UF, new PdfString(filename, unicode ? PdfObject.TEXT_UNICODE : PdfObject.TEXT_PDFDOCENCODING)); - } - - /** - * Sets a flag that indicates whether an external file referenced by the file - * specification is volatile. If the value is true, applications should never - * cache a copy of the file. - * @param volatile_file if true, the external file should not be cached - */ - public bool Volatile { - set { - Put(PdfName.V, new PdfBoolean(value)); - } - } - - /** - * Adds a description for the file that is specified here. - * @param description some text - * @param unicode if true, the text is added as a unicode string - */ - public void AddDescription(String description, bool unicode) { - Put(PdfName.DESC, new PdfString(description, unicode ? PdfObject.TEXT_UNICODE : PdfObject.TEXT_PDFDOCENCODING)); - } - - /** - * Adds the Collection item dictionary. - */ - public void AddCollectionItem(PdfCollectionItem ci) { - Put(PdfName.CI, ci); - } - } -} + public static PdfFileSpecification FileEmbedded(PdfWriter writer, String filePath, String fileDisplay, byte[] fileStore, String mimeType, PdfDictionary fileParameter, int compressionLevel) { + PdfFileSpecification fs = new PdfFileSpecification(); + fs.writer = writer; + fs.Put(PdfName.F, new PdfString(fileDisplay)); + PdfEFStream stream; + Stream inp = null; + PdfIndirectReference refi; + PdfIndirectReference refFileLength; + try { + refFileLength = writer.PdfIndirectReference; + if (fileStore == null) { + if (File.Exists(filePath)) { + inp = new FileStream(filePath, FileMode.Open, FileAccess.Read); + } + else { + if (filePath.StartsWith("file:/") || filePath.StartsWith("http://") || filePath.StartsWith("https://")) { + WebRequest w = WebRequest.Create(filePath); + inp = w.GetResponse().GetResponseStream(); + } + else { + inp = BaseFont.GetResourceStream(filePath); + if (inp == null) + throw new IOException(filePath + " not found as file or resource."); + } + } + stream = new PdfEFStream(inp, writer); + } + else + stream = new PdfEFStream(fileStore); + stream.Put(PdfName.TYPE, PdfName.EMBEDDEDFILE); + stream.FlateCompress(compressionLevel); + stream.Put(PdfName.PARAMS, refFileLength); + if (mimeType != null) + stream.Put(PdfName.SUBTYPE, new PdfName(mimeType)); + refi = writer.AddToBody(stream).IndirectReference; + if (fileStore == null) { + stream.WriteLength(); + } + PdfDictionary param = new PdfDictionary(); + if (fileParameter != null) + param.Merge(fileParameter); + param.Put(PdfName.SIZE, new PdfNumber(stream.RawLength)); + writer.AddToBody(param, refFileLength); + } + finally { + if (inp != null) + try{inp.Close();}catch{} + } + PdfDictionary f = new PdfDictionary(); + f.Put(PdfName.F, refi); + fs.Put(PdfName.EF, f); + return fs; + } + + /** + * Creates a file specification for an external file. + * @param writer the PdfWriter + * @param filePath the file path + * @return the file specification + */ + public static PdfFileSpecification FileExtern(PdfWriter writer, String filePath) { + PdfFileSpecification fs = new PdfFileSpecification(); + fs.writer = writer; + fs.Put(PdfName.F, new PdfString(filePath)); + return fs; + } + + /** + * Gets the indirect reference to this file specification. + * Multiple invocations will retrieve the same value. + * @throws IOException on error + * @return the indirect reference + */ + public PdfIndirectReference Reference { + get { + if (refi != null) + return refi; + refi = writer.AddToBody(this).IndirectReference; + return refi; + } + } + + /** + * Sets the file name (the key /F) string as an hex representation + * to support multi byte file names. The name must have the slash and + * backslash escaped according to the file specification rules + * @param fileName the file name as a byte array + */ + public byte[] MultiByteFileName { + set { + Put(PdfName.F, new PdfString(value).SetHexWriting(true)); + } + } + + /** + * Adds the unicode file name (the key /UF). This entry was introduced + * in PDF 1.7. The filename must have the slash and backslash escaped + * according to the file specification rules. + * @param filename the filename + * @param unicode if true, the filename is UTF-16BE encoded; otherwise PDFDocEncoding is used; + */ + public void SetUnicodeFileName(String filename, bool unicode) { + Put(PdfName.UF, new PdfString(filename, unicode ? PdfObject.TEXT_UNICODE : PdfObject.TEXT_PDFDOCENCODING)); + } + + /** + * Sets a flag that indicates whether an external file referenced by the file + * specification is volatile. If the value is true, applications should never + * cache a copy of the file. + * @param volatile_file if true, the external file should not be cached + */ + public bool Volatile { + set { + Put(PdfName.V, new PdfBoolean(value)); + } + } + + /** + * Adds a description for the file that is specified here. + * @param description some text + * @param unicode if true, the text is added as a unicode string + */ + public void AddDescription(String description, bool unicode) { + Put(PdfName.DESC, new PdfString(description, unicode ? PdfObject.TEXT_UNICODE : PdfObject.TEXT_PDFDOCENCODING)); + } + + /** + * Adds the Collection item dictionary. + */ + public void AddCollectionItem(PdfCollectionItem ci) { + Put(PdfName.CI, ci); + } + } +} diff --git a/src/core/iTextSharp/text/pdf/PdfSignatureAppearance.cs b/src/core/iTextSharp/text/pdf/PdfSignatureAppearance.cs index 47168d4..9674535 100644 --- a/src/core/iTextSharp/text/pdf/PdfSignatureAppearance.cs +++ b/src/core/iTextSharp/text/pdf/PdfSignatureAppearance.cs @@ -253,19 +253,18 @@ namespace iTextSharp.text.pdf { AcroFields.Item item = af.GetFieldItem(fieldName); if (item == null) throw new ArgumentException("The field " + fieldName + " does not exist."); - PdfDictionary merged = (PdfDictionary)item.merged[0]; + PdfDictionary merged = item.GetMerged(0); if (!PdfName.SIG.Equals(PdfReader.GetPdfObject(merged.Get(PdfName.FT)))) throw new ArgumentException("The field " + fieldName + " is not a signature field."); this.fieldName = fieldName; - PdfArray r = (PdfArray)PdfReader.GetPdfObject(merged.Get(PdfName.RECT)); - ArrayList ar = r.ArrayList; - float llx = ((PdfNumber)PdfReader.GetPdfObject((PdfObject)ar[0])).FloatValue; - float lly = ((PdfNumber)PdfReader.GetPdfObject((PdfObject)ar[1])).FloatValue; - float urx = ((PdfNumber)PdfReader.GetPdfObject((PdfObject)ar[2])).FloatValue; - float ury = ((PdfNumber)PdfReader.GetPdfObject((PdfObject)ar[3])).FloatValue; + PdfArray r = merged.GetAsArray(PdfName.RECT); + float llx = r.GetAsNumber(0).FloatValue; + float lly = r.GetAsNumber(1).FloatValue; + float urx = r.GetAsNumber(2).FloatValue; + float ury = r.GetAsNumber(3).FloatValue; pageRect = new Rectangle(llx, lly, urx, ury); pageRect.Normalize(); - page = (int)item.page[0]; + page = item.GetPage(0); int rotation = writer.reader.GetPageRotation(page); Rectangle pageSize = writer.reader.GetPageSizeWithRotation(page); switch (rotation) { @@ -828,8 +827,7 @@ namespace iTextSharp.text.pdf { PdfIndirectReference refSig = writer.PdfIndirectReference; writer.SigFlags = 3; if (fieldExists) { - ArrayList widgets = af.GetFieldItem(name).widgets; - PdfDictionary widget = (PdfDictionary)widgets[0]; + PdfDictionary widget = af.GetFieldItem(name).GetWidget(0); writer.MarkUsed(widget); widget.Put(PdfName.P, writer.GetPageReference(Page)); widget.Put(PdfName.V, refSig); diff --git a/src/core/iTextSharp/text/pdf/PdfStamperImp.cs b/src/core/iTextSharp/text/pdf/PdfStamperImp.cs index ddeb84e..0e596f3 100644 --- a/src/core/iTextSharp/text/pdf/PdfStamperImp.cs +++ b/src/core/iTextSharp/text/pdf/PdfStamperImp.cs @@ -1,10 +1,12 @@ using System; using System.IO; using System.Collections; +using System.Text; using System.util; using iTextSharp.text.pdf.interfaces; using iTextSharp.text.pdf.intern; using iTextSharp.text.pdf.collection; +using iTextSharp.text.xml.xmp; /* * Copyright 2003 by Paulo Soares. * @@ -90,7 +92,7 @@ namespace iTextSharp.text.pdf { */ internal PdfStamperImp(PdfReader reader, Stream os, char pdfVersion, bool append) : base(new PdfDocument(), os) { if (!reader.IsOpenedWithFullPermissions) - throw new ArgumentException("PdfReader not opened with owner password"); + throw new BadPasswordException("PdfReader not opened with owner password"); if (reader.Tampered) throw new DocumentException("The original document was reused. Read it again from file."); reader.Tampered = true; @@ -143,7 +145,11 @@ namespace iTextSharp.text.pdf { if (flatFreeText) FlatFreeTextFields(); AddFieldResources(); - PdfDictionary acroForm = (PdfDictionary)PdfReader.GetPdfObject(reader.Catalog.Get(PdfName.ACROFORM), reader.Catalog); + PdfDictionary catalog = reader.Catalog; + PdfDictionary pages = (PdfDictionary)PdfReader.GetPdfObject(catalog.Get(PdfName.PAGES)); + pages.Put(PdfName.ITXT, new PdfString(Document.Release)); + MarkUsed(pages); + PdfDictionary acroForm = (PdfDictionary)PdfReader.GetPdfObject(catalog.Get(PdfName.ACROFORM), reader.Catalog); if (acroFields != null && acroFields.Xfa.Changed) { MarkUsed(acroForm); if (!flat) @@ -153,6 +159,7 @@ namespace iTextSharp.text.pdf { if (acroForm != null) { acroForm.Put(PdfName.SIGFLAGS, new PdfNumber(sigFlags)); MarkUsed(acroForm); + MarkUsed(catalog); } } closed = true; @@ -160,12 +167,50 @@ namespace iTextSharp.text.pdf { SetOutlines(); SetJavaScript(); AddFileAttachments(); - PdfDictionary catalog = reader.Catalog; if (openAction != null) { catalog.Put(PdfName.OPENACTION, openAction); } if (pdf.pageLabels != null) catalog.Put(PdfName.PAGELABELS, pdf.pageLabels.GetDictionary(this)); + // OCG + if (documentOCG.Count > 0) { + FillOCProperties(false); + PdfDictionary ocdict = catalog.GetAsDict(PdfName.OCPROPERTIES); + if (ocdict == null) { + reader.Catalog.Put(PdfName.OCPROPERTIES, OCProperties); + } + else { + ocdict.Put(PdfName.OCGS, OCProperties.Get(PdfName.OCGS)); + PdfDictionary ddict = ocdict.GetAsDict(PdfName.D); + if (ddict == null) { + ddict = new PdfDictionary(); + ocdict.Put(PdfName.D, ddict); + } + ddict.Put(PdfName.ORDER, OCProperties.GetAsDict(PdfName.D).Get(PdfName.ORDER)); + ddict.Put(PdfName.RBGROUPS, OCProperties.GetAsDict(PdfName.D).Get(PdfName.RBGROUPS)); + ddict.Put(PdfName.OFF, OCProperties.GetAsDict(PdfName.D).Get(PdfName.OFF)); + ddict.Put(PdfName.AS, OCProperties.GetAsDict(PdfName.D).Get(PdfName.AS)); + } + } + // metadata + int skipInfo = -1; + PRIndirectReference iInfo = (PRIndirectReference)reader.Trailer.Get(PdfName.INFO); + PdfDictionary oldInfo = (PdfDictionary)PdfReader.GetPdfObject(iInfo); + String producer = null; + if (iInfo != null) + skipInfo = iInfo.Number; + if (oldInfo != null) + producer = oldInfo.GetAsString(PdfName.PRODUCER).ToString(); + if (producer == null) { + producer = Document.Version; + } + else if (producer.IndexOf(Document.Product) == -1) { + StringBuilder buf = new StringBuilder(producer); + buf.Append("; modified using "); + buf.Append(Document.Version); + producer = buf.ToString(); + } + // XMP byte[] altMetadata = null; PdfObject xmpo = PdfReader.GetPdfObject(catalog.Get(PdfName.METADATA)); if (xmpo != null && xmpo.IsStream()) { @@ -176,8 +221,21 @@ namespace iTextSharp.text.pdf { altMetadata = xmpMetadata; } // if there is XMP data to add: add it + PdfDate date = new PdfDate(); if (altMetadata != null) { - PdfStream xmp = new PdfStream(altMetadata); + PdfStream xmp; + try { + XmpReader xmpr = new XmpReader(altMetadata); + if (!xmpr.Replace("http://ns.adobe.com/pdf/1.3/", "Producer", producer)) + xmpr.Add("rdf:Description", "http://ns.adobe.com/pdf/1.3/", "pdf:Producer", producer); + if (!xmpr.Replace("http://ns.adobe.com/xap/1.0/", "ModifyDate", date.GetW3CDate())) + xmpr.Add("rdf:Description", "http://ns.adobe.com/xap/1.0/", "xmp:ModifyDate", date.GetW3CDate()); + xmpr.Replace("http://ns.adobe.com/xap/1.0/", "MetadataDate", date.GetW3CDate()); + xmp = new PdfStream(xmpr.SerializeDoc()); + } + catch { + xmp = new PdfStream(altMetadata); + } xmp.Put(PdfName.TYPE, PdfName.METADATA); xmp.Put(PdfName.SUBTYPE, PdfName.XML); if (crypto != null && !crypto.IsMetadataEncrypted()) { @@ -185,39 +243,24 @@ namespace iTextSharp.text.pdf { ar.Add(PdfName.CRYPT); xmp.Put(PdfName.FILTER, ar); } - catalog.Put(PdfName.METADATA, body.Add(xmp).IndirectReference); - MarkUsed(catalog); - } - if (documentOCG.Count != 0) { - FillOCProperties(false); - PdfDictionary ocdict = catalog.GetAsDict(PdfName.OCPROPERTIES); - if (ocdict == null) { - reader.Catalog.Put(PdfName.OCPROPERTIES, OCProperties); + if (append && xmpo != null) { + body.Add(xmp, xmpo.IndRef); } else { - ocdict.Put(PdfName.OCGS, OCProperties.Get(PdfName.OCGS)); - PdfDictionary ddict = ocdict.GetAsDict(PdfName.D); - ddict.Put(PdfName.ORDER, OCProperties.GetAsDict(PdfName.D).Get(PdfName.ORDER)); - ddict.Put(PdfName.RBGROUPS, OCProperties.GetAsDict(PdfName.D).Get(PdfName.RBGROUPS)); - ddict.Put(PdfName.OFF, OCProperties.GetAsDict(PdfName.D).Get(PdfName.OFF)); - ddict.Put(PdfName.AS, OCProperties.GetAsDict(PdfName.D).Get(PdfName.AS)); + catalog.Put(PdfName.METADATA, body.Add(xmp).IndirectReference); + MarkUsed(catalog); } } - PRIndirectReference iInfo = null; try { file.ReOpen(); AlterContents(); - iInfo = (PRIndirectReference)reader.trailer.Get(PdfName.INFO); - int skip = -1; - if (iInfo != null) - skip = iInfo.Number; int rootN = ((PRIndirectReference)reader.trailer.Get(PdfName.ROOT)).Number; if (append) { int[] keys = marked.GetKeys(); for (int k = 0; k < keys.Length; ++k) { int j = keys[k]; PdfObject obj = reader.GetPdfObjectRelease(j); - if (obj != null && skip != j && j < initialXrefSize) { + if (obj != null && skipInfo != j && j < initialXrefSize) { AddToBody(obj, j, j != rootN); } } @@ -231,7 +274,7 @@ namespace iTextSharp.text.pdf { else { for (int k = 1; k < reader.XrefSize; ++k) { PdfObject obj = reader.GetPdfObjectRelease(k); - if (obj != null && skip != k) { + if (obj != null && skipInfo != k) { AddToBody(obj, GetNewObjectNumber(reader, k, 0), k != rootN); } } @@ -262,7 +305,6 @@ namespace iTextSharp.text.pdf { PRIndirectReference iRoot = (PRIndirectReference)reader.trailer.Get(PdfName.ROOT); PdfIndirectReference root = new PdfIndirectReference(0, GetNewObjectNumber(reader, iRoot.Number, 0)); PdfIndirectReference info = null; - PdfDictionary oldInfo = (PdfDictionary)PdfReader.GetPdfObject(iInfo); PdfDictionary newInfo = new PdfDictionary(); if (oldInfo != null) { foreach (PdfName key in oldInfo.Keys) { @@ -280,8 +322,8 @@ namespace iTextSharp.text.pdf { newInfo.Put(keyName, new PdfString(value, PdfObject.TEXT_UNICODE)); } } - if (altMetadata == null) // hack because changing the modification data makes the XMP data inconsistent - newInfo.Put(PdfName.MODDATE, new PdfDate()); + newInfo.Put(PdfName.MODDATE, date); + newInfo.Put(PdfName.PRODUCER, new PdfString(producer)); if (append) { if (iInfo == null) info = AddToBody(newInfo, false).IndirectReference; @@ -481,9 +523,9 @@ namespace iTextSharp.text.pdf { FindAllObjects(reader, PdfReader.GetPdfObject(obj), hits); return; case PdfObject.ARRAY: - ArrayList lst = ((PdfArray)obj).ArrayList; - for (int k = 0; k < lst.Count; ++k) { - FindAllObjects(reader, (PdfObject)lst[k], hits); + PdfArray a = (PdfArray)obj; + for (int k = 0; k < a.Size; ++k) { + FindAllObjects(reader, a[k], hits); } return; case PdfObject.DICTIONARY: @@ -504,21 +546,20 @@ namespace iTextSharp.text.pdf { if (readers2intrefs.ContainsKey(fdf)) return; PdfDictionary catalog = fdf.Catalog; - catalog = (PdfDictionary)PdfReader.GetPdfObject(catalog.Get(PdfName.FDF)); + catalog = catalog.GetAsDict(PdfName.FDF); if (catalog == null) return; - PdfArray annots = (PdfArray)PdfReader.GetPdfObject(catalog.Get(PdfName.ANNOTS)); + PdfArray annots = catalog.GetAsArray(PdfName.ANNOTS); if (annots == null || annots.Size == 0) return; RegisterReader(fdf, false); IntHashtable hits = new IntHashtable(); Hashtable irt = new Hashtable(); ArrayList an = new ArrayList(); - ArrayList ar = annots.ArrayList; - for (int k = 0; k < ar.Count; ++k) { - PdfObject obj = (PdfObject)ar[k]; + for (int k = 0; k < annots.Size; ++k) { + PdfObject obj = annots[k]; PdfDictionary annot = (PdfDictionary)PdfReader.GetPdfObject(obj); - PdfNumber page = (PdfNumber)PdfReader.GetPdfObject(annot.Get(PdfName.PAGE)); + PdfNumber page = annot.GetAsNumber(PdfName.PAGE); if (page == null || page.IntValue >= reader.NumberOfPages) continue; FindAllObjects(fdf, obj, hits); @@ -550,7 +591,7 @@ namespace iTextSharp.text.pdf { for (int k = 0; k < an.Count; ++k) { PdfObject obj = (PdfObject)an[k]; PdfDictionary annot = (PdfDictionary)PdfReader.GetPdfObject(obj); - PdfNumber page = (PdfNumber)PdfReader.GetPdfObject(annot.Get(PdfName.PAGE)); + PdfNumber page = annot.GetAsNumber(PdfName.PAGE); PdfDictionary dic = reader.GetPageN(page.IntValue + 1); PdfArray annotsp = (PdfArray)PdfReader.GetPdfObject(dic.Get(PdfName.ANNOTS), dic); if (annotsp == null) { @@ -598,11 +639,10 @@ namespace iTextSharp.text.pdf { return; Hashtable fields = acroFields.Fields; foreach (AcroFields.Item item in fields.Values) { - ArrayList pages = item.page; - for (int k = 0; k < pages.Count; ++k) { - int p = (int)pages[k]; + for (int k = 0; k < item.Size; ++k) { + int p = item.GetPage(k); if (p >= page) - pages[k] = p + 1; + item.ForcePage(k, p + 1); } } } @@ -673,17 +713,16 @@ namespace iTextSharp.text.pdf { parentRef = new PRIndirectReference(reader, parentRef.Number); parent = (PdfDictionary)PdfReader.GetPdfObject(parentRef); PdfArray kids = (PdfArray)PdfReader.GetPdfObject(parent.Get(PdfName.KIDS), parent); - ArrayList ar = kids.ArrayList; - int len = ar.Count; + int len = kids.Size; int num = firstPageRef.Number; for (int k = 0; k < len; ++k) { - PRIndirectReference cur = (PRIndirectReference)ar[k]; + PRIndirectReference cur = (PRIndirectReference)kids[k]; if (num == cur.Number) { - ar.Insert(k, pref); + kids.Add(k, pref); break; } } - if (len == ar.Count) + if (len == kids.Size) throw new Exception("Internal inconsistence."); MarkUsed(kids); reader.pageRefs.InsertPage(pageNumber, pref); @@ -694,7 +733,7 @@ namespace iTextSharp.text.pdf { MarkUsed(parent); PdfNumber count = (PdfNumber)PdfReader.GetPdfObjectRelease(parent.Get(PdfName.COUNT)); parent.Put(PdfName.COUNT, new PdfNumber(count.IntValue + 1)); - parent = (PdfDictionary)PdfReader.GetPdfObject(parent.Get(PdfName.PARENT)); + parent = parent.GetAsDict(PdfName.PARENT); } } @@ -754,26 +793,24 @@ namespace iTextSharp.text.pdf { partialFlattening[obf] = null; } } - PdfDictionary acroForm = (PdfDictionary)PdfReader.GetPdfObject(reader.Catalog.Get(PdfName.ACROFORM)); - ArrayList acroFds = null; + PdfDictionary acroForm = reader.Catalog.GetAsDict(PdfName.ACROFORM); + PdfArray acroFds = null; if (acroForm != null) { - PdfArray array = (PdfArray)PdfReader.GetPdfObject(acroForm.Get(PdfName.FIELDS), acroForm); - if (array != null) - acroFds = array.ArrayList; + acroFds = (PdfArray)PdfReader.GetPdfObject(acroForm.Get(PdfName.FIELDS), acroForm); } foreach (DictionaryEntry entry in fields) { String name = (String)entry.Key; if (partialFlattening.Count != 0 && !partialFlattening.ContainsKey(name)) continue; AcroFields.Item item = (AcroFields.Item)entry.Value; - for (int k = 0; k < item.merged.Count; ++k) { - PdfDictionary merged = (PdfDictionary)item.merged[k]; - PdfNumber ff = (PdfNumber)PdfReader.GetPdfObject(merged.Get(PdfName.F)); + for (int k = 0; k < item.Size; ++k) { + PdfDictionary merged = item.GetMerged(k); + PdfNumber ff = merged.GetAsNumber(PdfName.F); int flags = 0; if (ff != null) flags = ff.IntValue; - int page = (int)item.page[k]; - PdfDictionary appDic = (PdfDictionary)PdfReader.GetPdfObject(merged.Get(PdfName.AP)); + int page = item.GetPage(k); + PdfDictionary appDic = merged.GetAsDict(PdfName.AP); if (appDic != null && (flags & PdfFormField.FLAGS_PRINT) != 0 && (flags & PdfFormField.FLAGS_HIDDEN) == 0) { PdfObject obj = appDic.Get(PdfName.N); PdfAppearance app = null; @@ -787,7 +824,7 @@ namespace iTextSharp.text.pdf { } else { if (objReal != null && objReal.IsDictionary()) { - PdfName as_p = (PdfName)PdfReader.GetPdfObject(merged.Get(PdfName.AS)); + PdfName as_p = merged.GetAsName(PdfName.AS); if (as_p != null) { PdfIndirectReference iref = (PdfIndirectReference)((PdfDictionary)objReal).Get(as_p); if (iref != null) { @@ -802,7 +839,7 @@ namespace iTextSharp.text.pdf { } } if (app != null) { - Rectangle box = PdfReader.GetNormalizedRectangle((PdfArray)PdfReader.GetPdfObject(merged.Get(PdfName.RECT))); + Rectangle box = PdfReader.GetNormalizedRectangle(merged.GetAsArray(PdfName.RECT)); PdfContentByte cb = GetOverContent(page); cb.SetLiteral("Q "); cb.AddTemplate(app, box.Left, box.Bottom); @@ -812,51 +849,49 @@ namespace iTextSharp.text.pdf { if (partialFlattening.Count == 0) continue; PdfDictionary pageDic = reader.GetPageN(page); - PdfArray annots = (PdfArray)PdfReader.GetPdfObject(pageDic.Get(PdfName.ANNOTS)); + PdfArray annots = pageDic.GetAsArray(PdfName.ANNOTS); if (annots == null) continue; - ArrayList ar = annots.ArrayList; - for (int idx = 0; idx < ar.Count; ++idx) { - PdfObject ran = (PdfObject)ar[idx]; + for (int idx = 0; idx < annots.Size; ++idx) { + PdfObject ran = annots[idx]; if (!ran.IsIndirect()) continue; - PdfObject ran2 = (PdfObject)item.widget_refs[k]; + PdfObject ran2 = item.GetWidgetRef(k); if (!ran2.IsIndirect()) continue; if (((PRIndirectReference)ran).Number == ((PRIndirectReference)ran2).Number) { - ar.RemoveAt(idx--); + annots.Remove(idx--); PRIndirectReference wdref = (PRIndirectReference)ran2; while (true) { PdfDictionary wd = (PdfDictionary)PdfReader.GetPdfObject(wdref); PRIndirectReference parentRef = (PRIndirectReference)wd.Get(PdfName.PARENT); PdfReader.KillIndirect(wdref); if (parentRef == null) { // reached AcroForm - for (int fr = 0; fr < acroFds.Count; ++fr) { - PdfObject h = (PdfObject)acroFds[fr]; + for (int fr = 0; fr < acroFds.Size; ++fr) { + PdfObject h = acroFds[fr]; if (h.IsIndirect() && ((PRIndirectReference)h).Number == wdref.Number) { - acroFds.RemoveAt(fr); + acroFds.Remove(fr); --fr; } } break; } PdfDictionary parent = (PdfDictionary)PdfReader.GetPdfObject(parentRef); - PdfArray kids = (PdfArray)PdfReader.GetPdfObject(parent.Get(PdfName.KIDS)); - ArrayList kar = kids.ArrayList; - for (int fr = 0; fr < kar.Count; ++fr) { - PdfObject h = (PdfObject)kar[fr]; + PdfArray kids = parent.GetAsArray(PdfName.KIDS); + for (int fr = 0; fr < kids.Size; ++fr) { + PdfObject h = kids[fr]; if (h.IsIndirect() && ((PRIndirectReference)h).Number == wdref.Number) { - kar.RemoveAt(fr); + kids.Remove(fr); --fr; } } - if (kar.Count != 0) + if (!kids.IsEmpty()) break; wdref = parentRef; } } } - if (ar.Count == 0) { + if (annots.IsEmpty()) { PdfReader.KillIndirect(pageDic.Get(PdfName.ANNOTS)); pageDic.Remove(PdfName.ANNOTS); } @@ -865,20 +900,19 @@ namespace iTextSharp.text.pdf { if (!fieldsAdded && partialFlattening.Count == 0) { for (int page = 1; page <= reader.NumberOfPages; ++page) { PdfDictionary pageDic = reader.GetPageN(page); - PdfArray annots = (PdfArray)PdfReader.GetPdfObject(pageDic.Get(PdfName.ANNOTS)); + PdfArray annots = pageDic.GetAsArray(PdfName.ANNOTS); if (annots == null) continue; - ArrayList ar = annots.ArrayList; - for (int idx = 0; idx < ar.Count; ++idx) { - PdfObject annoto = PdfReader.GetPdfObject((PdfObject)ar[idx]); + for (int idx = 0; idx < annots.Size; ++idx) { + PdfObject annoto = annots.GetDirectObject(idx); if ((annoto is PdfIndirectReference) && !annoto.IsIndirect()) continue; if (!annoto.IsDictionary() || PdfName.WIDGET.Equals(((PdfDictionary)annoto).Get(PdfName.SUBTYPE))) { - ar.RemoveAt(idx); + annots.Remove(idx); --idx; } } - if (ar.Count == 0) { + if (annots.IsEmpty()) { PdfReader.KillIndirect(pageDic.Get(PdfName.ANNOTS)); pageDic.Remove(PdfName.ANNOTS); } @@ -914,9 +948,8 @@ namespace iTextSharp.text.pdf { PdfArray kids = (PdfArray)PdfReader.KillIndirect(dic.Get(PdfName.KIDS)); if (kids == null) return; - ArrayList ar = kids.ArrayList; - for (int k = 0; k < ar.Count; ++k) { - SweepKids((PdfObject)ar[k]); + for (int k = 0; k < kids.Size; ++k) { + SweepKids(kids[k]); } } @@ -926,19 +959,18 @@ namespace iTextSharp.text.pdf { for (int page = 1; page <= reader.NumberOfPages; ++page) { PdfDictionary pageDic = reader.GetPageN(page); - PdfArray annots = (PdfArray)PdfReader.GetPdfObject(pageDic.Get(PdfName.ANNOTS)); + PdfArray annots = pageDic.GetAsArray(PdfName.ANNOTS); if (annots == null) continue; - ArrayList ar = annots.ArrayList; - for (int idx = 0; idx < ar.Count; ++idx) { - PdfObject annoto = PdfReader.GetPdfObject((PdfObject)ar[idx]); + for (int idx = 0; idx < annots.Size; ++idx) { + PdfObject annoto = annots.GetDirectObject(idx); if ((annoto is PdfIndirectReference) && !annoto.IsIndirect()) continue; PdfDictionary annDic = (PdfDictionary)annoto; if (!((PdfName)annDic.Get(PdfName.SUBTYPE)).Equals(PdfName.FREETEXT)) continue; - PdfNumber ff = (PdfNumber)PdfReader.GetPdfObject(annDic.Get(PdfName.F)); + PdfNumber ff = annDic.GetAsNumber(PdfName.F); int flags = (ff != null) ? ff.IntValue : 0; if ( (flags & PdfFormField.FLAGS_PRINT) != 0 && (flags & PdfFormField.FLAGS_HIDDEN) == 0) { @@ -960,7 +992,7 @@ namespace iTextSharp.text.pdf { } else { if (objReal.IsDictionary()) { - PdfName as_p = (PdfName)PdfReader.GetPdfObject(appDic.Get(PdfName.AS)); + PdfName as_p = appDic.GetAsName(PdfName.AS); if (as_p != null) { PdfIndirectReference iref = (PdfIndirectReference)((PdfDictionary)objReal).Get(as_p); if (iref != null) { @@ -975,7 +1007,7 @@ namespace iTextSharp.text.pdf { } } if (app != null) { - Rectangle box = PdfReader.GetNormalizedRectangle((PdfArray)PdfReader.GetPdfObject(annDic.Get(PdfName.RECT))); + Rectangle box = PdfReader.GetNormalizedRectangle(annDic.GetAsArray(PdfName.RECT)); PdfContentByte cb = this.GetOverContent(page); cb.SetLiteral("Q "); cb.AddTemplate(app, box.Left, box.Bottom); @@ -983,17 +1015,16 @@ namespace iTextSharp.text.pdf { } } } - for (int idx = 0; idx < ar.Count; ++idx) { - PdfObject annoto = PdfReader.GetPdfObject((PdfObject)ar[idx]); - if (annoto != null && annoto.IsDictionary()) { - PdfDictionary annot = (PdfDictionary)annoto; + for (int idx = 0; idx < annots.Size; ++idx) { + PdfDictionary annot = annots.GetAsDict(idx); + if (annot != null) { if (PdfName.FREETEXT.Equals(annot.Get(PdfName.SUBTYPE))) { - ar.RemoveAt(idx); + annots.Remove(idx); --idx; } } } - if (ar.Count == 0) { + if (annots.IsEmpty()) { PdfReader.KillIndirect(pageDic.Get(PdfName.ANNOTS)); pageDic.Remove(PdfName.ANNOTS); } @@ -1059,9 +1090,9 @@ namespace iTextSharp.text.pdf { foreach (PdfTemplate template in fieldTemplates.Keys) { PdfFormField.MergeResources(dr, (PdfDictionary)template.Resources, this); } - if (dr.Get(PdfName.ENCODING) == null) - dr.Put(PdfName.ENCODING, PdfName.WIN_ANSI_ENCODING); - PdfDictionary fonts = (PdfDictionary)PdfReader.GetPdfObject(dr.Get(PdfName.FONT)); +// if (dr.Get(PdfName.ENCODING) == null) +// dr.Put(PdfName.ENCODING, PdfName.WIN_ANSI_ENCODING); + PdfDictionary fonts = dr.GetAsDict(PdfName.FONT); if (fonts == null) { fonts = new PdfDictionary(); dr.Put(PdfName.FONT, fonts); @@ -1177,6 +1208,7 @@ namespace iTextSharp.text.pdf { } internal override void AddAnnotation(PdfAnnotation annot, int page) { + annot.Page = page; AddAnnotation(annot, reader.GetPageN(page)); } @@ -1423,7 +1455,7 @@ namespace iTextSharp.text.pdf { actionType.Equals(DID_PRINT))) { throw new PdfException("Invalid additional action type: " + actionType.ToString()); } - PdfDictionary aa = (PdfDictionary)PdfReader.GetPdfObject(reader.Catalog.Get(PdfName.AA)); + PdfDictionary aa = reader.Catalog.GetAsDict(PdfName.AA); if (aa == null) { if (action == null) return; @@ -1511,6 +1543,8 @@ namespace iTextSharp.text.pdf { documentOCG[o] = null; OCGRadioGroup = d.GetAsArray(PdfName.RBGROUPS); OCGLocked = d.GetAsArray(PdfName.LOCKED); + if (OCGLocked == null) + OCGLocked = new PdfArray(); } /** @@ -1538,19 +1572,19 @@ namespace iTextSharp.text.pdf { } } else if (obj.IsArray()) { - ArrayList sub = ((PdfArray)obj).ArrayList; - if (sub.Count == 0) return; - obj = (PdfObject)sub[0]; + PdfArray sub = (PdfArray)obj; + if (sub.IsEmpty()) return; + obj = sub[0]; if (obj.IsString()) { - layer = new PdfLayer(sub[0].ToString()); + layer = new PdfLayer(obj.ToString()); layer.OnPanel = true; RegisterLayer(layer); if (parent != null) { parent.AddChild(layer); } PdfArray array = new PdfArray(); - foreach (PdfObject o2 in sub) { - array.Add(o2); + for (ListIterator j = sub.GetListIterator(); j.HasNext(); ) { + array.Add((PdfObject)j.Next()); } AddOrder(layer, array, ocgmap); } @@ -1605,7 +1639,7 @@ namespace iTextSharp.text.pdf { internal PageStamp(PdfStamperImp stamper, PdfReader reader, PdfDictionary pageN) { this.pageN = pageN; pageResources = new PageResources(); - PdfDictionary resources = (PdfDictionary)PdfReader.GetPdfObject(pageN.Get(PdfName.RESOURCES)); + PdfDictionary resources = pageN.GetAsDict(PdfName.RESOURCES); pageResources.SetOriginalResources(resources, stamper.namePtr); } }