0

私は Windows フォーム プロジェクトに取り組んでおり、PDF ファイルを作成します。ユーザーが希望する場所にファイルを保存したい。これには SaveFileDialog を使用します。フォームの [PDF として保存] ボタンをクリックすると、このエラー コードが表示されます。

Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

SaveFileDialog を使用しない場合 (ファイルに静的な名前を付ける場合)、エラーは発生しません。

ボタンクリックのコードは次のとおりです。

    private void button2_Click(object sender, EventArgs e)
    {
        SaveFileDialog saveFileDialog1 = new SaveFileDialog();
        saveFileDialog1.InitialDirectory = Convert.ToString(Environment.SpecialFolder.MyDocuments);
        saveFileDialog1.Filter = "(*.pdf)|*.pdf|All Files (*.*)|*.*";
        saveFileDialog1.FilterIndex = 1;
        saveFileDialog1.ShowDialog();


        if (saveFileDialog1.FileName != "")
        {
            iTextSharp.text.Document pdfDosya = new iTextSharp.text.Document(PageSize.A4, 20, 20, 10, 10);
            PdfWriter.GetInstance(pdfDosya, new FileStream(saveFileDialog1.FileName, FileMode.Create));//TODO dosya ismi
            pdfDosya.Open();
        }
     }

どうすれば問題を解決できますか。

4

2 に答える 2

0

このコードを試してください

    SaveFileDialog saveFileDialog1 = new SaveFileDialog();
    saveFileDialog1.InitialDirectory =   Convert.ToString(Environment.SpecialFolder.MyDocuments);
    saveFileDialog1.Filter = "(*.pdf)|*.pdf|All Files (*.*)|*.*";
    saveFileDialog1.FilterIndex = 1;

   if(saveFileDialog1.ShowDialog() == DialogResult.OK) 
   {
        MemoryStream ms = new MemoryStream();
        iTextSharp.text.Document document = new Document(PageSize.A4, 10.0F, 10.0F,100.0F,0.0F);
        document.AddTitle("FileName.pdf");
        PdfWriter writer = PdfWriter.GetInstance(document, ms);
        document.Open();
    }
于 2013-04-18T11:38:06.567 に答える