Link to home
Start Free TrialLog in
Avatar of brettawv
brettawv

asked on

printersettings conversion to vb.net

I found this code which is what I need for my program.  I tried to convert it myself, but I get an error on
fi.SetValue(PD.PrinterSettings, outname)
It says object reference not set to an instance of an object.

Here is the original code:
System.Type type = typeof(PrinterSettings);
this.PrinterSettings.PrintToFile = true;
FieldInfo outputPort = type.GetField("outputPort",
BindingFlags.NonPublic | BindingFlags.Instance);
outputPort.SetValue(this.PrinterSettings,"FILE:");

Here is what I converted it to:
Dim PD As New Printing.PrintDocument
       
Dim fi As System.Reflection.FieldInfo
Dim outname As New Object

PD.PrinterSettings.PrintToFile = True
fi = PD.PrinterSettings.GetType().GetField("outputport", Reflection.BindingFlags.Instance)

outname = "C:\jimc.ps"
fi.SetValue(PD.PrinterSettings, outname)  "I get the error on this line"

PD.Print()

Can someone help me with this conversion?
Avatar of AlexFM
AlexFM

Possibly this happens because first GetField argument in C# code is "outputPort", and in VB code "outputport". Last parameter in VB code is missing NonPublic flag.
Avatar of brettawv

ASKER

I'm not sure how to add that to the VB code.  VB doesn't like the | sign.  Any ideas on how to rewrite that line in VB?
ASKER CERTIFIED SOLUTION
Avatar of gregoryyoung
gregoryyoung
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I added and I still get the same error. Seems like I'm missing creating an instance of one of the objects, but I'm not sure what.  I could be totally wrong to.  If you have any other ideas, I would appreciate the help.
I ended up using itextsharp to convert images directly to pdf which was the whole reason for asking this question.  I was trying to send my picture to a postscript file and then convert it to pdf using ghostscript.

Thanks for trying to help.  I'll try to work this out later, but I'm set for now.

I'm giving the points to gregoryyoung for at least helping me through part of the problem.
I appreciate the awarding of points for someone trying to help, but it is also misleading - it leads people to believe that the answer being rewarded solves the problem...which it does not.

I tried this and couldn't figure out why it was not working for me until I scrolled down and saw that the problem was not actually answered....