QR code merging on word document at specific location

Hi,

In our application, we are generating QR code with the use of Aspose.Barcode as mentioned in the below code snippet -

var document = new Document(templateFileName);
//Instantiate linear barcode object
BarCodeBuilder builder = new BarCodeBuilder();

//Set the Code text for the barcode
builder.CodeText = this.GenerateQRContents(FieldTicket);

//Set the symbology type to QR
builder.SymbologyType = Symbology.QR;
builder.CodeLocation = CodeLocation.None;
DocumentBuilder docBuilder = new DocumentBuilder(document);
docBuilder.InsertImage(builder.BarCodeImage);

As per the above code, it inserts the barcode image in the word document but I have to put that image in a particular area (right hand corner of the word document). Please find the attached screenshot for the requirement.

Kindly let me know in case of any confusion/query regarding my requirement. Please do reply ASAP because I need it urgently.

Thanks,
Sunil

Hi Sunil,


Thank you for your inquiry. You can adjust barcode image location by setting shape properties i.e. WrapType, Left and top etc. Please follow up the code below:

DocumentBuilder docBuilder = new DocumentBuilder();

Aspose.Words.Drawing.Shape shape = docBuilder.InsertImage(builder.BarCodeImage);

shape.WrapType = Aspose.Words.Drawing.WrapType.Square;

shape.Left = 450;

shape.Top = 10;

docBuilder.Document.Save("c:\\output.docx");



In case of further assistance and comments, please let me know.

Thanks for providing us the solution!

Here I have a bit different requirement. Suppose I have a shape in the word document as mentioned in the previous attachment and I want to fill in that shape by finding it through code. Or we can replace that shape by defining our own shape in the code.

Note* Actually I don't want to define shape alingment through code because in future it may be somewhere else in all over the document.

Thanks again for the quick reply.

Hi Sunil,


Thank you for your details. I’m moving your request in Aspose.Words forum. My colleagues from Aspose.Words component team will answer you shortly.We apologize for your inconvenience.

Hi Sunil,


Thanks for your inquiry. I would suggest you please first read the following article that outlines the usage of Shapes in Aspose.Words:
http://www.aspose.com/docs/display/wordsnet/About+Shapes+in+Aspose.Words

Secondly, I think you can retrieve the target Shape from the collection returned by document.GetChildNodes(NodeType.Shape, true); and then modify Shape’s attribute mentioned below:
http://www.aspose.com/docs/display/wordsnet/Shape+Members

If we can help you with anything else, please feel free to ask.

Best Regards,

Hi,

I am able to insert the QR code image at the specified location by using the below code -

//Instantiate linear barcode object
BarCodeBuilder builder = new BarCodeBuilder();

//Set the Code text for the barcode
builder.CodeText = this.GenerateQRContents(priceFieldTicket);

//Set the symbology type to QR
builder.SymbologyType = Symbology.QR;
builder.xDimension = 0.1f;
builder.yDimension = 0.1f;
builder.CodeLocation = CodeLocation.None;

NodeCollection shapes = document.GetChildNodes(NodeType.Shape, true);
int imageIndex = 0;
foreach (Aspose.Words.Drawing.Shape shape in shapes)
{
if (!shape.HasImage)
{
shape.ImageData.SetImage(builder.BarCodeImage);
}
}

But now I have to do the same thing with Aspose.Cells. I have to insert barcode image in excel file as well.

Please help me to resolve this issue as well.

Thanks in advance!

Sunil Goel

Hi Hafeez,

Thanks for the reply!

I am able to insert the QR code image at the specific location in word document by using the below code -

//Instantiate linear barcode object
BarCodeBuilder builder = new BarCodeBuilder();

//Set the Code text for the barcode
builder.CodeText = this.GenerateQRContents(jobPacketDetail, isOfferToolInsurance, false);

//Set the symbology type to QR
builder.SymbologyType = Symbology.QR;
builder.xDimension = 0.1f;
builder.yDimension = 0.1f;
builder.CodeLocation = CodeLocation.None;

NodeCollection shapes = document.GetChildNodes(NodeType.Shape, true);
foreach (Aspose.Words.Drawing.Shape shape in shapes)
{
if (!shape.HasImage)
{
shape.ImageData.SetImage(builder.BarCodeImage);
}
}

