【问题标题】:Read a PDF file and return as stream from a WCF service?读取 PDF 文件并从 WCF 服务作为流返回?
【发布时间】:2012-05-04 12:18:22
【问题描述】:

我想创建一个 WCF 服务(像 windows 服务一样工作)。该服务将从特定路径读取 PDF 文件,提取页面,创建新的 PDF 文件并将其返回给调用者。

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

等待您的帮助...

这只是示例代码:

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

【问题讨论】:

    标签: c# wcf web-services stream


    【解决方案1】:

    您可以将文件作为Stream 发送,参见How to: Enable Streaming,然后在客户端保存文件并让shell 执行它。 MSDN 文章包括一个示例GetStream 方法以及关于Writing a custom stream 的整个部分。

    如果您想要更完整示例代码,论坛帖子Streamed file transfer using WCF 以一些开头,但是请注意,作者将其发布在那里是因为他们在运行它时遇到了问题。

    关于 byte[] 或流,请参见 Uploading Image Blobs–Stream vs Byte ArrayStream vs Raw Bytes。第二种状态

    流对于大文件的性能会更好,因为不需要一次将所有文件都读入内存

    【讨论】:

    • 我添加了一个新的代码示例。返回 pdf 文件的最佳方式是什么?流或字节[],为什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-05
    • 1970-01-01
    • 2013-12-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多