Add Barcode Image in PDF using C#

@NehaKh

There are some undefined variables in the code snippet that you have have shared. We are unable to execute it and generate the PDF that you have shared. Please share a working code snippet with which we can try to replicate the issue and modify it to achieve desired results.

@asad.ali

I need help for barcode print using table.i am sharing old and new file.
in new file highlighted line should be shift in left side same as old file.Please compare both the file and provide the solutions.How we can acheive the line in the left side.
old.pdf (52.7 KB)
New.pdf (51.1 KB)

@naresh.mahto

Please try to set the HorizontalAlignment Property of the Table Cells in order to position the text as per your requirements. In case you still face any issues, please share sample code snippet that you used to generate the new.pdf file. We will test the scenario in our environment and address it accordingly.

Hi @asad.ali,

Actually the issue is not related to text alignment. In the new pdf file one cell line we need to shift to right . As I highlighted with red color in old file and blue color in new file. Please have a look and guide us . We have already did the alignment and all.
for the last two cells we have did rowspan to merge two rows. but after that the line is not coming to right side. please try to create the pdf file same as old file. As per company compliances we are enable to share you the code as the code is in live environment.
new.pdf (61.7 KB)
old.pdf (12.3 KB)

@NehaKh

There could be multiple ways to generate similar output as in old.pdf. Since we do not have any idea about the implementation of your existing code, we cannot suggest any modification in it. However, if you are using Table, you can achieve similar thing using nested tables. In the last row, there is another table with its own column and cell border which is the reason you are noticing the cell border shifted in old.pdf.

Please try to add another table in last row with its own column widths and you will be able to achieve it. Meanwhile, we will also try to create an example for you using table.

@asad.ali

We have tried what you have suggested related to pdf shared by us in the previous chat but did not achieve the things using nested tables. So can you please suggest what we can do to achieve the things. Also please let us know have you tried to create similar things shared by us. Please guide us this is urgent basis.
I have attached the which we needed.
old.pdf (12.3 KB)

@naresh.mahto

We will try to reproduce the similar resultant PDF at our end and will get back to you with a sample code snippet shortly. Please spare us little time.

Hi @asad.ali ,

We are waiting for your your reply. Can you please update . we have urgency to fix the issue . Can you please provide some solution .

@NehaKh

Please check the attached code sample and respective output PDF generated by it. You can modify the code snippet further to achieve your desired layout. We prepared it only to demonstrate how nested table implementation can be done:

Aspose.Pdf.Image img = new Image();
img.File = dataDir + "barcode.jpg";
img.FixHeight = 20;
img.FixWidth = 100;

Document doc = new Document();
Page page = doc.Pages.Add();

Table table = new Table();
table.Border = new BorderInfo(BorderSide.All, 1f, Color.Black);
table.ColumnAdjustment = ColumnAdjustment.Customized;
table.ColumnWidths = "70 175";

Row row = table.Rows.Add();
row.Cells.Add("Part Number").ColSpan = 2;

row = table.Rows.Add();
row.MinRowHeight = 25;
row.DefaultCellBorder = new BorderInfo(BorderSide.All, 1f, Color.LightGray);
row.Cells.Add();
row.Cells.Add();

row = table.Rows.Add();
row.MinRowHeight = 10;
row.Cells.Add().ColSpan = 2;

row = table.Rows.Add();
row.MinRowHeight = 30;
Cell cell = row.Cells.Add();
cell.ColSpan = 2;

Table nestedTable = new Table();
nestedTable.ColumnAdjustment = ColumnAdjustment.Customized;
nestedTable.ColumnWidths = "60 190";
nestedTable.IsBordersIncluded = true;

row = nestedTable.Rows.Add();
row.DefaultCellBorder = new BorderInfo(BorderSide.All, 1f, Color.LightGray);
row.Cells.Add("Dell ORDER #").RowSpan = 2;
row.Cells.Add("vxxx").Alignment = HorizontalAlignment.Right;

row = nestedTable.Rows.Add();
row.DefaultCellBorder = new BorderInfo(BorderSide.All, 1f, Color.LightGray);
Cell imgCell = row.Cells.Add();
imgCell.Paragraphs.Add(img);

cell.Paragraphs.Add(nestedTable);

page.Paragraphs.Add(table);
doc.Save(dataDir + "nestedtable.pdf");

nestedtable.pdf (19.8 KB)

Hi @asad.ali,

We have tried your shared code and able to get the line right side, but facing one more issue after that
we have three texts in nested tables which are not coming correct . I have used textfragment for the same to call three text in same cell.
After below line we need to add two more text which is in regular fontstlye and 07 fontsize.
row.Cells.Add(" ABC ORDER #").RowSpan = 2;

But for all the text the fontstyle and size are coming same .
for your reference attaching new output vs old with new with logic which I applied.
Please check and provide some solution to fix issue asap.
new output.PNG (7.3 KB)
Old output.PNG (6.0 KB)Latest logic applied.docx (14.4 KB)

Please check the things on urgent basis. Anticipating your quick response and thank you in advance.

@NehaKh

We tested using below code sequence for the cell under discussion and all of three text fragments were rendered with specified styles in the output PDF:

row = nestedTable.Rows.Add();
row.DefaultCellBorder = new BorderInfo(BorderSide.All, 1f, Color.LightGray);
Cell ordercell = row.Cells.Add();
ordercell.RowSpan = 2;

TextFragment ordertext = new TextFragment("Dell ORDER #");
ordertext.TextState.FontSize = 10;
ordertext.TextState.FontStyle = FontStyles.Bold;

TextFragment ordertext2 = new TextFragment("Unique stock");
ordertext2.TextState.FontSize = 6;

TextFragment ordertext3 = new TextFragment("Identifier");
ordertext3.TextState.FontSize = 6;

ordercell.Paragraphs.Add(ordertext);
ordercell.Paragraphs.Add(ordertext2);
ordercell.Paragraphs.Add(ordertext3);

nestedtable.pdf (20.1 KB)

Hi @asad.ali,

We have given you the same code yesterday, but by using your code also we are not getting the expected output. can you please try with font Courier New. As you see after ORDER all the text is not bold. Its regular , plz try to get the same output which we shared in previous chat.

@NehaKh

We used the same code snippet that you have shared and obtained the attached PDF document. Please note that we only changed column widths to adjust the values inside cell.

Aspose.Pdf.Image img = new Image();
            img.File = dataDir + "barcode.jpg";
            img.FixHeight = 20;
            img.FixWidth = 100;

            Document doc = new Document();
            Page page = doc.Pages.Add();

            Table table = new Table();
            table.Border = new BorderInfo(BorderSide.All, 1f, Color.Black);
            table.ColumnAdjustment = ColumnAdjustment.Customized;
            table.ColumnWidths = "100 210";

            Row row = table.Rows.Add();
            row.Cells.Add("Part Number").ColSpan = 2;

            row = table.Rows.Add();
            row.MinRowHeight = 25;
            row.DefaultCellBorder = new BorderInfo(BorderSide.All, 1f, Color.LightGray);
            row.Cells.Add();
            row.Cells.Add();

            row = table.Rows.Add();
            row.MinRowHeight = 10;
            row.Cells.Add().ColSpan = 2;

            row = table.Rows.Add();
            row.MinRowHeight = 30;
            Cell cell = row.Cells.Add();
            cell.ColSpan = 2;

            Table nestedTable = new Table();
            nestedTable.ColumnAdjustment = ColumnAdjustment.Customized;
            nestedTable.ColumnWidths = "90 220";
            nestedTable.IsBordersIncluded = true;

            row = nestedTable.Rows.Add();
            //row.MinRowHeight = 30;
            row.DefaultCellBorder = new BorderInfo(BorderSide.All, 1f, Color.LightGray);

            Cell cell3 = row.Cells.Add();
            cell3.RowSpan = 2;
            //Create text fragment
            TextFragment textFragment1 = new TextFragment($"ABC ORDER #");
            // Set text properties
            textFragment1.TextState.FontSize = 10;
            textFragment1.TextState.FontStyle = FontStyles.Bold;
            textFragment1.TextState.Font = FontRepository.FindFont("Courier New");
            textFragment1.TextState.ForegroundColor = Aspose.Pdf.Color.Black;
            cell3.Paragraphs.Add(textFragment1);
            //Create text fragment
            TextFragment textFragment2 = new TextFragment($"Unique stock");
            // Set text properties
            textFragment2.TextState.FontSize = 07;
            textFragment2.TextState.FontStyle = FontStyles.Regular;
            textFragment2.TextState.Font = FontRepository.FindFont("Courier New");
            textFragment2.TextState.ForegroundColor = Aspose.Pdf.Color.Black;
            textFragment2.Margin.Top = 10;
            // Create text fragment
            TextFragment textFragment3 = new TextFragment($"Identifier");
            // Set text properties
            textFragment3.TextState.FontSize = 07;
            textFragment3.TextState.FontStyle = FontStyles.Regular;
            textFragment3.TextState.Font = FontRepository.FindFont("Courier New");
            textFragment3.TextState.ForegroundColor = Aspose.Pdf.Color.Black;
            textFragment3.Margin.Top = 08;
            //text on the cell

            cell3.Paragraphs.Add(textFragment2);
            cell3.Paragraphs.Add(textFragment3);
            cell3.Margin = new MarginInfo(5.0, 1.0, 1.0, -25.0);
            cell3.RowSpan = 2;

            row = nestedTable.Rows.Add();
            row.DefaultCellBorder = new BorderInfo(BorderSide.All, 1f, Color.LightGray);
            Cell imgCell = row.Cells.Add();
            imgCell.Paragraphs.Add(img);

            cell.Paragraphs.Add(nestedTable);

            page.Paragraphs.Add(table);
            doc.Save(dataDir + "nestedtable.pdf");

