Solved

Get a memorystream from files for itextsharp to merge PDF documents

Posted on 2014-09-26
9
3,217 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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
 

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

770 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