简体   繁体   English

使用FileStream从Adobe PDF阅读器保存所选PDF时如何解决我的错误?

[英]How to solve my error when using FileStream to save selected PDF from Adobe PDF reader?

I am using Adobe PDF Reader to view scanned paper from my computer and try to save it to database in my windows form application. 我正在使用Adobe PDF Reader从计算机上查看扫描的纸张,并尝试将其保存到Windows窗体应用程序中的数据库中。

I select the PDF file by using open file dialog , Then I need to save it in my database. 我使用打开文件对话框选择PDF文件,然后将其保存在数据库中。 I am using the following code: 我正在使用以下代码:

private void btnPDF_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                PDFViewer.src = ofd.FileName;
            }
        }
private void btnsave_Click(object sender, EventArgs e)
        {
            MemoryStream msins = new MemoryStream();
            MemoryStream msid = new MemoryStream();
            pictureInsurance.Image.Save(msins, pictureInsurance.Image.RawFormat);
            pictureIDIQAMA.Image.Save(msid, pictureIDIQAMA.Image.RawFormat);
            byte[] byteInsurance = msins.ToArray();
            byte[] byteId = msid.ToArray();
            FileStream fStream = File.OpenRead("C:\\myfile.pdf");
            byte[] Doccontents = new byte[fStream.Length];
            fStream.Read(Doccontents, 0, (int)fStream.Length);
            fStream.Close();

            if (update == 1)
            {
                patient.ADD_PATIENT(textName.Text,
                                Convert.ToInt32(textAge.Text),
                                textMobile.Text,
                                textEmail.Text, textaddress.Text,
                                Convert.ToInt32(combogender.SelectedValue),
                                textIDNO.Text,
                                Convert.ToInt32(comboNat.SelectedValue), 
                                byteInsurance,byteId,Doccontents);
                MessageBox.Show("Patient Added Successfully", "ADD PATIENT", MessageBoxButtons.OK, MessageBoxIcon.Information);
}

When i use the fixed path its save the content in the database : 当我使用固定路径时,将内容保存在数据库中:

FileStream fStream = File.OpenRead("C:\\myfile.pdf");

but when i use the name of Adobe PDF reader to save the selected PDF file its show the following error "An unhandled exception of type 'System.ArgumentException' occurred in mscorlib.dll 但是当我使用Adobe PDF Reader的名称保存选定的PDF文件时,它显示以下错误“ mscorlib.dll中发生了'System.ArgumentException类型的未处理的异常”

Additional information: URI formats are not supported." 其他信息:不支持URI格式。”

This is the code with error : 这是有错误的代码:

FileStream fStream = File.OpenRead(PDFViewer.src);

And with this fix path it saved without error: 并使用此修复路径进行保存,没有错误:

FileStream fStream = File.OpenRead("C:\\myfile.pdf");

This is the part code for my void parameters calling stored procedure and varbinary column is it correct ? 这是我的void参数调用存储过程的零件代码,并且varbinary列正确吗?

public void ADD_PATIENT(string DocContent )
        {
            DAL.DataAccessLayer DAL = new DAL.DataAccessLayer();
            DAL.open();
            SqlParameter[] param = new SqlParameter[11];

            param[10] = new SqlParameter("@DocContent", SqlDbType.VarBinary);
            param[10].Value = DocContent;

            DAL.ExecuteCommand("ADD_PATIENT", param);
            DAL.close();

        }

What is the changes in code i need to do to save selected file instead of PDFViewer.src or how can i use byte[] method and memory stream like images to save PDF in the SQL server database ? 保存所选文件而不是PDFViewer.src时需要做哪些代码更改,或者如何使用byte []方法和图像之类的内存流将PDF保存在SQL Server数据库中?

The argument of File.OpenRead() requires a string. File.OpenRead()的参数需要一个字符串。 If PDFViewer.src is not of the type string but of the type URI (or similar) then you get the problem which you are mentioning. 如果PDFViewer.src不是字符串类型,而是URI(或类似类型),那么您将遇到所提到的问题。 Make sure that PDFViewer.src is a string (containing a valid path and filename) or cast it to a string, if possible: 确保PDFViewer.src是一个字符串(包含有效的路径和文件名),或者如果可能的话,将其强制转换为字符串:

 FileStream fStream = File.OpenRead((PDFViewer.src).ToString());

or 要么

 FileStream fStream = File.OpenRead((string)PDFViewer.src);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM