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

Free Support Forum - aspose.com

C# Check if PDF is Empty after Mail Merge

Hello Support,

In few situations we have encountered MS Word document having mail merge fields (letters) converted into PDF document produces empty PDF file.
We want to check it programmatically and report back to user so that they are aware of the issue and follow work around to continue the flow.

Thanks,
Parthiban

@parthiban.natarajan,

Please compress the following resources into ZIP format and attach the .zip file here for testing:

  • A simplified source Word document containing the mail merge fields
  • Aspose.Words v21.8 generated output file that you want to check if PDF is empty or not.
  • A standalone simplified Console Application (source code without compilation errors) that helps us to reproduce this problem on our end and attach it here for our testing. Please do not include Aspose.Words DLL files in it to reduce the file size.

We will then start investigation into your particular scenario and provide you more information.

Hi Hafeez,

Thank you for quick response.

I am looking to find C# code logic to check if PDF file is empty using Aspose library.

@parthiban.natarajan,

After performing mail merge on a Word document, you can check if the resultant PDF is empty (will have some text content in it or not) by using the following C# code:

public static void ConvertWordToPDF()
{
    Aspose.Words.Document doc = new Aspose.Words.Document("Word.docx");

    // Perform Mail Merge or any other operations etc
    //doc.MailMerge.Execute(new string[] { "merge field 1" }, new object[] { "none-empty value" });

    string text = doc.ToString(Aspose.Words.SaveFormat.Text).Trim();
    if (string.IsNullOrEmpty(text))
    {
        Console.WriteLine("Word document has no text. so we will not save to an empty PDF");
    }
    else
    {
        // Convert Word to PDF
        doc.Save("empty PDF to check.pdf");
    }
}

Hi Hafeez,

Thank you for quick response. Can you please also give C# code for checking empty PDF file?

@parthiban.natarajan

You can check for an empty page in a PDF file by using the following C# code:

public static bool CheckIfPdfIsEmpty(string pdfPath)
{
    bool pdfIsEmpty = true;

    Aspose.Pdf.Document emptyPdfFile = new Aspose.Pdf.Document("pdf to check.pdf");
    foreach (Aspose.Pdf.Page page in emptyPdfFile.Pages)
    {
        if (!page.IsBlank(0.01d))
        {
            pdfIsEmpty = false;
            break;
        }
    }

    return pdfIsEmpty;
}