We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

InvalidCastException - Aspose.Pdf.Kit.PdfFileSecurity.EncryptFile()

Unable to cast object of type ‘xeb116a323308e2f7.x081daeabc28689ee’ to type ‘xeb116a323308e2f7.x2918fb3cf7f48bc7’.

We are currently using Aspose.Pdf.Kit 4.3.0.2 with the following code

var security = new Aspose.Pdf.Kit.PdfFileSecurity(file, destFile);

Aspose.Pdf.Kit.DocumentPrivilege privilege = Aspose.Pdf.Kit.DocumentPrivilege.ForbidAll;
privilege.AllowCopy = true;
privilege.AllowPrint = true;
privilege.AllowFillIn = true;
privilege.AllowScreenReaders = true;
privilege.AllowModifyContents = false;
privilege.AllowModifyAnnotations = true;

bool encrypt = security.EncryptFile("", “ownerpassword”, privilege, false);


We are in the process of transitioning to Aspose.Pdf for .NET 8.0 in our production environment, and this exception does not occur with Aspose.Pdf 8.0, but we need a root cause of why this is failing with Aspose.Pdf.Kit 4.3.0.2 before we can move forward.

I have found this support article:
<!–[if gte mso 9]>
<w:WordDocument>
<w:View>Normal</w:View>
<w:Zoom>0</w:Zoom>
<w:TrackMoves/>
<w:TrackFormatting/>
<w:PunctuationKerning/>
<w:ValidateAgainstSchemas/>
<w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid>
<w:IgnoreMixedContent>false</w:IgnoreMixedContent>
<w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText>
<w:DoNotPromoteQF/>
<w:LidThemeOther>EN-US</w:LidThemeOther>
<w:LidThemeAsian>X-NONE</w:LidThemeAsian>
<w:LidThemeComplexScript>X-NONE</w:LidThemeComplexScript>
<w:Compatibility>
<w:BreakWrappedTables/>
<w:SnapToGridInCell/>
<w:WrapTextWithPunct/>
<w:UseAsianBreakRules/>
<w:DontGrowAutofit/>
<w:SplitPgBreakAndParaMark/>
<w:DontVertAlignCellWithSp/>
<w:DontBreakConstrainedForcedTables/>
<w:DontVertAlignInTxbx/>
<w:Word11KerningPairs/>
<w:CachedColBalance/>
</w:Compatibility>
<w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel>
<m:mathPr>
<m:mathFont m:val=“Cambria Math”/>
<m:brkBin m:val=“before”/>
<m:brkBinSub m:val="–"/>
<m:smallFrac m:val=“off”/>
<m:dispDef/>
<m:lMargin m:val=“0”/>
<m:rMargin m:val=“0”/>
<m:defJc m:val=“centerGroup”/>
<m:wrapIndent m:val=“1440”/>
<m:intLim m:val=“subSup”/>
<m:naryLim m:val=“undOvr”/>
</m:mathPr></w:WordDocument>
<![endif]–><span style=“font-size:11.0pt;font-family:“Calibri”,“sans-serif”;
mso-fareast-font-family:Calibri;mso-fareast-theme-font:minor-latin;mso-ansi-language:
EN-US;mso-fareast-language:EN-US;mso-bidi-language:AR-SA”>https://forum.aspose.com/t/120601<!–[if gte mso 10]>

/* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-priority:99; mso-style-qformat:yes; mso-style-parent:""; mso-padding-alt:0in 5.4pt 0in 5.4pt; mso-para-margin:0in; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:11.0pt; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-fareast-font-family:"Times New Roman"; mso-fareast-theme-font:minor-fareast; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:minor-bidi;}

<![endif]–>, so I know that this has already been fixed. I am looking specifically for WHY this is breaking.

I have attached a few pdf files that are causing the issue.
Thank you!

We have resolved the issue by changing the Footer’s Left Margin from ‘1’ to ‘0.5’ to match the rest of the footer margin widths.


Although we have found a resolution, we would still like to know whether or not we have found the issue (i.e. Aspose PDF Kit 4.3.0.2 cannot handle custom footer margins), or have we just patched a larger issue with the PDF’s that I submitted.

Thank you

Hi Cody,


Thanks for contacting support.

I have tested the scenario with latest Aspose.Pdf for .NET 8.3.0 while using the following code snippet and I am unable to notice any issue. Please try using the latest release version as I am afraid we might not be able to fix the problem in earlier release versions of disconnected product. The version which you are using is quite old and till the latest release, we have made many improvements and enhancements in our products so I would suggest you to please try using the latest release. In case you still encounter similar issue or you have any further query, please feel free to contact.

We are sorry for this inconvenience.

[C#]

var security = new
Aspose.Pdf.Facades.PdfFileSecurity();<o:p></o:p>

// bind the source PDF file

security.BindPdf("c:/pdftest/WCL-100-OH.pdf");

Aspose.Pdf.Facades.DocumentPrivilege privilege = Aspose.Pdf.Facades.DocumentPrivilege.ForbidAll;

privilege.AllowCopy = true;

privilege.AllowPrint = true;

privilege.AllowFillIn = true;

privilege.AllowScreenReaders = true;

privilege.AllowModifyContents = false;

privilege.AllowModifyAnnotations = true;

// apply encryption

bool encrypt = security.EncryptFile("test", "ownerpassword", privilege, KeySize.x256);

// save the updated PDF file

security.Save("c:/pdftest/WCL-100-OH_updated.pdf");