Please download the itext jar package first, and import it into the android project
This article uses itextg-5.5.10.jar, paste the code directly
Document doc = new Document(); String path = Environment.getExternalStorageDirectory() + File.separator +"PDF" + File.separator; File dir = new File(path); if (!dir.exists()) { dir.mkdirs(); } FileOutputStream fos = new FileOutputStream(new File(path +"test.pdf")); PdfWriter.getInstance(doc, fos); doc.open ();
1. If it is only a simple line of text display, follow the method below
//Add a main title and center it Paragraph mainTitleP = new Paragraph("mainTitle", setChineseFont(16)); mainTitleP.setAlignment(Paragraph.ALIGN_CENTER);
//Add a subtitle and center it Paragraph subTitleP = new Paragraph(subTitle, setChineseFont(14)); subTitleP.setAlignment(Paragraph.ALIGN_CENTER);
// add a line of simple memory Paragraph p = new Paragraph("Simple PDF content", setChineseFont(10));
doc.add(mainTitleP); doc.add(subTitleP); doc.add(p);
If the content of the PDF document is simply displayed line by line, you can follow the above method, just adjust the font size
Take a look at the setChineseFont() method used above,
//The parameter passed in by fontSize is the text size of the displayed content, which can be set according to the needs
public static Font setChineseFont(int fontSize) { BaseFont bf = null; Font fontChinese = null; try { bf = BaseFont.createFont("/assets/STSONG.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); fontChinese = new Font(bf, fontSize, Font.NORMAL); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return fontChinese; }
The above method needs to pay attention to the Chinese font file STSONG.TTF. This file is obtained from the Windows system and put into the assets of the project. I don’t care about the copyright issues involved in this font. We are only using it for demo testing. Follow-up Change the available Chinese font files by yourself.
2. If you use a table in the document, you can refer to the method
//For example, create a table with 3 columns, the number of rows is automatically generated according to the number of inserted cells
PdfPTable table = new PdfPTable(3);
//Set the width of each column separately
table.setTotalWidth(new float[]{300, 220, 240});
//Insert 6 cell contents
PdfPCell cellTable1 = new PdfPCell(new Paragraph("cell1", setChineseFont(12))); tableInhale.addCell(cellTable1); PdfPCell cellTable2 = new PdfPCell(new Paragraph("cell2", setChineseFont(12))); cellTable1.addCell(cellTable2 ); PdfPCell cellTable3 = new PdfPCell(new Paragraph("cell3", setChineseFont(12))); cellTable1.addCell(cellTable3 ); PdfPCell cellTable4 = new PdfPCell(new Paragraph("cell4", setChineseFont(12))); cellTable1.addCell(cellTable4 ); PdfPCell cellTable5 = new PdfPCell(new Paragraph("cell5", setChineseFont(12))); cellTable1.addCell(cellTable5 ); PdfPCell cellTable6 = new PdfPCell(new Paragraph("cell6", setChineseFont(12))); cellTable1.addCell(cellTable6 );
doc.add(cellTable1);
If a cell does not display any content, it is also necessary to insert a blank ""
If a cell does not need a border, it can be set (there is a border by default)
cell.setBorder(Rectangle.NO_BORDER);
If you need to merge the 2 rows of cells in the first column into one cell, you can set
cell1.setRowspan(2);
There are also some centering properties, see the requirements settings
cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
3. Finally add
doc.close(); fos.flush(); fos.close();
The above code can generate a simple PDF file