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

Free Support Forum - aspose.com

Insert unique barcode on each page of mail merged document

I have an existing word document that I’m mail merging to create a form letter. Each page must have a UNIQUE barcode inserted on the bottom of the page. I have everything working if the document is only 1 page, now I need to loop through each page and add the barcode. How would I loop through each page of an existing document? Or, how would I tell builder to move to the next page?

Current code below:

        Aspose.Words.Document doc = new Aspose.Words.Document(inputWordDoc);
        DocumentBuilder builder = new DocumentBuilder(doc);

#Region Start Page Loop

        double bitmapWidth = ConvertUtil.InchToPoint(0.5f);
        double bitmapHeight = bitmapWidth;

        double top = (builder.PageSetup.PageHeight + builder.PageSetup.TopMargin + builder.PageSetup.BottomMargin) - ConvertUtil.InchToPoint(1.55f);
        double left = 340f;

        builder.InsertImage(GetPageBarcode(guid, pageNumber), RelativeHorizontalPosition.Page, left, RelativeVerticalPosition.Page, top, bitmapWidth, bitmapHeight, WrapType.None);         

#Region End Page Loop

        doc.Save(@"C:\Temp\test\Document_Lock_Confirmation_Output_WithImage.doc");

This Topic is created by imran.rafique using the Email to Topic plugin.

@_

Thanks for your inquiry. Please check following code snippet to add different barcode images on different pages of Word document. Hopefully it will help you to accomplish the task.

Aspose.Words.Document doc = new Aspose.Words.Document("Input.docx");
DocumentBuilder builder = new DocumentBuilder(doc);

List<int> pages = new List<int>();

double bitmapWidth = ConvertUtil.InchToPoint(0.5f);
double bitmapHeight = bitmapWidth;

double top = (builder.PageSetup.PageHeight + builder.PageSetup.TopMargin + builder.PageSetup.BottomMargin) - ConvertUtil.InchToPoint(2.55f);
double left = 340f;

LayoutCollector layoutCollector = new LayoutCollector(doc);

NodeCollection paragraphs = doc.GetChildNodes(NodeType.Paragraph, true);

foreach (Paragraph para in paragraphs)
{

if (!pages.Exists(element => element == layoutCollector.GetStartPageIndex(para)))
{
    pages.Add(layoutCollector.GetStartPageIndex(para));

    builder.MoveTo(para);
    builder.InsertImage("barcode" + layoutCollector.GetStartPageIndex(para) + ".jpg", RelativeHorizontalPosition.Page, left, RelativeVerticalPosition.Page, top, bitmapWidth, bitmapHeight, WrapType.None);
}
}

doc.Save(@"output.docx");

This solved my problem! Thank you!