C#: How to Use Itextsharp to Manipulate PDF Files

summary

How to convert HTML file to PDF file?Some recruitment online resume into doc file, can not be used directly, this caused great trouble, then it also has a format, that is HTML format. Export the file to HTML format, and then convert it to PDF file, so that it can be used directly. Usually in the project is also a lot of such requirements, need to convert the content into PDF file.

Now let’s take a look at how to convert HTML to PDF using itextsharp.

code implementation

1. Nuget installs itextsharp.

using iTextSharp.text;
using iTextSharp.text.pdf;

2. Convert HTML document to PDF.

  /// <summary>
        /// Switch Html Document to pdf
        /// </summary>
        /// <param name="htmlText"></param>
        /// <returns></returns>
        public byte[] ConvertHtmlTextToPDF(string htmlText)
        {
            if (string.IsNullOrEmpty(htmlText))
                return null;
            //to avoid when the htmlText without any html tag tag plain text, to PDF will hang, so all with <p> tag
            htmlText = "<p>" + htmlText + "</p>";
            using (var outputStream = new MemoryStream())
            {
                byte[] data = Encoding.UTF8.GetBytes(htmlText);
                var msInput = new MemoryStream(data);
                var doc = new Document();//pdf file,default A4 formate
                var writer = PdfWriter.GetInstance(doc, outputStream);
                doc.Open();
                iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, msInput, null, Encoding.UTF8, new UnicodeFontFactory());
                var pdfDest = new PdfDestination(PdfDestination.XYZ, 0, doc.PageSize.Height, 1f);
                var action = PdfAction.GotoLocalPage(1, pdfDest, writer);
                writer.SetOpenAction(action);
                doc.Close();
                msInput.Close();
                outputStream.Close();
                return outputStream.ToArray();
            }
        }

3. Unicode font support.

   /// <summary>
        /// Unicode font support
        /// </summary>
        public class UnicodeFontFactory : FontFactoryImp
        {
            public override Font GetFont(string fontname, string encoding, bool embedded, float size, int style, BaseColor color, bool cached)
            {
                //var chineseFontPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "msyh.ttc,0");
                //BaseFont baseFont = BaseFont.CreateFont(@"c:\Windows\Fonts\simsun.ttc,0", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
                BaseFont baseFont = BaseFont.CreateFont(@"c:\Windows\Fonts\SIMHEI.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
                //var baseFont = BaseFont.CreateFont(chineseFontPath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
                return new Font(baseFont, size, style, color);
            }
        }

4. Call build.

   string content = temp.Content;
            foreach (var dict in dicts)
            {
                content = content.Replace("{{" + dict.Key + "}}", dict.Value);
            }
            var path = _esignInfo.Value.ContractPath;
            //if (entity.ContractType == ContractType.First)
            //{
            //    path += "/" + appId + "/Agreements";
            //}
            entity.OriginalFileUrl = _pdfHelper.WritePdfFile(content, contractNo, path, "PDF");
            bool isSucc = !String.IsNullOrEmpty(entity.OriginalFileUrl);

Read More: