0

当使用 HTML 到 PDF 时,我得到奇怪的结果,试图
在没有彩色背景的情况下将页面中心的一些文本对齐到 div。

现在,我似乎也无法让 h1 或 p 从顶部填充,
但我可以让 div 向下填充,但不能对齐中心。

请给我任何提示。

:}

PdfReader template = new PdfReader(Server.MapPath("~/Cirtificate.pdf"));
Document document = new Document(PageSize.A4);
PdfWriter writer = PdfWriter.GetInstance(document, new     FileStream(Server.MapPath("~/test.pdf"), FileMode.Create));
document.Open();

PdfContentByte cb = writer.DirectContent;
PdfImportedPage page = writer.GetImportedPage(template, 1);
document.NewPage();
cb.AddTemplate(page, 0, 0);

WebClient wc = new WebClient();
string htmlText = wc.DownloadString("http://localhost:65061/TEST_OUTPUT_PAGE.html");

StringReader sr = new StringReader(htmlText);
XMLWorkerHelper.GetInstance().ParseXHtml(writer, document, sr);  
document.Close();

Response.Redirect("~/test.pdf");

CSS

html, body {
    display: block;
    clear: both;
    float: left;
    position: static;
    overflow: visible;
    width: 100%;
    height: 100%;
    margin: 0px;
    border: 0px;
    padding: 0px;
    background-color: #fff;
}

body {
    font-family: Arial, Sans-Serif;
    text-align: center;
    color: #000;
    font-size: 12px;
    line-height: 100%;
    text-align: center;
}
4

1 回答 1

0

有关各种对象支持的 CSS 样式列表,请参阅此页面。我建议将您包装<p>在 .<div>上使用填充<div>和对齐<p>

var outputFile = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Test1.pdf");
using (var fs = new FileStream(outputFile, FileMode.Create, FileAccess.Write, FileShare.None)) {
    using (Document document = new Document(PageSize.A4)) {
        using (PdfWriter writer = PdfWriter.GetInstance(document, fs)) {
            document.Open();
            var html = "<html><head><title>Test</title></head><body><div style=\"padding-top:200px;\"><p style=\"text-align:center;padding-top:200px;\">This is a test</p></div></body></html>";
            using (StringReader sr = new StringReader(html)) {
                XMLWorkerHelper.GetInstance().ParseXHtml(writer, document, sr);
            }
            document.Close();
        }
    }
}
于 2013-04-02T13:14:01.407 回答