Esempio n. 1
0
        public virtual void SetMoreInfo(String key, String value)
        {
            PdfName keyName = new PdfName(key);

            if (value == null)
            {
                infoDictionary.Remove(keyName);
                infoDictionary.SetModified();
            }
            else
            {
                Put(keyName, new PdfString(value, PdfEncodings.UNICODE_BIG));
            }
        }
Esempio n. 2
0
        /// <summary>remove this outline from the document.</summary>
        /// <exception cref="iText.Kernel.PdfException"/>
        internal virtual void RemoveOutline()
        {
            PdfName type = content.GetAsName(PdfName.Type);

            if (type != null && type.Equals(PdfName.Outlines))
            {
                pdfDoc.GetCatalog().Remove(PdfName.Outlines);
                return;
            }
            iText.Kernel.Pdf.PdfOutline         parent   = this.parent;
            IList <iText.Kernel.Pdf.PdfOutline> children = parent.children;

            children.Remove(this);
            PdfDictionary parentContent = parent.content;

            if (children.Count > 0)
            {
                parentContent.Put(PdfName.First, children[0].content);
                parentContent.Put(PdfName.Last, children[children.Count - 1].content);
            }
            else
            {
                parent.RemoveOutline();
                return;
            }
            PdfDictionary next = content.GetAsDictionary(PdfName.Next);
            PdfDictionary prev = content.GetAsDictionary(PdfName.Prev);

            if (prev != null)
            {
                if (next != null)
                {
                    prev.Put(PdfName.Next, next);
                    next.Put(PdfName.Prev, prev);
                }
                else
                {
                    prev.Remove(PdfName.Next);
                }
            }
            else
            {
                if (next != null)
                {
                    next.Remove(PdfName.Prev);
                }
            }
        }
Esempio n. 3
0
 /// <summary>Defines if the outline needs to be closed or not.</summary>
 /// <remarks>
 /// Defines if the outline needs to be closed or not.
 /// By default, outlines are open.
 /// </remarks>
 /// <param name="open">if false, the outline will be closed by default</param>
 public virtual void SetOpen(bool open)
 {
     if (!open)
     {
         content.Put(PdfName.Count, new PdfNumber(-1));
     }
     else
     {
         if (children.Count > 0)
         {
             content.Put(PdfName.Count, new PdfNumber(children.Count));
         }
         else
         {
             content.Remove(PdfName.Count);
         }
     }
 }
Esempio n. 4
0
        public virtual void TestCreateAndUpdateXMP()
        {
            String      created     = destinationFolder + "testCreateAndUpdateXMP_create.pdf";
            String      updated     = destinationFolder + "testCreateAndUpdateXMP_update.pdf";
            PdfDocument pdfDocument = new PdfDocument(new PdfWriter(created));

            pdfDocument.AddNewPage();
            pdfDocument.GetXmpMetadata(true);
            // create XMP metadata
            pdfDocument.Close();
            pdfDocument = new PdfDocument(new PdfReader(created), new PdfWriter(updated));
            PdfXrefTable  xref    = pdfDocument.GetXref();
            PdfDictionary catalog = pdfDocument.GetCatalog().GetPdfObject();

            ((PdfIndirectReference)catalog.Remove(PdfName.Metadata)).SetFree();
            PdfIndirectReference ref0    = xref.Get(0);
            PdfIndirectReference freeRef = xref.Get(6);

            pdfDocument.Close();

            /*
             * Current xref structure:
             * xref
             * 0 8
             * 0000000006 65535 f % this is object 0; 6 refers to free object 6
             * 0000000203 00000 n
             * 0000000510 00000 n
             * 0000000263 00000 n
             * 0000000088 00000 n
             * 0000000015 00000 n
             * 0000000000 00001 f % this is object 6; 0 refers to free object 0; note generation number
             * 0000000561 00000 n
             */
            NUnit.Framework.Assert.IsTrue(freeRef.IsFree());
            NUnit.Framework.Assert.AreEqual(ref0.offsetOrIndex, freeRef.objNr);
            NUnit.Framework.Assert.AreEqual(1, freeRef.genNr);
        }
