Convert word document to pdf using CutePdf programatically in C# - 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.

Related

Print XFA file to PDF without opening it

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

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 convert window form data into PDF

I am working on an Salary Project and what i want to do is
when an user see their Salary slip and click on Downloads then the complete form data is converted into PDf file and stored on an predifined location..
plz suggest the code to meet my requirements..
I had faced this problem before, and the best I found to solve it was to user Microsoft Word Interops. You can put whatever you want in a word document and then save it as PDF, fortunately Microsoft word allows you to export the document to PDF.
The simplest way to do this would be to save your data as just plain text, but don't forget to well format them, and then run this method to convert the plain text to PDF.
public PDFWriter(String Path, String FileName) {
Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
try
{
word.Visible = false;
word.Documents.Open(Path);
word.ActiveDocument.SaveAs2(FileName + ".pdf", Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF);
this.Path = FileName + ".pdf";
}
catch (Exception e)
{
word.Quit();
throw new Exception(e.Message);
}
finally
{
word.Quit();
}
}
Download pdfSharp.dll pdfSharp
and add it as reference.
Capture your form as image and then
private void ImageToPdf()
{
PdfSharp.Pdf.PdfDocument doc = new PdfSharp.Pdf.PdfDocument();
PdfSharp.Pdf.PdfPage oPage = new PdfSharp.Pdf.PdfPage();
String destinaton = "your destination";
doc.Pages.Add(oPage);
XGraphics xgr;
XImage img;
img = XImage.FromGdiPlusImage(form image);
xgr = PdfSharp.Drawing.XGraphics.FromPdfPage(oPage);
xgr.DrawImage(img, 0, 0);
doc.Save(destinaton);
doc.Close();
}
valter

Convert docx to pdf

My documents are stored in a database that I want to send mail with attachments.
I want to convert stored docx to pdf.
var result = from c in valinor.documents
select new
{
c.document_name,
c.document_size,
c.document_content
};
var kk = result.ToList();
for (int i = 0; i<kk.Count; i++)
{
MemoryStream stream = new MemoryStream(kk[i].document_content);
Attachment attachment = new Attachment(stream, kk[i].document_name + ".pdf", "application/pdf");
mail.Attachments.Add(attachment);
}
How can I convert document_content to pdf?
You need to use Microsoft.Office.Interop.Word in MIcrosoft office dll.
add reference to your project Microsoft.Office.Interop.Word
Check my sample of Code.
It's nice and Easy. 100% work for me.
Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
wordDocument = word.Documents.Open(savedFileName, ReadOnly: true);
wordDocument.ExportAsFixedFormat(attahcmentPath + "/pdf" + attachment.BetAttachmentCode + ".pdf", Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF);
word.Quit(false);
You will need a third party component such as ABCpdf and (probably) Word installed on the machine and use that component to convert from docx to pdf.

Export data to word in c#

Below is the code that creates a pdf to write a file..Every time i call the below code it creates a pdf file to write into..My question is,is there a same method for exporting to word or for simplicity just creates a blank doc file so that i can export data into it..
public void showPDf() {
iTextSharp.text.Document doc = new iTextSharp.text.Document(
iTextSharp.text.PageSize.A4);
string combined = Path.Combine(txtPath.Text,".pdf");
PdfWriter pw = PdfWriter.GetInstance(doc, new FileStream(combined, FileMode.Create));
doc.Open();
}
1. Interop API
It is available in Namespace Microsoft.Office.Interop.Word.
You can use Word Interop COM API to do that using following code,
// Open a doc file.
Application application = new Application();
Document document = application.Documents.Open("C:\\word.doc");
// Loop through all words in the document.
int count = document.Words.Count;
for (int i = 1; i <= count; i++)
{
// Write the word.
string text = document.Words[i].Text;
Console.WriteLine("Word {0} = {1}", i, text);
}
// Close word.
application.Quit();
Only Drawback is you must have office installed to use this feature.
2. OpenXML
you can use openxml to build word documents, try the following link,
http://msdn.microsoft.com/en-us/library/bb264572(v=office.12).aspx
Did you try searching the web for this ?
How to automate Microsoft Word to create a new document by using Visual C#
There is a free solution to export data to word,
http://www.codeproject.com/Articles/151789/Export-Data-to-Excel-Word-PDF-without-Automation-f

Categories