The only way currently to get form XObjects from pdfwrite is to use pdfmarks. When a Form XObject pdfmark is contained within a form XObject pdfmark, the resulting PDF is incorrect. Test file: %! [ /_objdef {PDFForm} /BBox [0 0 100 100] /BP pdfmark [ /_objdef {NestedForm} /BBox [0 0 50 50] /BP pdfmark << /PatternType 1 /PaintType 1 /TilingType 1 /BBox [ 0 0 50 50 ] /XStep 50 /YStep 50 /PaintProc { pop 1 0 0 setrgbcolor 0 0 moveto 0 50 lineto 50 50 lineto 50 0 lineto 0 0 lineto stroke } >> [1 0 0 1 0 0] makepattern setpattern 0 0 100 100 rectfill [ /EP pdfmark [ {NestedForm} /SP pdfmark [ /EP pdfmark [ {PDFForm} /SP pdfmark showpage
Created attachment 6833 [details] patch for base/gdevpdfm.c to fix issue with /SP/BP/EP pdfmarks nested between /BP /EP pdfmarks IF /SP pdfmark is injected into the definition of a new Form XObject (that is, put between /BP-/EP pdfmarks), the newly created container Form XObject lacks the /XObject entry in its /Resources dictionary. The proposed patch fixes this bug. It maintains a stack of dictionaries which are filled with the XObject resources referenced in the current context, that is which are referenced (by /SP pdfmark) in the content stream of the Form XObject currently open (opened by the /BP pdfmark). When the current context is closed by an /EP pdfmark the /XObject entry is added to the /Resources dictionary. Hence, /BP-/EP and /SP can now be nested to arbitrary depth into other /BP-/EP pdfmark pairs. The patch also fixes bug 689883.
Created attachment 6857 [details] patch for nested form XObject pdfmarks The updated patch is much shorter. A correct PDF will be produced from the given Postscript example. Also fixes bug 689883.
Created attachment 6862 [details] PDF file, produced from testfile given above using patched version of gs (patch=attachment 6857 [details])
Bug still reproducible in Ghostscript 9.03
Patch (finally!) adopted in commit 3d544d51c3e1d63a2fd115dac23255c966ba0e Apologies for the length of time its taken to review and adopt this.