protected void ManipulatePdf(String dest) { PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest)); Document doc = new Document(pdfDoc); String code = "675-FH-A12"; Table table = new Table(UnitValue.CreatePercentArray(2)).UseAllAvailableWidth(); table.AddCell("Change baseline:"); Barcode128 code128 = new Barcode128(pdfDoc); // If value is positive, the text distance under the bars. If zero or negative, // the text distance above the bars. code128.SetBaseline(-1); code128.SetSize(12); code128.SetCode(code); code128.SetCodeType(Barcode128.CODE128); Image code128Image = new Image(code128.CreateFormXObject(pdfDoc)); // Notice that in iText5 in default PdfPCell constructor (new PdfPCell(Image img)) // this image does not fit the cell, but it does in addCell(). // In iText7 there is no constructor (new Cell(Image img)), // so the image adding to the cell can be done only using method add(). Cell cell = new Cell().Add(code128Image); table.AddCell(cell); table.AddCell("Add text and bar code separately:"); code128 = new Barcode128(pdfDoc); // Suppress the barcode text code128.SetFont(null); code128.SetCode(code); code128.SetCodeType(Barcode128.CODE128); // Let the image resize automatically by setting it to be autoscalable. code128Image = new Image(code128.CreateFormXObject(pdfDoc)).SetAutoScale(true); cell = new Cell(); cell.Add(new Paragraph("PO #: " + code)); cell.Add(code128Image); table.AddCell(cell); doc.Add(table); doc.Close(); }
public MemoryStream CreatePdf(string text) { if (text == null) { throw new ArgumentNullException(nameof(text)); } _logger.LogInformation("Creating barcode for '{Text}'", text); var properties = new WriterProperties(); properties.SetPdfVersion(PdfVersion.PDF_1_4); var stream = new MemoryStream(); using var writer = new PdfWriter(stream, properties); var pdfDoc = new PdfDocument(writer); PdfFont fontOcrb = PdfFontFactory.CreateFont("wwwroot/OCRB.otf", PdfEncodings.WINANSI, PdfFontFactory.EmbeddingStrategy.PREFER_EMBEDDED); Color cmykBlack = new DeviceCmyk(0, 0, 0, 100); var gState = new PdfExtGState().SetFillOverPrintFlag(true).SetStrokeOverPrintFlag(true).SetOverprintMode(1); var code128 = new Barcode128(pdfDoc); code128.SetBaseline(7.67f); code128.SetSize(9f); code128.SetFont(fontOcrb); code128.SetX(0.72f); code128.SetBarHeight(14.17f); code128.SetCode(text); code128.SetCodeType(Barcode128.CODE128); var xObject = code128.CreateFormXObject(cmykBlack, cmykBlack, pdfDoc); pdfDoc.AddNewPage(new iText.Kernel.Geom.PageSize(xObject.GetWidth(), xObject.GetHeight())); PdfCanvas canvas = new PdfCanvas(pdfDoc.GetFirstPage()); canvas.SaveState(); canvas.SetExtGState(gState); canvas.AddXObjectAt(xObject, 0f, 0f); canvas.RestoreState(); pdfDoc.Close(); return(stream); }
private void ManipulatePdf(string dest) { PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest)); Document doc = new Document(pdfDoc); doc.SetMargins(0f, 0f, 0f, 0f); String img_path = "../../resource/image/1.png"; String FONT = "../../resource/font/arial.ttf"; Image image_1 = new Image(ImageDataFactory.Create(img_path)); Image image_2 = new Image(ImageDataFactory.Create(img_path)); image_1.ScaleAbsolute(img_size_width, img_size_height); image_2.ScaleAbsolute(img_size_width, img_size_height); pdfDoc.AddNewPage(new PageSize(paper_width, paper_height)); image_1.SetFixedPosition(1, offset_x, paper_height - img_size_height); doc.Add(image_1); image_2.SetFixedPosition(1, offset_x + paper_width - img_size_width, paper_height - img_size_height); doc.Add(image_2); PdfFont TitleFont = PdfFontFactory.CreateFont(FONT, null, true); Paragraph p = new Paragraph().SetTextAlignment(TextAlignment.CENTER); Text text_header_1 = new Text("MONTEREY"); //PdfFont TitleFont = PdfFontFactory.CreateFont(FontConstants.COURIER); text_header_1.SetFont(TitleFont).SetBold().SetItalic().SetFontSize(20); p.Add(text_header_1); doc.Add(p); p = new Paragraph().SetTextAlignment(TextAlignment.CENTER); Text text_header_2 = new Text("BOTTLE DEPOT"); text_header_2.SetFont(TitleFont).SetBold().SetItalic().SetFontSize(13); p.SetMultipliedLeading(-0.8f); p.Add(text_header_2); doc.Add(p); p = new Paragraph().SetTextAlignment(TextAlignment.CENTER); Text text_header_3 = new Text("2240 - 68 ST NE"); text_header_3.SetFont(TitleFont).SetBold().SetFontSize(9); p.SetMultipliedLeading(0); p.Add(text_header_3); doc.Add(p); p = new Paragraph().SetTextAlignment(TextAlignment.CENTER); text_header_3 = new Text("CALGARY, AB T1Y 0A2"); text_header_3.SetFont(TitleFont).SetBold().SetFontSize(9); p.SetMultipliedLeading(0); p.Add(text_header_3); doc.Add(p); p = new Paragraph().SetTextAlignment(TextAlignment.CENTER); text_header_3 = new Text("PH:(403) 590-6665"); text_header_3.SetFont(TitleFont).SetBold().SetFontSize(9); p.SetMultipliedLeading(0); p.Add(text_header_3); doc.Add(p); PdfFont special_Font = PdfFontFactory.CreateFont(FONT, PdfEncodings.IDENTITY_H, true); p = new Paragraph().SetTextAlignment(TextAlignment.CENTER); Text text_header_4 = new Text("* * P A I D - R E P R I N T * *"); text_header_4.SetFont(special_Font).SetBold().SetFontSize(14); p.SetMultipliedLeading(2f); p.Add(text_header_4); doc.Add(p); String code = "741235"; Barcode128 code128 = new Barcode128(pdfDoc); code128.SetFont(null); code128.SetCode(code); code128.SetCodeType(Barcode128.CODE128); Image code128Image = new Image(code128.CreateFormXObject(pdfDoc)).ScaleAbsolute(paper_width * 0.4f, paper_width * 0.15f); Paragraph paragraph = new Paragraph(code).SetTextAlignment(TextAlignment.CENTER); code128Image.SetMarginLeft(paper_width * 0.29f); Cell cell = new Cell(); cell.Add(code128Image); cell.Add(paragraph); doc.Add(cell); string[] org_lines = System.IO.File.ReadAllLines("../../resource/input_table.csv"); Table table = new Table(UnitValue.CreatePercentArray(new float[] { 3, 3, 3 })).UseAllAvailableWidth(); table.SetTextAlignment(TextAlignment.CENTER); table.SetMargin(15f); for (int i = 0; i < org_lines.Length - 1; i++) { string[] temp = org_lines[i].Split(','); for (int j = 0; j < 3; j++) { table.AddCell(temp[j]); } } doc.Add(table); p = new Paragraph().SetTextAlignment(TextAlignment.CENTER); string[] total_temp = org_lines[org_lines.Length - 1].Split(','); text_header_3 = new Text(total_temp[1] + " " + total_temp[2]); text_header_3.SetFont(TitleFont).SetBold().SetFontSize(15); p.SetMultipliedLeading(0); p.Add(text_header_3); doc.Add(p); p = new Paragraph().SetTextAlignment(TextAlignment.CENTER); DateTime localDate = DateTime.Now; string format = "MMM d, yyyy\t\t\t\t\t"; text_header_3 = new Text(localDate.ToString(format) + localDate.ToString("t")); text_header_3.SetFont(TitleFont).SetFontSize(12); p.SetMultipliedLeading(2); p.Add(text_header_3); doc.Add(p); p = new Paragraph().SetTextAlignment(TextAlignment.CENTER); text_header_3 = new Text("You were served at MONTEREY-6 by Subash."); text_header_3.SetFont(TitleFont).SetFontSize(12); p.SetMultipliedLeading(2); p.Add(text_header_3); doc.Add(p); p = new Paragraph().SetTextAlignment(TextAlignment.CENTER); text_header_3 = new Text("Thank you for your patronage."); text_header_3.SetFont(TitleFont).SetBold().SetFontSize(12); p.SetMultipliedLeading(1); p.Add(text_header_3); doc.Add(p); doc.Close(); MessageBox.Show("File saved to C:\\Neosmart Solutions Inc\\PDFs\\result.pdf correctly!"); }