PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Wed Jan 29, 2020 11:46 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 54 posts ]  Go to page Previous  1, 2
Author Message
PostPosted: Tue Jan 20, 2015 10:58 am 
Offline

Joined: Tue Jan 20, 2015 10:46 am
Posts: 1
Hi to everyone!
For password issue, I've little changed the solution posted by @greenfire27 and reviewed by @dunwan (That works like a charm! :shock: Really. You saved my day. :| ) starting from the @dunwan one.

This is my change:

Code:
static public PdfDocument Open(MemoryStream sourceStream, string password, PdfDocumentOpenMode openmode)
{
  ........
  try
  {
    outDoc = (password == null) ?
      PdfReader.Open(sourceStream, openmode) :
      PdfReader.Open(sourceStream, password, openmode);
  }
  catch (PdfSharp.Pdf.IO.PdfReaderException)
  {
    .....
    iTextSharp.text.pdf.PdfReader reader = (password == null) ?
      new iTextSharp.text.pdf.PdfReader(sourceStream) :
      new iTextSharp.text.pdf.PdfReader(sourceStream, System.Text.ASCIIEncoding.ASCII.GetBytes(password));
    .....
  }
  .....
}


Seems work fine!!
I hope that this can be useful for someone.

Daniele


Top
 Profile  
Reply with quote  
PostPosted: Mon Dec 28, 2015 6:06 am 
Offline

Joined: Mon Dec 28, 2015 6:04 am
Posts: 8
rasepretrep wrote:
First of all, let me say that i really love the pdfsharp library. I use it extensively in my current project, which involves merging pdf files and
adding page counters etc. to it.

Unfortunately I have no control over the pdf files that are to be merged, so I sometimes got the "iref stream" error message. At the same time I really want to stick with pdfsharp because it is imho a pdf api with a very nice and clear api.

So I developed an (ugly) workaround which I post here to give back something to this community.
the idea is simple: If pdfsharp fails to import a pdf file, use Itextsharp to open the document and write a compatible pdf, which can then be imported again with pdfsharp.
Here is the code :

Code:
 static public class CompatiblePdfReader
    {

        /// <summary>
        /// uses itextsharp 4.1.6 to convert any pdf to 1.4 compatible pdf, called instead of PdfReader.open
        /// </summary>
        /// <param name="sFilename"></param>
        /// <returns></returns>
        static public PdfDocument Open(string sFilename)
        {

            PdfDocument reader = new PdfDocument();
           
            try {
                reader = PdfReader.Open(sFilename, PdfDocumentOpenMode.Import);
            }
            catch (PdfSharp.Pdf.IO.PdfReaderException) {
                //workaround if pdfsharp doesnt dupport this pdf
                string newName = WriteCompatiblePdf(sFilename);
                reader = PdfReader.Open(newName, PdfDocumentOpenMode.Import);
            }

            return reader;
        }


        /// <summary>
        /// uses itextsharp 4.1.6 to convert any pdf to 1.4 compatible pdf
        /// </summary>
        /// <param name="sFilename"></param>
        /// <returns></returns>
        static private string WriteCompatiblePdf(string sFilename)
        {
            string sNewPdf = Globals.TempPath + Guid.NewGuid().ToString() + ".pdf";

            iTextSharp.text.pdf.PdfReader reader = new iTextSharp.text.pdf.PdfReader(sFilename);

            // we retrieve the total number of pages
            int n = reader.NumberOfPages;
            // step 1: creation of a document-object
            iTextSharp.text.Document document = new iTextSharp.text.Document(reader.GetPageSizeWithRotation(1));
            // step 2: we create a writer that listens to the document
            iTextSharp.text.pdf.PdfWriter writer = iTextSharp.text.pdf.PdfWriter.GetInstance(document, new FileStream(sNewPdf, FileMode.Create));
            //write pdf that pdfsharp can understand
            writer.SetPdfVersion(iTextSharp.text.pdf.PdfWriter.PDF_VERSION_1_4);
            // step 3: we open the document
            document.Open();
            iTextSharp.text.pdf.PdfContentByte cb = writer.DirectContent;
            iTextSharp.text.pdf.PdfImportedPage page;

            int rotation;

            int i = 0;
            while (i < n)
            {
                i++;
                document.SetPageSize(reader.GetPageSizeWithRotation(i));
                document.NewPage();
                page = writer.GetImportedPage(reader, i);
                rotation = reader.GetPageRotation(i);
                if (rotation == 90 || rotation == 270)
                {
                    cb.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(i).Height);
                }
                else
                {
                    cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
                }
            }
            // step 5: we close the document
            document.Close();
            return sNewPdf;
        }
    }


You will have to modify the code a bit to make it work (this is jsut a "copy and patse" from my current project).
I chose itextsharp 4.1.6 because version 5 had problems with some pdfs in my "testsuite".


Top
 Profile  
Reply with quote  
PostPosted: Mon Dec 28, 2015 6:25 am 
Offline

Joined: Mon Dec 28, 2015 6:04 am
Posts: 8
You have handled "Cannot handle iref stream" issue,Using ITextSharp assembly If moving product to commercial then i required license of the ITextSharp?


Top
 Profile  
Reply with quote  
PostPosted: Mon Dec 28, 2015 9:05 am 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 517
Location: CCAA
rajarul wrote:
You have handled "Cannot handle iref stream" issue,Using ITextSharp
Try PDFsharp 1.50. It can handle IREF streams and the workaround with ITextSharp should be obsolete now.

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 54 posts ]  Go to page Previous  1, 2

All times are UTC


Who is online

Users browsing this forum: No registered users and 4 guests


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:  
cron
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group