c# - 生成 PDF 文件后立即打开它
问题描述
我想在程序生成 PDF 后打开它。我知道它已经在整个互联网上得到了回答,但我找不到一个令人满意的答案来解决我的问题。这是我生成 PDF 时的代码。
protected void btnPdf_Click(object sender, EventArgs e)
{
GridViewRow rows = TraineeGrid.SelectedRow;
Label varName = (Label)rows.FindControl("lbName");
string name = varName.Text;
Label varCourse = (Label)rows.FindControl("lbCourse");
string course = varCourse.Text;
Label varCertNum = (Label)rows.FindControl("lbCertNum");
string certNumber = varCertNum.Text;
Label varEndDate = (Label)rows.FindControl("lbEndDate");
string endDate = varEndDate.Text;
DateTime date = DateTime.Parse(endDate);
string finalEDate = date.ToString("MMMM dd, yyyy");
Label varStartDate = (Label)rows.FindControl("lbStartDate");
string startDate = varStartDate.Text;
DateTime date1 = DateTime.Parse(startDate);
string finalSDate = date1.ToString("MMMM dd, yyyy");
try
{
string inputCertificate = "D:\\Intern\\BASSWeb\\Certificates\\InitialCertificate.pdf";
string outputCertificate = "D:\\Intern\\BASSWeb\\Certificates\\" + course + "_" + name + ".pdf";
string newFileName = getNextFileName(outputCertificate);
// open the reader
PdfReader reader = new PdfReader(inputCertificate);
Rectangle size = reader.GetPageSizeWithRotation(1);
Document document = new Document();
// open the writer
FileStream fs = new FileStream(outputCertificate, FileMode.Create, FileAccess.Write);
PdfStamper stamp = new PdfStamper(reader, fs);
PdfContentByte cb = stamp.GetOverContent(1);
var pageSize = reader.GetPageSize(1);
ColumnText ct = new ColumnText(cb);
Font font = FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 45, iTextSharp.text.Color.BLACK);
ct.SetSimpleColumn(28f, 100f, 600f, 305f);
Paragraph certText = new Paragraph(new Phrase(20, name, font));
ct.AddElement(certText);
ct.Go();
Font font2 = FontFactory.GetFont(FontFactory.HELVETICA_OBLIQUE, 19, iTextSharp.text.Color.GRAY);
ct.SetSimpleColumn(28f, 100f, 600f, 370f);
certText = new Paragraph(new Phrase("This is to certify that ", font2));
ct.AddElement(certText);
ct.Go();
ct.SetSimpleColumn(28f, 100f, 500f, 277f);
certText = new Paragraph(new Phrase(" has successfully completed the " + course + " at BASS Philippines RHQ", font2));
ct.AddElement(certText);
ct.Go();
ct.SetSimpleColumn(28f, 100f, 500f, 210f);
certText = new Paragraph(new Phrase("From: " + finalSDate + "", font2));
ct.AddElement(certText);
ct.Go();
ct.SetSimpleColumn(28f, 100f, 500f, 186f);
certText = new Paragraph(new Phrase("To: " + finalEDate + "", font2));
ct.AddElement(certText);
ct.Go();
Font font3 = FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10, iTextSharp.text.Color.BLACK);
ct.SetSimpleColumn(0f, 100f, 500f, 620f);
certText = new Paragraph(new Phrase("Certificate Number: " + certNumber + "", font3));
ct.AddElement(certText);
ct.Go();
stamp.Close();
reader.Close();
fs.Close();
document.Open();
string script = "alert(\" A Certificate has been generated \");";
ScriptManager.RegisterStartupScript(this, GetType(), "ServerControlScript", script, true);
Response.Clear();
Response.ContentType = "application/pdf";
Response.WriteFile(outputCertificate);
Response.End(); }
我也尝试过Response.Redirect
,但仍然什么也没做。
解决方案
确保关闭 Pdf,然后执行Process.Start(outputCertificate)
.
为此,您需要using System.Diagnostics
另外,我看不出你这样做的原因document.Open();
,那肯定必须关闭才能使用Process.Start();
推荐阅读
- c++ - 在另一个函数中初始化的通过 ref 参数传递会产生无效转换错误
- android - 重量在哪里(在喷气背包中)?
- python - 从字典创建列表理解
- android - java.lang.IllegalStateException onSaveInstanceState 后无法执行此操作
- javascript - 难以理解函数中的调用堆栈
- python - 有没有比覆盖我的父 Python 类的每个方法更好的方法?
- firebase - firebase auth => auth/captcha-check-failed 和“找不到主机名匹配”错误
- html - 如何更改导航元素的填充?
- ios - XCode 构建中 react-native-config 的 PhaseScriptExecution 失败
- python - 值错误“”使用序列设置数组元素”,组套索参数