We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Remove blockquote white space in long email chain in C#

I need to convert a msg file that contains a long email chain to PDF. The problem is that because of the email’s blockquotes (previous responses) the PDF format (and Word format) becomes unreadable after few pages. The body of earlier emails turns into a column of single letters. Is there a way to handle the blockquotes? I’m basically interested in removing the extra space before/after the blockquotes. We are currently using Aspose.Words 22.12 and Aspose.Email 22.11. blockquote.png (21.8 KB)
blockquote-2.png (14.5 KB)
The code we use is below:

public static byte[] OutlookMailMessageToPDF(byte[] MessageMimeContent)
        {
            Aspose.Email.License myLic = new License();
            myLic.SetLicense(AsposeLicense);
            byte[] Returnval = new byte[] { 0 };

            using (MemoryStream msgMS = new MemoryStream(MessageMimeContent))
            {
                Aspose.Email.Mapi.MapiMessage myMsg = Aspose.Email.Mapi.MapiMessage.Load(msgMS);
                using (MemoryStream mhtmlMS = new MemoryStream())
                {
                    
                    myMsg.Save(mhtmlMS, mhtmlSaveOps);
                    Returnval = mhtmlMS.ToArray();
                    Returnval = WordDocHelper.WordDocToPDF(mhtmlMS.ToArray(), new WordToPDFConversionOptions()
                    {
                        FromMHTMLOrHTML = true
                    });
                }
            }

            return Returnval;
        }

    public static byte[] WordDocToPDF(byte[] WordDoc, WordToPDFConversionOptions convertOps)
        {
            Aspose.Words.License myLic = new License();
            myLic.SetLicense(AsposeLicense);
            byte[] Returnval = new byte[] { 0 };

            using (MemoryStream docMS = new MemoryStream(WordDoc))
            {
                Document myDoc = new Document(docMS);

                //Handle putting the document to landscape or portrait
                foreach (Section section in myDoc.Sections)
                {
                    //Set the papersize we selected
                    foreach (PaperSize paperSize in Enum.GetValues(typeof(PaperSize)).Cast<PaperSize>())
                    {
                        if (paperSize.ToString() == convertOps.PaperSize.ToString())
                        {
                            section.PageSetup.PaperSize = paperSize;
                        }
                    }

                    //Check the orientation desired
                    section.PageSetup.Orientation = (convertOps.PortraitOrientation) ? Orientation.Portrait : Orientation.Landscape;
                }

                //If HTML, fix the tables for width
                if(convertOps.FromMHTMLOrHTML)
                {
                    foreach (Section section in myDoc.Sections)
                    {
                        //Try to Fix any and all tables
                        foreach (Aspose.Words.Tables.Table table in section.GetChildNodes(NodeType.Table, true))
                        {
                            table.AutoFit(Aspose.Words.Tables.AutoFitBehavior.AutoFitToWindow);
                        }
                    }
                }
                
                //Processing to do if the type of message we are converting is html or mhtml
                if (convertOps.FromMHTMLOrHTML)
                {
                    Aspose.Words.Layout.LayoutCollector myLayout = new Aspose.Words.Layout.LayoutCollector(myDoc);
                    Aspose.Words.Layout.LayoutEnumerator myLayoutEnum = new Aspose.Words.Layout.LayoutEnumerator(myDoc);

                    foreach (Section section in myDoc.Sections)
                    {
                        //Try to Fix any images
                        foreach (Aspose.Words.Drawing.Shape shape in section.GetChildNodes(NodeType.Shape, true))
                        {
                            if (shape.IsImage)
                            {
                                double targetWidth = 0;
                                bool Resize = false;
                                object renderObject = myLayout.GetEntity(shape);
                                myLayoutEnum.Current = renderObject;
                                double availableWidth = section.PageSetup.PageWidth - section.PageSetup.LeftMargin - section.PageSetup.RightMargin;
                                if (shape.Width > availableWidth)
                                {
                                    Resize = true;
                                    targetWidth = availableWidth;
                                }

                                float Xrightmost = myLayoutEnum.Rectangle.Right;
                                float Xleftmost = myLayoutEnum.Rectangle.Left;
                                Aspose.Words.Rendering.PageInfo pageInfo = myLayoutEnum.Document.GetPageInfo(myLayoutEnum.PageIndex - 1);
                                if (Xrightmost > pageInfo.WidthInPoints)
                                {
                                    Resize = true;
                                    targetWidth = shape.Width - (Xrightmost - pageInfo.WidthInPoints);
                                }

                                if (Resize)
                                {
                                    shape.Height = (targetWidth / shape.Width) * shape.Height;
                                    shape.Width = targetWidth;
                                }
                            }
                        }
                    }
                }
                
                //Setup PDF Save options
                Aspose.Words.Saving.PdfSaveOptions myOps = (Aspose.Words.Saving.PdfSaveOptions)Aspose.Words.Saving.PdfSaveOptions.CreateSaveOptions(SaveFormat.Pdf);
                myOps.UseAntiAliasing = convertOps.UseAntiAliasing;
                myOps.UseHighQualityRendering = convertOps.UseHighQualityRendering;
                myOps.ImageCompression = Aspose.Words.Saving.PdfImageCompression.Jpeg;
                myOps.JpegQuality = convertOps.JpegQuality;
                myOps.PrettyFormat = true;
                myOps.TextCompression = (convertOps.TextCompression == PDFTextCompression.FLATE) ? Aspose.Words.Saving.PdfTextCompression.Flate :
                    Aspose.Words.Saving.PdfTextCompression.None;

                using (MemoryStream pdfMS = new MemoryStream())
                {
                    myDoc.Save(pdfMS, myOps);
                    Returnval = pdfMS.ToArray();
                }
            }

            return Returnval;
        }

@vferrucci Could you please attach your input and output documents here or our reference? We will check the issue and provide you more information.

Unfortunately, I cannot provide the documents because they contain some sensitive information. However, I believe I was able to resolve the issue. For this, I needed to specify the Margins for the sections and Indents for the paragraphs.

public static byte[] WordDocToPDF(byte[] WordDoc, WordToPDFConversionOptions convertOps)
{
    Aspose.Words.License myLic = new License();
    myLic.SetLicense(AsposeLicense);
    byte[] Returnval = new byte[] { 0 };

    using (MemoryStream docMS = new MemoryStream(WordDoc))
    {
        Document myDoc = new Document(docMS);
        NodeCollection paragraphs = myDoc.GetChildNodes(NodeType.Paragraph, true);
        //Handle putting the document to landscape or portrait
        foreach (Section section in myDoc.Sections)
        {

            section.PageSetup.LeftMargin = 15;
            section.PageSetup.RightMargin = 5;

            //Set the papersize we selected
            foreach (PaperSize paperSize in Enum.GetValues(typeof(PaperSize)).Cast<PaperSize>())
            {
                if (paperSize.ToString() == convertOps.PaperSize.ToString())
                {
                    section.PageSetup.PaperSize = paperSize;
                }
            }
            //Check the orientation desired
            section.PageSetup.Orientation = (convertOps.PortraitOrientation) ? Orientation.Portrait : Orientation.Landscape;
        }

        foreach (Paragraph para in paragraphs)
        {
            para.ParagraphFormat.LeftIndent = 0;//fixes blockquotes indenting
            para.ParagraphFormat.RightIndent = 0;//fixes blockquotes indenting

        }
        .....  }

    return Returnval;
}

@vferrucci It is perfect that you managed to resolve the issue on your side. Please feel free to ask in case of any other issues, we will be glad to help you.