Can not insert a image into a primary footer

Hey, I have code to insert a image to the primary footer, it works for most docs, however, it failed for the attached 2 docs. It total failed for the first one, and it insert the image to some pages for the second doc, but not all the pages.
Please advise ASAP.
C# codes:

private void CreateBarcodePrimaryFooter(String barcodeText)
{
    DocumentBuilder builder = new DocumentBuilder(_asposeDocument);
    // Get page setup for the current section.
    Aspose.Words.PageSetup pageSetup = builder.PageSetup;
    // Set to 'true' if you want different headers/footers for first, even and odd pages.
    pageSetup.DifferentFirstPageHeaderFooter = false;
    pageSetup.OddAndEvenPagesHeaderFooter = false;
    builder.MoveToHeaderFooter(Aspose.Words.HeaderFooterType.FooterPrimary);
    // We use table with two cells to make one part of the text on the line (with page numbering)
    // to be aligned left, and the other part of the text (with copyright) to be aligned right.
    builder.StartTable();
    // Calculate table width as total page width with left and right marins subtracted.
    double tableWidth = pageSetup.PageWidth - pageSetup.LeftMargin - pageSetup.RightMargin;
    builder.InsertCell();
    // Set first cell to the page width.
    builder.CellFormat.Width = tableWidth;
    builder.InsertImage(CreateBarCodeImage(barcodeText));
    // Align this text to the left.
    builder.CurrentParagraph.ParagraphFormat.Alignment = ParagraphAlignment.Center;
    builder.EndRow();
    builder.EndTable();
}
private Image CreateBarCodeImage(String barcodetext)
{
    BarCodeBuilder builder = new BarCodeBuilder();
    builder.SymbologyType = Aspose.BarCode.Symbology.DataMatrix;
    builder.CodeLocation = CodeLocation.None;
    builder.CodeText = barcodetext;
    return builder.GenerateBarCodeImage();
}

Hi
Thanks for your request. There are several sections in your documents. So I think that you can try using the following code.

DocumentBuilder builder = new DocumentBuilder(doc);
// Get page setup for the current section.
Aspose.Words.PageSetup pageSetup = builder.CurrentSection.PageSetup;
// Set to 'true' if you want different headers/footers for first, even and odd pages.
pageSetup.DifferentFirstPageHeaderFooter = false;
pageSetup.OddAndEvenPagesHeaderFooter = false;
for (int sectIndex = 0; sectIndex < doc.Sections.Count; sectIndex++)
{
    builder.MoveToSection(sectIndex);
    builder.MoveToHeaderFooter(Aspose.Words.HeaderFooterType.FooterPrimary);
    // We use table with two cells to make one part of the text on the line (with page numbering)
    // to be aligned left, and the other part of the text (with copyright) to be aligned right.
    builder.StartTable();
    // Calculate table width as total page width with left and right marins subtracted.
    double tableWidth = pageSetup.PageWidth - pageSetup.LeftMargin - pageSetup.RightMargin;
    builder.InsertCell();
    // Set first cell to the page width.
    builder.CellFormat.Width = tableWidth;
    builder.RowFormat.Height = 100;
    builder.InsertImage(CreateBarCodeImage("barcodeText"));
    // Align this text to the left.
    builder.CurrentParagraph.ParagraphFormat.Alignment = ParagraphAlignment.Center;
    builder.EndRow();
    builder.EndTable();
}

I hope this could help you.
Best regards.

Have you try and test the doc at your side?
Is it works?
Thanks,
Dong.

Hi
Yes, I tried to use your documents and all works properly on my side. Barcode image is inserted into Primary footer. But there are some sections in your documents that have first page footer. If you need, you can just remove these footers.
Best regards.

Hey, Alex:
Thanks for the reply.
I can not simple remove the first footer because they are useful information. In my case, I need to insert the barcode at the bottom of every page. So far as you said, if the page has the first footer, then the barcode can not insert into the page by using the primary footer.
Do you have other way to ensure the barcode insert into each page regarding the first footer exist or not?
Thanks,
Dong.

Hi
You can try inserting barcode image into the each footer in the document. Please try using the following code.

DocumentBuilder builder = new DocumentBuilder(doc);
// Get page setup for the current section.
Aspose.Words.PageSetup pageSetup = builder.CurrentSection.PageSetup;
// Set to 'true' if you want different headers/footers for first, even and odd pages.
pageSetup.DifferentFirstPageHeaderFooter = false;
pageSetup.OddAndEvenPagesHeaderFooter = false;
// Create collection of footer types
ArrayList footerTypesList = new ArrayList();
footerTypesList.Add(HeaderFooterType.FooterEven);
footerTypesList.Add(HeaderFooterType.FooterFirst);
footerTypesList.Add(HeaderFooterType.FooterPrimary);
for (int sectIndex = 0; sectIndex < doc.Sections.Count; sectIndex++)
{
    builder.MoveToSection(sectIndex);
    // InsertBarcode into the each footer in the document
    foreach (HeaderFooterType footerType in footerTypesList)
    {
        builder.MoveToHeaderFooter(footerType);
        // We use table with two cells to make one part of the text on the line (with page numbering)
        // to be aligned left, and the other part of the text (with copyright) to be aligned right.
        builder.StartTable();
        // Calculate table width as total page width with left and right marins subtracted.
        double tableWidth = pageSetup.PageWidth - pageSetup.LeftMargin - pageSetup.RightMargin;
        builder.InsertCell();
        // Set first cell to the page width.
        builder.CellFormat.Width = tableWidth;
        builder.RowFormat.Height = 100;
        builder.InsertImage(CreateBarCodeImage("barcodeText"));
        // Align this text to the left.
        builder.CurrentParagraph.ParagraphFormat.Alignment = ParagraphAlignment.Center;
        builder.EndRow();
        builder.EndTable();
    }
}

