Hi,
I looked at your code and noticed that the 'PdfCreate()' routine is missing a filename on the object assignment line. You need to define a file name variable, add a file name, and place the file for now in the bin\debug sub-directory. You also should comment out the 'document.Save(filename);' line (see modified example below). This should take care of the PDF generation error. I got both routines shown below to work properly.
Regards,
Rivien
Before:
PdfDocument document = new PdfDocument();
After:
string filename = "TestFile1.pdf";
PdfDocument document = new PdfDocument(filename);
BTW, I was able to create a PDF file and email it to myself. You should look at my email handler, since it's different from yours. I hope this helps!
/// <summary>
/// Sends an email with one PDF attachment
/// Requires namespaces: System.Net.Mail, System.Net.Mime, and System.Net
/// USAGE: CreateMessageWithAttachment("smtp.yourSMTPserver.com");
/// </summary>
public static void CreateMessageWithAttachment(string server)
{
// Specify the file to be attached and sent.
// This example assumes that a file named 'TestMyPDF.pdf' exists in the
// current working directory (probably 'bin\debug\'.
string file = "TestMyPDF.pdf";
// Create a message and set up the recipients.
MailMessage mailMessage = new MailMessage(
"toUser1@toDomain.com",
"toUser2@toDomain.com",
"My test PDF file.",
"See the attached pdf file.");
// Create the file attachment for this e-mail message.
Attachment data = new Attachment(file, MediaTypeNames.Application.Pdf);
// Add time stamp information for the file.
ContentDisposition disposition = data.ContentDisposition;
disposition.CreationDate = System.IO.File.GetCreationTime(file);
disposition.ModificationDate = System.IO.File.GetLastWriteTime(file);
disposition.ReadDate = System.IO.File.GetLastAccessTime(file);
// Add the file attachment to this e-mail message.
mailMessage.Attachments.Add(data);
//Send the message.
SmtpClient mailClient = new SmtpClient(server);
// Add credentials if the SMTP server requires them.
System.Net.NetworkCredential basicAuthenticationInfo = new System.Net.NetworkCredential("username", "password");
mailClient.UseDefaultCredentials = false;
mailClient.Credentials = basicAuthenticationInfo;
mailClient.Send(mailMessage);
data.Dispose();
}
/// <summary>
/// Creates a PDF file
/// </summary>
public static void PdfCreate()
{
int y = 0; //top of the page on the y axis
string filename = Guid.NewGuid().ToString().ToUpper() + ".pdf";
//create a pdf document
PdfDocument document = new PdfDocument(filename);
//create a pdf page
PdfPage page = document.AddPage();
//required for writing anything to a pdf
XGraphics gfx = XGraphics.FromPdfPage(page);
//required for writing text to pdf
XTextFormatter tf = new XTextFormatter(gfx);
//used to create the text boxes that the text goes into
XRect rect = new XRect();
//Draw the PDF
XFont fontH1 = new XFont("Times New Roman", 30, XFontStyle.Bold);
gfx.DrawString("PDF TEST FILE", fontH1, XBrushes.DarkBlue, 123, 50);
//y = DrawHeader(gfx, tf, rect, y);
//y = DrawContactBox(gfx, tf, rect, y);
//y = DrawVendortBox(gfx, tf, rect, y);
//y = DrawCustomerBox(gfx, tf, rect, y);
//y = DrawJobInfoBox(gfx, tf, rect, y);
//y = DrawConditionsBox(gfx, tf, rect, y);
//y = DrawServiceBox(gfx, tf, rect, y);
//y = DrawInstructionsBox(gfx, tf, rect, y);
// Save the document
//document.Save(filename); // YOU DO NOT NEED THIS LINE ... IT AUTO SAVES ON THE CLOSE EVENT
document.Close(); // doesn't seem to work
}
|