-
Clipboard
I have created a windows service which prints pdf file if the user request for it. When the user clicks the print button using web application, it adds a data entry in the db and the service gets the file name and tries to print it. I am getting "Object reference not set to an instance of an object" error whenever I try to print the pdf file. Here is the code
Code:
public void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
Type AcrobatRect = Type.GetTypeFromCLSID(new System.Guid("{6D12C400-4E34-101B-9CA8-9240CE2738AE}"));
Pen blackPen = new Pen(Color.Black, 3);
CAcroRect rect;
CAcroPDPage pdPage;
int x1, y1;
const int ZOOMFACTOR = 130;
if (pageNum >= totalPages)
{
return;
}
try
{
//rectangle coordinates
rect = (CAcroRect) Activator.CreateInstance(AcrobatRect);
pdPage = (CAcroPDPage)pddoc.AcquirePage(pageNum);
Acrobat.CAcroPoint oPoint = (CAcroPoint)pdPage.GetSize();
y1 = oPoint.y*ZOOMFACTOR/100;
rect.Top = 0;
x1 = oPoint.x*ZOOMFACTOR/100;
rect.bottom = (short)y1;
rect.Left = 0;
rect.right = (short)x1;
pdPage.CopyToClipboard(rect,0,0,ZOOMFACTOR); //draw the rectangle on clipboard
IDataObject clipboardData = Clipboard.GetDataObject();
//get the data as a bitmap
Bitmap pdfBitmap = (Bitmap)clipboardData.GetData(DataFormats.Bitmap);
ev.Graphics.DrawImageUnscaled(pdfBitmap,0, 0);
pageNum++;
Marshal.ReleaseComObject(pdPage);
Marshal.ReleaseComObject(oPoint);
Marshal.ReleaseComObject(rect);
pdfBitmap.Dispose();
ev.HasMorePages = pageNum >= totalPages ? false : true;
}
catch (Exception ex)
{
}
}
I am getting the error at the following line:
Code:
Bitmap pdfBitmap = (Bitmap)clipboardData.GetData(DataFormats.Bitmap);
Thanks for any help
-
Re: Clipboard
I don't know for sure but I doubt that you can cast a PDF file as a Bitmap. The help topic for Clipboard.GetDataObject shows an example that checks for the correct data format before getting the data. I suggest you use that method and see if that is your problem.
-
Re: Clipboard
I know the code works because I am using the code in an exe. The problem with an exe is that someone has to log in for it to run. I don't understand why in the service the clipboard won't work
-
Re: Clipboard
The Clipboard class is a member of the System.Windows.Forms namespace. Can you use that namespace in a service? I don't know but the fact that a service has no interface may mean you can't. Never built a service so can't speak from experience or test for myself.