Solved

Get a memorystream from files for itextsharp to merge PDF documents

Posted on 2014-09-26
9
3,133 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
ID: 40346737
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
ID: 40347573
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
ID: 40349898
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)
ID: 40350039
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:welcome 123
ID: 40350547
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)
ID: 40351346
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
ID: 40356073
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
ID: 40356082
Excellent piece of code which works!!!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Get id from json Data with NewtonSoft 3 35
Error on Add method 1 38
ASP.NET 5 Templates 2 68
VB.Net - Project Software to help the Developer 7 18
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
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 is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

863 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

28 Experts available now in Live!

Get 1:1 Help Now