简体   繁体   English

itextsharp pdf页面边框?

[英]itextsharp pdf page border?

I am using itext sharp for creating reports in PDF format. 我正在使用itext sharp来创建PDF格式的报告。
I want page borders. 我想要页面边框。 I tried some ways. 我试过一些方法。 I am not successful. 我不成功。
How can I get a page border for top, bottom, left, right using iText for .NET ? 如何使用iText for .NET获得顶部,底部,左侧,右侧的页面边框?
I added one image 1 . 我添加了一张图片1 I want borders like described in the image. 我想要图像中描述的边框。

You can try this code for adding the image for the header manually. 您可以尝试使用此代码手动添加标题的图像。

//Step 1: Add the Image file //步骤1:添加图像文件

strImgPath is refer the directory Info..

Image imgLogo = Image.GetInstance(strImgPath.ToString()+"\\abcdur compe.Jpg");
imgLogo.Alignment = Image.ALIGN_CENTER;
imgLogo.ScalePercent(50f);

// Step 2: // 第2步:

Add this ImgLogo to the PdfPTable by use of this
PdfPCell pdfcellImage = new PdfPCell(imgLogo, true);
pdfcellImage.FixedHeight = 40f;
pdfcellImage.HorizontalAlignment = Element.ALIGN_CENTER;
pdfcellImage.VerticalAlignment = Element.ALIGN_CENTER;
pdfcellImage.Border = Rectangle.NO_BORDER;
pdfcellImage.Border = Rectangle.NO_BORDER;
pdftblImage.AddCell(pdfcellImage);

// Step 3: //第3步:

Create Chunck to add Text for address or others
fntBoldComHd is a Base Font Library Object

Chunk chnCompany = new Chunk("Your CompanyName\nAddress", fntBoldComHd);

//Step 4:

Create Phrase For add the Chunks and PdfPTables

Phrase phHeader = new Phrase(); 

phHeader.Add(pdftblImage);
phHeader.Add(chnCompany);

// Step 5: //第5步:

Assign the Phrase to PDF Header
HeaderFooter header = new HeaderFooter(phHeader, false);
header.Border = Rectangle.NO_BORDER;
header.Alignment = Element.ALIGN_CENTER;
docPDF.Header = header;

I am using one hacky way of workaround but it will create the border.Use this method. 我使用一种hacky方式的解决方法,但它将创建边框。使用此方法。

private void CreateBorder(PdfContentByte cb, PdfWriter writer, Document doc)
    {
        iTextSharp.text.Rectangle r = doc.PageSize;
        float left = r.Left + 30;
        float right = r.Right - 30;
        float top = r.Top - 30;
        float bottom = r.Bottom + 30;
        float width = right - left;
        float height = top - bottom;

        PdfPTable tab = new PdfPTable(1);
        tab.TotalWidth = width;
        tab.LockedWidth = true;

        PdfPCell t = new PdfPCell(new Phrase(String.Empty));
        t.BackgroundColor = new BaseColor(250, 235, 215);
        t.FixedHeight = height;
        t.BorderWidth = 3;
        tab.AddCell(t);
        Paragraph pa = new Paragraph();
        pa.Add(tab);

        float h = tab.TotalHeight;
        PdfTemplate temp = cb.CreateTemplate(tab.TotalWidth, h);
        tab.WriteSelectedRows(0, -1, 0.0F, h, temp);
        iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(temp);
        img.SetAbsolutePosition(30, 30);
        cb.AddImage(img);
    }

If u want one more section for header create table width two rows.I hope this helps. 如果你想要一个标题创建表格宽度两行,我希望这有帮助。

Are you referring to the document margins? 你指的是文件边距吗? If yes, use the Document object's ctor to specify them: 如果是,请使用Document对象的ctor指定它们:

public Document(Rectangle pageSize, float marginLeft, float marginRight, float marginTop, float marginBottom);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM