PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Fri May 25, 2018 12:26 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Wed Aug 20, 2014 9:47 am 
Offline

Joined: Fri Jan 25, 2013 7:20 am
Posts: 9
Hi,

we got an external PDF File (v. 1.4) that we want to include into out PdfDocument.
We're using your sample code of TwoPagesOnOneon http://www.pdfsharp.net/wiki/TwoPagesOnOne-sample.ashx

But at the line
Code:
gfx.DrawImage(form, box);
it always fails with the SharpZipBaseException: Adler checksum doesn't match

With other pdf files it works correct.

After lot of research I still can't find a solution to it. Maybe the file was compressed (how to check that?) different. Also we tested to open the file by AdobeAcrobat to check if it contains errors but after that the exception still occures

I attached the pdfFile also


Attachments:
File comment: contains the pdf file
tablePDF.zip [18.8 KiB]
Downloaded 131 times
Top
 Profile  
Reply with quote  
PostPosted: Mon Aug 25, 2014 8:10 am 
Offline

Joined: Fri Jan 25, 2013 7:20 am
Posts: 9
Update:
We are using components of infragistics that creates the external pdf files. With all pdf's that they generate we got the same problem.
I contacted them and got the folling answer of them:
The Infragistics documents engine is using the Flate compression functionality in DotNet. The DotNet algorithm is apparently incompatible with the Flate compression used by Java. I'm not sure why, but they simply don't work together. So if compression is the issue, my guess is that PDFSharp is using the Java compressor. I could be wrong, of course, but you could probably check with the makers of PDF Sharp to confirm.
So..exception is always thrown by the Inflater.cs that Adler32 checksum isn' valid. I think that is the case?

Is it possible to implement a second decompression by .NET's algorithm if your first java algorithm failed?


Top
 Profile  
Reply with quote  
PostPosted: Tue Aug 26, 2014 11:58 am 
Offline

Joined: Fri Jan 25, 2013 7:20 am
Posts: 9
Damn, a lot of try and error later I found the solution in using itextSharp also!
Only working with PDFSharp doesn't worked. Tried also the same approach I did now with iTextsharp by using pdfSharp but got no result.

So for all other people facing a same problem, here is how I do it now ;)

Code:
/// <summary>
      /// Creates the PDF sharp conform PDF file from infragistics PDF file.
      /// THIS IS ONLY A MUST NEED BECAUSE THE PDF OF INFRAGISTICS SAVES ITS CONTENT COMPRESSED BY .NET ALGORITHM.
      /// BUT PDFSHARP USES THE JAVA COMPRESSION/DECOMPRESSION ALGORITHM TO READ IT IN -> THIS FAILS WITH AN DECOMPRESSION EXCEPTION!
      /// So we read in the pdf by iTextshap and generate a new pdf by it with uncompressed! content. This file can be used by PDFSharp also!
      /// </summary>
      /// <param name="infragisticsPDF">The infragistics PDF.</param>
      /// <returns>System.String.</returns>
      private string createPDFSharpConformPDFFileFromInfragisticsPDFFile(string infragisticsPDF)
      {
         //readin file and create a new doc for it
         iTextSharp.text.pdf.PdfReader inputReader = new iTextSharp.text.pdf.PdfReader(infragisticsPDF);
         iTextSharp.text.Rectangle newRect = inputReader.GetPageSize(1);
         iTextSharp.text.Document doc = new iTextSharp.text.Document(newRect);
         doc.SetMargins(0, 0, 0, 0);
         iTextSharp.text.Document.Compress = false; //THIS LINE IS IMPORTANT -> RESAVE FILE AS UNCOMPRESSED VERSION!
         //set ouput file
         var correctedFilePath = Path.Combine(_reportObjectSettings.TemporaryFilePathForSinglePdfFiles, Path.GetFileNameWithoutExtension(infragisticsPDF) + "_uncompressed.pdf");
         iTextSharp.text.pdf.PdfWriter outputWriter = iTextSharp.text.pdf.PdfWriter.GetInstance(doc, new FileStream(correctedFilePath, FileMode.Create));
         outputWriter.SetPdfVersion(iTextSharp.text.pdf.PdfWriter.PDF_VERSION_1_4); //only to get sure!
         doc.Open();
         //now write/copy each page of it into new doc
         iTextSharp.text.pdf.PdfContentByte cb = outputWriter.DirectContent;
         for (int pageNumber = 1 ; pageNumber <= inputReader.NumberOfPages ; pageNumber++)
         {
            iTextSharp.text.pdf.PdfImportedPage page = outputWriter.GetImportedPage(inputReader, pageNumber);
            cb.AddTemplate(page, 0, 0);
            doc.NewPage();
         }
         doc.Close();
         doc = null;
         return correctedFilePath;
      }


Top
 Profile  
Reply with quote  
PostPosted: Fri Feb 23, 2018 10:30 am 
Offline

Joined: Wed Feb 14, 2018 10:28 am
Posts: 8
wasers wrote:
Damn, a lot of try and error later I found the solution in using itextSharp also!
Only working with PDFSharp doesn't worked. Tried also the same approach I did now with iTextsharp by using pdfSharp but got no result.

So for all other people facing a same problem, here is how I do it now ;)

Code:
/// <summary>
      /// Creates the PDF sharp conform PDF file from infragistics PDF file.
      /// THIS IS ONLY A MUST NEED BECAUSE THE PDF OF INFRAGISTICS SAVES ITS CONTENT COMPRESSED BY .NET ALGORITHM.
      /// BUT PDFSHARP USES THE JAVA COMPRESSION/DECOMPRESSION ALGORITHM TO READ IT IN -> THIS FAILS WITH AN DECOMPRESSION EXCEPTION!
      /// So we read in the pdf by iTextshap and generate a new pdf by it with uncompressed! content. This file can be used by PDFSharp also!
      /// </summary>
      /// <param name="infragisticsPDF">The infragistics PDF.</param>
      /// <returns>System.String.</returns>
      private string createPDFSharpConformPDFFileFromInfragisticsPDFFile(string infragisticsPDF)
      {
         //readin file and create a new doc for it
         iTextSharp.text.pdf.PdfReader inputReader = new iTextSharp.text.pdf.PdfReader(infragisticsPDF);
         iTextSharp.text.Rectangle newRect = inputReader.GetPageSize(1);
         iTextSharp.text.Document doc = new iTextSharp.text.Document(newRect);
         doc.SetMargins(0, 0, 0, 0);
         iTextSharp.text.Document.Compress = false; //THIS LINE IS IMPORTANT -> RESAVE FILE AS UNCOMPRESSED VERSION!
         //set ouput file
         var correctedFilePath = Path.Combine(_reportObjectSettings.TemporaryFilePathForSinglePdfFiles, Path.GetFileNameWithoutExtension(infragisticsPDF) + "_uncompressed.pdf");
         iTextSharp.text.pdf.PdfWriter outputWriter = iTextSharp.text.pdf.PdfWriter.GetInstance(doc, new FileStream(correctedFilePath, FileMode.Create));
         outputWriter.SetPdfVersion(iTextSharp.text.pdf.PdfWriter.PDF_VERSION_1_4); //only to get sure!
         doc.Open();
         //now write/copy each page of it into new doc
         iTextSharp.text.pdf.PdfContentByte cb = outputWriter.DirectContent;
         for (int pageNumber = 1 ; pageNumber <= inputReader.NumberOfPages ; pageNumber++)
         {
            iTextSharp.text.pdf.PdfImportedPage page = outputWriter.GetImportedPage(inputReader, pageNumber);
            cb.AddTemplate(page, 0, 0);
            doc.NewPage();
         }
         doc.Close();
         doc = null;
         return correctedFilePath;
      }


Nice job, really appreciated finding this. Have rare yet persistent issues with PDF Sharp (1.50.4790-beta5a) not finding the page count, null reference exceptions, and Adler checksums when opening some PDFs. Suspect the issues are caused by the 3rd party applications used to create the PDF. Opening in iTextSharp, saving, and then re opening in PDF sharp works like a charm. Doing the work i need to do on the PDF with PDF Sharp is then possible. It's not ideal, but a great workaround for the moment for me.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group