简体   繁体   中英

how to add background color for border of itextsharp pdf page

How can I add back ground color [not border line color] at out side of border[four sides] of itextsharp pdf page ..

I tride with this .. but Its not working for back ground color of out side of border..

   content.EoFill();
   content.EoFillStroke();
   content.Fill();
   content.FillStroke();

but not working

public class pageborder : PdfPageEventHelper
{
    public override void OnEndPage(PdfWriter writer, Document document)
    {
        base.OnEndPage(writer, document);
        var content = writer.DirectContent;
        content.SetColorStroke(BaseColor.BLACK);
        content.RoundRectangle(35f,55f, 520f, 750f ,20f);
        content.Stroke();        
    }    
}

How can I add back ground color [not border line color] at out side of border[four sides] of itextsharp pdf page

I assume this to mean you want something like this:

彩色框架页面的屏幕截图

You can create that by overriding onEndPage like this (this is Java code but the corresponding C# code should be easy to derive from it):

public void onEndPage(PdfWriter writer, Document document)
{
    super.onEndPage(writer, document);
    PdfContentByte content = writer.getDirectContent();
    content.setColorFill(BaseColor.BLACK);
    content.rectangle(writer.getPageSize().getLeft(), writer.getPageSize().getBottom(),
                      writer.getPageSize().getWidth(), writer.getPageSize().getHeight());
    content.roundRectangle(35f,55f, 520f, 750f ,20f);
    content.eoFill();        
}    

(the complete sample: CreateWithFrame.java method testCreateFramedDocumentEoFill )

This works by adding another, page-sized rectangle to the path and using even-odd-fill. As the inner area is added to the path both as part of the original, rounded rectangle and the new rectangle, ie twice, it is not filled by eoFill . The area between the borders of the rectangles is only added once. Thus, it is filled by eoFill .


You can also use the normal fill method if you arrange subpath orientations accordingly, eg

public void onEndPage(PdfWriter writer, Document document)
{
    super.onEndPage(writer, document);
    PdfContentByte content = writer.getDirectContent();
    content.setColorFill(BaseColor.BLACK);
    content.rectangle(writer.getPageSize().getRight(), writer.getPageSize().getBottom(),
                     -writer.getPageSize().getWidth(), writer.getPageSize().getHeight());
    content.roundRectangle(35f,55f, 520f, 750f ,20f);
    content.fill();
}    

(the complete sample: CreateWithFrame.java method testCreateFramedDocumentFill )

Here the page-sized rectangle is added with negative orientation while the rounded rectangle is added with positive orientation. For the inner area of the rounded rectangle, therefore, the subpaths cancel out and the fill call does not fill.

You have copy/pasted code to draw a page border:

public class pageborder : PdfPageEventHelper
{
    public override void OnEndPage(PdfWriter writer, Document document)
    {
        base.OnEndPage(writer, document);
        var content = writer.DirectContent;
        content.SetColorStroke(BaseColor.BLACK);
        content.RoundRectangle(35f,55f, 520f, 750f ,20f);
        content.Stroke();        
    }    
}

Unfortunately, you don't understand what you've copied. You indeed need to replace Stroke() by Fill() , but that will fill the rectangle with the default fill color and you didn't define any fill color.

You also need to replace SetColorStroke() by SetColorFill() . For instance:

public class pageborder : PdfPageEventHelper
{
    public override void OnEndPage(PdfWriter writer, Document document)
    {
        var content = writer.DirectContent;
        content.SetColorFill(BaseColor.RED);
        content.RoundRectangle(35f,55f, 520f, 750f ,20f);
        content.Fill();        
    }    
}

Now you'll fill the rectangle with the rounded corners with red.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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