C# 使用文本框中的值通过iTextSharp保存PDF文件
需要通过使用多个文本框中的值自动填充文件名来保存PDF文件(使用iTextSharp)的帮助 我可以通过键入文件名来保存文件,但我希望在单击“保存”按钮时自动填充文件名 例如:C# 使用文本框中的值通过iTextSharp保存PDF文件,c#,winforms,pdf,itextsharp,C#,Winforms,Pdf,Itextsharp,需要通过使用多个文本框中的值自动填充文件名来保存PDF文件(使用iTextSharp)的帮助 我可以通过键入文件名来保存文件,但我希望在单击“保存”按钮时自动填充文件名 例如:![CID1CON4INV125][1](CID代表客户ID,即1,CON代表合同ID,即4,INV代表发票ID,即125) 这就是我目前所拥有的 SaveFileDialog dlg = new SaveFileDialog(); dlg.Filter = "PDF Files|*.pdf"; dlg.FilterInd
![CID1CON4INV125][1]
(CID
代表客户ID
,即1
,CON
代表合同ID
,即4
,INV
代表发票ID
,即125
)
这就是我目前所拥有的
SaveFileDialog dlg = new SaveFileDialog();
dlg.Filter = "PDF Files|*.pdf";
dlg.FilterIndex = 0;
if (dlg.ShowDialog() == DialogResult.OK)
{
string fileName = dlg.FileName;
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 42f, 35f);
PdfWriter.GetInstance(pdfDoc, new FileStream(fileName, FileMode.Create));
pdfDoc.Open();
}
它应该从文本框(由SQL数据填充)中提取值。我已经检查了JavaScript以供Acrobat参考 我发现了以下信息: 1. 每个PDF文档都有一个
Doc
对象(this
)。Doc
方法有一个saveAs
方法,允许您传递文件名:
this.saveAs({
cPath: "/c/customer/invoices/myDoc.pdf",
bPromptToOverwrite: true,
});
然而,这种方法的使用受到限制。根据规范:此方法只能在批处理或控制台事件期间执行,并且此方法可在Adobe Reader中用于具有保存使用权限的文档。我怀疑您的文档是否有使用权、特权方法等。。。因此:这不是您的选择。(如果您不知道使用权限是什么,则文档中没有这些权限。)
我对此进行了测试,当我打开JavaScript调试器时,我得到:
NotAllowedError:安全设置阻止访问此属性或方法。
文档另存为:1:字段保存:鼠标向上移动
这与JavaScript引用告诉我的一致
2.
有一个名为app.execMenuItem()
的app
方法。您可以使用它打开“另存为”菜单,如下所示:
app.execMenuItem("SaveAs");
我没有找到允许您传递文档新文件名的此方法的任何参数
3.
有一个属性documentFileName
,它包含文档引用的文档的基本文件名及其扩展名。此属性是只读的,因此您不能更改它,只能参考文件名:
console.println('"The file name of this document is '
+ this.documentFileName +'."');
穆罕默德·哈尼夫·艾哈迈德更新:
//string folderPath = "C:\\PDFs\\";
//if (!Directory.Exists(folderPath))
//{
// Directory.CreateDirectory(folderPath);
//}
//using (FileStream stream = new FileStream(folderPath + "CON" + txtContractID.Text + "CID" + txtCustomerID.Text + "INV" + txtInvoiceNo.Text + ".pdf", FileMode.Create))
SaveFileDialog dlg = new SaveFileDialog();
dlg.Filter = "PDF|*.pdf";
dlg.FilterIndex = 0;
string fileName = string.Format("CID{0}CON{1}INV{2}.pdf", txtCustomerID.Text, txtContractID.Text, txtInvoiceNo.Text);
if (dlg.ShowDialog() == DialogResult.OK)
{
fileName = dlg.FileName;
File.WriteAllText(Name, "test");
{
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 10f);
//PdfWriter.GetInstance(pdfDoc, stream);
pdfDoc.Open();
注释的代码实际上完成了这项工作,并将文件保存在目录中,但是当使用“另存为”对话框尝试同样的操作时,问题就会出现,文件名不会在“另存为”对话框中自动填充
布鲁诺·洛瓦吉的补充回答:
//string folderPath = "C:\\PDFs\\";
//if (!Directory.Exists(folderPath))
//{
// Directory.CreateDirectory(folderPath);
//}
//using (FileStream stream = new FileStream(folderPath + "CON" + txtContractID.Text + "CID" + txtCustomerID.Text + "INV" + txtInvoiceNo.Text + ".pdf", FileMode.Create))
SaveFileDialog dlg = new SaveFileDialog();
dlg.Filter = "PDF|*.pdf";
dlg.FilterIndex = 0;
string fileName = string.Format("CID{0}CON{1}INV{2}.pdf", txtCustomerID.Text, txtContractID.Text, txtInvoiceNo.Text);
if (dlg.ShowDialog() == DialogResult.OK)
{
fileName = dlg.FileName;
File.WriteAllText(Name, "test");
{
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 10f);
//PdfWriter.GetInstance(pdfDoc, stream);
pdfDoc.Open();
你在改变问题。当您说:我希望有一个按钮触发“SaveAs”,并根据字段值建议文件名时,听起来好像您希望在PDF文件中提供这样一个按钮。
但是:您显示的代码是关于PDF文件外的按钮强>
穆罕默德·哈尼夫·艾哈迈德更新:
//string folderPath = "C:\\PDFs\\";
//if (!Directory.Exists(folderPath))
//{
// Directory.CreateDirectory(folderPath);
//}
//using (FileStream stream = new FileStream(folderPath + "CON" + txtContractID.Text + "CID" + txtCustomerID.Text + "INV" + txtInvoiceNo.Text + ".pdf", FileMode.Create))
SaveFileDialog dlg = new SaveFileDialog();
dlg.Filter = "PDF|*.pdf";
dlg.FilterIndex = 0;
string fileName = string.Format("CID{0}CON{1}INV{2}.pdf", txtCustomerID.Text, txtContractID.Text, txtInvoiceNo.Text);
if (dlg.ShowDialog() == DialogResult.OK)
{
fileName = dlg.FileName;
File.WriteAllText(Name, "test");
{
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 10f);
//PdfWriter.GetInstance(pdfDoc, stream);
pdfDoc.Open();
我的错我可能没有正确地提出我的问题…现在问题被理解了,有解决办法了吗
布鲁诺更新:
//string folderPath = "C:\\PDFs\\";
//if (!Directory.Exists(folderPath))
//{
// Directory.CreateDirectory(folderPath);
//}
//using (FileStream stream = new FileStream(folderPath + "CON" + txtContractID.Text + "CID" + txtCustomerID.Text + "INV" + txtInvoiceNo.Text + ".pdf", FileMode.Create))
SaveFileDialog dlg = new SaveFileDialog();
dlg.Filter = "PDF|*.pdf";
dlg.FilterIndex = 0;
string fileName = string.Format("CID{0}CON{1}INV{2}.pdf", txtCustomerID.Text, txtContractID.Text, txtInvoiceNo.Text);
if (dlg.ShowDialog() == DialogResult.OK)
{
fileName = dlg.FileName;
File.WriteAllText(Name, "test");
{
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 10f);
//PdfWriter.GetInstance(pdfDoc, stream);
pdfDoc.Open();
你说现在问题已经明白了。那不是真的。我还是不知道你在问什么。请看我的评论:不幸的是,很难说出你的意思。你在说谜语
穆罕默德·哈尼夫·艾哈迈德更新:
//string folderPath = "C:\\PDFs\\";
//if (!Directory.Exists(folderPath))
//{
// Directory.CreateDirectory(folderPath);
//}
//using (FileStream stream = new FileStream(folderPath + "CON" + txtContractID.Text + "CID" + txtCustomerID.Text + "INV" + txtInvoiceNo.Text + ".pdf", FileMode.Create))
SaveFileDialog dlg = new SaveFileDialog();
dlg.Filter = "PDF|*.pdf";
dlg.FilterIndex = 0;
string fileName = string.Format("CID{0}CON{1}INV{2}.pdf", txtCustomerID.Text, txtContractID.Text, txtInvoiceNo.Text);
if (dlg.ShowDialog() == DialogResult.OK)
{
fileName = dlg.FileName;
File.WriteAllText(Name, "test");
{
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 10f);
//PdfWriter.GetInstance(pdfDoc, stream);
pdfDoc.Open();
我很难找到这个问题的解决方案,我试图在Windows窗体应用程序中使用iText Sharp保存PDF文件,但是每次尝试保存文件时,我都必须手动输入文件名,我想做的是当“另存为”时对话框出现。文件名应通过将3个不同文本框中的值串联为文件名来自动填充
例如,文件名:CID1CON4INV125(CID代表客户ID为1,CON代表合同ID为4,INV代表发票ID为125且唯一)
如果我每次都要选择一个文件名,那么上面的例子可能会非常乏味,有没有一种方法可以让我通过从txtCustomerID.Text和txtcontracid.Text和txtInvoiceID.Text中提取文件名并准备一个文件名保存为PDF来自动完成这个过程
布鲁诺的回答:
//string folderPath = "C:\\PDFs\\";
//if (!Directory.Exists(folderPath))
//{
// Directory.CreateDirectory(folderPath);
//}
//using (FileStream stream = new FileStream(folderPath + "CON" + txtContractID.Text + "CID" + txtCustomerID.Text + "INV" + txtInvoiceNo.Text + ".pdf", FileMode.Create))
SaveFileDialog dlg = new SaveFileDialog();
dlg.Filter = "PDF|*.pdf";
dlg.FilterIndex = 0;
string fileName = string.Format("CID{0}CON{1}INV{2}.pdf", txtCustomerID.Text, txtContractID.Text, txtInvoiceNo.Text);
if (dlg.ShowDialog() == DialogResult.OK)
{
fileName = dlg.FileName;
File.WriteAllText(Name, "test");
{
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 10f);
//PdfWriter.GetInstance(pdfDoc, stream);
pdfDoc.Open();
你的问题全错了:它与PDF无关。这一切都是关于在C#中创建一个“另存为”对话框并用默认文件名填充它。您应该创建一个新问题并删除对PDF和iTextSharp的所有引用强>
事实上,这样一个问题很可能会以重复的形式结束:
文档
和PdfWriter
的代码。那没有道理。您确定不需要PdfReader
和PdfStamper
吗?现在,我在表单上有一个保存按钮,可以进入“另存为”对话框,该对话框为空,我需要输入文件名以将其保存为PDF。现在我想要的是文件名,而不是我输入的名称,它应该使用文本框值自动填充文件名,就像我在上面的例子中提到的那样。如果可能的话,它必须通过在PDF中使用JavaScript来完成。我将不得不为Ac查询JavaScript