Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在尝试使用iText(c)将HTML转换为PDF时,主要内容与右边距重叠_C#_Itext - Fatal编程技术网

C# 在尝试使用iText(c)将HTML转换为PDF时,主要内容与右边距重叠

C# 在尝试使用iText(c)将HTML转换为PDF时,主要内容与右边距重叠,c#,itext,C#,Itext,我正在尝试使用iText库将html转换为pdf,但尽管pdf创建工作正常,但页边距却不正常 我试图手动设置边距,但程序似乎不仅忽略了我,更糟糕的是,正如您在图像中看到的,主要内容与右侧重叠 这是我代码的一部分,cabecera是html所在的字符串 主页 string file = string.Format("{0}LEGO_{1}_{2}_{3}_{4}_{5}.pdf",Constants.WebConfig.urlCopiaPdf, fecha.Day, fecha.Month, fe

我正在尝试使用iText库将html转换为pdf,但尽管pdf创建工作正常,但页边距却不正常

我试图手动设置边距,但程序似乎不仅忽略了我,更糟糕的是,正如您在图像中看到的,主要内容与右侧重叠

这是我代码的一部分,cabecera是html所在的字符串

主页

string file = string.Format("{0}LEGO_{1}_{2}_{3}_{4}_{5}.pdf",Constants.WebConfig.urlCopiaPdf, fecha.Day, fecha.Month, fecha.Year, fecha.Hour, fecha.Minute);
            string fileWeb = string.Format("{0}LEGO_{1}_{2}_{3}_{4}_{5}.pdf", Constants.WebConfig.urlCopiaPdfWeb, fecha.Day, fecha.Month, fecha.Year, fecha.Hour, fecha.Minute);
            string fileHTML = string.Format("{0}LEGO_{1}_{2}_{3}_{4}_{5}.html",Constants.WebConfig.urlCopiaPdf, fecha.Day, fecha.Month, fecha.Year, fecha.Hour, fecha.Minute);
            //string file = string.Format("c:\\docs\\LEGO{0}_{1}_{2}_{3}_{4}.pdf", fecha.Day, fecha.Month, fecha.Year, fecha.Hour, fecha.Minute);

            StringReader sr = new StringReader(cabecera);
            Document pdfDoc = new Document(PageSize.A4, 40f, 40f, 60f, 20f);
            pdfDoc.SetMarginMirroring(false);
            HeaderAndFooter PageEventHandler = new HeaderAndFooter();               

            PdfWriter writer = PdfWriter.GetInstance(pdfDoc, new FileStream(file, FileMode.Create));
            writer.PageEvent = PageEventHandler;
            writer.SetMargins(40f, 40f, 60f, 20f);
            writer.SetMarginMirroring(false);
            pdfDoc.Open();
            iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr);
            pdfDoc.Close();
是的,我复制了setMargin和setmarginmirroring,我只是在尝试是否有任何东西可以工作……我甚至尝试创建一个PdfPageEventHelper,并编写以下代码作为测试

public class HeaderAndFooter : PdfPageEventHelper
{
    public HeaderAndFooter()
        : base()
    {
    }


    public override void OnEndPage(PdfWriter writer, Document document)
    {
        PdfContentByte cb = writer.DirectContent;
        ColumnText.ShowTextAligned(cb, Element.ALIGN_CENTER, new Phrase(String.Format("dsadsadsadsad", writer.PageNumber)), (document.Right - 40), (document.Bottom - document.Top) / 2, 0);
    }
}
但什么都没用…有人能帮我吗


谢谢

最后,我的问题是我用来创建PDF的html格式不好,没有html、头和体标记,模板不是我的,但是…我应该在问任何问题之前检查一下,所以在添加它需要的东西后,效果非常好。

我无法重现这个问题。我认为你没有得到任何答案,因为人们无法重现这个问题。无法重现的问题很难修复,因为没有人知道要修复什么。我们把这个问题放在一边,以获得更高的优先级,但今天我再次解决了这个问题……瞧!看起来html的格式不是很好。修好后效果很好,谢谢。