PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Thu Mar 28, 2024 1:17 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: Wed Apr 11, 2007 9:01 pm 
Offline

Joined: Thu Apr 05, 2007 8:15 pm
Posts: 1
After I create a pdf I want to grab it and instantly send it out as an email attachment. It seems to attach and send the pdf perfectly. The problem is that when I open the sent attachment Acrobat says "A drawing error occurred." and displays a blank page.
I use the same file name and path that I used to create it so I know its grabbing the right file. The attachment even matches the one created in file size. My guess is that I'm not closing the created pdf out of memory before I send it. But as you can see in my code below I don't open the pdf in a viewer and I try to use document.close() but that doesn't fix it. Can anyone tell me what I'm doing wrong or how to fix this problem? Below is my code. Thanks in advance.

public void PdfCreate()
{
int y = 0; //top of the page on the y axis
//create a pdf document
PdfDocument document = new PdfDocument();
//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
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);
document.Close(); // doesn't seem to work
}

protected void Email()
{
try
{
MailMessage msgMail = new MailMessage();

msgMail.To = "you@mail.com";
msgMail.From = "me@mail.com";
msgMail.Subject = "Attachment Test";

msgMail.BodyFormat = MailFormat.Text;
msgMail.Body = "Check out the attachment!";
msgMail.Attachments.Add(new MailAttachment(filename));

SmtpMail.Send(msgMail);
}
catch (Exception ex)
{
}
}


Top
 Profile  
Reply with quote  
PostPosted: Fri Apr 13, 2007 8:35 pm 
Offline

Joined: Fri Apr 13, 2007 6:32 pm
Posts: 3
Location: USA
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
}


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 132 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group