> For the complete documentation index, see [llms.txt](https://docs.apryse.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.apryse.com/android/editing-page-content/write.md).

# Write page content to PDF in Android

Learn how to write page content to a new document using the PDFNet page writing API. Embed fonts, images, and graphical elements with full control over properties. Edit page content programmatically w

To write page content to a new document.

{% tabs %}
{% tab title="Java" %}
{% code lineNumbers="true" %}

```java
PDFDoc doc = new PDFDoc();

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

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

// Start a new page
Page page = doc.pageCreate();

// begin writing to the page
writer.begin(page);    

// Create an Image that can be reused multiple times in the document or multiple on the same page.
Image img = Image.create(doc.getSDFDoc(), imagename);
Element element = eb.createImage(img, new Matrix2D(200, -145, 20, 300, 200, 150));
writer.writePlacedElement(element);

// use the same image again (just change its matrix).
Gstate gstate = element.getGState();
gstate.setTransform(200, 0, 0, 300, 50, 450);
writer.writePlacedElement(element);

// use the same image again (just change its matrix).
writer.writePlacedElement(eb.createImage(img, 300, 600, 200, -150));

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

// Add the new page to the document sequence
doc.pagePushBack(page);
```

{% endcode %}
{% endtab %}

{% tab title="Kotlin" %}
{% code lineNumbers="true" %}

```kotlin
val doc = PDFDoc()

// ElementBuilder is used to build new Element objects
val builder = ElementBuilder()  

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

// Start a new page
var page = doc.pageCreate(Rect(0.0, 0.0, 612.0, 794.0))

// begin writing to the page
writer.begin(page)

// Create an Image that can be reused multiple times in the document or multiple on the same page.
val img = Image.create(doc.sdfDoc, imagename)
var element = builder.createImage(img, Matrix2D(200.0, -145.0, 20.0, 300.0, 200.0, 150.0))
writer.writePlacedElement(element)

// use the same image (just change its matrix)
var gstate = element.gState()    
gstate.setTransform(200.0, 0.0, 0.0, 300.0, 50.0, 450.0)
writer.writePlacedElement(element)

// use the same image again (just change its matrix).
writer.writePlacedElement(builder.createImage(img, 300.0, 600.0, 200.0, -150.0))

// save changes to the current page
writer.end()  

// Add the new page to the document sequence
doc.pagePushBack(page)
```

{% endcode %}
{% endtab %}
{% endtabs %}

[Build, Write, Embed Elements in PDF Pages](/android/get-started/samples.md#elementbuilder) 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:

![](https://226546913-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F0QItsdFBmuuL9ezHimHy%2Fuploads%2Fgit-blob-ea16d96af120df93136b29c81a91c2d2e6211a7a%2F27fe7de6ec2dcabb5c4f5d64e7449afb67067285-370x328.gif?alt=media)

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:

{% tabs %}
{% tab title="Java" %}
{% code lineNumbers="true" %}

```java
PDFDoc doc = new PDFDoc(filename);
Page first_page = doc.getPage(1);

ElementWriter writer = new ElementWriter();
ElementReader reader = new ElementReader();
reader.begin(page);
writer.begin(page, ElementWriter.e_replacement, false);

Element element;
while ((element = reader.next()) != null) 
{
    switch (element.getType()) {
      case Element.e_text: {
          // Set all text to blue color.
          GState gs = element.getGState();
          gs.setFillColorSpace(ColorSpace.createDeviceRGB());
          gs.setFillColor(new ColorPt(0, 0, 1));
          writer.writeElement(element);
      }
      break;
      default:
          writer.writeElement(element);
          break;
  }
}

writer.end();
reader.end();
```

{% endcode %}
{% endtab %}

{% tab title="Kotlin" %}
{% code lineNumbers="true" %}

```kotlin
val doc = PDFDoc(filename)
val first_page = doc.getPage(1)

val writer = ElementWriter()
val reader = ElementReader()
reader.begin(page)
writer.begin(page, ElementWriter.e_replacement, false)

var element = reader.next()
while (element != null)
{
  when (element.type) {
    Element.e_text -> {
        // Set all text to blue color.
        val gs = element.gState
        gs.fillColorSpace = ColorSpace.createDeviceRGB()
        gs.fillColor = ColorPt(0.0, 0.0, 1.0)
        writer.writeElement(element)
    }
    else -> writer.writeElement(element)
  }
  element = reader.next()
}

writer.end()
reader.end()
```

{% endcode %}
{% endtab %}
{% endtabs %}

[PDF Editor (Programmatic)](/android/get-started/samples.md#elementedit) Full code sample which strips all images from the page and changes text color to blue.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.apryse.com/android/editing-page-content/write.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