I hope this could help you.
Best regards.

Thanks for the reply.
I just test it. It is not what I want. After I apply your code, some page has more than one barcode images while some page still don’t have barcode at all.
Would you please spend some time and see any solution here?
Thanks,
Dong.

Unfortunately I can’t reproduce this issue on my side. I used your documents for testing and each footer has only one barcode image. Could you please attach document that will allow me to reproduce issue? Maybe your document already contains barcode in the footer and you can try removing all images from footer before inserting barcode.
Best regards.

At the first attached 07_17100A.doc, you will find out the first page don’t have the barcode.
At the second attached doc, you will find out the last page don’t have the barcode. and some other page do have multiple barcode image, but they are overrided each other, so you only see one barcode there. but indeed, it is more than one. The reason I know it is more than one is if i convert the doc to pdf, at the pdf, I see more than one barcode there.

I still can’t reproduce your issue. Could you please provide me your e-mail? I will send to you my output documents. I tried to open generated documents using DocumentExplorer (Aspose.Words demo app) and each footer has only one image.
I use the latest version of Aspose.Words and Aspose.Pdf for testing. Please try using the latest versions.
Best regards.

You are right, I find a problem at my coding, I still use:

builder.MoveToHeaderFooter(Aspose.Words.HeaderFooterType.FooterPrimary);

instead:

builder.MoveToHeaderFooter(footerType);

Now the issue is gone.
Thanks so much for your help.

Hey Alexey:
Will you please take a look the attached doc, I found out the barcode can only insert at the last page and the barcode image can not be fully view.
Thanks,
Dong.

Hi
Thanks for your request. Maybe you should try using the following code:

// InsertBarcode into the each footer in the document
foreach (HeaderFooterType footerType in footerTypesList)
{
    Image barCode = CreateBarCodeImage("BIC1#######/BIC2#######/TRADEREF1###########/TRADEREF2###########/2007-10-11/2007-09-12/2007-08-13/USD/452345#######/567###.17##");
    builder.MoveToHeaderFooter(footerType);
    // We use table with two cells to make one part of the text on the line (with page numbering)
    // to be aligned left, and the other part of the text (with copyright) to be aligned right.
    Table tab = builder.StartTable();
    // Calculate table width as total page width with left and right marins subtracted.
    double tableWidth = pageSetup.PageWidth - pageSetup.LeftMargin - pageSetup.RightMargin;
    builder.InsertCell();
    // Set first cell to the page width.
    builder.CellFormat.Width = tableWidth;
    builder.RowFormat.Height = ConvertUtil.PixelToPoint(barCode.Height);
    builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
    Shape barcode = builder.InsertImage(barCode);
    builder.EndRow();
    builder.EndTable();
    HeaderFooter hf = (HeaderFooter)builder.CurrentParagraph.GetAncestor(NodeType.HeaderFooter);
    hf.InsertBefore(tab, hf.FirstChild);
}

Hope this helps.
Best regards.

Thx.

Dong.

Hey Alexey:
The attached doc have two so called “first page footer” with section one and two, and it caused the footer display error after insert the barcode image.
Is any way to display the footer correctly in this case?
Thanks so much!
Dong.

Hi
Thanks for your inquiry. This occurs because footer in the second section is linked to previous. Please try using the following code:

for (int sectIndex = 0; sectIndex < doc.Sections.Count; sectIndex++)
{
    builder.MoveToSection(sectIndex);
    // InsertBarcode into the each footer in the document
    foreach (HeaderFooterType footerType in footerTypesList)
    {
        Image barCode = CreateBarCodeImage("BIC1#######/BIC2#######/TRADEREF1###########/TRADEREF2###########/2007-10-11/2007-09-12/2007-08-13/USD/452345#######/567###.17##");
        if (builder.Document.Sections[sectIndex].HeadersFooters[footerType] != null)
        {
            if (!builder.Document.Sections[sectIndex].HeadersFooters[footerType].IsLinkedToPrevious)
            {
                builder.MoveToHeaderFooter(footerType);
                // We use table with two cells to make one part of the text on the line (with page numbering)
                // to be aligned left, and the other part of the text (with copyright) to be aligned right.
                Table tab = builder.StartTable();
                // Calculate table width as total page width with left and right marins subtracted.
                double tableWidth = pageSetup.PageWidth - pageSetup.LeftMargin - pageSetup.RightMargin;
                builder.InsertCell();
                // Set first cell to the page width.
                builder.CellFormat.Width = tableWidth;
                builder.RowFormat.Height = ConvertUtil.PixelToPoint(barCode.Height);
                builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
                Shape barcode = builder.InsertImage(barCode);
                builder.EndRow();
                builder.EndTable();
                HeaderFooter hf = (HeaderFooter)builder.CurrentParagraph.GetAncestor(NodeType.HeaderFooter);
                hf.InsertBefore(tab, hf.FirstChild);
            }
        }
    }
}

Hope this helps.
Best regards.

Thx for the info.
Will test out.
Dong.

your latest code will cause the attached two docs can not insert the barcode. Anyway to fix it?

Also just find out the barcode can only inserted at the first page of the attached doc.
Please advise.
Thanks,
Dong.

Here is another one cause the display problem. please advise.