자바 워드 문서 에 쓸 그림 과 텍스트 만 들 기

5694 단어
필요 한 jar

			
			com.lowagie
			itext
			2.1.7
			
			
			com.lowagie
			itext-rtf
			2.1.7
			
			
			com.itextpdf
			itext-asian
			5.2.0
			
package com.test;

import java.awt.Color;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;

import com.lowagie.text.BadElementException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.rtf.RtfWriter2;

public class WordTest {
	public static void main(String[] args) {
		exportDoc();
	}

	/**
	 *
	 * @Description:         word @param @param file @param @throws
	 *               DocumentException @param @throws IOException      @return void
	 *                    @throws
	 */
	public static String exportDoc() {
		try {
			//       
			Document document = new Document(PageSize.A4);
			//        (Writer) document    ,     (Writer)           
			// ByteArrayOutputStream baos = new ByteArrayOutputStream();
			// C:\\Users\\orion\\Desktop\\home.jpg
			File file = new File("C:\\Users\\orion\\Desktop\\qwe.doc");
			RtfWriter2.getInstance(document, new FileOutputStream(file));
			document.open();
			//       
			BaseFont bfChinese = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
			//       
			// Font titleFont = new Font(bfChinese, 12, Font.BOLD);
			// Paragraph title = new Paragraph("    ");
			//           
			// title.setAlignment(Element.ALIGN_CENTER);
			// title.setFont(titleFont);
			// document.add(title);
			//       
			Font contextFont = new Font(bfChinese, 12, Font.BOLD);
			// Font contextFont = new Font(bfChinese, 11, Font.NORMAL);
			List list = new ArrayList<>();
			list.add("a");
			list.add("a");
			list.add("a");
			list.add("a");
			list.add("a");
			list.add("a");
			list.add("a");
			list.add("a");
			list.add("a");
			for (String string : list) {
				// code
				String code = "code :  ";
				Paragraph codeStyle = new Paragraph(code);
				//        
				codeStyle.setAlignment(Element.ALIGN_LEFT);
				codeStyle.setFont(contextFont);
				//      (  )    
				codeStyle.setSpacingBefore(20);
				//          
				// context.setFirstLineIndent(0);
				document.add(codeStyle);
				//   
				String codeContent = "  ";
				Paragraph codeContentStyle = new Paragraph(codeContent, FontFactory
						.getFont(FontFactory.HELVETICA_BOLDOBLIQUE, 11, Font.UNDERLINE, new Color(0, 0, 255)));
				//      (  )    
				codeContentStyle.setSpacingBefore(5);
				document.add(codeContentStyle);
				// result
				String result = "result :";
				Paragraph resultStyle = new Paragraph(result);
				//        
				resultStyle.setAlignment(Element.ALIGN_LEFT);
				resultStyle.setFont(contextFont);
				//          
				resultStyle.setSpacingBefore(10);
				//          
				// context.setFirstLineIndent(0);
				document.add(resultStyle);
				//    FontFactory  Font Color            
				//   
				String resultContent = "  ";
				Paragraph resultContentStyle = null;
				if (resultContent.equals("  ")) {
					resultContentStyle = new Paragraph(resultContent, FontFactory
							.getFont(FontFactory.HELVETICA_BOLDOBLIQUE, 11, Font.UNDERLINE, new Color(0, 255, 0)));
				} else {
					resultContentStyle = new Paragraph(resultContent, FontFactory
							.getFont(FontFactory.HELVETICA_BOLDOBLIQUE, 11, Font.UNDERLINE, new Color(255, 0, 0)));
				}
				//          
				resultContentStyle.setSpacingBefore(5);
				document.add(resultContentStyle);
				//      Image.getInstance                
				String imgPath = "C:\\Users\\orion\\Desktop\\home.jpg";
				Image img = Image.getInstance(imgPath);
				img.setAbsolutePosition(0, 0);
				img.setAlignment(Image.ALIGN_CENTER);//         
				img.scalePercent(30);//             50%
				// img.scaleAbsolute(60, 60);//         
				// img.scalePercent(25, 12);//         
				// img.setRotation(30);//        
				document.add(img);
				String log = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
				if (log != null && !"".equals(log)) {
					Paragraph exceptionStyle = new Paragraph("     :");
					//        
					exceptionStyle.setAlignment(Element.ALIGN_LEFT);
					exceptionStyle.setFont(contextFont);
					//          
					exceptionStyle.setSpacingBefore(20);
					document.add(exceptionStyle);
					document.add(new Paragraph(log,
							FontFactory.getFont(FontFactory.HELVETICA_BOLDOBLIQUE, 10, Font.NORMAL)));
				}
			}

			document.close();
			//      
			// wordFile = new ByteArrayInputStream(baos.toByteArray());
			// baos.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (BadElementException e) {
			e.printStackTrace();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (DocumentException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return "";

	}
}

좋은 웹페이지 즐겨찾기