Solved

Get a memorystream from files for itextsharp to merge PDF documents

Posted on 2014-09-26
9
3,463 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

710 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