Getting Error : Unable to cast object of type ' . ' to type '

Hello,

I am using Aspose.pdf.dll version 6.1.0.0. I am getting error in this line of code.

Unable to cast object of type ' . ' to type '

Error Line

pdfMergeDocument.Pages.Insert(nPage, mPdfPage);

Part of code

Document mPdfdocument = new Document(PDFfilename);

Page mPdfPage = mPdfdocument.Pages[1];

pdfMergeDocument.Pages.Delete(pdfpage.Number);

int nPage = pdfpage.Number;

pdfMergeDocument.Pages.Insert(nPage, mPdfPage);

pdfMergeDocument.Save(OutputFileName);

Kindly help me out what is this issue.

One more thing Ver 6.1.0.0 Download is not there in your web site download.

Thanks in Advance

P. Saravanan.

ACS is a Xerox Company

saravanan.palanisamy@xerox.com

Hi Saravanan,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Well, you are using an older version of Aspose.Pdf for .NET, I would suggest you to download and try the latest version of Aspose.Pdf for .NET v7.3 as I tested this feature with my test files and it works fine. In case you still face any issue, please share your template file with us. We will check your issue and get back to you.

Regarding Aspose.Pdf for .NET v6.1.0 download, I would like to inform you that the versions released in last one year will be available for download as per company policy. As v6.1.0 was release in August 2011, it is not available for download now.

Sorry for the inconvenience,

Hi Nausherwan,

I used ver 7.3 and I am getting this following error at the license check.

The subscription included in this license allows free upgrades until 25 Apr 2012, but this version of the product was released on 28 Aug 2012. Please renew the subscription or use a previous version of the product.

Hence kindly can you send me the latest license for v7.3.

I skipped the license check.

I am getting the following error.

System.InvalidCastException was unhandled
Message="Unable to cast object of type '.' to type '.'."
Source="Aspose.Pdf"
StackTrace:
at ..‚()
at ..‡()
at Aspose.Pdf.Page.( )
at Aspose.Pdf.Page.€( )
at Aspose.Pdf.Page.get_Number()
at LearningProject.Form1.button7_Click(Object sender, EventArgs e) in E:\SaravananD on 10.237.78.54\LearningVCProjects\LearningProject\LearningProject\Form1.cs:line 436
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at LearningProject.Program.Main() in E:\SaravananD on 10.237.78.54\LearningVCProjects\LearningProject\LearningProject\Program.cs:line 18
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:

Code Part:

private void button7_Click(object sender, EventArgs e)

{

Document outputPDF = new Document(@"E:\pdftest\201208291000028.PDF");

foreach (Page pdfpage in outputPDF.Pages)

{

outputPDF.Pages.Delete(pdfpage.Number);

Document PortPDF = new Document(@"E:\pdftest\multilayer.PDF");

Page PortPage = PortPDF.Pages[1];

outputPDF.Pages.Insert(pdfpage.Number, PortPage);

outputPDF.Save(@"E:\pdftest\201208291000028.PDF");

}

}

for your reference I am attaching the PDF files

Please use this PDF files otherwise take some sample file and run the above code.

Kindly reply to my email with solution.

Thanks & Regards,

P. Saravanan.

Hi Saravanan,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thank you for sharing the sample code and template file.

I am able to reproduce your mentioned issue using your sample code and template file (actually, Page.Number property is not updating after we delete a page and save the document). I have registered the issue in our issue tracking system with issue id: PDFNEWNET-34308. Our development team will further look into this issue and share the details.

As per my understanding, you are removing the pages from first PDF document and replacing them with the first page of second PDF document. In that case, perform the insertion and deletion of all the pages and then save the document at the end (this will not cause the problem). Please see the following sample code in this regard:

Document outputPDF = new Document(@"D:\AP Data\September2012\201208291000028.PDF");

foreach (Page pdfpage in outputPDF.Pages)

{

outputPDF.Pages.Delete(pdfpage.Number);

Document PortPDF = new Document(@"D:\AP Data\September2012\\multilayer.PDF");

Page PortPage = PortPDF.Pages[1];

outputPDF.Pages.Insert(pdfpage.Number, PortPage);

}

outputPDF.Save(@"D:\AP Data\September2012\201208291000028.PDF");

If your requirement is different, you can share the details with us and we can provide you the solution accordingly.

