Printing Email Messages to XPS and TIFF

ה Aspose.Email.Printing מרחב שמות זה מספק מגוון רחב של תכונות להדפסת הודעות דואר לפורמטים שונים (XPS או TIFF) ולתצורת פריסת דפים. מאמר זה מתאר אותם. ישנן מספר אפשרויות כיצד ניתן להדפיס הודעת דואר עם Aspose.Email:

  1. הדפסת גוף ההודעה בלבד.
  2. הדפסת גוף ההודעה והכותרות.
  3. הדפסת גוף HTML.
  4. הגדרת פריסת העמוד.
  5. התאמת גודל TIFF למדפסת באופן אוטומטי.
  6. התאמת ה-DPI המבוקש ל‑TIFF הפלט.

המחלקה המרכזית עבור פעולות אלו היא MailPrinter. ה- Print שיטה מקבלת MailMessage, שם קובץ יצוא, ו- PrintFormat הערך (XPS או Tiff).

הדפסת גוף ההודעה

קטטע הקוד הבא יוצר הודעה ומדפיס אותה ללא כותרות, תחילה לקובץ XPS ולאחר מכן לקובץ TIFF. הגדר את FormattingFlags מאפיין ל MessageFormattingFlags.None להצגת רק גוף ההודעה.

// Declare message as a MailMessage instance
MailMessage message = new MailMessage();
message.From = "user1@domain.com";
message.To = "user2@domain.com";
message.Subject = "My First Mail";
message.Date = DateTime.Now;
message.Body = "Text is the Mail Message";

// Instantiate an instance of MailPrinter and set the FormattingFlags to None to display only the message body
var printer = new Aspose.Email.Printing.MailPrinter();
printer.FormattingFlags = Aspose.Email.Printing.MessageFormattingFlags.None;

// Print the email to an XPS and TIFF file
printer.Print(message, dataDir + "PrintXPS_out.xps", Aspose.Email.Printing.PrintFormat.XPS);
printer.Print(message, dataDir + "PrintTIFF_out.tiff", Aspose.Email.Printing.PrintFormat.Tiff);

הדפסת כותרות הודעה וגוף

כדי להציג ולהדפיס את כותרות ההודעה יחד עם גוף ההודעה, שנה את FormattingFlags אל MessageFormattingFlags.MailInfo.

printer.FormattingFlags = Aspose.Email.Printing.MessageFormattingFlags.MailInfo;

הדפסת הודעה עם גוף HTML

ניתן גם להדפיס הודעות עם גוף HTML. הגדר את תוכן ה-HTML באמצעות HtmlBody property before printing.

// Body of the email message
message.IsBodyHtml = true;
message.HtmlBody = "<html><body><h1>Hello this is html body in Heading 1 format </h1></body></html>";

Setting the Page Layout for Printing

MailPrinter provides controls for setting the following page layout properties:

|Property|Description|Default Value| | :- | :- | :- | |FormattingFlags|Show or hide message details.|None [1]| |MarginTop|Get or set the top margin.|0.5| |MarginLeft|Get or set the left margin.|0.5| |MarginBottom|Get or set the bottom margin.|0.5| |MarginRight|Get or set the right margin.|0.5| |PageUnit|Get or set measurement units.|Inch [2]| |PageHeight|Get or set the page height.|11.69| |PageWidth|Get or set the page width.|8.27|

  • [1] There are two flags: MailInfo ו None.
  • [2] Page units can be one of Inch, Pixel, Point, Cm, or Millimeter.

The code snippet below uses arbitrary settings to illustrate how these properties are used. It sets up a page 20 cm high and 8 cm wide, with 2 cm margins.

// Instantiate an instance of MailPrinter
var printer = new Aspose.Email.Printing.MailPrinter();

// Just for testing, get the default property values
double width = printer.PageWidth;
double height = printer.PageHeight;

// Set page layout for printing
printer.PageUnit = Aspose.Email.Printing.PrinterUnit.Cm;
printer.MarginTop = 2;
printer.MarginBottom = 2;
printer.MarginLeft = 2;
printer.MarginRight = 2;
printer.PageWidth = 8;
printer.PageHeight = 20;

התאמה אוטומטית של TIFF

Aspose.Email.Printing מספקת את MessageFormattingFlags.AutoFitWidth flag, which allows you to auto-fit the TIFF to the printer. The following code snippet shows how to use auto-fit.

string dataDir = RunExamples.GetDataDir_KnowledgeBase();

// Instantiate an instance of MailPrinter
Printing.MailPrinter printer = new Printing.MailPrinter();
printer.FormattingFlags = Printing.MessageFormattingFlags.AutoFitWidth;
MailMessage msg = MailMessage.Load(dataDir + "message3.msg", new MsgLoadOptions());
printer.Print(msg, dataDir + "AutoFit_out.tiff", Printing.PrintFormat.Tiff);

כוון DPI יעד ל‑TIFF הפלט

השתמש ב- DpiX ו DpiY properties to control the resolution of the output TIFF.

// The path to the File directory and instantiate an instance of MailPrinter
string dataDir = RunExamples.GetDataDir_KnowledgeBase();
Printing.MailPrinter printer = new Printing.MailPrinter();
printer.DpiX = 300;
printer.DpiY = 300;
MailMessage msg = MailMessage.Load(dataDir + "message3.msg", new MsgLoadOptions());
printer.Print(msg, dataDir + "AdjustTargetDPI_out.tiff", Printing.PrintFormat.Tiff);