When mail merge outputs to PDF- an extra page is generated

Using Words 13.4 and PDF 8.0…


I’m executing a mail merge and saving the result to PDF. The resultant PDF has a first page that’s blank, and I don’t know why. I tried to use the code in one of the forums:

if (format == MailMergeOutputFormat.PDF)
{
// get rid of blank pages
var doc = new Aspose.Pdf.Document(dir.TargetFile.FullName);
int index = doc.Pages.Count;
while (index > 0)
{
Page page = doc.Pages[index];
if (IsPageBlank(page))
doc.Pages.Delete(index);
index–;
}

doc.Save();
}

But to no avail - programmatically, it looks like the first page has content, even though I am seeing none. I attached the word mail merge template and the resultant PDF. Help!

Hi Andrew,


Thanks for your inquiry. In reference to PDF, you can check a blank page using DOM. please check following code it checks whether page doesn’t contain any operators/annotations, data with white color, images with white pixels and no text. Please try IsBlankPage() instead of IsPageBlank as following.

Moreover I’m moving your request to Aspose.Total, there my colleague from Aspose.Words will address the generation of blank page during mail merge.


if (IsBlankPage(page))


static private bool HasOnlyWhiteColor(Page page)
{
foreach (Operator op in page.Contents)
if (op is Operator.SetColorOperator)
{
Operator.SetColorOperator opSC = op as Operator.SetColorOperator;
System.Drawing.Color color = opSC.getColor();
if (color.R != 255 || color.G != 255 || color.B != 255)
return false;
}

return true;
}

static private bool IsWhiteImage(XImage image)
{
MemoryStream ms = new MemoryStream();
image.Save(ms);
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(ms);
for (int j = 0; j < bmp.Height; j++)
for (int i = 0; i < bmp.Width; i++)
{
System.Drawing.Color color = bmp.GetPixel(i, j);
if (color.R != 255 || color.G != 255 || color.B != 255)
return false;
}
return true;
}

static private bool HasOnlyWhiteImages(Page page)
{
// return true if no images exist or all images are white
if (page.Resources.Images.Count == 0)
return true;
foreach (XImage image in page.Resources.Images)
if (!IsWhiteImage(image))
return false;
return true;
}


static private bool HasNoText(Page page)
{

var textabsorber = new Aspose.Pdf.Text.TextAbsorber();
page.Accept(textabsorber);
string content = textabsorber.Text;
if (content.Trim().Length == 0)
return true;
return false;
}

static private bool IsBlankPage(Page page)
{
if ((page.Contents.Count == 0 && page.Annotations.Count == 0) ||HasNoText(page)||
(HasOnlyWhiteColor(page) && HasOnlyWhiteImages(page)))
return true;
return false;
}


Best Regards

Hi Andrew,


Thanks for your inquiry. I have tested the mail merge scenario with shared Docx and have not found the blank page issue in output Pdf. I have found the text overlapped issue. Please check my reply from here:
https://forum.aspose.com/t/54644

Please supply us the code from your application that is causing the issue. We will investigate the issue on our side and provide you more information.