Search Persits Software Knowledge Base

 

Article PS110322179

Drawing on an existing PDF document produces no visible output

Problem Description

When writing on an existing PDF document with AspPDF or AspPDF.NET, the code such as

objPage.Canvas.DrawText( "Test", "x=10; y=30", objFont );

produces no visible output.

Solution

This usually happens if the PDF document being drawn on has modified its graphics state, or its pages have a non-standard crop box.

Reason 1: Modified Graphics State

All drawing of graphics and text in a PDF document occurs in the context of the current graphics state, which consists of the current coordinate transformation matrix, current fill and stroke colors, etc. The majority of PDF documents save the current graphics state before modifying it, and then restore it back to default, which is the recommended procedure by PDF specs.

However, there are PDF documents out there that do not follow this procedure. As a result, drawing on top of these documents occurs in a non-default graphics state. For example, the current coordinate transformation matrix may be modified in such a way that the origin is moved from its default location in the lower-left corner of the document to some new (and unknown) location. The current fill color may be modified from the default black to another color, etc.

AspPDF and AspPDF.NET provide the PdfPage method ResetCoordinates which restores the current coordinate transformation matrix to its default state. In addition to using the ResetCoordinates method, also try to explicitly specify the text color and rendering parameters to avoid relying on the current (and unknown) graphics state:

objPage.ResetCoordinates();
objPage.Canvas.DrawText( "Test", "x=10; y=30; color=black; rendering=0", objFont );

Reason 2: Non-standard CropBox

A crop box is a rectangle defining the visible region of the page. In most PDF documents, the crop box of a page is either not specified at all or lies within the normal page coordinates.

However, there are PDF documents out there that have non-standard and unexpected crop boxes. If that is the case, these have to be taken into account when determining the X and Y coordinates of your text.

The following script displays the lower-left and upper-right coordinates of the crop box of a page:

IPdfRect objRect = objPage.CropBox;
Response.Write( objRect.Left + " " + objRect.Bottom + " " + objRect.Right + " " + objRect.Top );

This script may produce an unexpected output such as:

0 5806.92 612 6598.92

This means the X-coordinate for this page is normal (between 0 and 612) while the Y-coordinate is in the range 5806.92 to 6598.92. Therefore, to produce visible output on a document like that, the Y-parameter to the DrawText method has be adjusted accordingly:

objPage.Canvas.DrawText( "Test", "x=10; y=5900", objFont );

Created: 3/22/2011 1:01:50 PM
Last Modified: 3/22/2011 1:01:50 PM

Copyright © Persits Software, Inc. 1998 - 2018
For technical support, write to support@persits.com.