I have the Graphics.DrawString method for Print Preview.
Now I would like to use the same code to save it as a PDF. I have a Form, I have created a printprewiew and I would like to save as it is in printPrview to pdf.
Is it posible to do this?
I have a solution this may help you, but it requires to download and install "CutePDF Writer". CutePDF Writer is free software. You can download it from there official website.
PrintDialog pDia = New PrintDialog()
PrinterSettings ps = New PrinterSettings()
pDia.Document = PrintDocument1
pDia.Document.DocumentName = "Your File Name"
ps.PrinterName = item.ToString()
ps.PrintToFile = False
//Create folder inside Bin folder to save PDFs
String strReportBackupPath = "C:\\"
String strFilePath = strReportBackupPath & "\\" & pDia.Document.DocumentName & ".pdf"
//To avoid the replace dialog of cutePDF - To save new file if have same name
ps.PrintFileName = strFilePath
PrintDocument1.PrinterSettings = ps
PrintDocument1.Print()
but it will show you save as dialog to save the file to pdf.
Related
Im trying to save a pdf file as a new pdf but printting it (required), Im using Adobe Acrobat to do that cause it brings me the option to introduce parameters.
The thing is : I need to do it silently (Not a single Window can been saw). So I need to set a printer, a path to my file and an output destination.
As I said I need to "print it" as a new pdf so I'm using Microsoft Print to PDF to do that (I don't know if it is the better option).
Thank you so much!
string file = name;
string pathFile = "C:\\DfPrinter\\" + name;
ProcessStartInfo infoPrintPdf = new ProcessStartInfo();
string printerName = "Microsoft Print to PDF";
infoPrintPdf.FileName = "C:\\Program Files (x86)\\Adobe\\Acrobat Reader DC\\Reader\\AcroRd32.exe";
infoPrintPdf.Arguments = string.Format("/t" + " '" + pathFile + "'" +"' " +"'"+printerName+"'");
infoPrintPdf.CreateNoWindow = true;
infoPrintPdf.UseShellExecute = false;
infoPrintPdf.WindowStyle = ProcessWindowStyle.Hidden;
Process printPdf = new Process();
printPdf.StartInfo = infoPrintPdf;
printPdf.Start();
System.Threading.Thread.Sleep(10000);
if (!printPdf.CloseMainWindow())
printPdf.Kill(); printPdf.WaitForExit();
I don't think Acrobat Reader can print silently, but you could consider using the Win2PDF "printpdf" command line:
win2pdfd.exe printpdf "filename.pdf" Win2PDF "newfilename.pdf"
This prints the PDF silently to a new PDF specified by "newfilename" by using the Win2PDF printer.
I'm using c#. I'm receiving an error about a path is currently accessed by other processes. What my system is trying to do is to access the path: #"C:\temps\" + client_ids + "_" + rown + ".pdf" and use the same path for attachment before sending it to client's email.
here's what I've done so far. I comment out some of my code because I'm not sure what to do.
FileStream fs = null;
using (fs = new FileStream(#"C:\\temps\\" + client_ids + "_" +
rown + ".pdf",
FileMode.Open,FileAccess.Read,FileShare.ReadWrite))
{
TextReader tr = new StreamReader(fs);
//report.ExportToDisk
//(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat,tr);
//report.Dispose();
//Attachment files = new Attachment(tr);
//Mailmsg.Attachments.Add(files);
//Clients.Send(Mailmsg);
}
you can make temp copy of file before you use it in mail attachment and then use the copy instead of the original file
You cannot attach a file to an email if that file is open. You must close (save) the file first.
While #ali answer is technically correct, it is unnecessary. Why go through the overhead of creating a copy of the file which then needs to be deleted, etc.?
Assuming I understand what you are trying to do correctly, simply move your code for mail to after the file is successfully created and saved. And, I don't think you need the overhead of either the filestream or the textreader. As long as your report object can save the file to disk someplace, you can attach that file to your email message and then send it.
While I do not claim to know anything about how Crystal Decisions handles exports, etc. Perhaps something like this would work:
(I got this code from: https://msdn.microsoft.com/en-us/library/ms226036(v=vs.90).aspx)
private void ExportToDisk (string fileName)
{
ExportOptions exportOpts = new ExportOptions();
DiskFileDestinationOptions diskOpts =
ExportOptions.CreateDiskFileDestinationOptions();
exportOpts.ExportFormatType = ExportFormatType.RichText;
exportOpts.ExportDestinationType =
ExportDestinationType.DiskFile;
diskOpts.DiskFileName = fileName;
exportOpts.ExportDestinationOptions = diskOpts;
Report.Export(exportOpts);
}
You will need to change the ExportFormatType property.
Then, simply attach the file to your email and send:
Attachment Files = new Attachment(filename);
Mailmsg.Attachments.add(files);
Clients.Send(Mailmsg);
I created a scheduled task that is supposed to scrape data from a website, store this in the database, create a daily pdf and send a weekly mail.
The first 2 parts run smoothly, but it seems that the other 2 are skipped regardless of what I try to do.
Creating a file is not the problem because it creates a daily .log file and writes to it.
For the pdf I use the PdfSharp library, for the mail the System.Net.Mail.
PDF
PdfDocumentRenderer renderer = new PdfDocumentRenderer(false, PdfFontEmbedding.Always);
renderer.Document = document;
renderer.RenderDocument();
string directory = DateTime.Now.Year.ToString();
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
string filename = directory + #"\NewCos" + DateTime.Now.DayOfWeek + ".pdf";
if (renderer.PageCount > 0)
renderer.PdfDocument.Save(filename);
Any help would be much appreciated.
first of all add iTextSharp nuget Pack to your project
itextsharp
then for creating a pdf you just need to do something like this:
Document myDocument = new Document(PageSize.A4, -60, -40, 10, 10);
PdfPTable MainTable = new PdfPTable(1);
PdfPCell pdfTableCell11 = new PdfPCell(new Paragraph("Hellod PDF!", RegularFont10));
MainTable.AddCell(pdfTableCell11);
string path = "C:\\Customer\\";
PdfWriter.GetInstance(myDocument, new FileStream(path + "\\Invoice_" + DateTime.Now.DayOfWeek + ".pdf", FileMode.Create));
myDocument.Open();
myDocument.Add(MainTable);
myDocument.Close();
if you have a complex pdf you can manage it by creating tables inside tables ... based on my experience it is better to have a main table with just one cell (which is all your doc) and then create any desire table and add those to this main table. so instead of "PdfPCell" you might have different "PdfPTable"s for each section.
It were simply some components that were not installed and the fact that I used relative paths iso fixed direcory settings
I need to create and write to a .dat file. I'm guessing that this is pretty much the same process as writing to a .txt file, but just using a different extension.
In plain english I would like to know how to:
-Create a .dat file
-Write to it
-And save the file using SaveFileDialog
There are a few pages that I've been looking at, but I think that my best explanation will come from this site because it allows me to state exactly what I need to learn.
The following code is what I have at the moment. Basically it opens a SaveFileDialog window with a blank File: section. Mapping to a folder and pressing save does not save anything because there is no file being used. Please help me use this to save files to different locations.
Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
dlg.FileName = "";
dlg.DefaultExt = "";
Nullable<bool> result = dlg.ShowDialog();
if (result == true)
{
string filename = dlg.FileName;
}
Pages that I've been looking at:
-http://msdn.microsoft.com/en-us/library/8bh11f1k.aspx
-http://social.msdn.microsoft.com/Forums/en-US/cd0b129f-adf1-4c4f-9096-f0662772c821/how-to-use-savefiledialog-for-save-text-file
-http://msdn.microsoft.com/en-us/library/system.io.file.createtext(v=vs.110).aspx
Note that the SaveFileDialog only yields a filename but does not actually save anything.
var sfd = new SaveFileDialog {
Filter = "Text Files (*.txt)|*.txt|All files (*.*)|*.*",
// Set other options depending on your needs ...
};
if (sfd.ShowDialog() == true) { // Returns a bool?, therefore the == to convert it into bool.
string filename = sfd.FileName;
// Save the file ...
}
Use the filename you are getting from the SaveFileDialog and do the following:
File.WriteAllText(filename, contents);
That's all if you intend to write text to the file.
You can also use:
File.WriteAllLines(filename, contentsAsStringArray);
using(StreamWriter writer = new StreamWriter(filename , true))
{
writer.WriteLine("whatever your text is");
}
I have a console application in which we are creating xlsx files using OPENXML, we are able to create xlsx file & save it into specific folder in application.
But Now we want to show that file as a Save/Open dialog pop up. Then we can able to specify a particular path to save/ to open the existing files.
I am new to this OpenXml, Can anyone please help me on this to proceed further? How can I acheive this? Do we have any built-in DLL for this?
Thanks.
se the Save file dialog. It will prompts the user to select a location for saving a file. After that you can use saveFileDialog.FileName.ToString() property to get the full path.
See the sample code below:
//Save a file in a particular format as specified in the saveAsType parameter
private void OpenSaveFileDialog(int saveAsType)
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.InitialDirectory = Convert.ToString(Environment.SpecialFolder.MyDocuments);
saveFileDialog.Filter = "CSV|*.csv|Excel|*.xlsx";
saveFileDialog.FilterIndex = saveAsType;
saveFileDialog.Title = "Save Data";
saveFileDialog.FileName = "My File";
saveFileDialog.ShowDialog();
if (saveFileDialog.FileName != "")
{
//File Path = m_fileName
m_fileName = saveFileDialog.FileName.ToString();
//FilterIndex property is one-based.
switch (saveFileDialog.FilterIndex)
{
case 1:
m_fileType = 1;
break;
case 2:
m_fileType = 2;
break;
}
}
}
Ref:http://msdn.microsoft.com/en-us//library/system.windows.forms.savefiledialog.aspx