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

和自甴很熟 提交于 2020-01-04 13:29:37

问题


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;
    }

回答1:


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



来源:https://stackoverflow.com/questions/10448694/read-a-pdf-file-and-return-as-stream-from-a-wcf-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!