iText创建WORD或PDF文件|Java开发|码途山海.智隐长卷 -

程序人生|重庆纽新

找回密码
立即注册

QQ登录

只需一步,快速开始

欢迎访问【程序人生-重庆纽新】,本网站为软件开发人员视觉的IT资讯、软件开发中各种问题的解决办法!!
搜索
发新帖


2308

积分

0

好友

259

主题
楼主
发表于 2017-8-3 10:36:18 | 查看: 2176| 回复: 1
MAVEN依赖:
                <dependency>
                    <groupId>com.lowagie</groupId>
                    <artifactId>itext</artifactId>
                    <version>2.1.7</version>
                </dependency>
                <dependency>
                    <groupId>com.lowagie</groupId>
                    <artifactId>itext-rtf</artifactId>
                    <version>2.1.7</version>
                </dependency>
  1. <span style="background-color: rgb(255, 255, 255);">代码示例:</span>
复制代码
  1. import java.awt.Color;  
  2. import java.io.IOException;  
  3. import java.io.OutputStream;  
  4. import java.net.MalformedURLException;  
  5. import java.util.HashMap;  
  6. import java.util.List;  
  7. import java.util.Map;  
  8.   
  9. import com.lowagie.text.Cell;  
  10. import com.lowagie.text.Document;  
  11. import com.lowagie.text.DocumentException;  
  12. import com.lowagie.text.Element;  
  13. import com.lowagie.text.Font;  
  14. import com.lowagie.text.FontFactory;  
  15. import com.lowagie.text.HeaderFooter;  
  16. import com.lowagie.text.Image;  
  17. import com.lowagie.text.PageSize;  
  18. import com.lowagie.text.Paragraph;  
  19. import com.lowagie.text.Phrase;  
  20. import com.lowagie.text.Rectangle;  
  21. import com.lowagie.text.Table;  
  22. import com.lowagie.text.pdf.BaseFont;  
  23. import com.lowagie.text.pdf.PdfWriter;  
  24. import com.lowagie.text.rtf.RtfWriter2;  
  25. import com.lowagie.text.rtf.headerfooter.RtfHeaderFooter;  
  26. import com.lowagie.text.rtf.style.RtfParagraphStyle;  
  27.   
  28. /**  
  29. * IText操作类  
  30. * @author shyh  
  31. *  
  32. */  
  33. public class ItextManager {  
  34.   
  35.     private Font font;  
  36.     private BaseFont bfChinese;  
  37.   
  38.     public ItextManager() throws Exception {  
  39.         // 设置中文字体  
  40. //      bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);  
  41.         bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", false);  
  42.         font = new Font(bfChinese);  
  43.         font.setSize(30);  
  44.         font.setStyle(FontFactory.HELVETICA);  
  45.         font.setColor(new Color(0,0,0));  
  46.          
  47.     }  
  48.   
  49.     public static ItextManager getInstance() throws Exception {  
  50.          
  51.         return new ItextManager();  
  52.     }  
  53.   
  54.     @SuppressWarnings({ "unused", "unchecked" })  
  55.     public void createRtfContext(List<String> imgList, OutputStream out,String type, List<Map<String, Object>> hostAppList, List<Map<String, Object>> result, Map<String, Object> resultMap, Map<String, Object> resultMap1, Map<String, Object> resultMap2) {  
  56.          
  57.         Document doc = new Document(PageSize.A4.rotate(), 80, 80, 80, 80);//word横向展示  
  58.         try {  
  59.             if("word".equals(type)){  
  60.                 RtfWriter2.getInstance(doc, out);  
  61.             }else if("pdf".equals(type)){  
  62.                 PdfWriter.getInstance(doc, out);  
  63.             }  
  64. //              // 添加页脚     
  65. //              HeaderFooter footer = new HeaderFooter(new Phrase("footer"), false);     
  66. //              footer.setAlignment(Rectangle.ALIGN_CENTER);     
  67. //              doc.setFooter(footer);  
  68.                 Image headerImage = Image.getInstance("D:/yemei.jpg");   
  69.                 // 创建有1行2列的表格   
  70.                 Table tables = new Table(2, 1);   
  71.                 // 设置table的边框宽度为0   
  72.                 tables.setBorderWidth(1f);   
  73.                  tables.setWidth(100);   
  74.                 // 设置表格右对齐,其中1为居中对齐,2为右对齐,3为左对齐   
  75.                 tables.setAlignment(1);   
  76.                 // 设置各列的宽度   
  77.                 int[] widthss = { 300, 300 };   
  78.                 tables.setWidths(widthss);   
  79.                 // 创建单元格,并且将单元格内容设置为图片   
  80.                 Cell cells = new Cell();   
  81.                 cells = new Cell(headerImage);   
  82.                 cells.setBorder(0);  
  83.                 tables.addCell(cells);   
  84.                 cells.setVerticalAlignment(3);  // 设置垂直居中   
  85.                 cells.setHorizontalAlignment(3);  // 设置水平居中   
  86.                 cells = new Cell("机器数据分析平台");   
  87.                 cells.setBorder(0);  
  88.                 tables.addCell(cells);  
  89.                 cells.setVerticalAlignment(2);  // 设置垂直居中   
  90.                 cells.setHorizontalAlignment(2);  // 设置水平居中   
  91.               
  92.                 RtfHeaderFooter header1 = new RtfHeaderFooter(tables);  
  93.                 header1.setAlignment(tables.MARKED);  
  94.                 doc.setHeader(header1);  
  95.                   
  96.                   
  97.                 doc.open();  
  98.                 //第一级标题样式  
  99.                 RtfParagraphStyle rtfGsBt1 = RtfParagraphStyle.STYLE_HEADING_1;  
  100.                 rtfGsBt1.setAlignment(Element.ALIGN_LEFT);  
  101.                 rtfGsBt1.setStyle(Font.BOLD);  
  102.                 rtfGsBt1.setSize(15);  
  103.                   
  104.                 //第二级标题样式  
  105.                 RtfParagraphStyle rtfGsBt2 = RtfParagraphStyle.STYLE_HEADING_2;  
  106.                 rtfGsBt2.setAlignment(Element.ALIGN_LEFT);  
  107.                 rtfGsBt2.setStyle(Font.BOLD);  
  108.                 rtfGsBt2.setSize(13);  
  109.                   
  110.                 //设置带有目录格式的标题(标题1格式)  
  111. //              Paragraph title = new Paragraph(titleStr);   
  112. //              // 设置标题格式对齐方式   
  113. //              title.setAlignment(elementAlign);   
  114. //              title.setFont(titleFont);   
  115.                 Paragraph title = new Paragraph("网络访问概况");  
  116.                 title.setAlignment(Element.ALIGN_CENTER);  
  117.                 title.setFont(font);  
  118.                 doc.add(title);  
  119.                   
  120.                 // 设置标题格式对齐方式   
  121. //              Paragraph title1 = new Paragraph("一、网络访问概况");  
  122. //              title.setFont(rtfGsBt1);  
  123. //              doc.add(title1);  
  124.                   
  125.                 Paragraph title2 = new Paragraph("1.1网络访问数量");  
  126.                 // 设置标题格式对齐方式   
  127.                 title2.setFont(rtfGsBt2);  
  128.                 doc.add(title2);  
  129.                   
  130.                 //网络访问概况:  
  131.                 Map<String,Object>titleMap=resultMap;  
  132.                 String t1 = titleMap.get("network_count").toString().substring(5,10);  
  133.                 String t2 = titleMap.get("network_host_count").toString().substring(7,10);  
  134.                 String t3 = titleMap.get("flow_fz").toString();  
  135.                 String t4 = titleMap.get("flow_aver").toString();  
  136.                 // 正文字体风格   
  137.                 Font contextFont = new Font(bfChinese, 10, Element.ALIGN_CENTER);   
  138.                 Paragraph context = new Paragraph("网络服务访问次数:"+t1+"次\n提供服务主机数量:"+t2+"台\n网络流量峰值:"+t3+"\n网络流量平均值:"+t4+"");   
  139.                 //设置行距   
  140.                 context.setLeading(3f);  
  141.                 // 正文格式左对齐   
  142.                 context.setFont(contextFont);   
  143.                 // 离上一段落(标题)空的行数   
  144.                 context.setSpacingBefore(1);  
  145.                 // 设置第一行空的列数   
  146.                 context.setFirstLineIndent(20);   
  147.                 doc.add(context);   
  148.                   
  149. //              Paragraph title3 = new Paragraph("1.2 TCP/UDP服务");  
  150. //              // 设置标题格式对齐方式   
  151. //              title.setFont(rtfGsBt2);  
  152. //              doc.add(title3);  
  153.                 Paragraph title3 = new Paragraph("1.2 TCP/UDP服务");  
  154.                 // 设置标题格式对齐方式   
  155.                 title3.setFont(rtfGsBt2);  
  156.                 doc.add(title3);  
  157.                   
  158.                 // 创建有三行的表格   
  159.                 Table table = new Table(2, 3);   
  160.                 // 设置table的边框宽度为0   
  161.                 table.setBorderWidth(1f);   
  162.                  table.setWidth(100);   
  163.                 // 设置表格右对齐,其中1为居中对齐,2为右对齐,3为左对齐   
  164.                 table.setAlignment(3);   
  165.                 // 设置各列的宽度   
  166.                 int[] widths = { 200, 200 };   
  167.                 table.setWidths(widths);   
  168.                 // table.setPadding(0);   
  169.                 // table.setSpacing(0);   
  170.                   
  171.                 Image img = null;  
  172.                 img = Image.getInstance(imgList.get(0));  
  173.                 float heigth = img.getHeight();  
  174.                 float width = img.getWidth();  
  175.                 int percent = getPercent3(heigth, width);  
  176.                 img.setAlignment(Image.LEFT);//图片居左显示  
  177.                 img.scalePercent(percent + 3);// 表示是原来图像的比例;  
  178.                   
  179.                 Image img4 = null;  
  180.                 img4 = Image.getInstance(imgList.get(4));  
  181.                 float heigth4 = img4.getHeight();  
  182.                 float width4 = img4.getWidth();  
  183.                 int percent4 = getPercent3(heigth4, width4);  
  184.                 img4.setAlignment(Image.LEFT);//图片居左显示  
  185.                 img4.scalePercent(percent4 + 3);// 表示是原来图像的比例;  
  186.                   
  187.                   
  188.                 //已登记服务数和未登记服务数  
  189.                 Map<String,Object>titleMap1=resultMap1;  
  190.                 String d1 = titleMap1.get("count_service").toString().substring(7,9);  
  191.                 String d2 = titleMap1.get("regist_service").toString().substring(7,8);  
  192.                   
  193.                 Map<String,Object>titleMap2=resultMap2;  
  194.                 String u1 = titleMap2.get("count_service").toString().substring(7,10);  
  195.                 String u2 = titleMap2.get("regist_service").toString().substring(7,8);  
  196.                 // 创建单元格,并且将单元格内容设置为图片   
  197.                 Cell cell = new Cell();   
  198.                 cell = new Cell("TCP");   
  199.                 cell.setBorder(0);  
  200.                 table.addCell(cell);   
  201.                 cell = new Cell("UDP");   
  202.                 cell.setBorder(0);  
  203.                 table.addCell(cell);  
  204.                 cell = new Cell("已登记服务数:"+d2);   
  205.                 cell.setBorder(0);  
  206.                 table.addCell(cell);   
  207.                 cell = new Cell("已登记服务数:"+u2);   
  208.                 cell.setBorder(0);  
  209.                 table.addCell(cell);   
  210.                 cell = new Cell("未登记服务数:"+d1);   
  211.                 cell.setBorder(0);  
  212.                 table.addCell(cell);   
  213.                 cell = new Cell("未登记服务数:"+u1);   
  214.                 cell.setBorder(0);  
  215.                 table.addCell(cell);   
  216.                 cell = new Cell(img);   
  217.                 cell.setBorder(0);// 设置单元格边框为0   
  218.                 table.addCell(cell);   
  219.                 cell.setVerticalAlignment(1);  // 设置垂直居中   
  220.                 cell.setHorizontalAlignment(1);  // 设置水平居中   
  221.                 cell = new Cell(img4);   
  222.                 cell.setBorder(0);  
  223.                 cell.setVerticalAlignment(1);   
  224.                 cell.setHorizontalAlignment(1);   
  225.                 table.addCell(cell);   
  226.                 doc.add(table);  
  227.                 doc.newPage();  
  228.                   
  229.                 Paragraph title4 = new Paragraph("二、应用服务TOP10:");  
  230.                 title.setFont(rtfGsBt1);  
  231.                 doc.add(title4);  
  232.                 Image img1 = null;  
  233.                 img1 = Image.getInstance(imgList.get(1));  
  234.                 float heigth1 = img1.getHeight();  
  235.                 float width1 = img1.getWidth();  
  236.                 int percent1 = getPercent3(heigth1, width1);  
  237.                 img1.setAlignment(Image.ALIGN_CENTER);//图片居右显示  
  238.                 img1.scalePercent(percent1 + 3);// 表示是原来图像的比例;  
  239.                 doc.add(img1);  
  240.                   
  241.                 Paragraph title7 = new Paragraph("2.1热点应用Top10");  
  242.                 // 设置标题格式对齐方式   
  243.                 title7.setFont(rtfGsBt2);  
  244.                 doc.add(title7);  
  245.                 // 创建有9行的表格   
  246.                 Table table2 = new Table(9,10);   
  247.                 // 设置table的边框宽度为0   
  248.                 table2.setBorderWidth(1f);   
  249.                 table2.setWidth(100);   
  250.                 // 设置表格右对齐,其中1为居中对齐,2为右对齐,3为左对齐   
  251.                 table2.setAlignment(3);   
  252.                 // 设置各列的宽度   
  253.                 int[] widths2 = { 5,15,10,10,10,10,10,10,10};   
  254.                 table2.setWidths(widths2);   
  255.                 Cell cell2 = new Cell();   
  256.                 cell2 = new Cell("序号");   
  257.                 table2.addCell(cell2);   
  258.                 cell2.setVerticalAlignment(1);   
  259.                 cell2.setHorizontalAlignment(1);   
  260.                 cell2 = new Cell("资产名称");   
  261.                 table2.addCell(cell2);  
  262.                 cell2.setVerticalAlignment(1);   
  263.                 cell2.setHorizontalAlignment(1);   
  264.                 cell2 = new Cell("IP");   
  265.                 table2.addCell(cell2);   
  266.                 cell2.setVerticalAlignment(1);   
  267.                 cell2.setHorizontalAlignment(1);   
  268.                 cell2 = new Cell("TCP:端口");   
  269.                 table2.addCell(cell2);   
  270.                 cell2.setVerticalAlignment(1);   
  271.                 cell2.setHorizontalAlignment(1);   
  272.                 cell2 = new Cell("会话数");   
  273.                 table2.addCell(cell2);   
  274.                 cell2.setVerticalAlignment(1);   
  275.                 cell2.setHorizontalAlignment(1);   
  276.                 cell2 = new Cell("字节数");   
  277.                 table2.addCell(cell2);   
  278.                 cell2.setVerticalAlignment(1);   
  279.                 cell2.setHorizontalAlignment(1);   
  280.                 cell2 = new Cell("数据字节数");   
  281.                 table2.addCell(cell2);   
  282.                 cell2.setVerticalAlignment(1);   
  283.                 cell2.setHorizontalAlignment(1);   
  284.                 cell2 = new Cell("包数");   
  285.                 table2.addCell(cell2);   
  286.                 cell2.setVerticalAlignment(1);   
  287.                 cell2.setHorizontalAlignment(1);   
  288.                 cell2 = new Cell("来访IP数");   
  289.                 table2.addCell(cell2);   
  290.                 cell2.setVerticalAlignment(1);  // 设置垂直居中   
  291.                 cell2.setHorizontalAlignment(1);  // 设置水平居中   
  292.                 for(int i=0; i<hostAppList.size(); i++){  
  293.                     Map<String, Object> map = hostAppList.get(i);  
  294.                     cell2 = new Cell(""+(i+1));  
  295.                      table2.addCell(cell2);  
  296.                     cell2.setVerticalAlignment(1);   
  297.                     cell2.setHorizontalAlignment(1);  
  298.                     cell2 = new Cell((map.get("assetname")).toString());  
  299.                     table2.addCell(cell2);  
  300.                     cell2.setVerticalAlignment(1);   
  301.                     cell2.setHorizontalAlignment(1);  
  302.                     cell2 = new Cell((map.get("ip")).toString());  
  303.                     table2.addCell(cell2);  
  304.                     cell2.setVerticalAlignment(1);   
  305.                     cell2.setHorizontalAlignment(1);  
  306.                     cell2 = new Cell((map.get("service")).toString());  
  307.                     table2.addCell(cell2);  
  308.                     cell2.setVerticalAlignment(1);   
  309.                     cell2.setHorizontalAlignment(1);  
  310.                     cell2 = new Cell((""+map.get("sessions")+""));  
  311.                     table2.addCell(cell2);  
  312.                     cell2.setVerticalAlignment(1);   
  313.                     cell2.setHorizontalAlignment(1);  
  314.                     cell2 = new Cell((map.get("bytes")).toString());  
  315.                     table2.addCell(cell2);  
  316.                     cell2.setVerticalAlignment(1);   
  317.                     cell2.setHorizontalAlignment(1);  
  318.                     cell2 = new Cell((map.get("dbytes")).toString());  
  319.                     table2.addCell(cell2);  
  320.                     cell2.setVerticalAlignment(1);   
  321.                     cell2.setHorizontalAlignment(1);  
  322.                     cell2 = new Cell((map.get("packets")).toString());  
  323.                     table2.addCell(cell2);  
  324.                     cell2.setVerticalAlignment(1);   
  325.                     cell2.setHorizontalAlignment(1);  
  326.                     cell2 = new Cell((map.get("visitor_ip")).toString());  
  327.                     table2.addCell(cell2);  
  328.                     cell2.setVerticalAlignment(1);   
  329.                     cell2.setHorizontalAlignment(1);  
  330.                 }  
  331.                 doc.add(table2);  
  332.                 doc.newPage();  
  333.                   
  334.                 Paragraph title5 = new Paragraph("2.2选择活跃终端TOP10展现指标");  
  335.                 title5.setFont(rtfGsBt2);  
  336.                 doc.add(title5);  
  337.                   
  338.                 Image img2 = null;  
  339.                 img2 = Image.getInstance(imgList.get(2));  
  340.                 float heigth2 = img2.getHeight();  
  341.                 float width2 = img2.getWidth();  
  342.                 int percent2 = getPercent(heigth2, width2);  
  343.                 img2.setAlignment(Image.MIDDLE);//图片居中显示  
  344.                 img2.scalePercent(percent2 + 3);// 表示是原来图像的比例;  
  345.                 doc.add(img2);  
  346.                 doc.newPage();  
  347.                   
  348.                   
  349.                   
  350.                 Paragraph title6 = new Paragraph("2.3活跃终端top10");  
  351.                 // 设置标题格式对齐方式   
  352.                 title6.setFont(rtfGsBt2);  
  353.                 doc.add(title6);  
  354.                 Image img3 = null;  
  355.                 // 图片  
  356.                 img3 = Image.getInstance(imgList.get(3));  
  357.                 float heigth3 = img3.getHeight();  
  358.                 float width3 = img3.getWidth();  
  359.                 int percent3 = getPercent(heigth3, width3);  
  360.                 img3.setAlignment(Image.MIDDLE);//图片居中显示  
  361.                 img3.scalePercent(percent3 + 3);// 表示是原来图像的比例;  
  362.                 doc.add(img3);  
  363.                 doc.newPage();  
  364.                   
  365.                   
  366.                 Paragraph title8 = new Paragraph("2.4活跃终端top10");  
  367.                 // 设置标题格式对齐方式   
  368.                 title8.setFont(rtfGsBt2);  
  369.                 doc.add(title8);  
  370.                 // 创建有三行的表格   
  371.                 Table table3 = new Table(8,10);   
  372.                 // 设置table的边框宽度为0   
  373.                 table3.setBorderWidth(1f);   
  374.                 table3.setWidth(100);   
  375.                 // 设置表格右对齐,其中1为居中对齐,2为右对齐,3为左对齐   
  376.                 table3.setAlignment(3);   
  377.                 // 设置各列的宽度   
  378.                 int[] widths3 = { 5,15,10,15,10,10,10,10};   
  379.                 table3.setWidths(widths3);   
  380.                 Cell cell3 = new Cell();   
  381.                 cell3 = new Cell("序号");   
  382.                 table3.addCell(cell3);   
  383.                 cell3.setVerticalAlignment(1);   
  384.                 cell3.setHorizontalAlignment(1);   
  385.                 cell3 = new Cell("资产名称");   
  386.                 table3.addCell(cell3);  
  387.                 cell3.setVerticalAlignment(1);   
  388.                 cell3.setHorizontalAlignment(1);   
  389.                 cell3 = new Cell("IP");   
  390.                 table3.addCell(cell3);   
  391.                 cell3.setVerticalAlignment(1);   
  392.                 cell3.setHorizontalAlignment(1);   
  393.                 cell3 = new Cell("所属地域");   
  394.                 table3.addCell(cell3);   
  395.                 cell3.setVerticalAlignment(1);   
  396.                 cell3.setHorizontalAlignment(1);   
  397.                 cell3 = new Cell("会话数");   
  398.                 table3.addCell(cell3);   
  399.                 cell3.setVerticalAlignment(1);   
  400.                 cell3.setHorizontalAlignment(1);   
  401.                 cell3 = new Cell("字节数");   
  402.                 table3.addCell(cell3);   
  403.                 cell3.setVerticalAlignment(1);   
  404.                 cell3.setHorizontalAlignment(1);   
  405.                 cell3 = new Cell("数据字节数");   
  406.                 table3.addCell(cell3);   
  407.                 cell3.setVerticalAlignment(1);   
  408.                 cell3.setHorizontalAlignment(1);   
  409.                 cell3 = new Cell("包数");   
  410.                 table3.addCell(cell3);   
  411.                 cell3.setVerticalAlignment(1);   
  412.                 cell3.setHorizontalAlignment(1);   
  413.                 for(int i=0; i<result.size(); i++){  
  414.                     Map<String, Object> res = result.get(i);  
  415.                     cell3 = new Cell(""+(i+1));  
  416.                     table3.addCell(cell3);  
  417.                     cell3.setVerticalAlignment(1);   
  418.                     cell3.setHorizontalAlignment(1);  
  419.                     cell3 = new Cell((res.get("assetname")).toString());  
  420.                     table3.addCell(cell3);  
  421.                     cell3.setVerticalAlignment(1);   
  422.                     cell3.setHorizontalAlignment(1);  
  423.                     cell3 = new Cell((res.get("ip")).toString());  
  424.                     table3.addCell(cell3);  
  425.                     cell3.setVerticalAlignment(1);   
  426.                     cell3.setHorizontalAlignment(1);  
  427.                     cell3 = new Cell((""+res.get("coun_reg_city"+"")));  
  428.                     table3.addCell(cell3);  
  429.                     cell3.setVerticalAlignment(1);   
  430.                     cell3.setHorizontalAlignment(1);  
  431.                     cell3 = new Cell((res.get("sessions").toString()));  
  432.                     table3.addCell(cell3);  
  433.                     cell3.setVerticalAlignment(1);   
  434.                     cell3.setHorizontalAlignment(1);  
  435.                     cell3 = new Cell((res.get("bytes")).toString());  
  436.                     table3.addCell(cell3);  
  437.                     cell3.setVerticalAlignment(1);   
  438.                     cell3.setHorizontalAlignment(1);  
  439.                     cell3 = new Cell((res.get("dbytes")).toString());  
  440.                     table3.addCell(cell3);  
  441.                     cell3.setVerticalAlignment(1);   
  442.                     cell3.setHorizontalAlignment(1);  
  443.                     cell3 = new Cell((res.get("packets")).toString());  
  444.                     table3.addCell(cell3);  
  445.                     cell3.setVerticalAlignment(1);   
  446.                     cell3.setHorizontalAlignment(1);  
  447.                 }  
  448.                 doc.add(table3);  
  449.                   
  450.                   
  451.                      
  452.             doc.close();  
  453.         } catch (DocumentException e) {  
  454.             e.printStackTrace();  
  455.         } catch (MalformedURLException e) {  
  456.             e.printStackTrace();  
  457.         } catch (IOException e) {  
  458.             e.printStackTrace();  
  459.         }  
  460.     }  
  461.   
  462.     /**  
  463.      * 第一种解决方案 在不改变图片形状的同时,判断,如果h>w,则按h压缩,否则在w>h或w=h的情况下,按宽度压缩  
  464.      *   
  465.      * @param h  
  466.      * @param w  
  467.      * @return  
  468.      */  
  469.   
  470.     public static int getPercent(float h, float w) {  
  471.         int p = 0;  
  472.         float p2 = 0.0f;  
  473.             p2 = 297 / h * 100;  
  474.         p = Math.round(p2);  
  475.         return p;  
  476.     }  
  477.   
  478.     /**  
  479.      * 第二种解决方案,统一按照宽度压缩 这样来的效果是,所有图片的宽度是相等的.  
  480.      *   
  481.      * @param args  
  482.      */  
  483.     public static int getPercent2(float h, float w) {  
  484.         int p = 0;  
  485.         float p2 = 0.0f;  
  486.         p2 = 530 / w * 80;  
  487.         p = Math.round(p2);  
  488.         return p;  
  489.     }  
  490.       
  491.     /**  
  492.      * 第三种解决方案,将图片统一按照固定的大小来压缩,只适用于个别图片  
  493.      *   
  494.      * @param args  
  495.      */  
  496.     public static int getPercent3(float h, float w) {  
  497.         int p = 0;   
  498.         float p2 = 0.0f;  
  499.         p2 = 530 / w * 35;  
  500.         p2 = 530 / h * 35;  
  501.         p = Math.round(p2);  
  502.         return p;  
  503.     }  
  504. }  
复制代码

收藏回复 只看该作者 道具 举报


沙发
发表于 2017-8-8 14:45:54
设置图片大小:
// image.setRotation(-20);//旋转 弧度
//image.setRotationDegrees(45);// 旋转 角度
// image.scaleAbsolute(200,100);//自定义大小
// image.scalePercent(20);//依照比例缩放

回复 只看该作者 道具 举报

高级模式
B Color Image Link Quote Code Smilies



QQ|小黑屋| 码途山海.智隐长卷 渝ICP备15002301号-2   渝公网安备50011202504426

GMT+8, 2025-5-18 05:17 , Processed in 0.044915 second(s), 23 queries .

©Copyright 程序人生!

©2012-2015重庆纽新

快速回复 返回顶部 返回列表