0

現在、さまざまな PDF を生成できる PDF webapp の開発を終了しました。ただし、ユーザーがアクセスするときに、携帯電話とコンピューターの両方に共通のダウンロード PDF ディレクトリが必要です。

今のところ、ローカルホストで webapp を実行しようとすると、これが PDF ファイルを保存する方法です。ただし、webappをオンラインで公開したとき、すべてのコンピューターに同様のファイルディレクトリがあるわけではありません/

var output = new FileStream(Path.Combine("C:\\Users\\apr13mpsip\\Downloads", filename), FileMode.Create);

したがって、携帯電話とコンピューターの両方に共通のファイル ディレクトリについて何かアイデアをお持ちの方はいらっしゃいませんか。

PDFボタン

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

4 に答える 4

1

さて、あなたのコメントに基づいて、これがお役に立てば幸いです。

の 2 番目のパラメーターは、iTextSharp.text.pdf.PdfWriter.GetInstance()実際には汎用System.IO.Streamオブジェクトです。これは、そのオブジェクトから継承する任意のオブジェクトを渡すことができることを意味します。いくつかのタイプがありますが、この場合に最も役立つのは、System.IO.FileStream通常使用する との 2 つですSystem.IO.MemoryStream。クラスは基本的に「メモリ内ファイル」のMemoryStreamようなものですが、幸いなことに、それについて考える必要さえありません。a を使用する場合の観点との唯一の違いは、MemoryStream閉じたときにDocumentバイトを抽出する必要があり、それは1行で実行できることです。

以下は、これを示すサンプル コードです。最初に、後で 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 と通信したくないので、どうなるかわかりません。)

于 2013-07-10T13:24:03.903 に答える
0

環境変数でこれを行うことができる場合があります。これにより、ユーザーのドキュメント (およびダウンロード) フォルダーがどこにあるかに関係なく、常に取得できるようになります。

現在のユーザーの一時フォルダーを取得する方法

C# で環境変数を取得および設定するにはどうすればよいですか?

于 2013-07-09T04:42:01.117 に答える
0

絶対パスの代わりに相対パスを使用できます。

于 2013-07-09T04:49:53.107 に答える