I know that I can use system drawning method but image with bar code is not so clear.
Here is a code that I use. I'm trying to get documents whit different sizes depending on the number of lines in the document. So I'm using memory stream (not shown here). My idea is to write data to the table in memory and count lines. After that I will create new pdf document and add table to that document.
If I comment a line:
table.AddCell(image128);
both documents are the same. But if I leave that line in code final document shows all rows but text in rows is not visible. If I open second docu,ent in firefox I can even see a bar codes.
Any suggestion will be great. Or is there a different way to get documents with different sizes depending on amount of content.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
using System.Drawing;
namespace testtables1
{
class Program
{
static void Main(string[] args)
{
iTextSharp.text.Rectangle pageSize = new iTextSharp.text.Rectangle(300, 720);
Document pdfToCreate = new Document(pageSize, 0, 0, 0, 0);
PdfWriter writer = PdfWriter.GetInstance(pdfToCreate, new FileStream(@"D:\\testfile.pdf", FileMode.Create));
pdfToCreate.Open();
PdfPTable table = new PdfPTable(1);
PdfPTable tableout = new PdfPTable(1);
WriteLineToPdf("This is first row. Below is image generated using SystemDrawing.", table, out tableout);
table = tableout;
// Create bar code
Barcode128 code128 = new Barcode128();
code128.CodeType = Barcode.CODE128;
code128.ChecksumText = true;
code128.GenerateChecksum = true;
code128.Code = "00100370006756555316";
// Create a new PdfWrite object, writing the output to a MemoryStream
var outputStream = new MemoryStream();
var pdfWriter = PdfWriter.GetInstance(pdfToCreate, outputStream);
PdfContentByte cb = new PdfContentByte(writer);
// Image generated using System.Drawing and rendering
System.Drawing.Bitmap bm = new System.Drawing.Bitmap(code128.CreateDrawingImage(System.Drawing.Color.Black, System.Drawing.Color.White));
iTextSharp.text.Image bmCoverted = iTextSharp.text.Image.GetInstance(bm, System.Drawing.Imaging.ImageFormat.Bmp);
table.AddCell(bmCoverted);
pdfToCreate.Open();
// Image generated using iTextSharp.text.Image
WriteLineToPdf("This is third row. Below is image generated using code128.CreateImageWithBarcode.", table, out tableout);
table = tableout;
iTextSharp.text.Image image128 = code128.CreateImageWithBarcode(cb, null, null);
table.AddCell(image128);
table = tableout;
WriteLineToPdf("This is fifth row.", table, out tableout);
pdfToCreate.Add(tableout);
// Create new document with height that depends on number of lines in document.
iTextSharp.text.Rectangle psFinal = new iTextSharp.text.Rectangle(200, 300);
Document pdfFinal = new Document(psFinal, 0, 0, 0, 0);
PdfWriter writer1 = PdfWriter.GetInstance(pdfFinal, new FileStream(@"D:\\finalfile.pdf", FileMode.Create));
pdfFinal.Open();
pdfFinal.Add(tableout);
cb = null;
pdfToCreate.Close();
pdfFinal.Close();
}
// Write a single line to the PDF.
private static void WriteLineToPdf(string tLine, PdfPTable ticTable0In, out PdfPTable ticTableIN)
{
// Define fonts.
BaseFont bfTimes = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false); // Base font.
iTextSharp.text.Font timesN = new iTextSharp.text.Font(bfTimes, 12, iTextSharp.text.Font.NORMAL, BaseColor.BLACK);
Paragraph par = new Paragraph(tLine, timesN);
par.Alignment = Element.ALIGN_CENTER;
PdfPCell cell;
cell = new PdfPCell();
//cell.FixedHeight = 12f;
//cell.Border = Rectangle.NO_BORDER;
cell.AddElement(par);
ticTable0In.AddCell(cell);
ticTableIN = ticTable0In;
par = null;
}
}
}
My other ideai is to use data from memory stream. I'm able to create result.pdf document with size that I want but after I write Data from memory size changes.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
namespace TableWithBC2
{
class Program
{
static void Main(string[] args)
{
using (MemoryStream myMemoryStream = new MemoryStream())
{
PdfPTable tableIn;
Document pdfDocument = new Document();
PdfWriter writer = PdfWriter.GetInstance(pdfDocument, myMemoryStream);
PdfPTable tableOut = new PdfPTable(new float[] { 200 });
tableIn = new PdfPTable(new float[] { 200 });
pdfDocument.Open();
for (int i = 0; i < 5; i++)
{
WriteLineToPdf("This is row no. " + i.ToString(), tableIn, out tableOut);
tableIn = tableOut;
}
// Create bar code
PdfContentByte cb = writer.DirectContent;
Barcode128 code128 = new Barcode128();
code128.CodeType = Barcode.CODE128;
code128.ChecksumText = true;
code128.GenerateChecksum = true;
code128.Code = "00100370006756555316";
Image barCodeImage = code128.CreateImageWithBarcode(cb, null, null);
PdfPCell cell1 = new PdfPCell(barCodeImage);
tableOut.AddCell(cell1);
tableIn = tableOut;
WriteLineToPdf("This is a last row.", tableIn, out tableOut);
pdfDocument.Add(tableOut);
pdfDocument.Close();
// Create final PDF document.
Document pdfDocument1 = new Document(new Rectangle(200, 250));
PdfWriter writer1 = PdfWriter.GetInstance(pdfDocument1, new FileStream(@"D:\\result.pdf", FileMode.Create));
pdfDocument1.Open();
pdfDocument1.Add(new Chunk());
pdfDocument1.Close();
byte[] content = myMemoryStream.ToArray();
// Write out PDF from memory stream.
using (FileStream fs = File.OpenWrite(@"D:\\nikola bd - final\\out\\dummy22.pdf"))
{
fs.Write(content, 0, (int)content.Length);
}
}
}
// Write a single line to the PDF.
private static void WriteLineToPdf(string tLine, PdfPTable tTableIn, out PdfPTable tTableOut)
{
BaseFont bfTimes = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false);
iTextSharp.text.Font timesN = new iTextSharp.text.Font(bfTimes, 12, iTextSharp.text.Font.NORMAL, BaseColor.RED);
Paragraph par = new Paragraph(tLine, timesN);
par.Alignment = Element.ALIGN_CENTER;
PdfPCell cell = new PdfPCell();
cell.AddElement(par);
tTableIn.AddCell(cell);
tTableOut = tTableIn;
}
}
}