Skip to content

Problem with XGraphics and XTextFormatter Draw methods #445

Open
@debirus

Description

@debirus

I found that after using any Draw method of the XGraphics object it is no longer possible to use the DrawString method of the XTextFormatter on the same object, example:

//-----
XGraphics graf = XGraphics.FromPdfPage(page);
//-----
graf.DrawImage(img, new XRect(x, y, w, h));
//-----
tf = new XTextFormatter(graf);
tf.Alignment = format;
tf.DrawString(text, fontCache.FontChar, fontCache.Brush, rect, XStringFormats.TopLeft);

When I save the PDF at the end, the img is present on the page while the text is not, it cannot be seen even if it is in effect present as text on the document, but remains invisible.

By reversing the order, i.e. using tf.DrawString first and then graf.DrawImage, both img and text can be seen clearly on the final PDF.

The same problem is present after using any graf Draw method.

After several attempts I found a workaround to solve the problem, after using a graf Draw method I recall these instructions:

PdfPage page = graf.PdfPage;
graf.Dispose();
graf = XGraphics.FromPdfPage(page);

Somehow it returns the graf object to its original state while maintaining the changes made to the pdf page

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions