Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用iTextSharp和MVC编辑现有pdf并发送下载_C#_Asp.net Mvc_Itext - Fatal编程技术网

C# 使用iTextSharp和MVC编辑现有pdf并发送下载

C# 使用iTextSharp和MVC编辑现有pdf并发送下载,c#,asp.net-mvc,itext,C#,Asp.net Mvc,Itext,所以我一直在尝试使用iTextSharp来编辑现有的pdf文件。我已经能够使用下面的代码实现这一点 public static void AddTextToPdf(string inputPdfPath, string outputPdfPath, string name, string mID, string dj) { //variables string pathin = inputPdfPath; strin

所以我一直在尝试使用iTextSharp来编辑现有的pdf文件。我已经能够使用下面的代码实现这一点

            public static void AddTextToPdf(string inputPdfPath, string outputPdfPath, string name, string mID, string dj)
    {
        //variables
        string pathin = inputPdfPath;
        string pathout = outputPdfPath;

        //create PdfReader object to read from the existing document
        using (PdfReader reader = new PdfReader(pathin))
        //create PdfStamper object to write to get the pages from reader 
        using (PdfStamper stamper = new PdfStamper(reader, new FileStream(pathout, FileMode.Create)))
        {
            //select two pages from the original document
            reader.SelectPages("1");

            //gettins the page size in order to substract from the iTextSharp coordinates
            var pageSize = reader.GetPageSize(1);

            // PdfContentByte from stamper to add content to the pages over the original content
            PdfContentByte pbover = stamper.GetOverContent(1);

            //add content to the page using ColumnText
            Font font = new Font();
            font.Size = 45;
            font.Color = Colors.stringToBaseColor("224,90,71");

            //Do Name
            DrawText(50, 260, pageSize, pbover, name, font);

            //Do Date
            font.Size = 16;
            DrawText(200, 402, pageSize, pbover, dj, font);

            //Do mID
            DrawText(180, 422, pageSize, pbover, mID, font);
        }
    }
我试图将其集成到一个MVC应用程序中,该应用程序将发送修改后的PDF文件供下载,但我遇到了一个思维障碍。如有任何帮助,我们将不胜感激。

请排队

using (PdfStamper stamper = new PdfStamper(reader, new FileStream(pathout, FileMode.Create)))
只需将FileStream替换为MemoryStream,并在方法末尾从内存流返回字节数组:

...
using (var memoryStream = new MemoryStream())
using (var stamper = new PdfStamper(reader, memoryStream))
{
    ...
    return memoryStream.ToArray();
}

使用itextsharp get byte array,您可以在google中轻松找到它。

现在,您已将编辑后的PDF保存到磁盘中。你下一步想做什么?你所说的发送下载是什么意思?我想的是,不保存文件,而是修改方法使其返回,在该点上,我可以将其发送到mvc控制器中进行下载。这将省去我将文件保存到磁盘的麻烦。