开发者

ItextSharp Expected a Dict Object When trying to print

开发者 https://www.devze.com 2023-02-07 21:16 出处:网络
I have a web page that allows a user to view a pdf and print pdf. The print pdf is a copy of the display pdf and i am using ItextSharp to inject the javascript to allow auto printing. I have a method

I have a web page that allows a user to view a pdf and print pdf. The print pdf is a copy of the display pdf and i am using ItextSharp to inject the javascript to allow auto printing. I have a method that allows a user to upload a pdf and it calls this method below to copy the display copy into a pdf. Both pdf's are then saved in the database. However , when a user goes to click on the print button on my web page they receive the following error "expected开发者_StackOverflow中文版 a dict object". below is my code that adds in the auto print, which works fine for me but not on my clients site.

I am doing anything wrong that could be corrupting the file. The original pdf content is passed in as a Binary Object.

Any help on this is much appreciated as i am highly confused on this one. Also i am using ASP.NET MVC2.

MemoryStream originalPdf = new MemoryStream(Content.BinaryData);    
MemoryStream updatedPdf = new MemoryStream();  
updatedPdf.Write(Content.BinaryData,0, Content.BinaryData.Length);  
PdfReader pdfReader = new PdfReader(originalPdf);  
PdfStamper pdfStamper = new PdfStamper(pdfReader, updatedPdf);    

if (autoPrinting)
{
    pdfStamper.JavaScript = "this.print(true);\r";
}
else
{
    pdfStamper.JavaScript = null;
}

pdfStamper.Close();
pdfReader.Close();

Content.BinaryData = updatedPdf.ToArray();


Don't write the original PDF to your output. pdfStamper.close() will do all the writing for you, even in append mode (which you're not using).

Your code should read:

MemoryStream originalPdf = new MemoryStream(Content.BinaryData);
MemoryStream updatedPdf = new MemoryStream();

// Don't do that.
//updatedPdf.Write(Content.BinaryData,0, Content.BinaryData.Length);

PdfReader pdfReader = new PdfReader(originalPdf);
PdfStamper pdfStamper = new PdfStamper(pdfReader, updatedPdf); 

if (autoPrinting) {
  pdfStamper.JavaScript = "this.print(true);\r";
} else {
  pdfStamper.JavaScript = null;
}

pdfStamper.Close(); // this does it for you.
pdfReader.Close();

Content.BinaryData = updatedPdf.ToArray();

I'm surprised that this "works for you". If nothing else, I'd expect the JS to fail because the byte offsets would be all wrong... in fact, all your offsets would be all wrong. I think my ignorance of C# is showing.

But Write() behaves the way I thought it would, so I'm back to being surprised.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号