Simply put, a PDF content stream is a stream object that describes how a PDF application renders a page. A page can have one or multiple content streams. It is also used to describe form XObject, and tiling patterns. Internally, it contains a series of instructions that describe how to draw elements (vectors, images, text) on the page.
Some instructions refer to content stream resources, such as fonts, images, form XObjects, patterns, shadings, color spaces, or marked content properties that are included in the accompanying resource dictionary and referenced from the content stream by a unique name.
PDF .Net converts a content
streams and their associated resource dictionaries into a tree of PdfContentElements
,
with PdfContent
as the root, facilitating
inspection and manipulation.
Manipulate PDF content streams in C# and VB.NET
In the following example you will see how to improve performance when writing text on multiple pages using the same font. Note that the embedded subset of fonts is computed only once, not aftereach pageis edited.
Complete code
using System;
using System.IO;
using SautinSoft;
using SautinSoft.Pdf;
using SautinSoft.Pdf.Content;
namespace Sample
{
class Sample
{
static void Main(string[] args)
{
/// <summary>
/// Manipulate PDF content streams and resources.
/// </summary>
/// <remarks>
/// Details: http://sautinsoft/products/pdf/help/net/developer-guide/content-streams-resources.php
/// </remarks>
using (var document = new PdfDocument())
{
using (var formattedText = new PdfFormattedText())
{
// Set the font to TrueType font that will be subset and embedded in the document.
formattedText.Font = new PdfFont("Calibri", 96);
// Draw a single letter on each page.
for (int i = 0; i < 2; ++i)
{
formattedText.Append(((char)('A' + i)).ToString());
var page = document.Pages.Add();
// Begin editing the page content, but don't end it until all pages are edited.
page.Content.BeginEdit();
page.Content.DrawText(formattedText, new PdfPoint(100, 500));
formattedText.Clear();
}
}
// End editing of all pages.
// This will convert the content of each page back to the underlying content stream and the accompanying resource dictionary.
// Subset of the 'Calibri' font, that contains only glyphs for characters 'A' to 'B' will be calculated just once before being
// embedded in the document.
foreach (var page in document.Pages)
page.Content.EndEdit();
document.Save("Content Streams And Resources.pdf");
}
}
}
}
PdfForm.Content
or the PdfTilingPattern.Content
, you must call PdfContent.BeginEdit()
before and PdfContent.EndEdit()
after editing.
PdfContent.BeginEdit()
and PdfContent.EndEdit()
is optional when
editing the PdfPage.Content
but might improve performance in some situations.PdfContent.EndEdit()
is called, Pdf converts back the PdfContent
and
all PdfContentElement
s underneath it to a content stream and the accompanying resource
dictionary.If you need a new code example or have a question: email us at support@sautinsoft.com or ask at Online Chat (right-bottom corner of this page) or use the Form below: