Solved

Get a memorystream from files for itextsharp to merge PDF documents

Posted on 2014-09-26
9
2,991 Views
Last Modified: 2014-10-01
I have to merge PDF documents using Itextsharp and I get an error value of one dimentional array of system.io.memorystream cannot be converted to ststem.io.memory when I try to convert the files to memory stream in the process below is the code:
Public Function CreateMergedPDFs(ByVal targetPDF As String, ByVal sourceDir As String, ByVal DocumentNumber As String, ByVal doc_nums As System.Text.StringBuilder) As Boolean
        Dim destination As String = String.Empty
         Dim files As MemoryStream()
   
        If DocumentNumber <> 0 Then
            destination = GetDirectory(DocumentNumber, 0, "PDF")
            ProcessAgentLogWrite(destination)
            If destination <> "" Then
                Using stream As New FileStream(destination, FileMode.Open)
                    ProcessAgentLogWrite("Using stream As New FileStream(destination, FileMode.Open)")
                    
                    Using pdfdoc As New Document(PageSize.A4)
                        Dim pdf As New PdfCopy(pdfdoc, stream)
                        
                        pdfdoc.Open()
                   
                        'loop the docu_nums to create memory streams
                        For Each doc As String In doc_nums.ToString.Split(",")
                           files = {SavePDFFileToMemoryStream(doc)}
			   'this now gives empty stream saying cannot access a closed stream 	
                        Next

                                           
                        Dim i As Integer = 1

                        For Each file As MemoryStream In files
			'file is used before assigned a value warming	
                            'Reset the position back to zero
                                            
                            file.Position = 0
                            pdf.AddDocument(New PdfReader(file))
                            
                            i += 1
                            file.Dispose()
                        Next
                        pdfdoc.Close()
                       
                    End Using
                    'If pdfdoc IsNot Nothing Then

                    If targetPDF <> "" Then
                        
                        RecreateNewPDF(destination, targetPDF)
                        'my own function to recreate can be ignored now
                    Else
                        Return False
                    End If
                   
                End Using
            End If
        End If
        Return True
    End Function

Open in new window

0
Comment
Question by:welcome 123
  • 5
  • 3
9 Comments
 

Author Comment

by:welcome 123
Comment Utility
all I need to merge pdfs using itextsharp or any open source in vb.net which works
0
 
LVL 32

Accepted Solution

by:
Robberbaron (robr) earned 500 total points
Comment Utility
I use PDFSharp, but it has an issue with some particular PDF's that need to use iTextSharp for edge case.

but this is the code that gets used to create the master page.
        public static bool ProcessFiles(ArrayList myFiles,bool sortList)
        {
            ArrayList errList = new ArrayList();

            if (sortList)
            {
                myFiles.Sort();
            }
            //create output file
            PdfDocument outPDF = new PdfDocument();
            PdfPage page;



            if (myFiles.Count == 0)
            { return false; }

            //add each pdf file
            foreach (string x in myFiles)
            {
                FileInfo fi = new FileInfo(x);
                if (fi.Exists)
                {
                    PdfDocument inputPDF = CompatiblePdfReader.Open(x, PdfDocumentOpenMode.Import);
                    int count = inputPDF.PageCount;
                    for (int idx = 0; idx < count; idx++)
                    {
                        PdfPage pgImport = inputPDF.Pages[idx];
                        page = outPDF.AddPage(pgImport);
                        // Create a sub bookmark
                    }
                }
                else { errList.Add("File not found:" + x); }

            }


            string filePath = Path.GetTempPath();
            string fileName = string.Format("{0}_{1:yyyyMMddHHmmssff}.PDF",Prefix,DateTime.Now);
            string tmpPDF = filePath + @"\" + fileName;

            outPDF.Save(tmpPDF);
        
            return true;
        }    

Open in new window


using System;
using System.IO;

namespace PdfSharp.Pdf.IO
{
    /// <summary>
    /// uses itextsharp 4.1.6 to convert any pdf to 1.4 compatible pdf, called instead of PdfReader.open
    /// </summary>

    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>
        static public PdfDocument Open(string PdfPath, PdfDocumentOpenMode openmode)
        {
            using (FileStream fileStream = new FileStream(PdfPath, FileMode.Open, FileAccess.Read))
            {
                int len = (int)fileStream.Length;
                Byte[] fileArray = new Byte[len];
                fileStream.Read(fileArray, 0, len);
                fileStream.Close();

                return Open(fileArray, openmode);
            }
        }

        /// <summary>
        /// uses itextsharp 4.1.6 to convert any pdf to 1.4 compatible pdf, called instead of PdfReader.open
        /// </summary>
        static public PdfDocument Open(byte[] fileArray, PdfDocumentOpenMode openmode)
        {
            return Open(new MemoryStream(fileArray), openmode);
        }

        /// <summary>
        /// uses itextsharp 4.1.6 to convert any pdf to 1.4 compatible pdf, called instead of PdfReader.open
        /// </summary>
        static public PdfDocument Open(MemoryStream sourceStream, PdfDocumentOpenMode openmode)
        {
            PdfDocument outDoc = null;
            sourceStream.Position = 0;

            try
            {
                outDoc = PdfReader.Open(sourceStream, openmode);
            }
            catch (PdfSharp.Pdf.IO.PdfReaderException)
            {
                //workaround if pdfsharp doesn't support this pdf
                sourceStream.Position = 0;
                MemoryStream outputStream = new MemoryStream();
                iTextSharp.text.pdf.PdfReader reader = new iTextSharp.text.pdf.PdfReader(sourceStream);
                iTextSharp.text.pdf.PdfStamper pdfStamper = new iTextSharp.text.pdf.PdfStamper(reader, outputStream);
                pdfStamper.FormFlattening = true;
                pdfStamper.Writer.SetPdfVersion(iTextSharp.text.pdf.PdfWriter.PDF_VERSION_1_4);
                pdfStamper.Writer.CloseStream = false;
                pdfStamper.Close();

                outDoc = PdfReader.Open(outputStream, openmode);
            }

            return outDoc;
        }
    }
}

Open in new window

0
 

Author Comment

by:welcome 123
Comment Utility
what are the classes to import ? there is a conflict in PdfDocumentOpenMode and PdfDocument shall I import Itextsharp.pdf.text ? can you give me the list of imports
0
 
LVL 32

Expert Comment

by:Robberbaron (robr)
Comment Utility
dont believe so,,,
references in my c# project

itextsharp.dll   v4.1.6
PdfSharp.dll     v1.32.2608

the CompatibleReader is an extension to PDFSharp, and only call iTextSharp when needed, (on failure of PDFDocument.Open stream
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:welcome 123
Comment Utility
I keep getting the error Unable to cast object of type 'PdfSharp.Pdf.PdfNull' to type 'PdfSharp.Pdf.Advanced.PdfReference on certain pages of which ever pdf file I use how can I avoid this?
0
 
LVL 32

Expert Comment

by:Robberbaron (robr)
Comment Utility
sorry dont get any errors....

but there are some imports at the top of my main class def that i didnt show earlier.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using PdfSharp;
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;
using System.IO;

//version 2. added sorted output by default
namespace PDFBatch
{
    static class ProcessPDF
        
    {
        const string Prefix = "PDFBatch"; 

        public static bool ProcessFiles(ArrayList myFiles)
        {
            return ProcessFiles(myFiles, true);
        }

        public static bool ProcessFiles(ArrayList myFiles,bool sortList)

Open in new window


note that I dont import the itextsharp definitions.
0
 

Author Comment

by:welcome 123
Comment Utility
it works and it works great, I just modified a little bit like I use itextsharp fisrt and if fails in exception use PDFsharp.

Thanks for the great code, I really appreciate your help and your excellent logic which works. I wish I can double the points to 1000. Great Expert!
0
 

Author Closing Comment

by:welcome 123
Comment Utility
Excellent piece of code which works!!!
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now