public void CopyStructTreeForPage(PdfNumber sourceArrayNumber, int newArrayNumber) { if (!openedDocuments[fileName]) { PdfObject res = writer.CopyObject(writer.CopyObject(structTreeRoot.Get(PdfName.K), true, true)); if (!(res is PdfIndirectReference)) { res = writer.AddToBody(res).IndirectReference; } structureTreeRoot.AddPageMark(newArrayNumber, (PdfIndirectReference)res); AddKid(structureTreeRoot, res); openedDocuments.Add(fileName, true); } if (CopyPageMarks(parentTree, sourceArrayNumber, newArrayNumber) == returnType.NOTFOUND) { throw new BadPdfFormatException(MessageLocalization.GetComposedMessage("structparent.not.found")); } }
private returnType FindAndCopyMarks(PdfArray pages, int arrayNumber, int newArrayNumber) { if (pages.GetAsNumber(0).IntValue > arrayNumber) { return(returnType.BELOW); } if (pages.GetAsNumber(pages.Size - 2).IntValue < arrayNumber) { return(returnType.ABOVE); } int cur = pages.Size / 4; int begin = 0; int curNumber; while (true) { curNumber = pages.GetAsNumber((begin + cur) * 2).IntValue; if (curNumber == arrayNumber) { PdfObject res = writer.CopyObject(pages[((begin + cur) * 2 + 1)], true, false); if (!(res is PdfIndirectReference)) { res = writer.AddToBody(res).IndirectReference; } structureTreeRoot.AddPageMark(newArrayNumber, (PdfIndirectReference)res); AddKid(structureTreeRoot, res); return(returnType.FOUND); } if (curNumber < arrayNumber) { begin += cur; cur /= 2; if (cur == 0) { cur = 1; } if (cur + begin == pages.Size) { return(returnType.NOTFOUND); } continue; } if (cur + begin == 0) { return(returnType.BELOW); } if (cur == 0) { return(returnType.NOTFOUND); } cur /= 2; } }
private ReturnType FindAndCopyMarks(PdfArray pages, int arrayNumber, int newArrayNumber) { if (pages.GetAsNumber(0).IntValue > arrayNumber) { return(ReturnType.BELOW); } if (pages.GetAsNumber(pages.Size - 2).IntValue < arrayNumber) { return(ReturnType.ABOVE); } int cur = pages.Size / 4; int begin = 0; int curNumber; while (true) { curNumber = pages.GetAsNumber((begin + cur) * 2).IntValue; if (curNumber == arrayNumber) { PdfObject obj = pages[(begin + cur) * 2 + 1]; PdfObject obj1 = obj; while (obj.IsIndirect()) { obj = PdfReader.GetPdfObjectRelease(obj); } //invalid Nums if (obj.IsArray()) { PdfObject firstNotNullKid = null; foreach (PdfObject numObj in (PdfArray)obj) { if (numObj.IsNull()) { if (nullReference == null) { nullReference = writer.AddToBody(new PdfNull()).IndirectReference; } structureTreeRoot.SetPageMark(newArrayNumber, nullReference); } else { PdfObject res = writer.CopyObject(numObj, true, false); if (firstNotNullKid == null) { firstNotNullKid = res; } structureTreeRoot.SetPageMark(newArrayNumber, (PdfIndirectReference)res); } } AttachStructTreeRootKids(firstNotNullKid); } else if (obj.IsDictionary()) { PdfDictionary k = GetKDict((PdfDictionary)obj); if (k == null) { return(ReturnType.NOTFOUND); } PdfObject res = writer.CopyObject(obj1, true, false); structureTreeRoot.SetAnnotationMark(newArrayNumber, (PdfIndirectReference)res); } else { return(ReturnType.NOTFOUND); } return(ReturnType.FOUND); } if (curNumber < arrayNumber) { begin += cur; cur /= 2; if (cur == 0) { cur = 1; } if (cur + begin == pages.Size) { return(ReturnType.NOTFOUND); } continue; } if (cur + begin == 0) { return(ReturnType.BELOW); } if (cur == 0) { return(ReturnType.NOTFOUND); } cur /= 2; } }
public void AddAnnotation(PdfAnnotation annot) { var allAnnots = new ArrayList(); if (annot.IsForm()) { var field = (PdfFormField)annot; if (field.Parent != null) { return; } expandFields(field, allAnnots); if (_cstp.FieldTemplates == null) { _cstp.FieldTemplates = new Hashtable(); } } else { allAnnots.Add(annot); } for (var k = 0; k < allAnnots.Count; ++k) { annot = (PdfAnnotation)allAnnots[k]; if (annot.IsForm()) { if (!annot.IsUsed()) { var templates = annot.Templates; if (templates != null) { foreach (var tpl in templates.Keys) { _cstp.FieldTemplates[tpl] = null; } } } var field = (PdfFormField)annot; if (field.Parent == null) { addDocumentField(field.IndirectReference); } } if (annot.IsAnnotation()) { var pdfobj = PdfReader.GetPdfObject(_pageN.Get(PdfName.Annots), _pageN); PdfArray annots = null; if (pdfobj == null || !pdfobj.IsArray()) { annots = new PdfArray(); _pageN.Put(PdfName.Annots, annots); } else { annots = (PdfArray)pdfobj; } annots.Add(annot.IndirectReference); if (!annot.IsUsed()) { var rect = (PdfRectangle)annot.Get(PdfName.Rect); if (rect != null && (rect.Left.ApproxNotEqual(0) || rect.Right.ApproxNotEqual(0) || rect.Top.ApproxNotEqual(0) || rect.Bottom.ApproxNotEqual(0))) { var rotation = _reader.GetPageRotation(_pageN); var pageSize = _reader.GetPageSizeWithRotation(_pageN); switch (rotation) { case 90: annot.Put(PdfName.Rect, new PdfRectangle( pageSize.Top - rect.Bottom, rect.Left, pageSize.Top - rect.Top, rect.Right)); break; case 180: annot.Put(PdfName.Rect, new PdfRectangle( pageSize.Right - rect.Left, pageSize.Top - rect.Bottom, pageSize.Right - rect.Right, pageSize.Top - rect.Top)); break; case 270: annot.Put(PdfName.Rect, new PdfRectangle( rect.Bottom, pageSize.Right - rect.Left, rect.Top, pageSize.Right - rect.Right)); break; } } } } if (!annot.IsUsed()) { annot.SetUsed(); _cstp.AddToBody(annot, annot.IndirectReference); } } }
public void AlterContents() { if (over == null && under == null) { return; } PdfArray ar = null; PdfObject content = PdfReader.GetPdfObject(pageN.Get(PdfName.CONTENTS), pageN); if (content == null) { ar = new PdfArray(); pageN.Put(PdfName.CONTENTS, ar); } else if (content.IsArray()) { ar = (PdfArray)content; } else if (content.IsStream()) { ar = new PdfArray(); ar.Add(pageN.Get(PdfName.CONTENTS)); pageN.Put(PdfName.CONTENTS, ar); } else { ar = new PdfArray(); pageN.Put(PdfName.CONTENTS, ar); } ByteBuffer out_p = new ByteBuffer(); if (under != null) { out_p.Append(PdfContents.SAVESTATE); ApplyRotation(pageN, out_p); out_p.Append(under.InternalBuffer); out_p.Append(PdfContents.RESTORESTATE); } if (over != null) { out_p.Append(PdfContents.SAVESTATE); } PdfStream stream = new PdfStream(out_p.ToByteArray()); stream.FlateCompress(cstp.CompressionLevel); PdfIndirectReference ref1 = cstp.AddToBody(stream).IndirectReference; ar.AddFirst(ref1); out_p.Reset(); if (over != null) { out_p.Append(' '); out_p.Append(PdfContents.RESTORESTATE); out_p.Append(PdfContents.SAVESTATE); ApplyRotation(pageN, out_p); out_p.Append(over.InternalBuffer); out_p.Append(PdfContents.RESTORESTATE); stream = new PdfStream(out_p.ToByteArray()); stream.FlateCompress(cstp.CompressionLevel); ar.Add(cstp.AddToBody(stream).IndirectReference); } pageN.Put(PdfName.RESOURCES, pageResources.Resources); }