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: