0

我正在使用 iTextSharp 创建 PDF 用于检查打印目的。我的问题是,我无法在支票信封上的小窗口所在的支票上打印姓名/地址。地址线间距过大。我在 HTML 文档的字体标签中设置行高,但它没有被继承。

这是地址部分,具体来说:

            conn.Open();
            using (SqlDataReader itemReader = strSQL.ExecuteReader())
            {
                checksHolder.InnerHtml += "<table cellSpacing=\"0\" cellPadding=\"0\" border=\"0\" width=\"100%\">\n";

                while (itemReader.Read())
                {

                    if (i % 3 == 0 && i != 0)
                    {
                        checksHolder.InnerHtml += "</table>\n";
                        checksHolder.InnerHtml += "<!-- pagebreak -->\n";
                        checksHolder.InnerHtml += "<table cellSpacing=\"0\" cellPadding=\"0\" border=\"0\" width=\"100%\">\n";
                        f = 0;
                    }

                    if (i > 0 && i % 3 != 0)
                    {
                        checksHolder.InnerHtml += "<tr><td colspan=\"3\"><br/><br/><br/><br/><br/><br/>";
                        checksHolder.InnerHtml += "</td></tr>\n";
                    }

                    String[] tmpArray = getConsignorInfo(itemReader["check_cs_id"].ToString());

                    int CheckNum = Int32.Parse(itemReader["check_number"].ToString());
                    String CheckAmount = itemReader["check_amount"].ToString();
                    String PayableTo = tmpArray[0];
                    String Memo = itemReader["check_memo"].ToString();

                    String name = tmpArray[0];
                    String address = tmpArray[1];
                    String city = tmpArray[2];
                    String state = tmpArray[3];
                    String zip = tmpArray[4];

                    Char NBSP = '\u00A0';

                    checksHolder.InnerHtml += "<tr><td colspan=\"3\"><br/><br/></td></tr>\n";
                    checksHolder.InnerHtml += "<tr>\n";
                    //checksHolder.InnerHtml += "<td width=\"5\"></td>\n";
                    checksHolder.InnerHtml += "<td colspan=\"2\">\n";
                    checksHolder.InnerHtml += "<font size=\"2\">\n";
                    checksHolder.InnerHtml += "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n";
                    checksHolder.InnerHtml += PayableTo + "</font>\n";
                    checksHolder.InnerHtml += "</td>\n";
                    checksHolder.InnerHtml += "<td align=\"right\">\n";
                    checksHolder.InnerHtml += "<font align=\"right\" size=\"2\">" + string.Format("{0:c}", Double.Parse(CheckAmount.ToString())).Replace("$", "") + "</font>\n";
                    checksHolder.InnerHtml += "</td>\n";
                    checksHolder.InnerHtml += "</tr>\n";
                    checksHolder.InnerHtml += "<tr>\n";
                    checksHolder.InnerHtml += "<td colspan=\"3\">\n";
                    checksHolder.InnerHtml += "&nbsp;\n";
                    checksHolder.InnerHtml += "</td>\n";
                    checksHolder.InnerHtml += "</tr>\n";
                    checksHolder.InnerHtml += "<tr>\n";
                    checksHolder.InnerHtml += "<td colspan=\"3\">\n";
                    checksHolder.InnerHtml += "<font size=\"2\">";
                    if (wordify(decimal.Parse(CheckAmount.ToString())).Length >= 90)
                    {
                        checksHolder.InnerHtml += wordify(decimal.Parse(CheckAmount.ToString()));
                    }
                    else
                    {
                        string exes = " ";
                        for (int x = wordify(decimal.Parse(CheckAmount.ToString())).Length; x <= 90; x++)
                        {
                            exes += "x";
                        }
                        checksHolder.InnerHtml += wordify(decimal.Parse(CheckAmount.ToString())) + exes;
                    }
                    checksHolder.InnerHtml += "</font>\n";
                    checksHolder.InnerHtml += "</td>\n";
                    checksHolder.InnerHtml += "</tr>\n";
                    checksHolder.InnerHtml += "<tr>\n";
                    checksHolder.InnerHtml += "<td colspan=\"2\">\n";

                    checksHolder.InnerHtml += "<table cellSpacing=\"0\" cellPadding=\"0\" border=\"0\" width=\"100%\">\n";
                    checksHolder.InnerHtml += "<tr>\n";
                    checksHolder.InnerHtml += "<td colspan=\"2\"><br/>\n";
                    checksHolder.InnerHtml += "<font size=\"2\" line-height=\"8p\">" + name + "<br/>\n";
                    checksHolder.InnerHtml += address + "<br/>\n";
                    checksHolder.InnerHtml += city + ",\n";
                    checksHolder.InnerHtml += state + " \n";
                    checksHolder.InnerHtml += zip + "</font>\n";
                    checksHolder.InnerHtml += "</td>\n";
                    checksHolder.InnerHtml += "</tr>\n";

                    checksHolder.InnerHtml += "<tr>\n";
                    checksHolder.InnerHtml += "<td colspan=\"3\">\n";
                    checksHolder.InnerHtml += "<br/></td>\n";
                    checksHolder.InnerHtml += "</tr>\n";

                    checksHolder.InnerHtml += "<tr>\n";
                    checksHolder.InnerHtml += "<td width=\"10\"></td>\n";
                    checksHolder.InnerHtml += "<td>\n";
                    checksHolder.InnerHtml += "<font size=\"2\">" + Memo.Substring(0, 50) + "...</font>\n";
                    checksHolder.InnerHtml += "</td>\n";
                    checksHolder.InnerHtml += "</tr>\n";
                    checksHolder.InnerHtml += "</table>\n";

                    checksHolder.InnerHtml += "</td>\n";
                    checksHolder.InnerHtml += "<td valign=\"bottom\" width=\"40\">\n";
                    checksHolder.InnerHtml += "<img valign=\"bottom\" align=\"left\" src=\"images/wesSig.png\" Alt=\"\" /><br/>\n";
                    checksHolder.InnerHtml += "</td>\n";
                    checksHolder.InnerHtml += "</tr>\n";


                    i++;
                    f++;
                }

                checksHolder.InnerHtml += "</table>\n";

                itemReader.Close();
            }

public void CreatePDFDocument(string strHtml)
{
    string strFileName = HttpContext.Current.Server.MapPath("printCheck.pdf");
    // step 1: creation of a document-object

    iTextSharp.text.Rectangle rec = new iTextSharp.text.Rectangle(612, 792);
    Document document = new Document(rec);
    document.SetMargins(45f, 30f, 55f, 30f);

    // step 2:
    // we create a writer that listens to the document
    var pdfWriter = PdfWriter.GetInstance(document, new FileStream(strFileName, FileMode.Create));
    pdfWriter.InitialLeading = 12.5f;

    string[] stringSeparators = new string[] { "<!-- pagebreak -->" };
    //Response.Write(strHtml);
    string[] newString = strHtml.Split(stringSeparators, StringSplitOptions.None);

    //HTMLWorker obj = new HTMLWorker(document);

    document.Open();

    var xmlWorkerHelper = XMLWorkerHelper.GetInstance();
    var cssResolver = new StyleAttrCSSResolver();
    var xmlWorkerFontProvider = new XMLWorkerFontProvider();

    var cssAppliers = new CssAppliersImpl(xmlWorkerFontProvider);
    var htmlContext = new HtmlPipelineContext(cssAppliers);
    htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory());

    PdfWriterPipeline pdfWriterPipeline = new PdfWriterPipeline(document, pdfWriter);
    HtmlPipeline htmlPipeline = new HtmlPipeline(htmlContext, pdfWriterPipeline);
    CssResolverPipeline cssResolverPipeline = new CssResolverPipeline(cssResolver, htmlPipeline);
    XMLWorker xmlWorker = new XMLWorker(cssResolverPipeline, true);
    XMLParser xmlParser = new XMLParser(xmlWorker);

    int i = 0;
    foreach (String currentString in newString) {
        if (i > 0)
        {
            document.NewPage();
        }

        StringReader sr = new StringReader(currentString);
        xmlParser.Parse(sr);

        i++;
    }

    document.Close();

    ShowPdf(strFileName);
}

我只有半英寸可以输入姓名/地址。我已经尝试同时指定 line-height8px以防8万一。没有任何效果。有谁知道我需要如何格式化 HTML 以保留行高?提前致谢。

4

1 回答 1

0

我升级到 itextsharp 的 xmlworker (http://sourceforge.net/projects/xmlworker/),它允许我使用 css 和 div 标签。不幸的是,Margin 不起作用,但 padding 起作用,所以我能够做我需要做的事情。

于 2013-01-21T13:38:33.930 回答