读取 PDF 文件并作为流从 WCF 服务返回? [英] Read a PDF file and return as stream from a WCF service?

查看:29
本文介绍了读取 PDF 文件并作为流从 WCF 服务返回?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个 WCF 服务(像 Windows 服务一样工作).此服务将从特定路径读取 PDF 文件、提取页面、创建新的 PDF 文件并将其返回给调用方.

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.

我该怎么做?我使用 QuickPDF 处理 PDF 文件,我可以提取和创建新的 PDF 文件.如何在 WCF 服务中使用它?

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 ?

等待您的帮助...

这只是示例代码:

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

我编辑了它:

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

推荐答案

您可以将文件作为 Stream 发送,请参阅 如何:启用流式传输,然后在客户端保存文件并让 shell 执行它.MSDN 文章包括一个示例 GetStream 方法以及关于编写自定义流的整个部分.

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.

如果您想要完整示例代码,请查看论坛帖子使用 WCF 的流式文件传输 以一些开头,但是请注意,作者将其发布在那里,因为他们在运行时遇到了问题.

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.

关于字节 [] 或流,请参阅 上传图像 Blob——流 vs 字节数组流与原始字节.第二种状态

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

这篇关于读取 PDF 文件并作为流从 WCF 服务返回?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