Esempio n. 5
0
 private static void CopyDDictionary(ICollection<PdfIndirectReference> fromOcgsToCopy, PdfDictionary fromDDict
     , PdfDictionary toOcProperties, PdfDocument toDocument) {
     if (toOcProperties.GetAsDictionary(PdfName.D) == null) {
         toOcProperties.Put(PdfName.D, new PdfDictionary());
     }
     PdfDictionary toDDict = toOcProperties.GetAsDictionary(PdfName.D);
     // The Name field is not copied because it will be given when flushing the PdfOCProperties
     // Delete the Creator field because the D dictionary are changing
     toDDict.Remove(PdfName.Creator);
     // The BaseState field is not copied because for dictionary D BaseState should have the value ON, which is the default
     iText.Kernel.Pdf.OcgPropertiesCopier.CopyDArrayField(PdfName.ON, fromOcgsToCopy, fromDDict, toDDict, toDocument
         );
     iText.Kernel.Pdf.OcgPropertiesCopier.CopyDArrayField(PdfName.OFF, fromOcgsToCopy, fromDDict, toDDict, toDocument
         );
     // The Intent field is not copied because for dictionary D Intent should have the value View, which is the default
     // The AS field is not copied because it will be given when flushing the PdfOCProperties
     iText.Kernel.Pdf.OcgPropertiesCopier.CopyDArrayField(PdfName.Order, fromOcgsToCopy, fromDDict, toDDict, toDocument
         );
     // The ListModel field is not copied because it only affects the visual presentation of the layers
     iText.Kernel.Pdf.OcgPropertiesCopier.CopyDArrayField(PdfName.RBGroups, fromOcgsToCopy, fromDDict, toDDict, 
         toDocument);
     iText.Kernel.Pdf.OcgPropertiesCopier.CopyDArrayField(PdfName.Locked, fromOcgsToCopy, fromDDict, toDDict, toDocument
         );
 }
