I will try to explain my vision of the situation.
Please look at the screenshot of the PDF document I created for this explanation.
This document contains
- "usual text" that is in the content stream of the page,
- free text annotation,
- and text field with pre-filled text

1.png
(54kb) downloaded 0 time(s).If I render this document with the following code, I will get an image with "usual text" only.
Code:using (var doc = PdfDocument.Load(@"d:\22\test.pdf"))
{
var page = doc.Pages[0];
int w = (int)page.Width;
int h = (int)page.Height;
using (var bmp = new PdfBitmap(w, h, true))
{
page.Render(bmp, 0, 0, w, h, PageRotate.Normal, RenderFlags.FPDF_LCD_TEXT);
bmp.Image.Save(@"d:\22\simple_render.png", ImageFormat.Png);
}
}
output:

2.png
(28kb) downloaded 0 time(s).Please note, I have loaded a document without
PdfForms, but this does not matter for
Render() method.
Pdfium is also have an other render method -
RenderForms(). This method designed for render annotations with user interaction (fields are also annotations of which type is widget).
These annotations are rendered considering user interaction - text carriage for example, or field highlights and text selection.
So if I render the document with following code, I will get another image.
code:
Code:using (var doc = PdfDocument.Load(@"d:\22\test.pdf", new PdfForms()))
{
var page = doc.Pages[0];
int w = (int)page.Width;
int h = (int)page.Height;
using (var bmp = new PdfBitmap(w, h, true))
{
bmp.FillRect(0, 0, w, h, Color.White);
page.RenderForms(bmp, 0, 0, w, h, PageRotate.Normal, RenderFlags.FPDF_LCD_TEXT);
bmp.Image.Save(@"d:\22\annots_render.png", ImageFormat.Png);
}
}
and output:
3.png
(30kb) downloaded 0 time(s). As you can see now the image does not have "usual text", but have annotations (including text field).
Also the text field is highlighted by default color. Keep in mind this fact for future explanation please (when we will discuss about
FPDF_ANNOT flag).
Please note this time I loaded the document with
PdfForms.
RenderForms() does not work if
PdfForms is not specified (interactive forms subsystem has not been initialized during loading of the document).
Thus if we want display annotations with user interaction support we must use
RenderForm method. And we must pass
PdfForms object to loading procedure to initialize interactive forms subsystem.
And vise versus, if we do not need user interaction we must not use
RenderForm. And we must not initialize interactive forms subsystem (must not pass
PdfForms on loading), otherwise the engine will send user interaction events.
Ok. What about PdfViewer?
PdfViewer uses both
Render() and
RenderForms(). Viewer generate first image, thеn generate second image, next combine these two images to getting the resulting image that finally displayed to the user.
And second image is rendered many times to provide interactivity (such as carriage blinking in the text field that have input focus)
As you can see the codes above does not use
FPDF_ANNOT flag in both cases (and PdfViewer does not use it too). You probably already guessed what this flag is may be used for.
Please look at this code. Now I using
FPDF_ANNOT flag for rendering
Code:using (var doc = PdfDocument.Load(@"d:\22\test.pdf"))
{
var page = doc.Pages[0];
int w = (int)page.Width;
int h = (int)page.Height;
using (var bmp = new PdfBitmap(w, h, true))
{
bmp.FillRect(0, 0, w, h, Color.White);
page.Render(bmp, 0, 0, w, h, PageRotate.Normal, RenderFlags.FPDF_LCD_TEXT | RenderFlags.FPDF_ANNOT);
bmp.Image.Save(@"d:\22\simple_render2.png", ImageFormat.Png);
}
}
Output:
4.png
(30kb) downloaded 0 time(s). The code produce the image that contains all elements. But you can see that the text field is not highlighted like on previous image. This is because all annotations are now displayed without they interactivities.
Because of the foregoing, I believe that the most correct way to achieve read-only mode is to load a document without initializing
PdfForms and use the
FPDF_ANNOT flag on displaying.
So I can suggest you load document with
PdfForms, check if it should be read only and than reload it without
PdfForms.
This is why we still haven't implement ReadOnly property in PDFViewer. Because documents can be loaded outside of viewer. This behavior is currently controlled by the developers who use the viewer, but not by viewer itself. If only the Document property is used, not its own LoadDocument method.
I hope the explanations were clear. I apologize if somewhere it is not.
Please ask any questions, I will try to answer them.
Edited by user Sunday, May 5, 2019 5:56:55 AM(UTC)
| Reason: Not specified