public void Purge() { _doc.PageNumber = 1; foreach (int id in _containers) { IndirectObject io = _doc.ObjectSoup[id]; while (io != null) { Atom rez = io.Resolve(Atom.GetItem(io.Atom, "Resources")); DictAtom fonts = io.Resolve(Atom.GetItem(rez, "Font")) as DictAtom; if ((fonts != null) && (fonts.Count > 0)) { List <string> fontsToRemove = new List <string>(); foreach (KeyValuePair <string, Atom> pair in fonts) { IndirectObject font = io.ResolveObj(pair.Value); if (font == null) { continue; // shouldn't ever happen } if (!_fonts.Contains(font.ID)) { fontsToRemove.Add(pair.Key); } } foreach (string key in fontsToRemove) { fonts.Remove(key); } } io = io.ResolveObj(Atom.GetItem(io.Atom, "Parent")); } } }
public IndirectObject <PdfInteger> SetData(string data) { DefilteredLength = data.Length; Value = FlateDecode.DeflateString(data); Length = new IndirectObject <PdfInteger>(new PdfInteger(Buffer.Length), 0, 0); return(Length); }
public IndirectObject <PdfInteger> SetData(byte[] data) { DefilteredLength = data.Length; Value = data.DeflateString(); Length = new IndirectObject <PdfInteger>(new PdfInteger(Buffer.Length), 0, 0); return(Length); }
public override PdfObject Swap( PdfObject other ) { /* * NOTE: Fail fast if the referenced indirect object is undefined. */ return(IndirectObject.Swap(((PdfReference)other).IndirectObject).Reference); }
public void ReadIndirectObjects() { // 7.3.10 Indirect Objects string fragment = "12 0 obj true endobj"; byte[] bytes = System.Text.UTF8Encoding.UTF8.GetBytes(fragment); Tokenizer feed = new Tokenizer(new MemoryStream(bytes)); Objectizer objectizer = new Objectizer(feed); IndirectObject actual = (IndirectObject)objectizer.NextObject(); Assert.Equal(12, actual.Number); Assert.Equal(0, actual.Generation); var childs = actual.Childs <BooleanObject>(); Assert.Single(childs); Assert.True(childs[0].Value); }
private bool InsertObject() { //if (Object.InInventory) //{ // Print(String.Format("You need to be holding the {0} before you can put it into something else.", Object.Name)); // return true; //} var beforeReceive = IndirectObject.Before <Receive>(); if (beforeReceive != null) { return(beforeReceive()); } var c = IndirectObject as Container; if (c == null) { Print("That can't contain things."); return(true); } if (!c.IsOpen) { Print(String.Format("The {0} is closed.", c.Name)); return(true); } Inventory.Remove(Object); c.Add(Object); var afterReceive = IndirectObject.After <Receive>(); if (afterReceive != null) { return(afterReceive()); } Print(String.Format("You put the {0} into the {1}.", Object.Name, IndirectObject.Name)); return(true); }
public void WritesIndirectObject() { // Arrange var indirectObj = new IndirectObject <NullObject>(new PdfObjectId(10, 2), new NullObject()); string result; // Act using (var pdfStream = new MemoryStream()) { using (var writer = new PdfWriter(pdfStream)) { indirectObj.WriteObject(writer); writer.Flush(); result = ReadAsString(pdfStream); } } // Assert Assert.Equal("10 2 obj\r\nnull\r\nendobj\r\n", result); }
/// <summary> /// String representation of the PDF document /// </summary> /// <returns></returns> public override string ToString() { StringBuilder sb = new StringBuilder(); sb.Append(Header); IEnumerator enu = _indirectObjects.GetEnumerator(); while (enu.MoveNext()) { IndirectObject iobj = (IndirectObject)enu.Current; iobj.Offset = sb.Length; ReferenceTable.Add(new CrossRef(iobj.Offset, 0)); sb.Append(iobj.ToString()); } Trailer.CrossRefStartOffset = sb.Length; sb.Append(ReferenceTable); Trailer.Size = this._indirectObjects.Count + 1; sb.Append(Trailer.ToString()); return(sb.ToString()); }
public void ReadsIndirectObject() { // Arrange IndirectObject indirectObj; long position; // Act using (var stream = BuildTestStream($"10 0 obj\r\nnull\r\nendobj\r\n")) { using (var reader = new PdfReader(stream)) { reader.Position = 0; indirectObj = IndirectObject.FromReader(reader); position = reader.Position; } } // Assert Assert.NotNull(indirectObj); Assert.IsType <IndirectObject <NullObject> >(indirectObj); Assert.Equal(22, position); }
private DocumentCatalog ReadXRef(Tokenizer tokenizer) { long xrefPosition = XrefPosition(tokenizer); tokenizer.MoveToPosition(xrefPosition); Token token = tokenizer.Token(); if (!TokenValidator.Validate(token, CharacterSetType.Regular, "xref")) { throw new PdfException(PdfExceptionCodes.INVALID_XREF, "Expected xref"); } token = tokenizer.Token(); if (!TokenValidator.IsWhiteSpace(token)) { throw new PdfException(PdfExceptionCodes.INVALID_XREF, "after xref must be a whitspace"); } tokenizer.GetInteger(); // objectNumber tokenizer.TokenExcludedComments(); int numberOfEntries = tokenizer.GetInteger(); tokenizer.TokenExcludedComments(); PDFObjects pdfObjects = new PDFObjects(); for (int i = 0; i < numberOfEntries; i++) { int pos = tokenizer.GetInteger(); // position tokenizer.TokenExcludedComments(); // whitespace tokenizer.GetInteger(); // generation tokenizer.TokenExcludedComments(); // whitespace string type = tokenizer.Token().ToString(); // f or n tokenizer.TokenExcludedComments(); // whitespace if (type != "f" && type != "n") { throw new PdfException(PdfExceptionCodes.INVALID_XREF, "only xref f or n entries allowed"); } if (type == "f") // free element { continue; } tokenizer.SavePosition(); tokenizer.MoveToPosition(pos); IndirectObject obj = new IndirectObject(tokenizer); pdfObjects.AddObject(obj); tokenizer.RestorePosition(); } if (tokenizer.Token().ToString() != "trailer") { throw new PdfException(PdfExceptionCodes.INVALID_TRAILER, "expected trailer"); } var trailer = new DictionaryObject(tokenizer); var rootIndirect = (IndirectReferenceObject)trailer.Dictionary["Root"]; return(pdfObjects.GetDocument <DocumentCatalog>(rootIndirect)); }
/// <summary> /// Instanciates a new Document Catalog with an indirect Page Tree. /// </summary> /// <param name="pageTree">The <see cref="IndirectObject{PageTree}"/> that is the root of the document’s page tree.</param> public DocumentCatalog(IndirectObject <PageTreeNode> pageTree) { internalDictionary[TypeKey] = new NameObject(TypeValue); internalDictionary[PagesKey] = pageTree ?? throw new ArgumentNullException(nameof(pageTree)); }
/// <summary> /// Instantiate a page object /// </summary> /// <param name="pages">Pages object the contains this page</param> public Page(IndirectObject indPages) : base() { base.Add(new Name("Type"), new Name("Page")); this.Parent = new Reference(indPages); }
public void Delete( ) { IndirectObject.Delete(); }
public void Purge() { // First establish the count of images; per page (or xobject), per image. Dictionary <int, Dictionary <int, int> > imageCountPerParent = new Dictionary <int, Dictionary <int, int> >(); foreach (ImageProperties image in _images) { foreach (ImageRendition rendition in image.Renditions) { int parent = rendition.StreamObject is FormXObject ? rendition.StreamID : rendition.PageID; Dictionary <int, int> imageCount = null; imageCountPerParent.TryGetValue(parent, out imageCount); if (imageCount == null) { imageCount = new Dictionary <int, int>(); imageCountPerParent[parent] = imageCount; } int count = 0; if (!imageCount.TryGetValue(image.PixMap.ID, out count)) { imageCount[image.PixMap.ID] = 0; count = 0; } if (!_redactionSet.Contains(rendition)) { imageCount[image.PixMap.ID] = count + 1; } } } // Then remove the ones that have been redacted foreach (KeyValuePair <int, Dictionary <int, int> > pair1 in imageCountPerParent) { HashSet <int> imagesToRemove = new HashSet <int>(); foreach (KeyValuePair <int, int> pair2 in pair1.Value) { if (pair2.Value == 0) // no references left { imagesToRemove.Add(pair2.Key); } } if (imagesToRemove.Count > 0) { IndirectObject io = _doc.ObjectSoup[pair1.Key]; while (io != null) { Atom rez = io.Resolve(Atom.GetItem(io.Atom, "Resources")); DictAtom xobjs = io.Resolve(Atom.GetItem(rez, "XObject")) as DictAtom; if ((xobjs != null) && (xobjs.Count > 0)) { List <string> namesToRemove = new List <string>(); foreach (KeyValuePair <string, Atom> pair in xobjs) { IndirectObject xobj = io.ResolveObj(pair.Value); if (xobj == null) { continue; // shouldn't ever happen } if (imagesToRemove.Contains(xobj.ID)) { namesToRemove.Add(pair.Key); } } foreach (string key in namesToRemove) { xobjs.Remove(key); } } io = _imagesCoverWholeDoc ? io.ResolveObj(Atom.GetItem(io.Atom, "Parent")) : null; } } } }
public override int GetHashCode( ) { return(IndirectObject.GetHashCode()); }
public override PdfObject Swap( PdfObject other ) { return(IndirectObject.Swap(((PdfReference)other).IndirectObject).Reference); }
public override object Clone( File context ) { return(((PdfIndirectObject)IndirectObject.Clone(context)).Reference); }
static void Main(string[] args) { PDFSize paper = new PDFSize(842, 595); //A4 paper size 210mm×297mm // Create a document object and initialize it PDFDocument doc = new PDFDocument(); doc.VersionMinor = 4; // Create a catalog Catalog cat = new Catalog(); // Create an indirect obejct pointing to the catalog // Look how we get the id number from the document IndirectObject ioCat = new IndirectObject(cat, doc.NextObjectID()); doc.IndirectObjects.Add(ioCat); doc.Trailer.Root = new Reference(ioCat); // Create pages object Pages pages = new Pages(); IndirectObject ioPages = new IndirectObject(pages, doc.NextObjectID()); doc.IndirectObjects.Add(ioPages); Reference refPages = new Reference(ioPages); // Create a reference to the Pages object and add it to teh catalog object cat.Pages = refPages; Page page = new Page(); // define the dimention of the pages page.MediaBox = new PDFArray() { new Number(0), new Number(0), new Number(paper.Width), new Number(paper.Height) }; // Set the page objects parent page.Parent = refPages; // Create indirect object for pages IndirectObject ioPage = new IndirectObject(page, doc.NextObjectID()); doc.IndirectObjects.Add(ioPage); pages.AddPageRef(new Reference(ioPage)); // Create resources object for each pages page.Resources = new Resources(); PDFFontDescriptor fontDescriptor = new PDFFontDescriptor(); fontDescriptor.FontBBox = new PDFArray() { new Number(0), new Number(0), new Number(0), new Number(0) }; IndirectObject _fontDescriptorID = new IndirectObject(fontDescriptor, doc.NextObjectID()); doc.IndirectObjects.Add(_fontDescriptorID); Reference fontDescriptorRef = new Reference(_fontDescriptorID); PDFFont descendantFont = new PDFFont(); IndirectObject descendantFontID = new IndirectObject(descendantFont, doc.NextObjectID()); doc.IndirectObjects.Add(descendantFontID); descendantFont.SubType = new Name("CIDFontType2"); descendantFont.BaseFont = new Name("SimSun"); descendantFont.FontDescriptor = fontDescriptorRef; descendantFont.CIDSystemInfo = new PDFDictionary(); descendantFont.CIDSystemInfo.Add(new Name("Registry"), "(Adobe)"); descendantFont.CIDSystemInfo.Add(new Name("Ordering"), "(GB1)"); // Create font andf initialize it PDFFont ascii = new PDFFont(); ascii.Name = new Name("F2"); ascii.Encoding = new Name("WinAnsiEncoding"); ascii.BaseFont = new Name("Helvetica"); ascii.SubType = new Name("TrueType"); IndirectObject arialID = new IndirectObject(ascii, doc.NextObjectID()); doc.IndirectObjects.Add(arialID); // Create font and initialize it PDFFont gb = new PDFFont(); gb.SubType = new Name("Type0"); gb.Name = new Name("F1"); gb.BaseFont = new Name("SimSun"); gb.Encoding = new Name("UniGB-UCS2-H"); gb.FontDescriptor = fontDescriptorRef; gb.DescendantFonts = new PDFArray() { new Reference(descendantFontID) }; IndirectObject fontID = new IndirectObject(gb, doc.NextObjectID()); doc.IndirectObjects.Add(fontID); // add the font dictionary to the resources PDFDictionary dicFont = new PDFDictionary(); dicFont.CarriageReturn = string.Empty; dicFont.Add(ascii.Name, new Reference(arialID)); dicFont.Add(gb.Name, new Reference(fontID)); page.Resources.Add(new Name("Font"), dicFont); // Create the procedure set ProcSet procset = new ProcSet(); procset.Add(new Name("PDF")); procset.Add(new Name("Text")); IndirectObject ioProcset = new IndirectObject(procset, doc.NextObjectID()); doc.IndirectObjects.Add(ioProcset); // add the proc set to the resources page.Resources.Add(new Name("ProcSet"), new Reference(ioProcset)); PDFStream content = new PDFStream(); IndirectObject ioStream = new IndirectObject(content, doc.NextObjectID()); doc.IndirectObjects.Add(ioStream); page.Content = new Reference(ioStream); Info info = new Info(); IndirectObject ioInfo = new IndirectObject(info, doc.NextObjectID()); doc.IndirectObjects.Add(ioInfo); doc.Trailer.Info = new Reference(ioInfo); //draw double unit = 2.834; PDFGraphics g = new PDFGraphics(); g.state(1, 0, 0, 1, 0, 0); g.SetLineWidth(1f); g.SetRGBFillColor(Color.SteelBlue); g.SetRGBStrokeColor(Color.Black); g.DrawRectangle(20 * unit, paper.Height - 20 * unit, 10 * unit, 10 * unit); g.FillAndStrokePath(); content.Write(g); //Set font and write // create the text object to write to TextObject text = new TextObject(); text.SetFont(gb, 8); text.SetRGBFillColor(Color.Blue); text.SetRGBStrokeColor(Color.Blue); text.Move(10 * unit, paper.Height - 5 * unit); text.SetRGBFillColor(Color.Black); text.SetRGBStrokeColor(Color.Black); text.SetFont(gb, 9); Func <string, string> str2code = (string s) => { StringBuilder o = new StringBuilder("<"); foreach (var c in s) { o.Append(Convert.ToString(c, 16).ToUpper().PadLeft(4, '0')); } o.Append('>'); return(o.ToString()); }; text.WriteBinary(str2code("绘制一个大小边长1厘米的正方形")); content.Write(text); doc.Save("demo.pdf"); }
private bool WriteObject(IndirectObject obj) { if (obj == null) return false; WriteIndent() .Write(obj.Reference.Id.ToString()) .Write(" ") .Write(obj.Reference.Generation.ToString()) .WriteLine(" obj").Indent(); WriteIndent().WriteObject(obj.Value); WriteLine().Outdent().WriteIndent().WriteLine("endobj"); return true; }