Esempio n. 6
0
        /// <summary>Writes cross reference table and trailer to PDF.</summary>
        /// <exception cref="System.IO.IOException"/>
        /// <exception cref="iText.Kernel.PdfException"/>
        protected internal virtual void WriteXrefTableAndTrailer(PdfDocument document, PdfObject fileId, PdfObject
                                                                 crypto)
        {
            PdfWriter writer = document.GetWriter();

            if (document.IsAppendMode())
            {
                // Increment generation number for all freed references.
                foreach (int?objNr in freeReferences)
                {
                    xref[(int)objNr].genNr++;
                }
            }
            else
            {
                foreach (int?objNr in freeReferences)
                {
                    xref[(int)objNr] = null;
                }
            }
            freeReferences.Clear();
            for (int i = count; i > 0; --i)
            {
                PdfIndirectReference lastRef = xref[i];
                if (lastRef == null || (lastRef.IsFree() && lastRef.GetGenNumber() == 0) || (!lastRef.CheckState(PdfObject
                                                                                                                 .FLUSHED) && !(document.properties.appendMode && !lastRef.CheckState(PdfObject.MODIFIED))))
                {
                    --count;
                }
                else
                {
                    break;
                }
            }
            IList <int> sections = new List <int>();
            int         first    = 0;
            int         len      = 1;

            if (document.IsAppendMode())
            {
                first = 1;
                len   = 0;
            }
            for (int i_1 = 1; i_1 < Size(); i_1++)
            {
                PdfIndirectReference reference = xref[i_1];
                if (reference != null)
                {
                    if ((document.properties.appendMode && !reference.CheckState(PdfObject.MODIFIED)) || (reference.IsFree() &&
                                                                                                          reference.GetGenNumber() == 0) || (!reference.CheckState(PdfObject.FLUSHED)))
                    {
                        reference = null;
                    }
                }
                if (reference == null)
                {
                    if (len > 0)
                    {
                        sections.Add(first);
                        sections.Add(len);
                    }
                    len = 0;
                }
                else
                {
                    if (len > 0)
                    {
                        len++;
                    }
                    else
                    {
                        first = i_1;
                        len   = 1;
                    }
                }
            }
            if (len > 0)
            {
                sections.Add(first);
                sections.Add(len);
            }
            if (document.properties.appendMode && sections.Count == 0)
            {
                // no modifications.
                xref = null;
                return;
            }
            long startxref = writer.GetCurrentPos();

            if (writer.IsFullCompression())
            {
                PdfStream xrefStream = ((PdfStream) new PdfStream().MakeIndirect(document));
                xrefStream.MakeIndirect(document);
                xrefStream.Put(PdfName.Type, PdfName.XRef);
                xrefStream.Put(PdfName.ID, fileId);
                if (crypto != null)
                {
                    xrefStream.Put(PdfName.Encrypt, crypto);
                }
                xrefStream.Put(PdfName.Size, new PdfNumber(this.Size()));
                List <PdfObject> tmpArray = new List <PdfObject>(3);
                tmpArray.Add(new PdfNumber(1));
                tmpArray.Add(new PdfNumber(4));
                tmpArray.Add(new PdfNumber(2));
                xrefStream.Put(PdfName.W, new PdfArray(tmpArray));
                xrefStream.Put(PdfName.Info, document.GetDocumentInfo().GetPdfObject());
                xrefStream.Put(PdfName.Root, document.GetCatalog().GetPdfObject());
                PdfArray index = new PdfArray();
                foreach (int?section in sections)
                {
                    index.Add(new PdfNumber((int)section));
                }
                if (document.properties.appendMode)
                {
                    PdfNumber lastXref = new PdfNumber(document.reader.GetLastXref());
                    xrefStream.Put(PdfName.Prev, lastXref);
                }
                xrefStream.Put(PdfName.Index, index);
                iText.Kernel.Pdf.PdfXrefTable xrefTable = document.GetXref();
                for (int k = 0; k < sections.Count; k += 2)
                {
                    first = (int)sections[k];
                    len   = (int)sections[k + 1];
                    for (int i_2 = first; i_2 < first + len; i_2++)
                    {
                        PdfIndirectReference reference = xrefTable.Get(i_2);
                        if (reference == null)
                        {
                            continue;
                        }
                        if (reference.IsFree())
                        {
                            xrefStream.GetOutputStream().Write(0);
                            //NOTE The object number of the next free object should be at this position due to spec.
                            xrefStream.GetOutputStream().Write(IntToBytes(0));
                            xrefStream.GetOutputStream().Write(ShortToBytes(reference.GetGenNumber()));
                        }
                        else
                        {
                            if (reference.GetObjStreamNumber() == 0)
                            {
                                xrefStream.GetOutputStream().Write(1);
                                System.Diagnostics.Debug.Assert(reference.GetOffset() < int.MaxValue);
                                xrefStream.GetOutputStream().Write(IntToBytes((int)reference.GetOffset()));
                                xrefStream.GetOutputStream().Write(ShortToBytes(reference.GetGenNumber()));
                            }
                            else
                            {
                                xrefStream.GetOutputStream().Write(2);
                                xrefStream.GetOutputStream().Write(IntToBytes(reference.GetObjStreamNumber()));
                                xrefStream.GetOutputStream().Write(ShortToBytes(reference.GetIndex()));
                            }
                        }
                    }
                }
                xrefStream.Flush();
            }
            else
            {
                writer.WriteString("xref\n");
                iText.Kernel.Pdf.PdfXrefTable xrefTable = document.GetXref();
                for (int k = 0; k < sections.Count; k += 2)
                {
                    first = (int)sections[k];
                    len   = (int)sections[k + 1];
                    writer.WriteInteger(first).WriteSpace().WriteInteger(len).WriteByte((byte)'\n');
                    for (int i_2 = first; i_2 < first + len; i_2++)
                    {
                        PdfIndirectReference reference = xrefTable.Get(i_2);
                        writer.WriteString(DecimalFormatUtil.FormatNumber(reference.GetOffset(), objectOffsetFormatter)).WriteSpace
                            ().WriteString(DecimalFormatUtil.FormatNumber(reference.GetGenNumber(), objectGenerationFormatter)).WriteSpace
                            ();
                        if (reference.IsFree())
                        {
                            writer.WriteBytes(freeXRefEntry);
                        }
                        else
                        {
                            writer.WriteBytes(inUseXRefEntry);
                        }
                    }
                }
                PdfDictionary trailer = document.GetTrailer();
                // Remove all unused keys in case stamp mode in case original file has full compression, but destination file has not.
                trailer.Remove(PdfName.W);
                trailer.Remove(PdfName.Index);
                trailer.Remove(PdfName.Type);
                trailer.Remove(PdfName.Length);
                trailer.Put(PdfName.Size, new PdfNumber(this.Size()));
                trailer.Put(PdfName.ID, fileId);
                if (crypto != null)
                {
                    trailer.Put(PdfName.Encrypt, crypto);
                }
                writer.WriteString("trailer\n");
                if (document.properties.appendMode)
                {
                    PdfNumber lastXref = new PdfNumber(document.reader.GetLastXref());
                    trailer.Put(PdfName.Prev, lastXref);
                }
                writer.Write(document.GetTrailer());
                writer.Write('\n');
            }
            WriteKeyInfo(writer);
            writer.WriteString("startxref\n").WriteLong(startxref).WriteString("\n%%EOF\n");
            xref = null;
        }
Esempio n. 7
0
        /// <summary>Writes cross reference table and trailer to PDF.</summary>
        protected internal virtual void WriteXrefTableAndTrailer(PdfDocument document, PdfObject fileId, PdfObject
                                                                 crypto)
        {
            PdfWriter writer = document.GetWriter();

            if (!document.properties.appendMode)
            {
                for (int i = count; i > 0; --i)
                {
                    PdfIndirectReference lastRef = xref[i];
                    if (lastRef == null || lastRef.IsFree())
                    {
                        RemoveFreeRefFromList(i);
                        --count;
                    }
                    else
                    {
                        break;
                    }
                }
            }
            IList <int> sections = CreateSections(document, false);

            if (document.properties.appendMode && sections.Count == 0)
            {
                // no modifications.
                xref = null;
                return;
            }
            long startxref  = writer.GetCurrentPos();
            long xRefStmPos = -1;

            if (writer.IsFullCompression())
            {
                PdfStream xrefStream = (PdfStream) new PdfStream().MakeIndirect(document);
                xrefStream.MakeIndirect(document);
                xrefStream.Put(PdfName.Type, PdfName.XRef);
                xrefStream.Put(PdfName.ID, fileId);
                if (crypto != null)
                {
                    xrefStream.Put(PdfName.Encrypt, crypto);
                }
                xrefStream.Put(PdfName.Size, new PdfNumber(this.Size()));
                int offsetSize = GetOffsetSize(Math.Max(startxref, Size()));
                xrefStream.Put(PdfName.W, new PdfArray(JavaUtil.ArraysAsList((PdfObject) new PdfNumber(1), new PdfNumber(offsetSize
                                                                                                                         ), new PdfNumber(2))));
                xrefStream.Put(PdfName.Info, document.GetDocumentInfo().GetPdfObject());
                xrefStream.Put(PdfName.Root, document.GetCatalog().GetPdfObject());
                PdfArray index = new PdfArray();
                foreach (int?section in sections)
                {
                    index.Add(new PdfNumber((int)section));
                }
                if (document.properties.appendMode && !document.reader.hybridXref)
                {
                    // "not meaningful in hybrid-reference files"
                    PdfNumber lastXref = new PdfNumber(document.reader.GetLastXref());
                    xrefStream.Put(PdfName.Prev, lastXref);
                }
                xrefStream.Put(PdfName.Index, index);
                iText.Kernel.Pdf.PdfXrefTable xrefTable = document.GetXref();
                for (int k = 0; k < sections.Count; k += 2)
                {
                    int first = (int)sections[k];
                    int len   = (int)sections[k + 1];
                    for (int i = first; i < first + len; i++)
                    {
                        PdfIndirectReference reference = xrefTable.Get(i);
                        if (reference.IsFree())
                        {
                            xrefStream.GetOutputStream().Write(0);
                            xrefStream.GetOutputStream().Write(reference.GetOffset(), offsetSize);
                            xrefStream.GetOutputStream().Write(reference.GetGenNumber(), 2);
                        }
                        else
                        {
                            if (reference.GetObjStreamNumber() == 0)
                            {
                                xrefStream.GetOutputStream().Write(1);
                                xrefStream.GetOutputStream().Write(reference.GetOffset(), offsetSize);
                                xrefStream.GetOutputStream().Write(reference.GetGenNumber(), 2);
                            }
                            else
                            {
                                xrefStream.GetOutputStream().Write(2);
                                xrefStream.GetOutputStream().Write(reference.GetObjStreamNumber(), offsetSize);
                                xrefStream.GetOutputStream().Write(reference.GetIndex(), 2);
                            }
                        }
                    }
                }
                xrefStream.Flush();
                xRefStmPos = startxref;
            }
            // For documents with hybrid cross-reference table, i.e. containing xref streams as well as regular xref sections,
            // we write additional regular xref section at the end of the document because the /Prev reference from
            // xref stream to a regular xref section doesn't seem to be valid
            bool needsRegularXref = !writer.IsFullCompression() || document.properties.appendMode && document.reader.hybridXref;

            if (needsRegularXref)
            {
                startxref = writer.GetCurrentPos();
                writer.WriteString("xref\n");
                iText.Kernel.Pdf.PdfXrefTable xrefTable = document.GetXref();
                if (xRefStmPos != -1)
                {
                    // Get rid of all objects from object stream. This is done for hybrid documents
                    sections = CreateSections(document, true);
                }
                for (int k = 0; k < sections.Count; k += 2)
                {
                    int first = (int)sections[k];
                    int len   = (int)sections[k + 1];
                    writer.WriteInteger(first).WriteSpace().WriteInteger(len).WriteByte((byte)'\n');
                    for (int i = first; i < first + len; i++)
                    {
                        PdfIndirectReference reference = xrefTable.Get(i);
                        StringBuilder        off       = new StringBuilder("0000000000").Append(reference.GetOffset());
                        StringBuilder        gen       = new StringBuilder("00000").Append(reference.GetGenNumber());
                        writer.WriteString(off.JSubstring(off.Length - 10, off.Length)).WriteSpace().WriteString(gen.JSubstring(gen
                                                                                                                                .Length - 5, gen.Length)).WriteSpace();
                        if (reference.IsFree())
                        {
                            writer.WriteBytes(freeXRefEntry);
                        }
                        else
                        {
                            writer.WriteBytes(inUseXRefEntry);
                        }
                    }
                }
                PdfDictionary trailer = document.GetTrailer();
                // Remove all unused keys in case stamp mode in case original file has full compression, but destination file has not.
                trailer.Remove(PdfName.W);
                trailer.Remove(PdfName.Index);
                trailer.Remove(PdfName.Type);
                trailer.Remove(PdfName.Length);
                trailer.Put(PdfName.Size, new PdfNumber(this.Size()));
                trailer.Put(PdfName.ID, fileId);
                if (xRefStmPos != -1)
                {
                    trailer.Put(PdfName.XRefStm, new PdfNumber(xRefStmPos));
                }
                if (crypto != null)
                {
                    trailer.Put(PdfName.Encrypt, crypto);
                }
                writer.WriteString("trailer\n");
                if (document.properties.appendMode)
                {
                    PdfNumber lastXref = new PdfNumber(document.reader.GetLastXref());
                    trailer.Put(PdfName.Prev, lastXref);
                }
                writer.Write(document.GetTrailer());
                writer.Write('\n');
            }
            WriteKeyInfo(document);
            writer.WriteString("startxref\n").WriteLong(startxref).WriteString("\n%%EOF\n");
            xref = null;
            freeReferencesLinkedList.Clear();
        }
