Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Get a memorystream from files for itextsharp to merge PDF documents

Posted on 2014-09-26
9
Medium Priority
?
3,959 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 2000 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

963 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