Print XFA file to PDF without opening it - c#

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

Related

how to print file C#

I wonder how to print any printable document such as doc/docx/txt/pdf using C# (WPF/WinForm whatever). MSDN documentation shows how to print .txt files (like this one https://msdn.microsoft.com/en-us/library/system.drawing.printing.printdocument%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396). But when i'm trying to print .docx i've got XML-like text. I need to print file from directory only. I don't want to edit it etc. So i wonder i can skip few steps and send my file to printer directly, can't i?
using (var pd = new PrintDialog())
{
pd.ShowDialog();
var info = new ProcessStartInfo()
{
Verb = "print",
CreateNoWindow = true,
FileName = #"D:\Desktop\00762.pdf",
WindowStyle = ProcessWindowStyle.Hidden
};
Process.Start(info);
}

How can I generate PDF from Printdocument?

[Language: C#]
I have wrote some code to printdocument and I get a preview with microsoft's controls built-in (System.Windows.Forms.PrintPreviewControl, System.Drawing.Printing.PrintDocument).
How can I convert in PDF the file that I print with this code:
String filename="c:\\file.prt";
PrintDialog dlg = new PrintDialog();
dlg.Document = this;
dlg.AllowSelection = this.ListView.SelectedIndices.Count > 0;
dlg.AllowSomePages = true;
dlg.PrinterSettings.PrintToFile = true;
dlg.PrinterSettings.PrintFileName = filename;
I don't want print by virtual pdf printer software, because there is a "Save as" dialog...
Can I convert in PDF by Ghostscript, but this one want a Postscript file, don't work with "PrintFileName" format type.
How can i convert printfile to postscript PS, or in pdf?
Thanks.
I have resolved with this: http://www.nathanpjones.com/wp/2013/03/output-to-pdf-in-wpf-for-free/
The format is XPS....
Thanks.

How to Notify Print status of a pdf document after print?

Is it possible to print a PDF document using 'System.Printing' Namespace? How does it differ from System.Drawing.Printing'? I want to print a document and check whether it is printed or not! Presently i use a process to print pdf as
var fileName = filepath;
ProcessStartInfo psInfo = new ProcessStartInfo();
psInfo.Arguments = "HP LaserJet P1505n";
psInfo.FileName = fileName;
psInfo.WindowStyle = ProcessWindowStyle.Hidden;
psInfo.Verb = "print";
psInfo.CreateNoWindow = false;
psInfo.UseShellExecute = true;
process = Process.Start(psInfo);
Here i cannot determine page printed or not!
How can i accomplish this? It is possible to check status of printer using Win32_Printer with System.Management namespace but not "printing" status

load an adobe dynamic form file

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.

Convert word document to pdf using CutePdf programatically in C#

I have the following code to convert word document to pdf using CutePdf Writer
PrintDialog pDia = new PrintDialog();
PrinterSettings ps = new PrinterSettings();
pDia.Document = printDocumentMessageBoxTest;
pDia.Document.DocumentName = "C:\\FinalGap.doc";
ps.PrinterName = "CutePDF Writer";
ps.PrintToFile = true;
ps.PrintFileName = "C:\\" + Path.GetFileNameWithoutExtension(pDia.Document.DocumentName) + ".pdf";
// take printer settings from the dialog and set into the PrintDocument object
pDia.Document.OriginAtMargins = true;
ps.DefaultPageSettings.Margins.Left = 2;
//printDocumentMessageBoxTest.PrinterSettings = ps;
// start the printing process, catch exceptions
try
{
printDocumentMessageBoxTest.Print();
}
catch (Exception exc)
{
MessageBox.Show("Printing error!\n" + exc.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
When I run the application, it is printing the word document bu the output file is not generated.
Can anyone tell me how to convert word document to pdf using CUTEPDF programatically and waht's wrong with the above code?
CutePdf Writer does not support automation. You can't use it the manner you are trying to use it. You can purchase Custom Pdf Writer from them, then you code will be something like this:
string regKey = #"HKEY_CURRENT_USER\Software\Custom PDF Printer";
Registry.SetValue(regKey, "OutputFile", #"C:\Sample.pdf", RegistryValueKind.String);
Registry.SetValue(regKey, "BypassSaveAs", #"1", RegistryValueKind.String);
Application wordApp = new Word.Application();
Document wordDoc = wordApp.Documents.Open(#"C:\test.doc");
wordApp.ActivePrinter = "Custom PDF Printer";
wordApp.PrintOut();
wordDoc.Close();
Registry.SetValue(regKey, "BypassSaveAs", #"0", RegistryValueKind.String);
Also see:
automation of Doc to PDF in c#
DOC to PDF library (not necessarily free)
convert doc to pdf in c#
Convert single doc file to pdf
C# free Doc 2 PDF solution
Convert .doc and .txt format file into pdf file for for .aspnet?
I personally used ABCPdf for a project and I liked it, however my goal was convert not from doc but from html to pdf and the component were not free.

Categories