Mennan
Mennan

Reputation: 4497

Read a PDF file and return as stream from a WCF service?

I want to create a WCF service (working like windows service). This service will read a PDF file from a specific path, extract pages, create a new PDF file and return it to the caller.

How can I do this ? I use QuickPDF to process on PDF files, I can extract and create new PDF file. How can use this in a WCF service ?

Waiting your helps...

This is only sample code :

public Stream ExtractPdf(string PathOfOriginalPdfFile, int StartPage,int PageCount)
{
        PDFLibrary qp = new PDFLibrary();
        Stream Stream_ = null;

        if (qp.UnlockKey(".................") == 0)
        {
            string fileName = @"..\..\Test Files\sample1.pdf";
            string OutputFile = @"..\..\Test Files\sample1_extracted.pdf";

            if (qp.Unlocked() == 1)
            {

                int docID = qp.LoadFromFile(fileName, "");

                int extractPageSuccess = qp.ExtractPages(StartPage, PageCount);

                if (extractPageSuccess == 0)
                {
                    // error
                }
                else
                {
                    qp.SaveToFile(OutputFile);
                }
            }
        }

        //
        // Codes here
        //
        return Stream_;
    }

I edited it :

 public byte[] ExtractPdf(string PathOfOriginalPdfFile, int StartPage,int PageCount)
    {

        QuickPDFDLL0815.PDFLibrary qp = new QuickPDFDLL0815.PDFLibrary(@"C:\Program Files (x86)\Quick PDF Library\DLL\QuickPDFDLL0815.dll");

        string fileName = @"..\..\Test Files\sample1.pdf";
        byte[] binFile = null;

        if (qp.UnlockKey("...................") == 0)
        {


            if (qp.Unlocked() == 1)
            {

                int docID = qp.LoadFromFile(fileName, "");

                int extractPageSuccess = qp.ExtractPages(StartPage, PageCount);

                if (extractPageSuccess == 0)
                {
                    // error
                }
                else
                {
                   binFile = qp.SaveToString();
                }
            }
        }

        return binFile;
    }

Upvotes: 2

Views: 8221

Answers (1)

Joshua Drake
Joshua Drake

Reputation: 2746

You could send the file as a Stream, see How to: Enable Streaming, then on the client save off the file and have the shell execute it. The MSDN article includes a sample GetStream method as well as a whole section on Writing a custom stream.

If you would like fuller sample code the forum post Streamed file transfer using WCF starts with some, however, note that the author posted it there because they were encountering issues running it.

As to byte[] or stream see Uploading Image Blobs–Stream vs Byte Array and Stream vs Raw Bytes. The second states

Streams will perform better for large files since not all of it needs to be read into memory at one time

Upvotes: 3

Related Questions