I have a .pdf created through Adobe LiveCycle Designer (it's a dynamic pdf ) i want to add this pdf to my windows app
this is what i tried
File file1 = new File(fileName);
System.Xml.XmlDocument xfadoc = new System.Xml.XmlDocument();
xfadoc.LoadXml(fileName);
here's how i get filename
OpenFileDialog dialog = new OpenFileDialog();
dialog.InitialDirectory = "c:\\";
dialog.Filter = "pdf files (*.pdf) | *.pdf | All Files (*.*) | *.* | xdp files (*.xdp) | *.xdp ";
dialog.FilterIndex = 2;
dialog.RestoreDirectory = true;
dialog.CheckFileExists = true;
dialog.DefaultExt = "pdf | xdp";
fileName = dialog.FileName.ToString();
but when i click on open file button and browse to where i have stored it ; it doesnot even appear
Also when i try to load this file in my C# windows app it gives me an exception at the following line
xfadoc.LoadXml(fileName);
the exception says that
'Data at root level is invalid'
If i say that i have loaded the string (filepath name) someone please tell me how can i extract the xml part only form this dynamic file
Try Filter without spaces in extensions part.
dialog.Filter = "pdf files (*.pdf)|*.pdf|All Files (*.*)|*.*|xdp files (*.xdp)|*.xdp";
LoadXml loads the document from the string parameter. You want to use the Load method.
Related
I am attempting to auto-fill/sign/print the federal i-9 form using Spire.PDF and C#. The i9 file is an XFA form and is protected and doesn't allow for signing. However, if I fill the i9 and print to PDF, then I can sign that new file.
The step I'm getting stuck on is printing the filled i9 to a PDF file without actually opening Acrobat or having direct interaction from the end-user to specify a file name. I say 'printing' because if I just save it as a PDF file it never flattens the XFA form and remains locked against signing.
So far I have automated printing of the file using this code:
Process proc = new Process();
proc.StartInfo.Verb = "PrintTo";
proc.StartInfo.FileName = filename;
proc.StartInfo.Arguments = "\"" + printername + "\"";
proc.StartInfo.UseShellExecute = true;
proc.StartInfo.CreateNoWindow = true;
proc.Start();
and I think I might be able to force use of the Microsoft Print to PDF 'printer' here, but I don't know if there's a way to specify the file name to use so that the user isn't prompted?
If I try printing using the Spire.PDF control, I am only able to get a file with the "Please wait...
If this message is not eventually replaced by the proper contents of the document, your PDF
viewer may not be able to display this type of document.." message as a result.
When the form is opened to print via Acrobat I get a popup of "This form contains incomplete or invalid information. Are you sure you want to print?" If I click Yes then I can successfully print to PDF and then I can sign that file.
So, I believe whatever data-checking is happening is causing the failure to print via code and I'm hoping those wiser than I might have some ideas of ways around this issue.
Thank you in advance for your help! If you just search for Federal i9 you should find the file I'm working with. I didn't see a space to attach a file here.
This is the code that I'm using to try to accomplish my task via the Spire.PDF control.
PdfDocument doc = new PdfDocument();
string i9path = "locationofi9file"
string newi9path = "locationoffilledi9file"
doc.LoadFromFile(i9path);
/*fill form here*/
doc.Form.IsFlatten = true;
doc.SaveToFile(newi9path, FileFormat.PDF);
doc.Close();
doc.Dispose();
doc.LoadFromFile(newi9path);
string file = "printi9";
if (System.IO.File.Exists(file))
System.IO.File.Delete(file);
string directory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
PrinterSettings settings = new PrinterSettings();
PageSettings pages = new PageSettings();
string printername = "Microsoft Print to PDF";
settings.PrinterName = printername;
settings.PrintToFile = true;
settings.PrintFileName = Path.Combine(directory, file + ".pdf");
PrintDocument printDoc = doc.PrintDocument;
printDoc.PrinterSettings = settings;
printDoc.Print();
doc.Close();
doc.Dispose();
Hi I have a need to store some data in a excel file (.xlsx) so I can send the file to our customers and extract data from it again later when they send us the file back to update some content.
The customer should not be able to see the data we store in the file, and certainly not be able to remove it (accidentally). In fact he should not be aware of it in any way. Also we want to add this info from a service on a system that doesn't have Excel installed.
I know that a .xlsx file is basicaly a zip file so I can extract the data and add a file to it, zip it again and have a valid file that can be opened by Excel. Only problem here is that after saving that file in Excel my custom xml file is removed from the package. So I need to know how to fix this.
What I have:
XNamespace MyNamespace = "http://stackoverflow.com/questions/ask";
XNamespace ExcelNamespace = "http://schemas.openxmlformats.org/package/2006/relationships";
string ExtractionPath = #"C:\temp\test\";
string ExcelFile = #"C:\temp\example.xlsx";
Directory.CreateDirectory(ExtractionPath);
System.IO.Compression.ZipFile.ExtractToDirectory(ExcelFile, ExtractionPath);
var Root = new XElement(MyNamespace + "tracker",
new XAttribute("version", "1.0.0.1"),
new XElement("connections"));
var file = Path.Combine(ExtractionPath, "connectioninfo.xml");
if (!File.Exists(file))
{
var relsPath = Path.Combine(ExtractionPath, "_rels", ".rels");
var rels = XElement.Load(relsPath);
rels.Add(new XElement(ExcelNamespace + "Relationship",
new XAttribute("Id", "XXXX"),
new XAttribute("Type", "http://schemas.openxmlformats.org/officeDocument/2006/relationships/customXml"),
new XAttribute("Target", "connectioninfo.xml")));
rels.Save(relsPath);
}
Root.Save(file);
if (File.Exists(ExcelFile)) File.Delete(ExcelFile);
System.IO.Compression.ZipFile.CreateFromDirectory(ExtractionPath, ExcelFile, System.IO.Compression.CompressionLevel.NoCompression, false);
When I run this code I end up with a file that contains my connectioninfo.xml file and that I can open in Excel. But when I save that file in excel and unzip the package again, then the connectioninfo.xml file is gone.
So question -> what am I missing to keep the file in the package after saving?
PS: I have also tried following code, but same problem ...
(using System.IO.Packaging;)
using (Package package = Package.Open(ExcelFile, FileMode.Open, FileAccess.ReadWrite))
{
Uri uriPartTarget = new Uri("/customXml/example.xml", UriKind.Relative);
if (!package.PartExists(uriPartTarget))
{
PackagePart customXml = package.CreatePart(uriPartTarget,
"application/vnd.openxmlformats-officedocument.customXmlProperties+xml");
using (Stream partStream = customXml.GetStream(FileMode.Create,
FileAccess.ReadWrite))
{
var doc = new XElement("test", new XElement("content","Hello world!"));
doc.Save(partStream);
}
}
}
How can I avoid the multiple extensions in the retrieved file name from save dialog?
I have filtered the dialog only to Rich Text File and .doc files.
When I change the selection in the combo Box below the text Box with file name, the extension is added to the file name instead of changing the existing extension.
SaveFileDialog dialog= new SaveFileDialog();
dialog.Title = "Please select the directory in which the document will be created.";
// set a default file name
dialog.FileName = my_File_Name;
// set filters
dialog.Filter = "RTF Files (*.rtf)|*.rtf | Wordfile (*.doc)|*.doc | Text Files (*.txt)|*.txt";
if (dialog.ShowDialog() == DialogResult.OK)
{
//selected folder path
string placeToSaveDocument = Path.GetFullPath(dialog.FileName);
}
That is the result:
myFileName.rtf.doc.rtf.txt
Problem : Your Filter string is not proper as it has spaces after extension string *.rtf and *.doc
Solution : You need to eliminate the space after extension strings.Remove spaces after *.rtf and *.doc
Try This:
dialog.Filter = "RTF Files (*.rtf)|*.rtf|Wordfile (*.doc)|*.doc|Text Files (*.txt)|*.txt";
My code will generate the excel document like this
|id | Name | Address | company_Name | Destination|
|----|-------|----------|--------------|------------|
|##1 | xxx | xxxx | xxx | xxxxx |
But I want like this...
-----------------------------------------------------
| Personal Information | Working INFO |
-----------------------------------------------------
|id | Name | Address | company_Name | Destination|
|----|-------|----------|--------------|------------|
|##1 | xxx | xxxx | xxx | xxxxx |
-----------------------------------------------------
I get the data from the API and I will save it using the SaveFileDialog
SaveFileDialog dialog = new SaveFileDialog();
dialog.Title = "Save file as...";
dialog.Filter = "Text files (*.csv)|*.csv";
dialog.RestoreDirectory = true;
if (dialog.ShowDialog() == DialogResult.OK)
{
System.IO.StreamWriter writer = new System.IO.StreamWriter(dialog.FileName); //open the file for writing.
writer.Write(report); //write the current date to the file. change this with your date or something.
writer.Close(); //remember to close the file again.
writer.Dispose(); //remember to dispose it from the memory.
program.ShowInformationMessage("File Save successfully");
}
There is no problem with it.
I want to make the heading as the inline something like it.
...
System.IO.StreamWriter writer = new System.IO.StreamWriter(dialog.FileName); //open the file for writing.
writer.Write("Personal Information" + delimiter + delimiter + "Working INFO" + delimiter);
writer.Write(report); //write the current date to the file. change this with your date or something.
...
Csv format doesn't support merged cells, but still you can arrange header line like described above. Just replace delimiter with whatever your cell delimiter is.
Have you considered using closedxml (https://closedxml.codeplex.com/).
var wb = new XLWorkbook(report); //open spreadsheet
IXLWorksheet ws = wb.Worksheets.First(); //get first sheet
ws.Row(1).InsertRowsAbove(1); //insert row
ws.Cell("A1").Value = "Personal Information";
ws.Cell("A4").Value = " Working INFO";
ws.Range("A1:A3").Row(1).Merge(); // merge first title
ws.Range("A4:A6").Row(1).Merge(); // merge second
wb.SaveAs(writer);
You can also open csv's and save as csv
Firstly create your excel file template in excel and format it as you want.
Put only one line where lots of rows will be printed.
Put labels into the data row to replace with real data.
Save it as MHT file. (styles will be saved, too like html)
Open the mht file with text editor.
Put < !#> at the begining and end of the data row so that you can split the file content from your program and populate real data lines dynamically by replacing your labels with real properties.
<!#>
<tr height=3D20 style=3D'height:15.0pt'>
<td height=3D20 class=3Dxl67 style=3D'height:15.0pt;border-top:none'>[id]=</td>
<td class=3Dxl67 style=3D'border-top:none;border-left:none'>[name]</td>
<td class=3Dxl67 style=3D'border-top:none;border-left:none'>[company<span style=3D'display:none'>]</span></td>
<td class=3Dxl67 style=3D'border-top:none;border-left:none'>[destination]=</td>
</tr>
<!#>
Save the file from text editor.
From your program, you will read the mht file content, you will split it with the < !#> and multiply the data row part, append all together and save to another file..thats it..
You need to have installed Microsoft Visual Studio Tools for Office.
After that create common .NET project and add the reference to COM object Microsoft.Office.Interop.Excel.dll via 'Add Reference..' dialog.
Application excel = new Application();
Workbook wb = excel.Workbooks.Open(path);
//Get All available worksheets
//Excel.Sheets excelSheets = wb.Worksheets;
//Get Specific WorkSheet
string currentSheet = "Sheet1";
Excel.Worksheet newSheet = (Excel.Worksheet)wb.get_Item(currentSheet);
newSheet.Cells[i, j].HorizontalAlignment = ExcelAlignment.xlLeft; //or Excel.XlHAlign.xlHAlignLeft
I try to tell you my problem. with ClosedXML i have SaveAs() method, but when i use SaveAs(string name), it saves my excel document to some strange folder with some strange path. so i've decide to use savefiledialog to give user posibility to select folder and name for document. how can i use savefiledialog with closedXML?
SaveAs() also have SaveAs(Path path). Can i use it?
The "strange" folder is the folder your application is running from (since you're not specifying a path).
If you want you can use the SaveFileDialog to get the path and pass it to the SaveAs method.
var saveFileDialog = new SaveFileDialog
{
Filter = "Excel files|*.xlsx",
Title = "Save an Excel File"
};
saveFileDialog.ShowDialog();
if (!String.IsNullOrWhiteSpace(saveFileDialog.FileName))
workbook.SaveAs(saveFileDialog.FileName);
var saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "Excel files|*.xlsx";
//serialVal is name of a variable, not necessary at all if you don't need a specific file name
saveFileDialog.FileName = serialVal;
if (saveFileDialog.ShowDialog() == true)
{
workbook.SaveAs(saveFileDialog.FileName);
workbook.Dispose();
return;
}