Regarding the license issue, you will need to upgrade your license subscription. You can contact our sales team using Aspose.Purchase forum for upgrade. For testing purpose of the latest release, you may get a 30 days free temporary license. For that, please use the below link to acquire a temporary license (choose “Get a Temporary License” option).

http://www.aspose.com/purchase/default.aspx

Thank You & Best Regards,

Hi,
Still i am getting the issue with v6.1.0.0. I downloaded the v7.3.0 and I received the temp license. But this time I am getting different error in my project.
pdfMergeDocument.Save(OutputFileName); // In this line following Error
’pdfMergeDocument.LogicalStructure’ threw an exception of type ‘System.NullReferenceException’
‘pdfMergeDocument.OpenAction’ threw an exception of type 'System.NullReferenceException’
Please help me out what i will do for this error.
My Actual Requirement is

  1. I have one zip file. it contains the collection PDF files.
  2. Extract the PDF file one by one from the zip file.
  3. Open the PDF file using PDF document object. I check the PDF pages. If that page is matching with some template I need to Overlay that template Image to that PDF page. I will do overlay all the PDF pages in that PDF file.
  4. As of now I am getting this error in Particular PDF file.
    Overlay Part Code:
//Overlay Process
Document pdfMergeDocument = new Document(OutputFileName);
foreach (Page pdfpage in pdfMergeDocument.Pages)
{
//Overlay Image Merged with New OCR PDF
string Overlay = string.Empty;
string ImagePageName = sPDF.Key.ToString().Trim() + “.” + pdfpage.Number.ToString().PadLeft(3, ‘0’);
if (ImageOCRTemplates.ContainsKey(ImagePageName))
Overlay = LookupOverlay(ImageOCRTemplates[ImagePageName]);
try
{
string overlayFileName = string.Empty;
try
{
overlayFileName = Path.Combine(FOverlayPath, Overlay);
}
catch (Exception e)
{
throw new Exception("Error Combine " + FOverlayPath + Overlay + e.Message);
}
byte[] ImageData;
if (string.IsNullOrEmpty(Overlay))
{
// No Overlay
}
else
{
if (!File.Exists(overlayFileName))
throw new Exception("Overlay File not found: " + overlayFileName);
Bitmap OverlayImageBmp;
try
{
OverlayImageBmp = OpenImage(overlayFileName);
}
catch (Exception e)
{
throw new Exception("Error Openning GDI+ Images: " + e.Message);
}
Page spdfpage = pdfMergeDocument.Pages[pdfpage.Number];
MemoryStream ms = new MemoryStream();
Aspose.Pdf.Devices.Resolution resolution = new Aspose.Pdf.Devices.Resolution(300);
Aspose.Pdf.Devices.BmpDevice bmpDevice = new Aspose.Pdf.Devices.BmpDevice(OverlayImageBmp.Width, OverlayImageBmp.Height, resolution);
bmpDevice.Process(spdfpage, ms);
Bitmap DocImageBmp = new Bitmap(ms);
try
{
Bitmap MergedBMP;
try
{
MergedBMP = new Bitmap(DocImageBmp.Width, DocImageBmp.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
}
catch (Exception e)
{
throw new Exception(“Error Creating Merged GDI+ Image: " + e.Message);
}
try
{
try
{
MergedBMP.SetResolution(DocImageBmp.HorizontalResolution, DocImageBmp.VerticalResolution);
}
catch (Exception e)
{
throw new Exception(“GDI+ Error Setting Image Resolution: " + e.Message);
}
try
{
Bitmap transOverlay = new Bitmap(OverlayImageBmp.Width, OverlayImageBmp.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
Graphics g = Graphics.FromImage(transOverlay);
try
{
g.DrawImage(OverlayImageBmp, 0, 0, OverlayImageBmp.Width, OverlayImageBmp.Height);
}
finally
{
g.Dispose();
g = null;
}
OverlayImageBmp.Dispose();
OverlayImageBmp = null;
OverlayImageBmp = transOverlay;
OverlayImageBmp.MakeTransparent(System.Drawing.Color.White);
}
catch (Exception e)
{
throw new Exception(“GDI+ Error Setting Image Transparency: " + e.Message);
}
try
{
Graphics g = Graphics.FromImage(MergedBMP);
try
{
g.FillRectangle(System.Drawing.Brushes.White, 0, 0, DocImageBmp.Width, DocImageBmp.Height);
g.DrawImage(DocImageBmp, new System.Drawing.Rectangle(0, 0, DocImageBmp.Width, DocImageBmp.Height));
g.DrawImage(OverlayImageBmp, new System.Drawing.Rectangle(0, 0, DocImageBmp.Width, DocImageBmp.Height));
}
finally
{
g.Dispose();
g = null;
}
}
catch (Exception e)
{
throw new Exception(“Graphics Error: " + e.Message);
}
try
{
ImageData = SaveImage(ConvertBitmap(MergedBMP));
string BMPfilename;
BMPfilename = FPDFPath + @”\MergedBMP.BMP”;
string PDFfilename;
PDFfilename = FPDFPath + @”\MergedPDF.PDF”;
MergedBMP.Save(BMPfilename, ImageFormat.Bmp);
Aspose.Pdf.Generator.Pdf pdf1 = new Aspose.Pdf.Generator.Pdf();
pdf1.PageSetup.Margin.Bottom = 0;
pdf1.PageSetup.Margin.Left = 0;
pdf1.PageSetup.Margin.Right = 0;
pdf1.PageSetup.Margin.Top = 0;
//pdf1.PageSetup.PageHeight = 2100; //2200
//pdf1.PageSetup.PageWidth = 1700; //1700
//Create a section in the Pdf object
Aspose.Pdf.Generator.Section sec1 = pdf1.Sections.Add();
Aspose.Pdf.Generator.Image image1 = new Aspose.Pdf.Generator.Image(sec1);
sec1.Paragraphs.Add(image1);
//image1.ImageHeight = 2050;
//image1.ImageWidth = 1500;
image1.ImageInfo.File = BMPfilename;
image1.ImageInfo.ImageFileType = Aspose.Pdf.Generator.ImageFileType.Bmp;
if (File.Exists(PDFfilename))
File.Delete(PDFfilename);
pdf1.Save(PDFfilename);
pdfMergeDocument.Pages.Delete(pdfpage.Number);
Document mPdfdocument = new Document(PDFfilename);
Page mPdfPage = mPdfdocument.Pages[1];
pdfMergeDocument.Pages.Insert(pdfpage.Number, mPdfPage);
File.Delete(PDFfilename);
File.Delete(BMPfilename);
}
catch (Exception e)
{
throw new Exception("PDF Merge Error: " + e.Message);
}
}
finally
{
MergedBMP.Dispose();
MergedBMP = null;
}
}
finally
{
DocImageBmp.Dispose();
OverlayImageBmp.Dispose();
DocImageBmp = null;
OverlayImageBmp = null;
}
GC.Collect();
}
}
catch (Exception e)
{
throw new Exception("Error Adding Overlay: " + e.Message);
}
}
pdfMergeDocument.Save(OutputFileName);
pdfMergeDocument = null;
//Overlay process end

I am getting error in the RED marked line.
Thanks & Regards,
P. Saravanan.

Hi Saravanan,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thank you for the feedback.

We will further look into your issue and get back to you soon.

Sorry for the inconvenience,

Please help out on this. any update


Our Customer is waiting for the results.

It is very urgent.

Please fix this issue in v6.1.0.0. And send us the latest DLL version.


Hi Saravanan,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Sorry for a delay in response.

I tried testing your shared code, however, due to missing variables and methods, I am unable to execute the code. To test your issue, I would request you to create a sample application and post it here. We will check it and get back to you soon.

Also, I would like to inform you that any update/fix is done in the latest version of the product and as per company policy we don’t perform or provide any fix in the older versions.

Sorry for the inconvenience,

Hi Saravanan,


Thanks for your patience.

We have further investigated this issue and have found that in your code, source document is accessed after saving which causes error. Also, page number of delete page is accessed which is incorrect (page number may be obtained only when page belongs to the document).

The PageNumber property algorithm is also improved to avoid error message, but returned page number is -1. Please try using the following code snippet.

[C#]

Document outputPDF = new
Document(@“D:\pdftest\201208291000028.PDF”);<o:p></o:p>

//i is page number

for (int i = 1; i <= outputPDF.Pages.Count; i++)

{

outputPDF.Pages.Delete(i);

Document PortPDF = new Document(@"D:\pdftest\multilayer.PDF");

Page PortPage = PortPDF.Pages[1];

outputPDF.Pages.Insert(i, PortPage);

}

outputPDF.Save(@"D:\pdftest\201208291000028-out.PDF");

The issues you have found earlier (filed as PDFNEWNET-34308) have been fixed in Aspose.Pdf for .NET 7.5.0.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.