public static void FoliarPdf(string RutaPDf) { //System.Drawing.Color? color = System.Drawing.Color.Black; //FolioPosition folioPosition = FolioPosition.BottomRight; //FolioType folioType = FolioType.Numeric; string fechaProceso = $"Fecha Proceso: {DateTime.Now.ToString("dd/MM/yyyy")}"; string folioPrefix = "Pag. "; string folioSuffix = ""; //string folioFont = FontFactory.TIMES_ROMAN.ToString(); //FolioFace folioFace = FolioFace.Roman; float folioSize = 9; PdfReader reader = new PdfReader(RutaPDf); using (MemoryStream memoryStream = new MemoryStream()) { int totalNumberOfPages = reader.NumberOfPages; string folioTotalNumber = totalNumberOfPages.ToString(); PdfStamper pdfStamper = new PdfStamper(reader, memoryStream); for (int i = 1; i <= totalNumberOfPages; i++) { // skip adding folio if not started yet if (i < 1) { continue; } // page number preparation string folioNumber = i.ToString(); string folio = folioPrefix + folioNumber; folio += " de " + folioTotalNumber; folio += folioSuffix; BaseFont baseFont = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, BaseFont.NOT_EMBEDDED); PdfContentByte pdfPageContents = pdfStamper.GetUnderContent(i); // apply style pdfPageContents.BeginText(); pdfPageContents.SetFontAndSize(baseFont, folioSize); //pdfPageContents.SetRGBColorFill(Convert.ToInt32(color.Value.R), // Convert.ToInt32(color.Value.G), Convert.ToInt32(color.Value.B)); // prepare x,y cords 0,0 = bottom left Rectangle pageSize = reader.GetPageSizeWithRotation(i); float strWidth = pdfPageContents.GetEffectiveStringWidth(folio, false); const int padding = 10; float foliox = padding; float folioy = folioSize; foliox = pageSize.Width - strWidth - padding; pdfPageContents.ShowTextAligned(PdfContentByte.ALIGN_LEFT, folio, foliox, folioy, 0); strWidth = pdfPageContents.GetEffectiveStringWidth(fechaProceso, false); pdfPageContents.ShowTextAligned(PdfContentByte.ALIGN_RIGHT, fechaProceso, padding + strWidth, folioy, 0); pdfPageContents.EndText(); } pdfStamper.Close(); reader.Close(); File.WriteAllBytes(RutaPDf, memoryStream.ToArray()); } }
public override void OnEndPage(PdfWriter writer, Document document) { base.OnEndPage(writer, document); float effectiveStrWidthFirstPart = 0.0f; int pageN = writer.PageNumber; String text = "Page " + pageN.ToString() + " of "; Console.WriteLine("###-->> OnEndPageText = " + text); float len = 10.5f; iTextSharp.text.Rectangle pageSize = document.PageSize; cb.SetRGBColorFill(100, 100, 100); cb.BeginText(); cb.SetFontAndSize(BaseFont.CreateFont(), 10.5f); cb.SetTextMatrix(document.LeftMargin, pageSize.GetBottom(document.BottomMargin - 10)); cb.ShowText(text); effectiveStrWidthFirstPart = cb.GetEffectiveStringWidth(text, true); Console.WriteLine("###-->> Effective Str Width (first part) = " + effectiveStrWidthFirstPart); cb.EndText(); cb.AddTemplate(template, document.LeftMargin + len, pageSize.GetBottom(document.BottomMargin - 10)); }
private bool CreateLayerInLegend(PdfContentByte content, Configuration.MapTabRow mapTab, List <CommonLayer> layerList, LegendProperties properties, CommonLayer layer, float indent) { if (!layerList.Contains(layer)) { return(false); } float layerHeight = GetLayerHeightInLegend(layerList, properties, layer); if (properties.CurrentY < properties.Height && properties.CurrentY - layerHeight < 0) { if (properties.CurrentColumn == properties.NumColumns) { return(true); } properties.CurrentX += properties.ColumnWidth + properties.ColumnSpacing; properties.CurrentY = properties.Height; properties.CurrentColumn += 1; } int numClasses = GetNumClasses(layer); Configuration.LayerRow configLayer = mapTab.GetMapTabLayerRows().Where(o => String.Compare(o.LayerRow.LayerName, layer.Name, true) == 0).Select(o => o.LayerRow).FirstOrDefault(); string layerName = configLayer != null && !configLayer.IsDisplayNameNull() ? configLayer.DisplayName : layer.Name; // write the layer name if (layer.Type == CommonLayerType.Group || numClasses > 1) { properties.CurrentY -= properties.FontSize; string name = layerName; try { while (content.GetEffectiveStringWidth(name, false) > properties.ColumnWidth - indent) { name = name.Substring(0, name.Length - 1); } } catch { } content.BeginText(); content.SetFontAndSize(properties.BaseFont, properties.FontSize); content.SetRGBColorFill(0, 0, 0); content.ShowTextAligned(PdfContentByte.ALIGN_LEFT, name, properties.OriginX + properties.CurrentX + indent, properties.OriginY + properties.CurrentY + (properties.SwatchHeight - properties.FontSize) / 2, 0); content.EndText(); } if (layer.Type == CommonLayerType.Group) { properties.CurrentY -= properties.LayerSpacing; foreach (CommonLayer childLayer in layer.Children) { CreateLayerInLegend(content, mapTab, layerList, properties, childLayer, indent + 1.5f * properties.FontSize); } } else { properties.CurrentY -= properties.ClassSpacing; foreach (CommonLegendGroup legendGroup in layer.Legend.Groups) { foreach (CommonLegendClass legendClass in legendGroup.Classes) { if (!legendClass.ImageIsTransparent) { properties.CurrentY -= properties.SwatchHeight; MemoryStream stream = new MemoryStream(legendClass.Image); System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(stream); float w = properties.SwatchHeight * bitmap.Width / bitmap.Height; iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(legendClass.Image); image.SetAbsolutePosition(properties.OriginX + properties.CurrentX + indent, properties.OriginY + properties.CurrentY - properties.SwatchHeight * 0.1f); image.ScaleAbsolute(w, properties.SwatchHeight); content.AddImage(image); string label = numClasses > 1 ? legendClass.Label : layerName; try { while (content.GetEffectiveStringWidth(label, false) > properties.ColumnWidth - properties.SwatchWidth - properties.ClassSpacing) { label = label.Substring(0, label.Length - 1); } } catch { } content.BeginText(); content.SetFontAndSize(properties.BaseFont, properties.FontSize); content.SetRGBColorFill(0, 0, 0); content.ShowTextAligned(PdfContentByte.ALIGN_LEFT, label, properties.OriginX + properties.CurrentX + indent + properties.SwatchWidth + properties.ClassSpacing, properties.OriginY + properties.CurrentY + (properties.SwatchHeight - properties.FontSize) / 2, 0); content.EndText(); properties.CurrentY -= properties.ClassSpacing; } } } properties.CurrentY -= properties.LayerSpacing - properties.ClassSpacing; } return(false); }
public void Draw(PdfContentByte cb, IList <float[]> lines, IList <IElement> list) { if (lines.Count < 2) { return; //Do nothing } //TODO check all the style elements cb.SetLineWidth(3); //first set a letter type BaseFont bf = BaseFont.CreateFont("c:/windows/fonts/arial.ttf", BaseFont.WINANSI, BaseFont.EMBEDDED); cb.SetFontAndSize(bf, fontsize); float xPrevious = lines[0][0], yPrevious = lines[0][1]; int indexOfTextElement = 0, indexOfCharacter = 0; String currentCharacter = GetCharacter(indexOfTextElement, indexOfCharacter); while (string.IsNullOrEmpty(currentCharacter)) { if (indexOfCharacter >= GetLengthOfText(indexOfTextElement) - 1) { //this was the last character of the text element if (indexOfTextElement == list.Count - 1) { //this was the last text element; exit while loop return; //stop } else { //goto first character of the next element //TODO set the css indexOfTextElement++; indexOfCharacter = 0; } } currentCharacter = GetCharacter(indexOfTextElement, indexOfCharacter); } double halfWidthOfCharacter = cb.GetEffectiveStringWidth(currentCharacter, true) / 2.0; double totalLength = 0; bool lookForStart = true; for (int j = 1; j < lines.Count; j++) { float[] point = lines[j]; double lengthLijnStuk = CalculateDistance(xPrevious, yPrevious, point[0], point[1]); //System.out.Println(lengthLijnStuk); totalLength = totalLength + lengthLijnStuk; //System.out.Println(totalLength); while (totalLength >= halfWidthOfCharacter) { double tussen = totalLength - halfWidthOfCharacter; double xyAlongThis = lengthLijnStuk - tussen - halfWidthOfCharacter; double[] xy = GetPointOnLine(xPrevious, yPrevious, point[0], point[1], xyAlongThis); if (lookForStart) { ShowText(cb, point[0], point[1], xPrevious, yPrevious, xy[0], xy[1], currentCharacter); lookForStart = false; totalLength = tussen; //distance to the end of the line segment } else { //look for the end point lookForStart = true; totalLength = tussen; //distance to the end of the line segment indexOfCharacter++; currentCharacter = GetCharacter(indexOfTextElement, indexOfCharacter); while (currentCharacter == null || currentCharacter.Length == 0) { if (indexOfCharacter >= GetLengthOfText(indexOfTextElement) - 1) { //this was the last character of the text element if (indexOfTextElement == list.Count - 1) { //this was the last text element; exit while loop return; } else { //goto first character of the next element indexOfTextElement++; indexOfCharacter = 0; } } currentCharacter = GetCharacter(indexOfTextElement, indexOfCharacter); } halfWidthOfCharacter = cb.GetEffectiveStringWidth(currentCharacter, true) / 2.0; } } xPrevious = point[0]; yPrevious = point[1]; } }
void DrawBox( Document doc, PdfContentByte cb, Certificate c, CertificateArea box, string text, bool wrap) { if (null == box || !box.Enabled || String.IsNullOrEmpty(text)) { return; } var origin = (float)box.Left; var alignment = PdfContentByte.ALIGN_LEFT; switch (box.Alignment) { case "center": alignment = PdfContentByte.ALIGN_CENTER; origin = (box.Left + box.Width / 2); break; case "right": alignment = PdfContentByte.ALIGN_RIGHT; origin = box.Left + box.Width; break; } var size = 0 == box.FontSize ? 12f : box.FontSize; cb.SetFontAndSize( BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, false), size); cb.BeginText(); if (wrap) { var y = 0; var line = 0; for (var x = 0; x < text.Length; x++) { var sub = text.Substring(y, x - y); var width = cb.GetEffectiveStringWidth(sub, false); if (width >= 10f * Dpi * box.Width / c.BackgroundSize.Width) { // backup to the last whitespace char while (x > y && Char.IsLetterOrDigit(text[x])) { x--; } sub = text.Substring(y, x - y); cb.ShowTextAligned( alignment, sub, (0.5f * Dpi) + (10f * Dpi * origin / c.BackgroundSize.Width), (0.5f * Dpi) + (7.5f * Dpi) - (7.5f * Dpi * box.Top / c.BackgroundSize.Height) - size - (line * size), 0f ); y = x; line++; } } } else { cb.ShowTextAligned( alignment, text, (0.5f * Dpi) + (10f * Dpi * origin / c.BackgroundSize.Width), (0.5f * Dpi) + (7.5f * Dpi) - (7.5f * Dpi * box.Top / c.BackgroundSize.Height) - size, 0f ); } cb.EndText(); }
public static string OperatPDFForName(string PDFPath, string PDFFile, string userID, string fullName) { string returnPath = ""; try { // 创建一个PdfReader对象 PdfReader reader = new PdfReader(PDFFile); // 获得文档页数 int n = reader.NumberOfPages; // 获得第一页的大小 iTextSharp.text.Rectangle psize = reader.GetPageSize(1); float width = psize.Width; float height = psize.Height; // 创建一个文档变量 Document document = new Document(psize, 50, 50, 50, 50); // 创建该文档 returnPath = PDFPath + Guid.NewGuid() + ".pdf"; PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(returnPath, FileMode.Create)); // 打开文档 document.Open(); int i = 0; int p = 0; while (i < n) { document.NewPage(); // 添加内容 PdfContentByte cb = writer.DirectContent; p++; i++; PdfImportedPage page = writer.GetImportedPage(reader, i); cb.AddTemplate(page, 0, 0); BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED); cb.BeginText(); cb.SetFontAndSize(bf, 10);//14 cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, "Easy Training", width / 2, height - 26, 0); cb.SetFontAndSize(bf, 10);//12 cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, "Novo Nordisk Site Tianjin", width / 2, height - 12, 0); cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, "page " + p + " of " + n, width / 2, 6, 0); string strTime = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); float LenTime = cb.GetEffectiveStringWidth(strTime, true) / 2; cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, strTime, width - LenTime - 10, 6, 0); string strForSave = "Saved by " + userID + "-" + fullName; float Len = cb.GetEffectiveStringWidth(strForSave, true) / 2; cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, strForSave, width - Len - 10, 20, 0); cb.EndText(); } // 关闭文档 document.Close(); } catch (Exception de) { Console.Error.WriteLine(de.Message); Console.Error.WriteLine(de.StackTrace); } return(returnPath); }