Write page content to PDF on Server/Desktop

To write page content to a new document.

1doc = PDFDoc.new()
2
3# ElementBuilder is used to build new Element objects
4builder = ElementBuilder.new()
5
6# ElementWriter is used to write Elements to the page
7writer = ElementWriter.new()
8
9# Start a new page ------------------------------------
10page = doc.PageCreate(Rect.new(0, 0, 612, 794))
11
12writer.Begin(page) # begin writing to the page
13
14# Create an Image that can be reused in the document or on the same page.
15img = Image.Create(doc.GetSDFDoc(), imagename)
16element = builder.CreateImage(img, Matrix2D.new(300, -145, 20, 200, 200, 150))
17writer.WritePlacedElement(element)
18
19gstate = element.GetGState() # use the same image (just change its matrix)
20gstate.SetTransform(200, 0, 0, 300, 50, 450)
21writer.WritePlacedElement(element)
22
23# use the same image again (just change its matrix)
24writer.WritePlacedElement(builder.CreateImage(img, 300, 600, 200, -150))
25
26# save changes to the current page
27writer.End()
28
29# Add the new page to the document sequence
30doc.PagePushBack(page)

Build, Write, Embed Elements in PDF Pages
Full code sample which illustrates how to use PDFNet page writing API, how to embed fonts and images and how to copy graphical elements from one page to another.

About 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:

Apryse Docs Image

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 from an existing page and changes text color to blue:

1doc = PDFDoc.new(filename)
2first_page = doc.GetPage(1)
3
4writer = ElementWriter.new()
5reader = ElementReader.new()
6reader.Begin(page)
7writer.Begin(page, ElementWriter::E_replacement, false)
8
9element = reader.Next() # Read page contents
10while !element.nil? do
11 type = element.GetType()
12 case type
13 when Element::E_text
14 # Set all text to blue color.
15 gs = element.GetGState()
16 gs.SetFillColorSpace(ColorSpace.CreateDeviceRGB())
17 cp = ColorPt.new(0, 0, 1)
18 gs.SetFillColor(cp)
19 writer.WriteElement(element)
20 else
21 writer.WriteElement(element)
22 end
23 element = reader.Next()
24end
25
26writer.End()
27reader.End()

PDF Editor (Programmatic)
Full code sample which strips all images from the page and changes text color to blue.

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales