如何在XSL-FO中为A4页面添加边框并设置fo:block圆角
Hey there! Let's work through your two XSL-FO requirements—adding a border to your A4 page and setting rounded corners for an fo:block—using your existing code as a base.
To add a border around the entire content area of your A4 page, you can apply border properties directly to the fo:region-body element in your page master. This will create a border around the main content flow of your page.
Here's how to update your fo:simple-page-master section:
<fo:simple-page-master master-name="A4" page-height="11.40in" page-width="8.27in" margin-top="0.30in" margin-bottom="0.30in" margin-left="0.30in" margin-right="0.30in"> <fo:region-body region-name="body" margin=".30in" border="1pt solid black"/> </fo:simple-page-master>
- The
border="1pt solid black"property adds a solid 1-point black border around the entire body region. You can tweak the thickness (e.g.,2pt), style (e.g.,dashed,dotted), and color (e.g.,#333) to match your needs.
If you want a border that spans the full page (including the margin areas), adjust the page margins to 0 and set the region-body's margin to your desired spacing instead:
<fo:simple-page-master master-name="A4" page-height="11.40in" page-width="8.27in" margin-top="0" margin-bottom="0" margin-left="0" margin-right="0"> <fo:region-body region-name="body" margin="0.30in" border="1pt solid black"/> </fo:simple-page-master>
This way, the border will sit right at the edges of the A4 page, with 0.30in of padding between the border and your content.
fo:block XSL-FO doesn't have native support for rounded corners, but Apache FOP (a popular XSL-FO processor) provides an extension for this: fox:border-radius. Since your code already includes the FOP extension namespace (xmlns:fox="http://xmlgraphics.apache.org/fop/extensions"), you can use this directly.
Update your fo:block to include the rounded corner property:
<fo:block text-align="center" border="1pt solid black" fox:border-radius="10pt"> How to create rounded border </fo:block>
fox:border-radius="10pt"sets a 10-point radius for all four corners. If you want different radii for each corner, you can specify them individually:
This would create asymmetrical rounded corners.fox:border-top-left-radius="15pt" fox:border-top-right-radius="5pt" fox:border-bottom-right-radius="15pt" fox:border-bottom-left-radius="5pt"
Putting it all together, here's your complete XSL-FO code with both the page border and rounded block corners:
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:fox="http://xmlgraphics.apache.org/fop/extensions"> <fo:layout-master-set> <fo:simple-page-master master-name="A4" page-height="11.40in" page-width="8.27in" margin-top="0.30in" margin-bottom="0.30in" margin-left="0.30in" margin-right="0.30in"> <fo:region-body region-name="body" margin=".30in" border="1pt solid black"/> </fo:simple-page-master> </fo:layout-master-set> <fo:page-sequence master-reference="A4"> <fo:flow flow-name="body"> <fo:block text-align="center" border="1pt solid black" fox:border-radius="10pt" padding="8pt"> How to create rounded border </fo:block> </fo:flow> </fo:page-sequence> </fo:root>
I added a small padding="8pt" to the block to keep the text from touching the border—this is optional but improves readability.
内容的提问来源于stack exchange,提问作者Sam