Now I have to do the same thing in excel file as well. I have tried with the below code -

//Set the Code text for the barcode
builder.CodeText = this.GenerateQRContents(jobPacketDetail, false, true);

//Set the symbology type to QR
builder.SymbologyType = Symbology.QR;
builder.xDimension = 0.1f;
builder.yDimension = 0.1f;
builder.CodeLocation = CodeLocation.None;

//Creating memory stream
System.IO.MemoryStream ms = new System.IO.MemoryStream();

//Saving barcode image to memory stream
builder.BarCodeImage.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);

Worksheet worksheet = workbookDesigner.Workbook.Worksheets[0];
Aspose.Cells.Drawing.ShapeCollection shapes = worksheet.Shapes;

foreach (Aspose.Cells.Drawing.Shape shape in shapes)
{
if (shape.Name == "QRCodeImage")
{
shape.FillFormat.ImageData = ms.ToArray();
}
}

But it doesn't show the bar code image. Please help me to resolve the same issue.

Thanks in advance!

Sunil

Hi Sunil,


Thanks for the additional information. As your problem is related to Aspose.Cells, I will move your request in Aspose.Cells forum. My colleagues from Aspose.Cells component team will answer you shortly.

Best Regards,
Hi,

librasunil1:

Now I have to do the same thing in excel file as well. I have tried with the below code -

//Set the Code text for the barcode
builder.CodeText = this.GenerateQRContents(jobPacketDetail, false, true);

//Set the symbology type to QR
builder.SymbologyType = Symbology.QR;
builder.xDimension = 0.1f;
builder.yDimension = 0.1f;
builder.CodeLocation = CodeLocation.None;

//Creating memory stream
System.IO.MemoryStream ms = new System.IO.MemoryStream();

//Saving barcode image to memory stream
builder.BarCodeImage.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);

Worksheet worksheet = workbookDesigner.Workbook.Worksheets[0];
Aspose.Cells.Drawing.ShapeCollection shapes = worksheet.Shapes;

foreach (Aspose.Cells.Drawing.Shape shape in shapes)
{
if (shape.Name == "QRCodeImage")
{
shape.FillFormat.ImageData = ms.ToArray();
}
}

But it doesn't show the bar code image. Please help me to resolve the same issue.


We are working over your issue and get back to you soon.


Thank you.

Hi,


Well, I have tested your scenario with Aspose.BarCode for .NET v5.1.0 and Aspose.Cells for .NET: Aspose.Cells for .NET v7.3.3.2
it works fine. The shape’s fill format is changed with bar code image.

Here is my sample runnable code. i have also attached my input and output Excel files for your reference.

Sample code:

BarCodeBuilder builder = new BarCodeBuilder();

//Set the Code text for the barcode
builder.CodeText = “my test code”;

//Set the symbology type to QR
builder.SymbologyType = Symbology.QR;
builder.xDimension = 0.1f;
builder.yDimension = 0.1f;
builder.CodeLocation = CodeLocation.None;

//Creating memory stream
System.IO.MemoryStream ms = new System.IO.MemoryStream();

//Saving barcode image to memory stream
builder.BarCodeImage.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
///builder.BarCodeImage.Save(“e:\test2\outbarcodeimg.bmp”);

Workbook workbook = new Workbook(“e:\test2\BookRect.xlsx”);

Worksheet worksheet = workbook.Worksheets[0];
Aspose.Cells.Drawing.ShapeCollection shapes = worksheet.Shapes;

foreach (Aspose.Cells.Drawing.Shape shape in shapes)
{
if (shape.Name == “QRCodeImage”)
{
shape.FillFormat.ImageData = ms.ToArray();
}
}

workbook.Save(“e:\test2\outBookRect.xlsx”);

(Note: In the template Excel file, I have a Rectangle shape named as “QRCodeImage”. I just specify its fill format to replace with barcode image).

If you still find any issue with Aspose.Cells for .NET v7.3.3.2, please create a sample console application (runnable), zip it and post it here to show the issue on our end. We will check it soon.

Thank you.