Q: How do I create a 'PostScript XObject' in PDF?
Should the code below create a pdf with the ps object embeded? Is code
wrong? could chess.ps type/version not be supported? is there a bug?
PDFDoc doc = new PDFDoc();
ElementBuilder builder = new ElementBuilder();
ElementWriter writer = new ElementWriter();
Page page = doc.PageCreate();
writer.Begin(page);
// Embed a custom stream (file postscript.ps).
StdFile embed_file = new StdFile("chess.ps",
StdFile.OpenMode.e_read_mode);
FilterReader mystm = new FilterReader(embed_file);
Obj ps_stm = doc.CreateIndirectStream(mystm);
ps_stm.PutName("Subtype", "PS");
Element element = builder.CreateForm(ps_stm);
writer.WriteElement(element);
writer.End(); // save changes to the current page
doc.PagePushBack(page);
doc.Save(output_path + "addimage.pdf",
SDFDoc.SaveOptions.e_linearized);
doc.Close();
----
A: Actually there is an error in the code. PostScript object in PDF
(Section 4.7.1 'PostScript XObjects' in PDF Reference) is not the same
as form XObject, so element_builder.CreateForm() will not work and the
PostScript object will not be written to the page. You can add a
reference to a PostScript object as follows:
static void Main(string[] args)
{
PDFNet.Initialize();
PDFNet.SetResourcesPath("../../../../../resources");
// Relative path to the folder containing test files.
string input_path = "../../../../TestFiles/";
string output_path = "../../../../TestFiles/Output/";
try
{
Console.WriteLine("-------------------------------------------------");
PDFDoc doc = new PDFDoc();
// ----------------------------------------------------------
//You can embed PostScript stream in PDF as follows (C# sample):
// Embed a custom stream (file postscript.ps).
StdFile embed_file = new StdFile(input_path + "postscript.ps",
StdFile.OpenMode.e_read_mode);
FilterReader mystm = new FilterReader(embed_file);
Obj ps_stm = doc.CreateIndirectStream(mystm);
ps_stm.PutName("Subtype", "PS");
// ----------------------------------------------------------
Page page = doc.PageCreate(); // Create a new page
// Add a reference the PostScript object on a given page:
Obj res = page.GetResourceDict();
if (res == null) res = page.GetSDFObj().PutDict("Resources");
Obj xobjs = res.FindObj("XObject");
if (xobjs == null) xobjs = res.PutDict("XObject");
xobjs.Put("ps000", ps_stm);
ElementWriter writer = new ElementWriter(); // Used to write
Elements to the page
writer.Begin(page); // Begin writing to this page
// writer.WriteString(" 612 0 0 792 0 0 cm ");
writer.WriteString(" /ps000 Do ");
writer.End(); // save changes to the current page
doc.PagePushBack(page); // Add the page to a document.
doc.Save(output_path + "addimage.pdf",
SDFDoc.SaveOptions.e_linearized);
Console.WriteLine("Done. Result saved in addimage.pdf...");
doc.Close();
}
catch (PDFNetException e)
{
Console.WriteLine(e.Message);
}
PDFNet.Terminate();
}
The PS content is not visible in Acrobat viewer, but according to the
spec it should be passed to a PostScript printer (for more info,
please refer to section 4.7.1 'PostScript XObjects' in PDF Reference).
Please keep in mind that the use of this PDF feature is not encouraged
and that in future this feature may be deprecated.