static void Main(string[] args) { // If using Professional version, put your serial key below. ComponentInfo.SetLicense("FREE-LIMITED-KEY"); PdfDocument document = PdfDocument.Load("Reading.pdf"); // Get document's trailer dictionary. var trailer = document.GetDictionary(); // Get document catalog dictionary from the trailer. var catalog = (PdfDictionary)((PdfIndirectObject)trailer[PdfName.Create("Root")]).Value; // Either retrieve "PieceInfo" entry value from document catalog or create a page-piece dictionary and set it to document catalog under "PieceInfo" entry. PdfDictionary pieceInfo; var pieceInfoKey = PdfName.Create("PieceInfo"); var pieceInfoValue = catalog[pieceInfoKey]; switch (pieceInfoValue.ObjectType) { case PdfBasicObjectType.Dictionary: pieceInfo = (PdfDictionary)pieceInfoValue; break; case PdfBasicObjectType.IndirectObject: pieceInfo = (PdfDictionary)((PdfIndirectObject)pieceInfoValue).Value; break; case PdfBasicObjectType.Null: pieceInfo = PdfDictionary.Create(); catalog[pieceInfoKey] = PdfIndirectObject.Create(pieceInfo); break; default: throw new InvalidOperationException("PieceInfo entry must be dictionary."); } // Create page-piece data dictionary for "GemBox.Pdf" conforming product and set it to page-piece dictionary. var data = PdfDictionary.Create(); pieceInfo[PdfName.Create("GemBox.Pdf")] = data; // Create a private data dictionary that will hold private data that "GemBox.Pdf" conforming product understands. var privateData = PdfDictionary.Create(); data[PdfName.Create("Data")] = privateData; // Set "Title" and "Version" entries to private data. privateData[PdfName.Create("Title")] = PdfString.Create(ComponentInfo.Title); privateData[PdfName.Create("Version")] = PdfString.Create(ComponentInfo.Version); // Specify date of the last modification of "GemBox.Pdf" private data (required by PDF specification). data[PdfName.Create("LastModified")] = PdfString.Create("D:" + DateTimeOffset.Now.ToString("yyyyMMddHHmmssK", CultureInfo.InvariantCulture).Replace(':', '\'') + "'", PdfEncoding.ASCII, PdfStringForm.Literal); document.SaveOptions.CloseOutput = true; document.Save("Basic Objects.pdf"); }
static void Main() { // If using Professional version, put your serial key below. ComponentInfo.SetLicense("FREE-LIMITED-KEY"); using (var document = new PdfDocument()) { // Specify content stream's content as a sequence of content stream operands and operators. var content = new StringBuilder(); // Begin a text object. content.AppendLine("BT"); // Set the font and font size to use, installing them as parameters in the text state. // In this case, the font resource identified by the name F1 specifies the font externally known as Helvetica. content.AppendLine("/F1 12 Tf"); // Specify a starting position on the page, setting parameters in the text object. content.AppendLine("70 760 Td"); // Paint the glyphs for a string of characters at that position. content.AppendLine("(GemBox.Pdf) Tj"); // End the text object. content.AppendLine("ET"); // Create content stream and write content to it. var contentStream = PdfStream.Create(); contentStream.Filters.AddFilter(PdfFilterType.FlateDecode); using (var stream = contentStream.Open(PdfStreamDataMode.Write, PdfStreamDataState.Decoded)) { var contentBytes = PdfEncoding.Byte.GetBytes(content.ToString()); stream.Write(contentBytes, 0, contentBytes.Length); } // Create font dictionary for Standard Type 1 'Helvetica' font. var font = PdfDictionary.Create(); font[PdfName.Create("Type")] = PdfName.Create("Font"); font[PdfName.Create("Subtype")] = PdfName.Create("Type1"); font[PdfName.Create("BaseFont")] = PdfName.Create("Helvetica"); // Add font dictionary to resources. var fontResources = PdfDictionary.Create(); fontResources[PdfName.Create("F1")] = PdfIndirectObject.Create(font); var resources = PdfDictionary.Create(); resources[PdfName.Create("Font")] = fontResources; // Create new empty A4 page. var page = document.Pages.Add(); // Set contents and resources of a page. var pageDictionary = page.GetDictionary(); pageDictionary[PdfName.Create("Contents")] = PdfIndirectObject.Create(contentStream); pageDictionary[PdfName.Create("Resources")] = resources; document.Save("Content Stream.pdf"); } }