Some test text!

Writing page content

New page content can be added to an existing page or a blank new page using ElementBuilder and ElementWriter. ElementBuilder is used to instantiate one or more Elements that can be written to one or more pages using ElementWriter:

Adding new content to a page.

The following sample illustrates how to write page content to a new document:

PDFDoc doc = new PDFDoc();
doc.InitSecurityHandler();

// ElementBuilder is used to build new Element objects
ElementBuilder f = new ElementBuilder();

// ElementWriter is used to write Elements to the page
ElementWriter writer = new ElementWriter();

// Start a new page
// Position an image stream on several places on the page
Page page = doc.PageCreate();

// Begin writing to this page
writer.Begin(page);
// Attach ElementBuilder to the page    
f.Begin(page);        

// Import an Image that can be reused multiple
// times in the document or  multiple times on the
// same page.
MappedFile img_file = new MappedFile("peppers.jpg");
FilterReader img_data = new FilterReader(img_file);
Image img = Image.Create(doc.GetSDFDoc(),
   img_data,
   Image.ImageCompression.e_jpeg,
   400, 600, 8,
   ColorSpace.CreateDeviceRGB());

Element element = f.CreateImage(img,
  new Matrix2D(200, -145, 20, 300, 200, 150));

writer.WritePlacedElement(element);

GState gstate = element.GetGState();    

// Use the same image (just change its matrix)
gstate.SetTransform(200, 0, 0, 300, 50, 450);
writer.WritePlacedElement(element);

// Use the same image (just change its matrix)
writer.WritePlacedElement(
  f.CreateImage(img, 300, 600, 200, -150));

// save changes to the current page
writer.End();  

// Add a new page to the document sequence
doc.PagePushBack(page);

// Start a new page
page = doc.PageCreate();
writer.Begin(page);    
f.Begin(page);        

// Construct and draw a path object using
// different GState attributes
f.PathBegin();
f.MoveTo(306, 396);
f.CurveTo(681, 771, 399.75, 864.75, 306, 771);
f.CurveTo(212.25, 864.75, -69, 771, 306, 396);
f.ClosePath();

// path is now constructed
element = f.PathEnd();            
element.SetPathFill(true);        

// Set the path color space and color.
gstate = element.GetGState();
gstate.SetFillColorSpace(
  ColorSpace.CreateDeviceCMYK());
gstate.SetFillColor(
  new ColorPt(1, 0, 0, 0));  // cyan
gstate.SetTransform(
  0.5, 0, 0, 0.5, -20, 300);
writer.WritePlacedElement(element);

// Draw the same path using a different
// stroke color.
// The path will be filled and stroked.
element.SetPathStroke(true);        
gstate.SetFillColor(
  new ColorPt(0, 0, 1, 0)); // yellow
gstate.SetStrokeColorSpace(
  ColorSpace.CreateDeviceRGB());
gstate.SetStrokeColor(new ColorPt(1, 0, 0)); // red
gstate.SetTransform(0.5, 0, 0, 0.5, 280, 300);
gstate.SetLineWidth(20);
writer.WritePlacedElement(element);

// Draw the same path with with a given dash pattern.
// This path is should be only stroked.
element.SetPathFill(false);    
gstate.SetStrokeColor(new ColorPt(0, 0, 1)); // blue
gstate.SetTransform(0.5, 0, 0, 0.5, 280, 0);
double[] dash_pattern = {30};
gstate.SetDashPattern(ref dash_pattern, 0);
writer.WritePlacedElement(element);

writer.End();  // save changes to the current page
doc.PagePushBack(page);

doc.Save("out.pdf", PDFDoc.SaveOptions.e_remove_unused);

Note that once the Element is instantiated using ElementBuilder, you have full control over its properties and graphics state.

Page content can also come from existing pages. For example, you can use ElementReader to read paths, text, and images from existing pages and copy them to the current page. Note that, along the way, you can fully modify an Element's properties or its graphics state. This is how to perform page content editing. For example, the following copies all Elements except images from an existing page and changes text color to blue:

ElementWriter writer = new ElementWriter();
ElementReader reader = new ElementReader();
Element element;

reader.Begin(doc.PageBegin().Current());

Page new_page = doc.PageCreate(new Rect(0, 0, 612, 794));
doc.PagePushBack(new_page);

writer.Begin(new_page);
while ((element = reader.Next()) != null)
{
  if (element.GetType() == Element.ElementType.e_text)
  {
    // Set all text to blue color.
    GState gs = element.GetGState();
    gs.SetFillColorSpace(
      ColorSpace.CreateDeviceRGB());
    gs.SetFillColor(new ColorPt(0, 0, 1));
  }
  else if (element.GetType()
     == Element.ElementType.e_image)
  {
    // remove all images
    continue;
  }

  writer.WriteElement(element);
}

writer.End();
reader.End();

Get the answers you need: Support