nestedtable.pdf (111.8 KB)

You can notice in the output PDF that font as well as font style and size were applied correctly. However, there is a margin issue for the cell which is due to the values you are already setting. You can change it as per your requirements. In case you still face any issues, please share the PDF generated at your end so that we can also check it.

Hi @asad.ali ,

We have tried whatever you suggest but we are not getting expected output. Also in first table we need to take column width as 100, 165 only its a standard. You can see that in first table cell the text is coming as light if we select regular text and color black . same type of functionality we need in second table.
for your reference I am sharing you expected PDF(old) with PDF which we are getting(new).
Old (Expected PDF).pdf (5.7 KB)
New PDF.pdf (93.0 KB)

Hi @asad.ali,

Requesting you to check the above request also.

@NehaKh

If you change the column width, you will also need to change the font size of text so that it could fit in the given cell width. Anyways, we again tested the case and we could not reproduce the issue in our environment while using 22.3 version of the API. The text style and font is being applied correctly in the nested table as well:

Aspose.Pdf.Image img = new Image();
img.File = dataDir + "barcode.jpg";
img.FixHeight = 20;
img.FixWidth = 100;

Document doc = new Document();
Page page = doc.Pages.Add();

Table table = new Table();
table.Border = new BorderInfo(BorderSide.All, 1f, Color.Black);
table.ColumnAdjustment = ColumnAdjustment.Customized;
table.ColumnWidths = "100 165";

Row row = table.Rows.Add();
row.Cells.Add("Part Number").ColSpan = 2;

row = table.Rows.Add();
row.MinRowHeight = 25;
row.DefaultCellBorder = new BorderInfo(BorderSide.All, 1f, Color.LightGray);
row.Cells.Add();
row.Cells.Add();

row = table.Rows.Add();
row.MinRowHeight = 10;
row.Cells.Add().ColSpan = 2;

row = table.Rows.Add();
row.MinRowHeight = 30;
Cell cell = row.Cells.Add();
cell.ColSpan = 2;

Table nestedTable = new Table();
nestedTable.ColumnAdjustment = ColumnAdjustment.Customized;
nestedTable.ColumnWidths = "90 180";
nestedTable.IsBordersIncluded = true;

row = nestedTable.Rows.Add();
//row.MinRowHeight = 30;
row.DefaultCellBorder = new BorderInfo(BorderSide.All, 1f, Color.LightGray);

Cell cell3 = row.Cells.Add();
cell3.RowSpan = 2;
//Create text fragment
TextFragment textFragment1 = new TextFragment($"ABC ORDER #");
// Set text properties
textFragment1.TextState.FontSize = 7;
textFragment1.TextState.FontStyle = FontStyles.Bold;
textFragment1.TextState.Font = FontRepository.FindFont("Courier New");
textFragment1.TextState.ForegroundColor = Aspose.Pdf.Color.Black;
cell3.Paragraphs.Add(textFragment1);
//Create text fragment
TextFragment textFragment2 = new TextFragment($"Unique stock");
// Set text properties
textFragment2.TextState.FontSize = 5;
textFragment2.TextState.FontStyle = FontStyles.Regular;
textFragment2.TextState.Font = FontRepository.FindFont("Courier New");
textFragment2.TextState.ForegroundColor = Aspose.Pdf.Color.Black;
textFragment2.Margin.Top = 10;
// Create text fragment
TextFragment textFragment3 = new TextFragment($"Identifier");
// Set text properties
textFragment3.TextState.FontSize = 5;
textFragment3.TextState.FontStyle = FontStyles.Regular;
textFragment3.TextState.Font = FontRepository.FindFont("Courier New");
textFragment3.TextState.ForegroundColor = Aspose.Pdf.Color.Black;
textFragment3.Margin.Top = 08;
//text on the cell

cell3.Paragraphs.Add(textFragment2);
cell3.Paragraphs.Add(textFragment3);
cell3.Margin = new MarginInfo(5.0, 1.0, 1.0, -15.0);
cell3.RowSpan = 2;

row = nestedTable.Rows.Add();
row.DefaultCellBorder = new BorderInfo(BorderSide.All, 1f, Color.LightGray);
Cell imgCell = row.Cells.Add();
imgCell.Paragraphs.Add(img);

cell.Paragraphs.Add(nestedTable);

page.Paragraphs.Add(table);
doc.Save(dataDir + "nestedtable.pdf");

nestedtable.pdf (111.9 KB)

We also noticed that you are using the API without any license due to which it is possible that API is not applying styles to all the text because, API will only process 4 elements in evaluation mode. Please try to use a valid or 30-days temporary license and test again. In case issue still persists, please let us know.