用于在计算机和手机中保存PDF的通用文件目录

时间:2013-07-09 04:30:53

标签: c# asp.net pdf itextsharp

目前我已经完成了我的PDF webapp的开发,我可以生成各种PDF。但是,当用户访问时,我需要为我的手机和计算机提供一个通用的下载PDF目录。

至于现在,当我尝试在localhost上运行我的webapp时,这就是我保存PDF文件的方式。但是,当我在线发布我的webapp /

时,并非所有计算机都有类似的文件目录
var output = new FileStream(Path.Combine("C:\\Users\\apr13mpsip\\Downloads", filename), FileMode.Create);

因此,我想询问是否有任何人对移动电话和计算机的公共文件目录有任何想法。

PDFbutton

protected void btnPDF_Click(object sender, EventArgs e)
    {

        var doc1 = new Document();
        var filename = "MyTestPDF" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".pdf";
        var output = new FileStream(Path.Combine("C:\\Users\\apr13mpsip\\Downloads", filename), FileMode.Create);
        iTextSharp.text.pdf.PdfWriter.GetInstance(doc1, output);
        doc1.Open();

        PdfPTable table = new PdfPTable(1);
        table.TotalWidth = 585f;
        table.LockedWidth = true;

        var logo = iTextSharp.text.Image.GetInstance(Server.MapPath("~/image/logo.jpg"));
        doc1.Add(logo);

        var titleFont = FontFactory.GetFont("Arial", 18, Font.BOLD);
        doc1.Add(new Paragraph("Officer's Profile. Officer's Police ID: " + DDLCase.SelectedValue, titleFont));

        var normalFont = FontFactory.GetFont(FontFactory.HELVETICA, 14, Font.BOLD);
        var phrase = new Phrase();

        SqlConnection con = new SqlConnection("Data Source = localhost; Initial Catalog = project; Integrated Security = SSPI");

        SqlCommand cm = new SqlCommand("Select lrp.fullname,  pp.profilepic, pp.rank, lrp.policeid, lrp.nric, lrp.email, lrp.contact, lrp.address,lrp.location, pp.achievement, pp.medal1, pp.medal2, pp.medal3, pp.medal4, pp.medal5 from LoginRegisterPolice lrp, PoliceProfile pp where lrp.policeid = '" + DDLCase.SelectedValue + "' and pp.policeid = lrp.policeid", con);
        con.Open();
        SqlDataReader dr;

        dr = cm.ExecuteReader();

        if (dr.Read())
        {
            phrase.Add(new Chunk("Full Name :", normalFont));
            phrase.Add(dr[0].ToString());

            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);


            phrase.Add(new Chunk("Profile Picture :\u00a0", normalFont));
            Byte[] bytes1 = (Byte[])dr[1];
            iTextSharp.text.Image image1 = iTextSharp.text.Image.GetInstance(bytes1);
            image1.ScaleToFit(100f, 250f);
            Chunk imageChunk1 = new Chunk(image1, 0, 0);
            phrase.Add(imageChunk1);

            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);

            phrase.Add(new Chunk("Rank : ", normalFont));
            Byte[] bytes2 = (Byte[])dr[2];
            iTextSharp.text.Image image2 = iTextSharp.text.Image.GetInstance(bytes2);
            image2.ScaleToFit(40f, 300f);
            Chunk imageChunk2 = new Chunk(image2, 0, 0);
            phrase.Add(imageChunk2);

            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);

            phrase.Add(new Chunk("Police ID :", normalFont));
            phrase.Add(dr[3].ToString());

            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);

            phrase.Add(new Chunk("NRIC :", normalFont));
            phrase.Add(dr[4].ToString());

            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);

            phrase.Add(new Chunk("Email :", normalFont));
            phrase.Add(dr[5].ToString());

            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);

            phrase.Add(new Chunk("Contact :", normalFont));
            phrase.Add(dr[6].ToString());

            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);

            phrase.Add(new Chunk("Address :", normalFont));
            phrase.Add(dr[7].ToString());

            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);

            phrase.Add(new Chunk("Location :", normalFont));
            phrase.Add(dr[8].ToString());

            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);

            phrase.Add(new Chunk("Achievement :", normalFont));
            phrase.Add(dr[9].ToString());

            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);



            phrase.Add(new Chunk("Medal1", normalFont));
            Byte[] bytes3 = (Byte[])dr[10];
            iTextSharp.text.Image image3 = iTextSharp.text.Image.GetInstance(bytes3);
            image3.ScaleToFit(800f, 800f);
            Chunk imageChunk3 = new Chunk(image3, 0, 0);
            phrase.Add(imageChunk3);


            phrase.Add(new Chunk("Medal2", normalFont));
            Byte[] bytes5 = (Byte[])dr[11];
            iTextSharp.text.Image image5 = iTextSharp.text.Image.GetInstance(bytes5);
            image5.ScaleToFit(800f, 800f);
            Chunk imageChunk5 = new Chunk(image5, 0, 0);
            phrase.Add(imageChunk5);



            phrase.Add(new Chunk("Medal3", normalFont));
            Byte[] bytes6 = (Byte[])dr[12];
            iTextSharp.text.Image image6 = iTextSharp.text.Image.GetInstance(bytes6);
            image6.ScaleToFit(800f, 800f);
            Chunk imageChunk6 = new Chunk(image6, 0, 0);
            phrase.Add(imageChunk6);          

            phrase.Add(new Chunk("Medal4", normalFont));
            Byte[] bytes7 = (Byte[])dr[13];
            iTextSharp.text.Image image7 = iTextSharp.text.Image.GetInstance(bytes7);
            image7.ScaleToFit(800f, 800f);
            Chunk imageChunk7 = new Chunk(image7, 0, 0);
            phrase.Add(imageChunk7);

            phrase.Add(new Chunk("Medal5", normalFont));
            Byte[] bytes8 = (Byte[])dr[14];
            iTextSharp.text.Image image8 = iTextSharp.text.Image.GetInstance(bytes8);
            image8.ScaleToFit(800f, 800f);
            Chunk imageChunk8 = new Chunk(image8, 0, 0);
            phrase.Add(imageChunk8);

            table.AddCell(phrase);


        }

        dr.Close();
        doc1.Add(table);
        doc1.Close();
        btnPDF.Enabled = false;
    }
}

4 个答案:

答案 0 :(得分:1)

好的,根据您的评论,我希望这可以帮助您。

iTextSharp.text.pdf.PdfWriter.GetInstance()的第二个参数实际上是一个通用的System.IO.Stream对象。这意味着您可以传递从该对象继承的任何对象。有几种类型,但在这种情况下最有用的两种类型是您经常使用的System.IO.FileStreamSystem.IO.MemoryStreamMemoryStream类基本上就像一个“内存文件”,但幸运的是你甚至不必考虑这个问题。使用MemoryStream时,与您的观点的唯一区别在于,当您关闭Document时,您需要提取字节,这可以通过一个简单的行完成。

下面是显示此功能的示例代码。首先,它创建一个空的字节数组,我们稍后将存储我们的PDF。然后在创建MemoryStream而不是FileStream。然后它创建一个非常简单的PDF。完成后,它会将MemoryStream中的字节转储到我们上面创建的字节数组中。之后,iTextSharp不再参与,我们只处理ASP.Net。

(旁注:不是写入MemoryStream,而是提取字节并将其抽到浏览器中,您经常会看到人们直接写入Response.OutputStream。这是100%有效但是我建议不要这样做,因为它可能更难调试。我更喜欢先做iTextSharp工作并调试任何错误,然后处理ASP.Net和浏览器问题。)

protected void btnPDF_Click(object sender, EventArgs e) {
    //Byte array that will eventually hold our PDF, currently empty
    Byte[] bytes;

    //Instead of a FileStream we'll use a MemoryStream
    using (var MS = new System.IO.MemoryStream()) {

        //Standard PDF setup, iText doesn't care what type of stream we're using
        var doc = new iTextSharp.text.Document();
        var writer = iTextSharp.text.pdf.PdfWriter.GetInstance(doc, MS);
        doc.Open();
        doc.Add(new iTextSharp.text.Paragraph("Hello world"));
        doc.Close();

        //Grab the raw bytes from the MemoryStream
        bytes = MS.ToArray();
    }



    //At this point all iText work is done and we're only dealing with raw ASP.Net parts

    //Clear the current response buffer
    Response.Clear();
    //Instead of a normal text/html header tell the browser that we've got a PDF
    Response.ContentType = "application/pdf";
    //Tell the browser that you want the file downloaded (ideally) and give it a pretty filename
    Response.AddHeader("content-disposition", "attachment;filename=MySampleFile.pdf");
    //Write our bytes to the stream
    Response.BinaryWrite(bytes);
    //Close the stream (otherwise ASP.Net might continue to write stuff on our behalf)
    Response.End();
}

此外,这会创建用户浏览器下载的文件。但是,根据浏览器以及用户配置浏览器的方式,您可能会得到不同的结果。例如,我的计算机上的Google Chrome会立即将该文件下载到我之前设置的当前下载文件夹中。另一方面,Mozilla Firefox会提示我选择要保存文件的位置。 (由于某种原因,我的Internet Explorer今天不想和localhost交谈,所以我不确定它会做什么。)

答案 1 :(得分:0)

您可以使用环境变量执行此操作。这应该让您始终获得用户文档(和下载)文件夹,无论它位于何处。

How to get temporary folder for current user

How do I get and set Environment variables in C#?

答案 2 :(得分:0)

您可以使用 relative 路径而不是绝对路径。

答案 3 :(得分:0)

使用文件流保存到移动设备可能会有问题,我会使用localstorage,应该同时使用PC和移动设备。首先要做的事情:http://msdn.microsoft.com/en-us/hh562072.aspx,否则只需搜索localstorage和webapp