Esempio n. 8
0
        /// <summary>Writes cross reference table and trailer to PDF.</summary>
        /// <exception cref="System.IO.IOException"/>
        protected internal virtual void WriteXrefTableAndTrailer(PdfDocument document, PdfObject fileId, PdfObject
                                                                 crypto)
        {
            PdfWriter writer = document.GetWriter();

            if (!document.properties.appendMode)
            {
                for (int i = count; i > 0; --i)
                {
                    PdfIndirectReference lastRef = xref[i];
                    if (lastRef == null || lastRef.IsFree())
                    {
                        RemoveFreeRefFromList(i);
                        --count;
                    }
                    else
                    {
                        break;
                    }
                }
            }
            IList <int> sections = new List <int>();
            int         first    = 0;
            int         len      = 0;

            for (int i = 0; i < Size(); i++)
            {
                PdfIndirectReference reference = xref[i];
                if (document.properties.appendMode && reference != null && !reference.CheckState(PdfObject.MODIFIED))
                {
                    reference = null;
                }
                if (reference == null)
                {
                    if (len > 0)
                    {
                        sections.Add(first);
                        sections.Add(len);
                    }
                    len = 0;
                }
                else
                {
                    if (len > 0)
                    {
                        len++;
                    }
                    else
                    {
                        first = i;
                        len   = 1;
                    }
                }
            }
            if (len > 0)
            {
                sections.Add(first);
                sections.Add(len);
            }
            if (document.properties.appendMode && sections.Count == 0)
            {
                // no modifications.
                xref = null;
                return;
            }
            long startxref = writer.GetCurrentPos();

            if (writer.IsFullCompression())
            {
                PdfStream xrefStream = (PdfStream) new PdfStream().MakeIndirect(document);
                xrefStream.MakeIndirect(document);
                xrefStream.Put(PdfName.Type, PdfName.XRef);
                xrefStream.Put(PdfName.ID, fileId);
                if (crypto != null)
                {
                    xrefStream.Put(PdfName.Encrypt, crypto);
                }
                xrefStream.Put(PdfName.Size, new PdfNumber(this.Size()));
                List <PdfObject> tmpArray = new List <PdfObject>(3);
                tmpArray.Add(new PdfNumber(1));
                tmpArray.Add(new PdfNumber(4));
                tmpArray.Add(new PdfNumber(2));
                xrefStream.Put(PdfName.W, new PdfArray(tmpArray));
                xrefStream.Put(PdfName.Info, document.GetDocumentInfo().GetPdfObject());
                xrefStream.Put(PdfName.Root, document.GetCatalog().GetPdfObject());
                PdfArray index = new PdfArray();
                foreach (int?section in sections)
                {
                    index.Add(new PdfNumber((int)section));
                }
                if (document.properties.appendMode)
                {
                    PdfNumber lastXref = new PdfNumber(document.reader.GetLastXref());
                    xrefStream.Put(PdfName.Prev, lastXref);
                }
                xrefStream.Put(PdfName.Index, index);
                iText.Kernel.Pdf.PdfXrefTable xrefTable = document.GetXref();
                for (int k = 0; k < sections.Count; k += 2)
                {
                    first = (int)sections[k];
                    len   = (int)sections[k + 1];
                    for (int i = first; i < first + len; i++)
                    {
                        PdfIndirectReference reference = xrefTable.Get(i);
                        if (reference.IsFree())
                        {
                            xrefStream.GetOutputStream().Write(0);
                            System.Diagnostics.Debug.Assert(reference.GetOffset() < int.MaxValue);
                            xrefStream.GetOutputStream().Write(IntToBytes((int)reference.GetOffset()));
                            xrefStream.GetOutputStream().Write(ShortToBytes(reference.GetGenNumber()));
                        }
                        else
                        {
                            if (reference.GetObjStreamNumber() == 0)
                            {
                                xrefStream.GetOutputStream().Write(1);
                                System.Diagnostics.Debug.Assert(reference.GetOffset() < int.MaxValue);
                                xrefStream.GetOutputStream().Write(IntToBytes((int)reference.GetOffset()));
                                xrefStream.GetOutputStream().Write(ShortToBytes(reference.GetGenNumber()));
                            }
                            else
                            {
                                xrefStream.GetOutputStream().Write(2);
                                xrefStream.GetOutputStream().Write(IntToBytes(reference.GetObjStreamNumber()));
                                xrefStream.GetOutputStream().Write(ShortToBytes(reference.GetIndex()));
                            }
                        }
                    }
                }
                xrefStream.Flush();
            }
            else
            {
                writer.WriteString("xref\n");
                iText.Kernel.Pdf.PdfXrefTable xrefTable = document.GetXref();
                for (int k = 0; k < sections.Count; k += 2)
                {
                    first = (int)sections[k];
                    len   = (int)sections[k + 1];
                    writer.WriteInteger(first).WriteSpace().WriteInteger(len).WriteByte((byte)'\n');
                    for (int i = first; i < first + len; i++)
                    {
                        PdfIndirectReference reference = xrefTable.Get(i);
                        StringBuilder        off       = new StringBuilder("0000000000").Append(reference.GetOffset());
                        StringBuilder        gen       = new StringBuilder("00000").Append(reference.GetGenNumber());
                        writer.WriteString(off.JSubstring(off.Length - 10, off.Length)).WriteSpace().WriteString(gen.JSubstring(gen
                                                                                                                                .Length - 5, gen.Length)).WriteSpace();
                        if (reference.IsFree())
                        {
                            writer.WriteBytes(freeXRefEntry);
                        }
                        else
                        {
                            writer.WriteBytes(inUseXRefEntry);
                        }
                    }
                }
                PdfDictionary trailer = document.GetTrailer();
                // Remove all unused keys in case stamp mode in case original file has full compression, but destination file has not.
                trailer.Remove(PdfName.W);
                trailer.Remove(PdfName.Index);
                trailer.Remove(PdfName.Type);
                trailer.Remove(PdfName.Length);
                trailer.Put(PdfName.Size, new PdfNumber(this.Size()));
                trailer.Put(PdfName.ID, fileId);
                if (crypto != null)
                {
                    trailer.Put(PdfName.Encrypt, crypto);
                }
                writer.WriteString("trailer\n");
                if (document.properties.appendMode)
                {
                    PdfNumber lastXref = new PdfNumber(document.reader.GetLastXref());
                    trailer.Put(PdfName.Prev, lastXref);
                }
                writer.Write(document.GetTrailer());
                writer.Write('\n');
            }
            WriteKeyInfo(document);
            writer.WriteString("startxref\n").WriteLong(startxref).WriteString("\n%%EOF\n");
            xref = null;
            freeReferencesLinkedList.Clear();
        }
Esempio n. 9
0
 private static void CopyDArrayField(PdfName fieldToCopy, ICollection<PdfIndirectReference> fromOcgsToCopy, 
     PdfDictionary fromDict, PdfDictionary toDict, PdfDocument toDocument) {
     if (fromDict.GetAsArray(fieldToCopy) == null) {
         return;
     }
     PdfArray fromArray = fromDict.GetAsArray(fieldToCopy);
     if (toDict.GetAsArray(fieldToCopy) == null) {
         toDict.Put(fieldToCopy, new PdfArray());
     }
     PdfArray toArray = toDict.GetAsArray(fieldToCopy);
     ICollection<PdfIndirectReference> toOcgsToCopy = new HashSet<PdfIndirectReference>();
     foreach (PdfIndirectReference fromRef in fromOcgsToCopy) {
         toOcgsToCopy.Add(fromRef.GetRefersTo().CopyTo(toDocument, false).GetIndirectReference());
     }
     if (PdfName.Order.Equals(fieldToCopy)) {
         // Stage 1: delete all Order the entire branches from the output document in which the copied OCGs were
         IList<int> removeIndex = new List<int>();
         for (int i = 0; i < toArray.Size(); i++) {
             PdfObject toOrderItem = toArray.Get(i);
             if (iText.Kernel.Pdf.OcgPropertiesCopier.OrderBranchContainsSetElements(toOrderItem, toArray, i, toOcgsToCopy
                 , null, null)) {
                 removeIndex.Add(i);
             }
         }
         for (int i = removeIndex.Count - 1; i > -1; i--) {
             toArray.Remove(removeIndex[i]);
         }
         PdfArray toOcgs = toDocument.GetCatalog().GetPdfObject().GetAsDictionary(PdfName.OCProperties).GetAsArray(
             PdfName.OCGs);
         // Stage 2: copy all the Order the entire branches in which the copied OСGs were
         for (int i = 0; i < fromArray.Size(); i++) {
             PdfObject fromOrderItem = fromArray.Get(i);
             if (iText.Kernel.Pdf.OcgPropertiesCopier.OrderBranchContainsSetElements(fromOrderItem, fromArray, i, fromOcgsToCopy
                 , toOcgs, toDocument)) {
                 toArray.Add(fromOrderItem.CopyTo(toDocument, false));
             }
         }
     }
     else {
         // Stage 3: remove from Order OCGs not presented in the output document. When forming
         // the Order dictionary in the PdfOcProperties constructor, only those OCGs that are
         // in the OCProperties/OCGs array will be taken into account
         if (PdfName.RBGroups.Equals(fieldToCopy)) {
             // Stage 1: delete all RBGroups from the output document in which the copied OCGs were
             for (int i = toArray.Size() - 1; i > -1; i--) {
                 PdfArray toRbGroup = (PdfArray)toArray.Get(i);
                 foreach (PdfObject toRbGroupItemObj in toRbGroup) {
                     if (toOcgsToCopy.Contains(toRbGroupItemObj.GetIndirectReference())) {
                         toArray.Remove(i);
                         break;
                     }
                 }
             }
             // Stage 2: copy all the RBGroups in which the copied OCGs were
             foreach (PdfObject fromRbGroupObj in fromArray) {
                 PdfArray fromRbGroup = (PdfArray)fromRbGroupObj;
                 foreach (PdfObject fromRbGroupItemObj in fromRbGroup) {
                     if (fromOcgsToCopy.Contains(fromRbGroupItemObj.GetIndirectReference())) {
                         toArray.Add(fromRbGroup.CopyTo(toDocument, false));
                         break;
                     }
                 }
             }
         }
         else {
             // Stage 3: remove from RBGroups OCGs not presented in the output
             // document (is in the PdfOcProperties#fillDictionary method)
             foreach (PdfObject fromObj in fromArray) {
                 iText.Kernel.Pdf.OcgPropertiesCopier.AttemptToAddObjectToArray(fromOcgsToCopy, fromObj, toArray, toDocument
                     );
             }
         }
     }
     if (toArray.IsEmpty()) {
         toDict.Remove(fieldToCopy);
     }
 }