iTextSharp PDF页面边框?

3

我正在使用itext sharp创建PDF格式的报告。
我想要页面边框。我尝试了一些方法,但没有成功。
使用iText for .NET,如何获得上、下、左、右四个方向的页面边框?
我添加了一个图片1,我想要的边框就像图片里展示的那样。


1
很抱歉,我找不到你添加的图片。你能再添加一次吗? - mkl
3个回答

4
您可以尝试使用以下代码手动添加页眉图片。
//步骤1:添加图片文件
strImgPath is refer the directory Info..

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

// 第二步:

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);

// 第三步:

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);

// 第五步:

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;

谢谢,它正在工作。你能告诉我整个PDF边框的代码吗? - Prasad
1
希望这可以帮到你... http://www.mikesdotnetting.com/Article/89/iTextSharp-Page-Layout-with-Columns - Karthika Subramanian
然后尝试手动添加矩形就像边框一样...那可能会有帮助。 - Karthika Subramanian

1

我正在使用一种巧妙的解决方法,但它会创建边框。请使用这种方法。

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);
    }

如果您想为标题创建一个更多的部分,请创建一个具有两行宽度的表格。希望这可以帮到您。

0

您是指文档边距吗?如果是的话,请使用Document对象的ctor来指定它们:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接