How can I generate PDF from Printdocument? - c#

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

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 to show PDF file in MigraDoc.Rendering.Forms.DocumentPreview (in WinForms)?

I have a simple questions. How can you show a PDf file by using PagePreview?
I have a full pathname document.FileName = "c:\scans\Insurance_34345.pdf";
pagePreview.Preview(document.FileName); or something...
If there another way for showing a pdf. It's okay. I want to show it on a WinForms Form.
I tried this. I don't know what I have to do...
in the Designer
private MigraDoc.Rendering.Forms.DocumentPreview dpvScannedDoc;
Part of the code
string fullPadnaam = Path.Combine(defaultPath, document.FileName);
//PdfDocument pdfDocument = new PdfDocument(fullPadnaam);
//PdfPage page = new PdfPage(pdfDocument);
//XGraphics gfx = XGraphics.FromPdfPage(page);
MigraDoc.DocumentObjectModel.Document pdfDocument = new MigraDoc.DocumentObjectModel.Document();
pdfDocument.ImagePath = fullPadnaam;
var docRenderer = new DocumentRenderer(pdfDocument);
docRenderer.PrepareDocument();
var inPdfDoc = PdfReader.Open(fullPadnaam, PdfDocumentOpenMode.ReadOnly);
for (var i = 0; i < inPdfDoc.PageCount; i++)
{
pdfDocument.AddSection();
docRenderer.PrepareDocument();
var page = inPdfDoc.Pages[i];
var gfx = XGraphics.FromPdfPage(page);
docRenderer.RenderPage(gfx, i + 1);
}
var renderer = new PdfDocumentRenderer();
renderer.Document = pdfDocument;
renderer.RenderDocument();
// MigraDoc.DocumentObjectModel.IO.DdlWriter dw = new MigraDoc.DocumentObjectModel.IO.DdlWriter("HelloWorld.mdddl");
// dw.WriteDocument(pdfDocument);
// dw.Close();
//renderer.PdfDocument.rea(outFilePath);
//string ddl = MigraDoc.DocumentObjectModel.IO.DdlWriter.WriteToString(document1);
dpvScannedDoc.Show( pdfDocument);
PDFsharp does not render PDF files. You cannot show PDF files using the PagePreview.
If you use the XGraphics class for drawing then you can use shared code that draws on the PagePreview and on PDF pages.
The PagePreview sample can be found in the sample package and here:
http://www.pdfsharp.net/wiki/Preview-sample.ashx
If you have code that creates a new PDF file using PDFsharp then you can use the PagePreview to show on screen what you would otherwise draw on PDF pages. You cannot draw existing PDF pages using the PagePreview because PDF does not render PDF.
The MigraDoc DocumentPreview can display MDDDL files (your sample code creates a file "HelloWorld.mdddl"), but it cannot display PDF files.
If the MDDDL uses PDF files as images, they will not show up in the preview. They will show when creating a PDF from the MDDDL.

itextsharp and pdf

i want to read a pdf file line per line but i want to maintain his original format
¿can i do this with itextsharp?
i use the next code :
private void button1_Click(object sender, EventArgs e)
{
string text = string.Empty;
string path = string.Empty;
path = "C:\\Documents and Settings\\Rafael\\Desktop\\Imprimiendo\\Print1.pdf";
PdfReader reader = new PdfReader(path);
for (int page = 1; page <= reader.NumberOfPages; page++)
{
text = PdfTextExtractor.GetTextFromPage(reader, page);
richTextBox1.Text = text;
}
reader.Close();
return;
}
thanks, i really need your help
If you want to read PDF file with small data in it, iTextsharp would be the best choice, you may find answer here:
Reading PDF content with itextsharp dll in VB.NET or C#
However, if you have huge data in your PDF file, iTextsharp will have problems in realizing this task. in such a case, you may need a third party library. This article may help you much:
Read PDF file in C#

Export xps to pdf in C#

I am a newbie and i want to generate PDF from Infragistics, Xamdatagrid. However as Infragistics doesnt provide this functionality ,i have generated XPS of Xamdatagrid and wants to convert that to XPS programitically. What are the possible work around and third party tool to do that?
If you export the xamDataGrid in an excel file then is pretty simple to use Excel.Interop and ask excel to export its workbook in PDF format
// Export an excel workbok in PDF format with landscape orientation
private static void ExportWorkbookToPDF(string workbook, string output)
{
Microsoft.Office.Interop.Excel.Application excelApplication =
new Microsoft.Office.Interop.Excel.Application();
excelApplication.ScreenUpdating = false;
excelApplication.DisplayAlerts = false;
excelApplication.Visible = false;
Microsoft.Office.Interop.Excel.Workbook excelWorkbook =
excelApplication.Workbooks.Open(workbook);
if (excelWorkbook == null)
{
excelApplication.Quit();
excelApplication = null;
excelWorkbook = null;
throw new NullReferenceException("Cannot create new excel workbook.");
}
try
{
((Microsoft.Office.Interop.Excel._Worksheet)excelWorkbook.ActiveSheet).PageSetup.Orientation =
Microsoft.Office.Interop.Excel.XlPageOrientation.xlLandscape;
excelWorkbook.ExportAsFixedFormat(XlFixedFormatType.xlTypePDF, output);
}
finally
{
excelWorkbook.Close();
excelApplication.Quit();
excelApplication = null;
excelWorkbook = null;
}
}
if you wish to create only pdf. then the simplest thing have any pdf printer on machine. One like PDF Creater and then just call the printing thing like below on the XamDataGrid.
make sure you select the PDF Printer on the Printer Selection Dialog Box.
// 1. Create Report object
Report reportObj = new Report();
// 2. Create EmbeddedVisualReportSection section.
// Put the grid you want to print as a parameter of section's constructor
EmbeddedVisualReportSection section = new EmbeddedVisualReportSection(xamdg);
// 3. Add created section to report's section collection
reportObj.Sections.Add(section);
// Optional. If you have progress indicator set its Report property to created report
// progressInfo.Report = reportObj;
// 4. Call print method
reportObj.Print(true, false);
You can also use thirdparty software like GhostXPS.
http://www.ghostscript.com/download/gxpsdnld.html
Just start the convert process with the correct arguments and it will generate the PDF for you. The drawback is that you have to save the files temporary to the disk and check the return code. Also make sure you are not violating the GNU license

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