Inserting Barcode on Existing document

Hi,

I have an existing document. I want to insert a 2D matrix barcode on the existing document at the top right hand corner of a page. I want to iterate page by page and place the barcode only on the even number page. Is it possible to do this, can you please share an example for the same?

Thanks.

Hi Sayandip,


Thanks for your inquiry. I believe you can achieve what you are looking for after using the following code snippet:
DocumentBuilder builder = new DocumentBuilder();
builder.getDocument().getFirstSection().getPageSetup().setOddAndEvenPagesHeaderFooter(true);
builder.moveToHeaderFooter(HeaderFooterType.HEADER_EVEN);
Shape shape = builder.insertImage(“C:\Temp\barcodeImage.png”);
builder.getCurrentParagraph().getParagraphFormat().setAlignment(ParagraphAlignment.RIGHT);
builder.moveToDocumentEnd();
for (int i = 0; i < 200; i++)
builder.writeln(“Some Paragraphs”);
builder.getDocument().save(“C:\Temp\out.docx”);
Please let me know if I can be of any further assistance.

Best regards,

Thanks Awais, for the reply.

My barcode image is not fixed but it is dynamic. I need to place a different barcode image on every even page number. For example Image1 goes on page No 2, Image 2 goes on page number 4, Image 3 goes on page number 6 and so on.

Following is the logic I am looking in for-

Step1- Get the total number of pages in the existing document.

Step 2- Depending on the total page count construct the barcodes for every even number page.

Step 3- Insert the created barcodes on the respective even page at the top right hand corner.

The thing is the document is created dynamically so only once the document is created will I come to know how many pages it has and how many barcodes it will require.

Please help me with this logic using Aspose Words. Please let me know if there is any other way to achieve this. Thanks.

Hi Sayandip,


Thanks for the additional information. I believe, you can achieve what you need after using the code suggested below:
Document doc = new Document(“C:\Temp\input.docx”);

LayoutCollector layoutCollector = new LayoutCollector(doc);

NodeCollection paragraphs = doc.getChildNodes(NodeType.PARAGRAPH, true);
for (int page = 1; page <= doc.getPageCount(); page++) {
for (Paragraph paragraph : (Iterable<Paragraph>) paragraphs) {
if (layoutCollector.getStartPageIndex(paragraph) == page) {
AddImageToPage(paragraph, page);
break;
}
}
}

doc.save(“C:\Temp\out.docx”);

public static void AddImageToPage(Paragraph para, int page) {
Document doc = (Document) para.getDocument();
DocumentBuilder builder <font color="BLUE">=</font> <font color="RED"><b>new</b></font> DocumentBuilder<font color="BLUE"><b>(</b></font>doc<font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>
builder<font color="BLUE"><b>.</b></font>moveTo<font color="BLUE"><b>(</b></font>para<font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>

Shape shape <font color="BLUE">=</font> builder<font color="BLUE"><b>.</b></font>insertImage<font color="BLUE"><b>(</b></font><font color="PURPLE">"C:\\Temp\\Aspose.Words.png"</font><font color="BLUE"><b>,</b></font> RelativeHorizontalPosition<font color="BLUE"><b>.</b></font>PAGE<font color="BLUE"><b>,</b></font> <font color="BROWN">60</font><font color="BLUE"><b>,</b></font>
        RelativeVerticalPosition<font color="BLUE"><b>.</b></font>PAGE<font color="BLUE"><b>,</b></font> <font color="BROWN">60</font><font color="BLUE"><b>,</b></font> <font color="BLUE">-</font><font color="BROWN">1</font><font color="BLUE"><b>,</b></font> <font color="BLUE">-</font><font color="BROWN">1</font><font color="BLUE"><b>,</b></font> WrapType<font color="BLUE"><b>.</b></font>NONE<font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>

Shape textBox <font color="BLUE">=</font> <font color="RED"><b>new</b></font> Shape<font color="BLUE"><b>(</b></font>doc<font color="BLUE"><b>,</b></font> ShapeType<font color="BLUE"><b>.</b></font>TEXT_BOX<font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>

textBox<font color="BLUE"><b>.</b></font>setWrapType<font color="BLUE"><b>(</b></font>WrapType<font color="BLUE"><b>.</b></font>NONE<font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>
textBox<font color="BLUE"><b>.</b></font>setRelativeHorizontalPosition<font color="BLUE"><b>(</b></font>RelativeHorizontalPosition<font color="BLUE"><b>.</b></font>PAGE<font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>
textBox<font color="BLUE"><b>.</b></font>setRelativeVerticalPosition<font color="BLUE"><b>(</b></font>RelativeVerticalPosition<font color="BLUE"><b>.</b></font>PAGE<font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>

textBox<font color="BLUE"><b>.</b></font>setHeight<font color="BLUE"><b>(</b></font><font color="BROWN">30</font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>
textBox<font color="BLUE"><b>.</b></font>setWidth<font color="BLUE"><b>(</b></font><font color="BROWN">200</font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>
textBox<font color="BLUE"><b>.</b></font>setLeft<font color="BLUE"><b>(</b></font><font color="BROWN">150</font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>
textBox<font color="BLUE"><b>.</b></font>setTop<font color="BLUE"><b>(</b></font><font color="BROWN">80</font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>

textBox<font color="BLUE"><b>.</b></font>appendChild<font color="BLUE"><b>(</b></font><font color="RED"><b>new</b></font> Paragraph<font color="BLUE"><b>(</b></font>doc<font color="BLUE"><b>)</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>
builder<font color="BLUE"><b>.</b></font>insertNode<font color="BLUE"><b>(</b></font>textBox<font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>
builder<font color="BLUE"><b>.</b></font>moveTo<font color="BLUE"><b>(</b></font>textBox<font color="BLUE"><b>.</b></font>getFirstChild<font color="BLUE"><b>(</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>
builder<font color="BLUE"><b>.</b></font>writeln<font color="BLUE"><b>(</b></font><font color="PURPLE">"This is a custom note for page "</font> <font color="BLUE">+</font> page<font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>

}

Please let me know if I can be of any further assistance.

Best regards,

Hi Awais,

Thanks for the reply. I tried running the code on my target document(.doc file) but I am getting following error. The input document is .DOC initially generated from Aspose and not a .DOCX file -

The error comes at line

if (layoutCollector.getStartPageIndex(paragraph) == page)

java.util.EmptyStackException

at java.util.Stack.pop(Stack.java:80)

at com.aspose.words.ëZXB.ëZHZ(Unknown Source)

at com.aspose.words.ëZXB.ëZ(Unknown Source)

at com.aspose.words.LayoutCollector.ë4n(Unknown Source)

at com.aspose.words.LayoutCollector.ë4p(Unknown Source)

at com.aspose.words.LayoutCollector.getStartPageIndex(Unknown Source)

I tried running the code on one another document and there were two things observed-

1) The total page count returned was 1 greater than the actual. For e.g it returned 14 instead of 13.

2) Until page no 8 it inserted the barcodes properly but after that it inserted 2 barcodes on the same page of the word Doc.

Can you please help with the error. Also can we do it without using the LayoutCollector?Please let me know.

Thanks.

Hi Sayandip,


Thanks for your inquiry. Could you please attach your input Word document here for testing? I will investigate the issue on my side and provide you more information.

Best regards,

Hi Awais,

Please find attached the input document and the output document produced. If you see the output, the barcode is properly added on the first page but on the second page which contains a table the barcode gets displaced. It does not gets added properly at the top right hand corner.

The code I have used is as below-

Barcode Generation

private static BufferedImage generateBarcode()

{

BarCodeBuilder barCodeBuilder = new BarCodeBuilder();

String barcodeText="XYZsdflksdkjfls;dfk;dsf;lk";

barCodeBuilder.setCodeText(barcodeText);

barCodeBuilder.setDataMatrixEncodeMode(DataMatrixEncodeMode.Auto);

barCodeBuilder.setSymbologyType(Symbology.DataMatrix);

barCodeBuilder.setCodeLocation(CodeLocation.None);

return barCodeBuilder.getBarCodeImage();

}

public static void AddImageToPage(Paragraph para, int page) throws Exception {

Document doc = (Document) para.getDocument();

DocumentBuilder builder = new DocumentBuilder(doc);

builder.moveTo(para);

builder.insertImage(generateBarcode(), RelativeHorizontalPosition.PAGE, 540,RelativeVerticalPosition.PAGE, 40, -1, -1, WrapType.NONE);

}

Main()

{

Document doc = new Document("AsposeInput.docx");

System.out.println(doc.getPageCount());

LayoutCollector layoutCollector = new LayoutCollector(doc);

NodeCollection paragraphs = doc.getChildNodes(NodeType.PARAGRAPH, true);

for (int page = 1; page <= doc.getPageCount(); page++) {

for (Paragraph paragraph : (Iterable) paragraphs) {

if (layoutCollector.getStartPageIndex(paragraph) == page) {

AddImageToPage(paragraph, page);

break;

}

}

}

doc.save("AsposeOutput.docx");

}

Please let me know the fix for this.

Also will need one more help from you. If you see the output document the barcode image is somewhat bigger in size. How can I make the barcode size samller on the output document?

Thanks for your help

Hi Awais,

Please let me know if you need more information. Waiting for your response on the previous post. Thanks.

Hi Sayandip,


Thanks for your inquiry and sorry for the delayed response.

After an initial test, I managed to reproduce this issue on my side. The problem occurs because for some reasons the Bar-code image on second page is getting anchored inside the Table. Instead, it should be anchored at the first Paragraph on the second page. I have logged a new task in our issue tracking system as WORDSNET-8182 for our development team to further look into the details of this problem. Your thread has also been linked to this task and you will be notified as soon as it is worked out. Sorry for the inconvenience.

Secondly, you can resize the image by supplying appropriate values to the shape.setWidth() and shape.setHeight() properties. I hope, this helps.

Best regards,

Thanks Awais. I tested some more documents today and I faced the similar problem wherever a table is used. I will be waiting for the Aspose development team to resolve the problem.

I need one more help from you. Can you please let me know how to insert a blank page at the end of an existing document. Thanks.

Hi Sayandip,


Thanks for your inquiry.

Regarding WORDSNET-8182, the issue indirectly occurs because of the page break at the end of the first page. The presence of this page break means the paragraph that is on the bottom of the first page and on the top of the second page are the same paragraph (a page break is just a character so the paragraph spans across the two pages). This means GetStartPageIndex will return ‘1’ for the first Paragraph on second page and not be used for the image on the second page. To workaround this problem, please substitute getStartPageIndx with getEndPageIndex and this issue disappears. This works because the DocumentBuilder inserts nodes at the end of a Paragraph, so as long as the paragraph ends on the target page the image will appear in the right place.

Secondly, to insert a blank page at the end of an existing document please try running the following code snippet:
Document doc = new Document(“C:\Temp\AsposeInput.docx”);
DocumentBuilder builder = new DocumentBuilder(doc);

builder.moveToDocumentEnd();
builder.insertBreak(BreakType.PAGE_BREAK);

doc.save(“C:\Temp\out.docx”);

I hope, this helps.

Best regards,

Thanks for the information Awais. How do I see the page break characters in the document? I am not sure if getEndPageIndex will work for all of my documents. If I can do some minor adjustments in my input document as a work around then it will be fine. For now I am sure that whenever a table comes in a page the barcode gets attached to some other position either on the same page or on the next page.

Hi Sayandip,


Thanks for your inquiry. On the Home tab, in the Paragraph group, click Show/Hide paragraph mark command button; this will display formatting marks including the Page Break marker. I hope, this helps.

Best regards,

Thanks Awais. Can you please confirm if the 2D Matrix barcode generated from Aspose is compatible with ECC 000 – 140 and ECC 200 symbol for printer reading . Also do we need to make any specific setting in the code so that the output barcode is compatible with this. Thanks.

Hi Sayandip,


Thanks for your inquiry. Well, if you insert an image into Word document using Aspose.Words, it does no change to the image. The image content remains preserved.

Secondly, I feel your query is more related to Aspose.BarCode. I am moving your request to Aspose.BarCode forum where you will be guided appropriately.

Best regards,
Hi Sayandip,

Thanks for your inquiry. I would like to update you that the latest version of Aspose.BarCode for Java 5.4.0 supports ECC200 during the recognition. I do think ECC200 also supported with generation but I'm not sure. We need to investigate it. Also, I'm not sure about ECC 000-140 because those are deprecated ECC versions and probably we don't support them at least in the generation. I have logged your query in our issue tracking system as BARCODENET-33536. Our development team will look into the matter. As soon as any information is shared with me, I will be more than happy to share that with you. We're sorry for your inconvenience.

Hi Sayandip,

Thanks for your patience. From the specifics of ticket id BARCODENET-33536, Currently we support only ECC200 which is always enabled and generated. The other error correction algorithms are not supported. I would like to advise that you can use a newer ECC200 because it is an improved version of the previous error correction algorithms. But if you required to use the older ECC schemes we can plan the implementation in our road map. Please let me know your thoughts.

Thanks Imran. ECC200 is good for us. Please let me know if we need to set any value in the Java code while Barcode Generation, so that the output Barcode is in ECC200 format?

Thanks.

Hi Sayandip,

Thanks for your inquiry. I would like to update you that the ECC200 is supported with generation and we don't need to set any property/value etc. Because we have only ECC200 now. By default the output bar code is in ECC200 format.

Please let me know in case of further assistance and questions.

Hi Sayandip,


Regarding WORDSNET-8182, it is to update you that our development team has completed the work on this task and has come to a conclusion that this issue and the undesired behaviour you’re observing is actually not a bug in Aspose.Words. So, we’ve closed this issue as ‘Not a Bug’. If we can help you with anything else, please feel free to ask.

Best regards,