java使用itext创建pdf时,分不同列显示

根据itext官方回复http://developers.itextpdf.com/question/how-format-string-resulting-two-column-display 实施;

 方法声明为:public ByteBuffer outputPDF(File file,String hints,String[] title,List<String[]> contents,int[] widths)

 
            BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            Font fontChinese_italic = new Font(bfChinese, 13, Font.ITALIC, BaseColor.LIGHT_GRAY);
            Font fontChinese_title = new Font(bfChinese, 14, Font.BOLD, BaseColor.BLACK);
            Font fontChinese_content = new Font(bfChinese, 14, Font.NORMAL, BaseColor.BLACK); 
Paragraph hintT = createParagraphWithTab(fontChinese_title,title);
//            Paragraph hintT = createParagraphWithSpaces(fontChinese_title,title);
//            document.add(hintT);
//
//            int sz = contents.size();
//            for(int j=0;j<sz;j++)
//            {
                Paragraph hintC = createParagraphWithTab(fontChinese_content,contents.get(j));
//                Paragraph hintC = createParagraphWithSpaces(fontChinese_content, contents.get(j));
//                document.add(hintC);
//            }

            PdfPTable table = new PdfPTable(7);//7为列,列要写,行可以不写
            table.setWidthPercentage(100);
            table.setHorizontalAlignment(Element.ALIGN_LEFT);
            table.setWidths({2,1,1,1,2,1,4});//这7列,每列的相对宽度,本身宽度与总和商是列宽比例,列宽数组大小要与之前构造函数中的列数严格一致
            table.getDefaultCell().setBorder(Rectangle.NO_BORDER);

            for(String tt:title)
            {
                table.addCell(new Phrase(10,tt,fontChinese_title));
            }

            for(String[] ss:contents)
            {
                for(String s:ss)
                {
                   table.addCell(new Phrase(10,s,fontChinese_content));
                }
            }

            document.add(table);



 private Paragraph createParagraphWithTab(Font font, String[] content) {
        Paragraph p = new Paragraph();
        p.setFont(font);
//        p.set
        for(String s:content)
        {
            p.add(s);
            p.add(Chunk.TAB);
        }
        return p;
    }

    private Paragraph createParagraphWithSpaces(Font font, String[] content) {
        Paragraph p = new Paragraph();
        p.setFont(font);
        for(String s:content)
        {
            p.add(String.format("%-10s", s));
        }
        return p;
    }

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用iText库可以方便地创建PDF文档并生成目录。 首先需要导入iText库,可以通过Maven或Gradle方式导入。 接着可以创建一个PdfDocument对象,并设置文档属性: ```java PdfDocument pdfDoc = new PdfDocument(new PdfWriter("output.pdf")); Document document = new Document(pdfDoc); pdfDoc.setTagged(); pdfDoc.getCatalog().setLang(new PdfString("en-us")); pdfDoc.getCatalog().setViewerPreferences(new PdfViewerPreferences().setDisplayDocTitle(true)); document.setProperty(Property.SPLIT_CHARACTERS, new DefaultSplitCharacters() { @Override public boolean isSplitCharacter(GlyphLine text, int glyphPos) { return false; } }); ``` 然后可以添加章节标题和内容: ```java // 添加章节标题 Paragraph chapterTitle = new Paragraph("Chapter 1").setFont(PdfFontFactory.createFont(FontConstants.HELVETICA_BOLD)).setFontSize(16); Chapter chapter = new Chapter(chapterTitle, 1); document.add(chapter); // 添加章节内容 Paragraph para1 = new Paragraph("This is the first paragraph."); Paragraph para2 = new Paragraph("This is the second paragraph."); document.add(para1); document.add(para2); ``` 最后可以生成目录: ```java pdfDoc.addEventHandler(PdfDocumentEvent.START_PAGE, new IEventHandler() { @Override public void handleEvent(Event event) { try { PdfDocumentEvent docEvent = (PdfDocumentEvent) event; PdfPage page = docEvent.getPage(); PdfCanvas canvas = new PdfCanvas(page.newContentStreamBefore(), page.getResources(), pdfDoc); Rectangle rect = new Rectangle(36, 750, 523, 36); Canvas canvas1 = new Canvas(canvas, pdfDoc, rect); Paragraph p = new Paragraph().add("Table of Contents").setFont(PdfFontFactory.createFont(FontConstants.HELVETICA_BOLD)).setFontSize(12); canvas1.add(p); canvas1.close(); PdfOutline root = pdfDoc.getOutlines(false); for (int i = 1; i <= 3; i++) { PdfOutline chapterOutline = root.addOutline(String.format("Chapter %d", i)); chapterOutline.addDestination(PdfDestination.makeDestination(new PdfString(String.format("chapter_%d", i)))); } } catch (Exception ex) { ex.printStackTrace(); } } }); ``` 完整的代码示例可参考: ```java import com.itextpdf.kernel.colors.ColorConstants; import com.itextpdf.kernel.font.PdfFont; import com.itextpdf.kernel.font.PdfFontFactory; import com.itextpdf.kernel.geom.Rectangle; import com.itextpdf.kernel.pdf.*; import com.itextpdf.layout.Document; import com.itextpdf.layout.element.AreaBreak; import com.itextpdf.layout.element.Chapter; import com.itextpdf.layout.element.Paragraph; import com.itextpdf.layout.element.Text; import com.itextpdf.layout.property.AreaBreakType; import com.itextpdf.layout.property.Property; import com.itextpdf.layout.renderer.DocumentRenderer; import com.itextpdf.layout.renderer.DrawContext; import com.itextpdf.layout.renderer.IRenderer; import com.itextpdf.layout.renderer.ParagraphRenderer; import com.itextpdf.layout.splitting.DefaultSplitCharacters; import com.itextpdf.layout.splitting.GlyphLine; import com.itextpdf.layout.splitting.ISplitCharacters; import com.itextpdf.layout.splitting.SplitCharacters; import com.itextpdf.layout.splitting.SplitCharacters.DefaultSplitCharacter; import com.itextpdf.layout.splitting.SplitCharacters.SplitCharacter; import java.io.IOException; public class PdfGenerator { public static void main(String[] args) throws IOException { PdfDocument pdfDoc = new PdfDocument(new PdfWriter("output.pdf")); Document document = new Document(pdfDoc); pdfDoc.setTagged(); pdfDoc.getCatalog().setLang(new PdfString("en-us")); pdfDoc.getCatalog().setViewerPreferences(new PdfViewerPreferences().setDisplayDocTitle(true)); document.setProperty(Property.SPLIT_CHARACTERS, new DefaultSplitCharacters() { @Override public boolean isSplitCharacter(GlyphLine text, int glyphPos) { return false; } }); // 添加章节 Paragraph chapterTitle = new Paragraph("Chapter 1").setFont(PdfFontFactory.createFont(FontConstants.HELVETICA_BOLD)).setFontSize(16); Chapter chapter = new Chapter(chapterTitle, 1); chapter.setDestination("chapter_1"); document.add(chapter); Paragraph para1 = new Paragraph("This is the first paragraph."); Paragraph para2 = new Paragraph("This is the second paragraph."); document.add(para1); document.add(para2); document.add(new AreaBreak(AreaBreakType.NEXT_PAGE)); chapterTitle = new Paragraph("Chapter 2").setFont(PdfFontFactory.createFont(FontConstants.HELVETICA_BOLD)).setFontSize(16); chapter = new Chapter(chapterTitle, 2); chapter.setDestination("chapter_2"); document.add(chapter); para1 = new Paragraph("This is the third paragraph."); para2 = new Paragraph("This is the fourth paragraph."); document.add(para1); document.add(para2); document.add(new AreaBreak(AreaBreakType.NEXT_PAGE)); chapterTitle = new Paragraph("Chapter 3").setFont(PdfFontFactory.createFont(FontConstants.HELVETICA_BOLD)).setFontSize(16); chapter = new Chapter(chapterTitle, 3); chapter.setDestination("chapter_3"); document.add(chapter); para1 = new Paragraph("This is the fifth paragraph."); para2 = new Paragraph("This is the sixth paragraph."); document.add(para1); document.add(para2); // 添加目录 pdfDoc.addEventHandler(PdfDocumentEvent.START_PAGE, new IEventHandler() { @Override public void handleEvent(Event event) { try { PdfDocumentEvent docEvent = (PdfDocumentEvent) event; PdfPage page = docEvent.getPage(); PdfCanvas canvas = new PdfCanvas(page.newContentStreamBefore(), page.getResources(), pdfDoc); Rectangle rect = new Rectangle(36, 750, 523, 36); Canvas canvas1 = new Canvas(canvas, pdfDoc, rect); Paragraph p = new Paragraph().add("Table of Contents").setFont(PdfFontFactory.createFont(FontConstants.HELVETICA_BOLD)).setFontSize(12); canvas1.add(p); canvas1.close(); PdfOutline root = pdfDoc.getOutlines(false); for (int i = 1; i <= 3; i++) { PdfOutline chapterOutline = root.addOutline(String.format("Chapter %d", i)); chapterOutline.addDestination(PdfDestination.makeDestination(new PdfString(String.format("chapter_%d", i)))); } } catch (Exception ex) { ex.printStackTrace(); } } }); document.close(); } } ``` 在运行代码后,将会生成一个名为"output.pdf"的PDF文档,包含了三个章节和一个目录。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值