0

Need help in saving a PDF file (using iTextSharp) by automatically populating the file name using values from multiple textboxes.

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)

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)ю

abatishchev
  • 98,240
  • 88
  • 296
  • 433
  • Which save button are you talking about? The one in your application or the one in Adobe Reader? What are text boxes? Are you referring to text fields (AcroForms) or text annotations? Why can't you use the SQL data directly instead of getting the same data out of the PDF? Be more specific. You may be asking something that is impossible in PDF (in general, not because of iTextSharp) or you may be asking something that is simple, but as your question is badly phrased, there's no way to tell. – Bruno Lowagie Feb 03 '15 at 00:12
  • What's also confusing: at first it sounds as you have an existing PDF file with some values inside, but then you show code involving `Document` and `PdfWriter`. That doesn't make sense. Are you sure you don't need `PdfReader` and `PdfStamper`? – Bruno Lowagie Feb 03 '15 at 00:37
  • Ok now I have a save button on the form which takes me to "Save as" dialog box which is empty and I need to enter the file name for saving it as PDF. Now what I want here is that file name instead of me inputtingthe name it should automatically populating the name of the file using the textboxes value just like i mentioned in my ex above – Mohammad Haneef Ahmad Feb 03 '15 at 06:28
  • *If* it is possible, it will have to be done by using JavaScript inside the PDF. I'll have to consult the JavaScript for Acrobat reference manual. If I find anything, I'll post it as an answer. – Bruno Lowagie Feb 03 '15 at 08:04
  • you have updated my answer, but what you say is again very confusing. You say: *problem arises when the same thing is tried with save as dialogbox, the file names dont fill in automatically in save as dialog box.* Unfortunately, it is hard to tell what you mean by this. – Bruno Lowagie Feb 03 '15 at 18:07

1 Answers1

0

I have checked the JavaScript for Acrobat reference.

I have found the following information:

1.

Each PDF document has a Doc object (this). The Doc method has a saveAs method that allows you to pass a filename:

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. 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:

NotAllowedError: Security settings prevent access to this property or method. Doc.saveAs:1:Field Save:Mouse Up

This is in line with what the JavaScript reference tells me.

2.

There's an app method called app.execMenuItem(). 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.

There is an attribute documentFileName that contains the base file name, with extension, of the document referenced by the 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:

//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:

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.

However: the code that you are showing is about a button outside the PDF file!

Update by Mohammad Haneef Ahmad:

My bad i may have not put my question correctly...now that the problem is understood is there a a solution.

Update by 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:

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.

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)

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.

Answer by Bruno:

Your question is all wrong: it has nothing to do with PDF. It is all about creating a SaveAs Dialog box in C# and populating it with a default file name. You should create a new question and remove all references to PDF and iTextSharp!

Actually, such a question would probably be closed as duplicate:

Community
  • 1
  • 1
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165