Pdf页边距在新页面上重置在C#上使用Itext

时间:2019-04-17 04:59:21

标签: c# css pdf itext document

我正在为我的身体设置边距,以使其不与页眉和页脚重叠。页边距似乎在第一页上工作正常,但在第一页后会重置并与我的页眉重叠。我正在使用itext编写pdf文件,并在设计中使用了CSS。

FileStream ms = new FileStream("C:\\Users\\Dip\\Desktop\\111.pdf", FileMode.Create);
        // MessageBox.Show(Convert.ToString(ms));
        Document doc = new Document(PageSize.A4,40,40,250,80);
        PdfWriter writer = PdfWriter.GetInstance(doc, ms);
         writer.PageEvent = new ITextEvents();
        doc.SetMargins(40, 40, 250, 80);

        string html = "C:\\Users\\Dip\\Desktop\\11.xml ";

        string csspath = "D:\\Office\\cases_converter_tool\\cases_converter_tool\\bin\\Debug\\styleCase.css ";
        doc.SetMargins(70, 70, 250, 120);
        doc.Open();
        Paragraph k = new Paragraph("  ");
        doc.Add(k);
        var css = "D:\\Office\\cases_converter_tool\\cases_converter_tool\\bin\\Debug\\styleCase.css ";
        //  XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, new StreamReader("C:\\Users\\Dip\\Desktop\\MLRA_1982_402.xml"));
        var tq = new StringReader("C:\\Users\\Dip\\Desktop\\11.xml");
        var tq1 = new FileStream("C:\\Users\\Dip\\Desktop\\MLRA_1982_402.xml", FileMode.Open);

        using (var strReader = new StringReader("C:\\Users\\Dip\\Desktop\\11.xml"))
        {
            //Set factories
            HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
            htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory());
            //Set css
            ICSSResolver cssResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(false);
            cssResolver.AddCssFile(csspath, true);
            //Export
            IPipeline pipeline = new CssResolverPipeline(cssResolver, new HtmlPipeline(htmlContext, new PdfWriterPipeline(doc, writer)));
            var worker = new XMLWorker(pipeline, true);
            var xmlParse = new XMLParser(true, worker);
            xmlParse.Parse(tq1);
            if(doc.NewPage())
            {
                doc.SetMargins(40, 40, 250, 80);
            }
            tq1.Close();
            // XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, new StreamReader("C:\\Users\\Dip\\Desktop\\MLRA_1982_402.xml"));
        }
        doc.Close();

0 个答案:

没有答案