简体   繁体   English

通过使用文本框中的值通过iTextSharp保存PDF文件

[英]Saving PDF file via iTextSharp by using values from textboxes

Need help in saving a PDF file (using iTextSharp) by automatically populating the file name using values from multiple textboxes. 通过使用来自多个文本框的值自动填充文件名,需要在保存PDF文件(使用iTextSharp)方面获得帮助。

I am able to save the file by typing the name of the file but what I would want is to have the name populated automatically when the save button is clicked. 我可以通过输入文件名来保存文件,但是我想要的是在单击保存按钮时自动填充名称。

For Eg: ![CID1CON4INV125][1] ( CID stands for Customer ID which is 1 , CON stands for Contract ID which is 4 , INV stands for Invoice ID which is 125 ) 例如: ![CID1CON4INV125][1]CID代表Customer ID1CON代表Contract ID4INV代表Invoice ID125

This is what i have so far. 这是我到目前为止所拥有的。

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();
}

It should pick up values from textboxes (which are populated by SQL Data)ю 它应该从文本框(由SQL数据填充)中选取值。

I have checked the JavaScript for Acrobat reference. 我已经检查了JavaScript以供Acrobat参考。

I have found the following information: 我发现以下信息:

1. 1。

Each PDF document has a Doc object ( this ). 每个PDF文档都有一个Doc对象( this )。 The Doc method has a saveAs method that allows you to pass a filename: Doc方法具有saveAs方法,该方法允许您传递文件名:

this.saveAs({
    cPath: "/c/customer/invoices/myDoc.pdf",
    bPromptToOverwrite: true,
});

However, the use of this method is restricted. 但是,此方法的使用受到限制。 From the spec: This method can only be executed during a batch or console event and This method is available in Adobe Reader for documents that have Save usage rights. 根据规范: 此方法只能在批处理或控制台事件期间执行,并且该方法在Adobe Reader中适用于具有“保存”使用权限的文档。 I doubt that your document has usage rights, privileged methods, etc... Hence: this is not an option for you. 我怀疑您的文档具有使用权,特权方法等。因此:这不是您的选择。 (if you don't know what usage rights are, your document doesn't have them.) (如果您不知道什么是使用权利,则您的文档中没有它们。)

I tested this and when I open the JavaScript debugger, I get: 我对此进行了测试,并且在打开JavaScript调试器时得到:

NotAllowedError: Security settings prevent access to this property or method. NotAllowedError:安全设置阻止访问此属性或方法。 Doc.saveAs:1:Field Save:Mouse Up Doc.saveAs:1:字段保存:鼠标向上

This is in line with what the JavaScript reference tells me. 这与JavaScript参考告诉我的内容一致。

2. 2。

There's an app method called app.execMenuItem() . 有一个名为app.execMenuItem()app方法。 You can use it to open the Save As menu like this: 您可以使用它来打开“另存为”菜单,如下所示:

app.execMenuItem("SaveAs");

I didn't find any parameter for this method that allows you to pass a new file name of the document. 我没有找到此方法的任何参数,该参数使您可以传递文档的新文件名。

3. 3。

There is an attribute documentFileName that contains the base file name, with extension, of the document referenced by the Doc. 有一个属性documentFileName ,包含Doc引用的文档的基本文件名(带有扩展名)。 This attribute is Read Only , hence you can not change it, you can only consult the file name: 此属性为只读 ,因此您不能更改它,只能查询文件名:

console.println('"The file name of this document is '
    + this.documentFileName +'."');

Update by Mohammad Haneef Ahmad: Mohammad Haneef Ahmad更新:

//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();

The commented codes actually does the job and saves the file in the directory, however problem arises when the same thing is tried with save as dialogbox, the file names dont fill in automatically in save as dialog box 注释的代码实际上完成了该工作并将文件保存在目录中,但是当尝试使用“另存为”对话框尝试相同操作时会出现问题,文件名不会自动填充到“另存为”对话框中

Additional answer by Bruno Lowagie: Bruno Lowagie的其他答案:

You are changing the question. 您正在更改问题。 When you say: I want to have a button that triggers "SaveAs" and that proposes a file name based on the values of a field, it sounds as if you want to provide such a button in the PDF file. 当您说:我想要一个按钮来触发“另存为”并根据字段的值建议文件名时,听起来好像您想在PDF文件中提供这样的按钮

However: the code that you are showing is about a button outside the PDF file! 但是:您显示的代码与PDF文件外部的按钮有关

Update by Mohammad Haneef Ahmad: Mohammad Haneef Ahmad更新:

My bad i may have not put my question correctly...now that the problem is understood is there aa solution. 我的坏事我可能没有正确提出我的问题...现在知道问题已经解决了。

Update by Bruno: Bruno更新:

You say now that the problem is understood. 现在说问题已被理解。 That's not true. 这不是真的。 I still don't have a clue as to what you are asking. 对于您的要求,我仍然一无所知。 See my comment: Unfortunately, it is hard to tell what you mean by this. 查看我的评论: 不幸的是,很难说出您的意思。 You are talking in riddles. 你在胡说八道。

Update by Mohammad Haneef Ahmad: Mohammad Haneef Ahmad更新:

I am having a hard time finding solution to this problem, I am trying to save a PDF file using iText Sharp in Windows Form Application however everytime I try to save the file I have to feed the file name manually, What i want to do is that when "Save As" Dialog Box appears the file name should auto populate by concatenating values from 3 different textboxes as a file name. 我很难找到解决这个问题的方法,我试图在Windows Form Application中使用iText Sharp保存PDF文件,但是每次尝试保存文件时,我都必须手动输入文件名,我要做的是当出现“另存为”对话框时,文件名应通过将3个不同文本框中的值串联为文件名来自动填充。

For eg. 例如。 File Name: CID1CON4INV125 (CID stands for Customer ID which is 1, CON stands for Contract ID which is 4, INV stands for Invoice ID which is 125 and is unique) 文件名:CID1CON4INV125(CID代表客户ID为1,CON代表合同ID为4​​,INV代表发票ID为125,并且是唯一的)

Above example may be very tedious to remember if i have to chose a file name everytime, is there a way that i can automate this process by taking the file name from txtCustomerID.Text and txtContractID.Text and txtInvoiceID.Text and prepare a file name to be saved as PDF. 上面的示例可能很难记住,如果我每次都必须选择一个文件名,是否有一种方法可以通过从txtCustomerID.Text和txtContractID.Text和txtInvoiceID.Text中获取文件名并准备文件名来自动执行此过程保存为PDF。

Answer by Bruno: 布鲁诺回答:

Your question is all wrong: it has nothing to do with PDF. 您的问题全错了:与PDF 无关 It is all about creating a SaveAs Dialog box in C# and populating it with a default file name. 这一切都是关于在C#中创建“另存为”对话框并使用默认文件名填充它。 You should create a new question and remove all references to PDF and iTextSharp! 您应该创建一个新问题,并删除所有对PDF和iTextSharp的引用!

Actually, such a question would probably be closed as duplicate: 实际上,这样的问题很可能会重复出现:

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

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