簡體   English   中英

在 pdf 的頂部添加一個 0 邊距的圖像,但是主體的 rest 具有左右邊距/填充(Itext7,C#)

[英]Add an image at the top of the pdf with 0 margin, however the rest of the body has left and right margin/padding (Itext7, C#)

我想創建一個 pdf,其中包含第一頁頂部的 0 邊距的公告徽標/圖像。 但是我想要正文中內容的空白/邊距,如下所示:

在此處輸入圖像描述

我正在使用 IText7 和 C# (.net 4.6.1) 來嘗試實現這一目標。 我將文檔邊距設置為 0,然后添加圖像並遍歷 html(來自我的所見即所得編輯器)將其分解為元素並將每個元素添加到文檔中。 但是,當我這樣做時,它也會將正文中內容的邊距設置為 0。

在此處輸入圖像描述

有沒有辦法做到這一點,我可以為我在正文中添加的每個元素添加邊距? 還是有什么其他方法? 先感謝您。

這是我的代碼:

                string bulletinpdf = Server.MapPath("~/generatedfiles/pdf/text.pdf");
                PdfWriter writer = new PdfWriter(bulletinpdf);
                PdfDocument pdfDoc = new PdfDocument(writer);
                pdfDoc.SetDefaultPageSize(PageSize.LETTER);
                Document document2 = new Document(pdfDoc);
                document2.SetMargins(0, 0, 0, 0);        //set document margins to 0 here

                string outputpdf = Server.MapPath("~/Components/Images/bulletinheader.jpg");
                ImageData imageData = ImageDataFactory.Create(outputpdf);
                Image image2 = new Image(imageData);
                //image2.SetMargins(0, 0, 0, 0);      //i've tried commenting out the above and trying this. to no avail.
                document2.Add(image2);

                ConverterProperties properties = new ConverterProperties();
                IList elements = (IList)HtmlConverter.ConvertToElements(createForm.BulletinText, properties);
                foreach (IElement element in elements)
                { 
                    document2.Add((IBlockElement) element);
                }

                document2.Close();

這是 header 圖像: 在此處輸入圖像描述

這是 creatForm.BulletinText 的 html(從 CK 內容編輯器中提取的 html)我已經清空了圖像元素,但出於復制目的附上了下面的圖像:

<p><img alt="" src="" style="float:left; height:400px; width:400px">Testing 123Testing 123Testing 123Testing 123Testing 123Testing 123Testing 123Testing 123Testing 123Testing 123Testing 123Testing 123Testing 123Testing 123Testing 123Testing 123Testing 123Testing 123Testing 123Testing 123Testing 123Testing 123&nbsp;</p>

<p>&nbsp;</p>

<p>&nbsp;</p>

<p>&nbsp;</p>

<p>&nbsp;</p>

<p>&nbsp;</p>

<p>&nbsp;</p>

<p>&nbsp;</p>

<p>&nbsp;</p>

<p>&nbsp;</p>

<p>&nbsp;</p>

<p>&nbsp;</p>

<table border="1" cellpadding="1" cellspacing="1" style="width:500px">
    <tbody>
        <tr>
            <td>This is a test</td>
            <td>This is a test</td>
        </tr>
        <tr>
            <td>This is a test</td>
            <td>This is a test</td>
        </tr>
        <tr>
            <td>This is a test</td>
            <td>This is a test</td>
        </tr>
    </tbody>
</table>



這是放置在 html img 元素中的圖像,並修改 src 以包含此圖片的路徑: 在此處輸入圖像描述

您可以通過稍微調整 HTML 來達到預期的目標,您必須編寫的所有代碼都是幾行代碼:

FileInfo htmlPath = new FileInfo("C:/Users/x/Desktop/html.html");

HtmlConverter.ConvertToPdf(new FileStream(htmlPath.FullName, FileMode.Open), 
new FileStream("C:/Users/x/Desktop/out.pdf", FileMode.OpenOrCreate),
new ConverterProperties().SetBaseUri(htmlPath.Directory.FullName));

現在在您的 HTML 中,您需要添加將包含 header 的<img> ,為其設置 ID 並將其設置為(與 PDF 的寬度匹配):

<img id="header" style="width: 210mm" src="header.jpg"/>

然后我們將文檔上的所有左右邊距設置為 0(我們將通過將邊距設置為<body>元素來補償它),並將第一頁的上邊距設置為 0。我們將徽標圖像標記為運行元素並將其包含在第一頁的邊距框區域。 全部用 CSS 聲明方式!

<style>

@page {
    margin-left: 0;
    margin-right: 0;
}

@page:first {
    margin-top: 138pt;
    
    @top-left {
        content: element(header);
    }
}

#header {
    position: running(header);
}
</style>

完整的 HTML(我將表格放在下一頁只是為了驗證第二頁看起來也符合預期):

<head>
<style>

@page {
    margin-left: 0;
    margin-right: 0;
}

@page:first {
    margin-top: 138pt;
    
    @top-left {
        content: element(header);
    }
}

#header {
    position: running(header);
}
</style>
</head>

<body style="margin-left: 36pt; margin-right: 36pt;">

<img id="header" style="width: 210mm" src="header.jpg"/>

<p>Testing 123Testing 123Testing 123Testing 123Testing 123Testing 123Testing 123Testing 123Testing 123Testing 123Testing 123Testing 123Testing 123Testing 123Testing 123Testing 123Testing 123Testing 123Testing 123Testing 123Testing 123Testing 123&nbsp;</p>

<p>&nbsp;</p>

<p>&nbsp;</p>

<p>&nbsp;</p>

<p>&nbsp;</p>

<p>&nbsp;</p>

<p>&nbsp;</p>

<p>&nbsp;</p>

<p>&nbsp;</p>

<p>&nbsp;</p>

<p>&nbsp;</p>

<p>&nbsp;</p>


<table border="1" cellpadding="1" cellspacing="1" style="width:500px; page-break-before: always;">
    <tbody>
        <tr>
            <td>This is a test</td>
            <td>This is a test</td>
        </tr>
        <tr>
            <td>This is a test</td>
            <td>This is a test</td>
        </tr>
        <tr>
            <td>This is a test</td>
            <td>This is a test</td>
        </tr>
    </tbody>
</table>

</body>

視覺結果:

結果

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM