public void Go() { var OUT_FILE = Helpers.IO.GetClassOutputPath(this); var content = @" Allein ist besser als mit Schlechten im Verein: mit Guten im Verein, ist besser als allein. "; var table = new PdfPTable(1); // make sure .dll is in correct /bin directory StreamUtil.AddToResourceSearch("itext-hyph-xml.dll"); using (var stream = new MemoryStream()) { using (var document = new Document(PageSize.A8.Rotate())) { PdfWriter.GetInstance(document, stream); document.Open(); var chunk = new Chunk(content) .SetHyphenation(new HyphenationAuto("de", "DR", 3, 3)); table.AddCell(new Phrase(chunk)); document.Add(table); } File.WriteAllBytes(OUT_FILE, stream.ToArray()); } }
private static iTextSharp.text.Font CreateChineseFont() { //如果不使用CID字体,下面三行不需要 //BaseFont.AddToResourceSearch("iTextAsian.dll"); //BaseFont.AddToResourceSearch("iTextAsianCmaps.dll"); //新版的AddToResourceSearch移到了StreamUtil StreamUtil.AddToResourceSearch("iTextAsian.dll"); StreamUtil.AddToResourceSearch("iTextAsianCmaps.dll"); //"UniGB-UCS2-H" "UniGB-UCS2-V"是简体中文,分别表示横向字 和 // 纵向字 //" STSong-Light"是字体名称 BaseFont baseFT = BaseFont.CreateFont(@"c:\windows\fonts\SIMHEI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); //BaseFont baseFT = BaseFont.CreateFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED); iTextSharp.text.Font font = new iTextSharp.text.Font(baseFT); return(font); }
public virtual void SetUp() { StreamUtil.AddToResourceSearch(@"iTextAsian.dll"); }
protected void Application_Start() { AreaRegistration.RegisterAllAreas(); RouteConfig.RegisterRoutes(RouteTable.Routes); StreamUtil.AddToResourceSearch(HttpContext.Current.Server.MapPath("~/AsianFonts/iTextAsian.dll")); }
static PdfDocumentBase() { StreamUtil.AddToResourceSearch("iTextAsian.dll"); StreamUtil.AddToResourceSearch("iTextAsianCmaps.dll"); }
// =========================================================================== public void Write(Stream stream) { // step 1 using (Document document = new Document( new Rectangle(240, 240), 10, 10, 10, 10 )) { // step 2 PdfWriter writer = PdfWriter.GetInstance(document, stream); // step 3 document.Open(); // step 4 // create a long Stringbuffer with movie titles StringBuilder sb = new StringBuilder(); IEnumerable <Movie> movies = PojoFactory.GetMovies(1); foreach (Movie movie in movies) { // replace spaces with non-breaking spaces sb.Append(movie.MovieTitle.Replace(' ', '\u00a0')); // use pipe as separator sb.Append('|'); } // Create a first chunk Chunk chunk1 = new Chunk(sb.ToString()); // wrap the chunk in a paragraph and add it to the document Paragraph paragraph = new Paragraph("A:\u00a0"); paragraph.Add(chunk1); //paragraph.Alignment = Element.ALIGN_JUSTIFIED; document.Add(paragraph); document.Add(Chunk.NEWLINE); // define the pipe character as split character chunk1.SetSplitCharacter(new PipeSplitCharacter()); // wrap the chunk in a second paragraph and add it paragraph = new Paragraph("B:\u00a0"); paragraph.Add(chunk1); //paragraph.Alignment = Element.ALIGN_JUSTIFIED; document.Add(paragraph); document.Add(Chunk.NEWLINE); // create a new StringBuffer with movie titles sb = new StringBuilder(); foreach (Movie movie in movies) { sb.Append(movie.MovieTitle); sb.Append('|'); } // Create a second chunk Chunk chunk2 = new Chunk(sb.ToString()); // wrap the chunk in a paragraph and add it to the document paragraph = new Paragraph("C:\u00a0"); paragraph.Add(chunk2); // paragraph.Alignment = Element.ALIGN_JUSTIFIED; document.Add(paragraph); document.NewPage(); // load hyphen xml resources StreamUtil.AddToResourceSearch(Path.Combine(Utility.ResourceXml, "itext-hyph-xml.dll")); // define hyphenation for the chunk chunk2.SetHyphenation(new HyphenationAuto("en", "US", 2, 2)); // wrap the second chunk in a second paragraph and add it paragraph = new Paragraph("D:\u00a0"); paragraph.Add(chunk2); // paragraph.Alignment = Element.ALIGN_JUSTIFIED; document.Add(paragraph); // go to a new page document.NewPage(); // define a new space/char ratio writer.SpaceCharRatio = PdfWriter.NO_SPACE_CHAR_RATIO; // wrap the second chunk in a third paragraph and add it paragraph = new Paragraph("E:\u00a0"); paragraph.Add(chunk2); // paragraph.Alignment = Element.ALIGN_JUSTIFIED; document.Add(paragraph); } }
public static void FillInPdfTemplateFields(string templatPath, Stream pdfOut, Dictionary <string, string> hashTable, byte[] qrCode, bool isEncrypt) { PdfReader pdfReader = new PdfReader(templatPath); PdfStamper pdfStamper = new PdfStamper(pdfReader, pdfOut); try { if (isEncrypt) { pdfStamper.SetEncryption(PdfWriter.STRENGTH40BITS, null, null, PdfWriter.AllowPrinting); } StreamUtil.AddToResourceSearch(Assembly.Load("iTextAsian")); BaseFont font = BaseFont.CreateFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); AcroFields pdfFormFields = pdfStamper.AcroFields; foreach (KeyValuePair <string, AcroFields.Item> de in pdfFormFields.Fields) { string fieldKey = de.Key.ToString(); if (string.IsNullOrWhiteSpace(fieldKey)) { continue; } if (fieldKey.Contains("img_")) { int page = Convert.ToInt32(fieldKey.Split('_')[1]); string hasKey = fieldKey.Split('_')[2]; //if (hashTable.ContainsKey(hasKey)) //{ IList <AcroFields.FieldPosition> fieldPositions = pdfFormFields.GetFieldPositions(fieldKey); AcroFields.FieldPosition fieldPosition = fieldPositions[0]; iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(qrCode); image.ScaleToFit(fieldPosition.position.Width, fieldPosition.position.Height); float x = fieldPosition.position.Left; float y = fieldPosition.position.Top - image.ScaledHeight; image.SetAbsolutePosition(x, y); var pdfContentByte = pdfStamper.GetOverContent(page); pdfContentByte.AddImage(image); //} } else if (hashTable.ContainsKey(fieldKey)) { pdfFormFields.SetFieldProperty(fieldKey, "textfont", font, null); string fieldValue = hashTable[fieldKey]; if (string.IsNullOrWhiteSpace(fieldValue)) { fieldValue = string.Empty; } pdfFormFields.SetField(fieldKey, fieldValue); } } pdfStamper.FormFlattening = true; } catch (Exception) { } finally { pdfStamper.Close(); pdfReader.Close(); } }