70002195 Save the printer settings in a file stream

Article 70002195
Type HowTo
Product Engine
Version 9
Date Added 4/19/2022 12:00:00 AM
Fixed 10.1001.0.2 (4/19/2022 12:00:00 AM)
Submitted by Yiannis Makarounis

Summary

How can I save the printer settings (name, paper type, orientation, margins etc) in a file and set it back later to another drawing?

Solution

To save the printer settings of Model or a vdLayout to a file you can try a code like:

        private void PRNSaveToFile()
        {
            //save a vdPrint to a memory stream
            MemorySerializer serializer = new MemorySerializer(null);
            serializer.Serialize(doc.Model.Printer, "printer");
            serializer.Flush();

            MemoryStream stream = serializer.ToMemoryStream();
            serializer.Close();
            //then write it on disk
            using (FileStream file = new FileStream(@"C:\test\prnsettings.prs", FileMode.Create, System.IO.FileAccess.Write))
            {
                file.Write(stream.GetBuffer(), 0, (int)stream.Length);
            }
            //end of save to stream 
        }

        private void PRNLoadFromFile()
        {
            //load from file
            using (FileStream file = new FileStream(@"C:\test\prnsettings.prs", FileMode.Open, FileAccess.Read))
            {
                MemoryDeSerializer ds = new MemoryDeSerializer(file, doc.Activator);
                object[] objs = ds.Read();
                ds.Close();

                vdPrint printer = null;
                if (objs.Length > 0) printer = objs[0] as vdPrint;
                //end of load from a file
                //copy properties of a stream loaded printer to a Layout printer
                if (printer != null)
                {
                    printer.SetLayout(doc.Model);//must set a temporary layout to loaded printer first...
                    doc.Model.Printer.CopyFrom(printer);//copy printer properties
                }
            }
        }
Alternative if you like to save/load to a memory stream so you can add this to a database you can try this code:
 
                //save a vdPrint to a memory stream
                MemorySerializer serializer = new MemorySerializer(null);
                serializer.Serialize(doc.Model.Printer, "printer");
                serializer.Flush();
                
                MemoryStream stream = serializer.ToMemoryStream();
                serializer.Close();
                //end of save to stream 
and
                //load a vdPrint from a stream
                MemoryDeSerializer ds = new MemoryDeSerializer(stream, doc.Activator);
                object[] objs = ds.Read();
                ds.Close();
 
                vdPrint printer = null;
                if(objs.Length > 0) printer = objs[0] as vdPrint;
 
                //end of load from a stream
 
                //copy properties of a stream loaded printer to a Layout printer
                if(printer != null)
                {
                    printer.SetLayout(doc.Model);//must set a temporary layout to loaded printer first...
                    doc.Model.Printer.CopyFrom(printer);//copy printer properties
                }

Send comments on this topic.