Java Code Examples for com.lowagie.text.Rectangle#rotate()

The following examples show how to use com.lowagie.text.Rectangle#rotate() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DocExportUtil.java    From DWSurvey with GNU Affero General Public License v3.0 6 votes vote down vote up
public void createDoc() throws FileNotFoundException{
	 /** 创建Document对象(word文档)  */
       Rectangle rectPageSize = new Rectangle(PageSize.A4);
       rectPageSize = rectPageSize.rotate();
       // 创建word文档,并设置纸张的大小
       doc = new Document(PageSize.A4);
       file=new File(path+docFileName);
       fileOutputStream=new FileOutputStream(file);
       /** 建立一个书写器与document对象关联,通过书写器可以将文档写入到输出流中 */
       RtfWriter2.getInstance(doc, fileOutputStream );
       doc.open();
       //设置页边距,上、下25.4毫米,即为72f,左、右31.8毫米,即为90f  
       doc.setMargins(90f, 90f, 72f, 72f);
       //设置标题字体样式,粗体、二号、华文中宋  
       tfont  = DocStyleUtils.setFontStyle("华文中宋", 22f, Font.BOLD);  
       //设置正文内容的字体样式,常规、三号、仿宋_GB2312  
       bfont = DocStyleUtils.setFontStyle("仿宋_GB2312", 16f, Font.NORMAL);
}
 
Example 2
Source File: PdfReader.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Gets the rotated page from a page dictionary.
 *
 * @param page the page dictionary
 * @return the rotated page
 */
public Rectangle getPageSizeWithRotation(PdfDictionary page) {
	Rectangle rect = getPageSize(page);
	int rotation = getPageRotation(page);
	while (rotation > 0) {
		rect = rect.rotate();
		rotation -= 90;
	}
	return rect;
}
 
Example 3
Source File: PdfReader.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Gets the rotated page from a page dictionary.
 * @param page the page dictionary
 * @return the rotated page
 */
public Rectangle getPageSizeWithRotation(PdfDictionary page) {
    Rectangle rect = getPageSize(page);
    int rotation = getPageRotation(page);
    while (rotation > 0) {
        rect = rect.rotate();
        rotation -= 90;
    }
    return rect;
}
 
Example 4
Source File: PdfDocumentFactory.java    From javamelody with Apache License 2.0 5 votes vote down vote up
private Rectangle getPageSize(boolean landscape) {
	Rectangle pageSize;
	if (Locale.US.getCountry().equals(I18N.getCurrentLocale().getCountry())) {
		// Letter size paper is used in the US instead of the ISO standard A4
		pageSize = PageSize.LETTER;
	} else {
		pageSize = PageSize.A4;
	}
	if (landscape) {
		pageSize = pageSize.rotate();
	}
	return pageSize;
}
 
Example 5
Source File: PdfReader.java    From MesquiteCore with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Gets the rotated page from a page dictionary.
 * @param page the page dictionary
 * @return the rotated page
 */    
public Rectangle getPageSizeWithRotation(PdfDictionary page) {
    Rectangle rect = getPageSize(page);
    int rotation = getPageRotation(page);
    while (rotation > 0) {
        rect = rect.rotate();
        rotation -= 90;
    }
    return rect;
}
 
Example 6
Source File: AcroFields.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
PdfAppearance getAppearance(PdfDictionary merged, String text, String fieldName) throws IOException, DocumentException {
	topFirst = 0;
	TextField tx = null;
	if (fieldCache == null || !fieldCache.containsKey(fieldName)) {
		tx = new TextField(writer, null, null);
		tx.setExtraMargin(extraMarginLeft, extraMarginTop);
		tx.setBorderWidth(0);
		tx.setSubstitutionFonts(substitutionFonts);
		decodeGenericDictionary(merged, tx);
		// rect
		PdfArray rect = merged.getAsArray(PdfName.RECT);
		Rectangle box = PdfReader.getNormalizedRectangle(rect);
		if (tx.getRotation() == 90 || tx.getRotation() == 270) {
			box = box.rotate();
		}
		tx.setBox(box);
		if (fieldCache != null) {
			fieldCache.put(fieldName, tx);
		}
	} else {
		tx = (TextField) fieldCache.get(fieldName);
		tx.setWriter(writer);
	}
	PdfName fieldType = merged.getAsName(PdfName.FT);
	if (PdfName.TX.equals(fieldType)) {
		tx.setText(text);
		return tx.getAppearance();
	}
	if (!PdfName.CH.equals(fieldType)) {
		throw new DocumentException("An appearance was requested without a variable text field.");
	}
	PdfArray opt = merged.getAsArray(PdfName.OPT);
	int flags = 0;
	PdfNumber nfl = merged.getAsNumber(PdfName.FF);
	if (nfl != null) {
		flags = nfl.intValue();
	}
	if ((flags & PdfFormField.FF_COMBO) != 0 && opt == null) {
		tx.setText(text);
		return tx.getAppearance();
	}
	if (opt != null) {
		String choices[] = new String[opt.size()];
		String choicesExp[] = new String[opt.size()];
		for (int k = 0; k < opt.size(); ++k) {
			PdfObject obj = opt.getPdfObject(k);
			if (obj.isString()) {
				choices[k] = choicesExp[k] = ((PdfString) obj).toUnicodeString();
			} else {
				PdfArray a = (PdfArray) obj;
				choicesExp[k] = a.getAsString(0).toUnicodeString();
				choices[k] = a.getAsString(1).toUnicodeString();
			}
		}
		if ((flags & PdfFormField.FF_COMBO) != 0) {
			for (int k = 0; k < choices.length; ++k) {
				if (text.equals(choicesExp[k])) {
					text = choices[k];
					break;
				}
			}
			tx.setText(text);
			return tx.getAppearance();
		}
		int idx = 0;
		for (int k = 0; k < choicesExp.length; ++k) {
			if (text.equals(choicesExp[k])) {
				idx = k;
				break;
			}
		}
		tx.setChoices(choices);
		tx.setChoiceExports(choicesExp);
		tx.setChoiceSelection(idx);
	}
	PdfAppearance app = tx.getListAppearance();
	topFirst = tx.getTopFirst();
	return app;
}
 
Example 7
Source File: AcroFields.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
PdfAppearance getAppearance(PdfDictionary merged, String text, String fieldName) throws IOException, DocumentException {
    topFirst = 0;
    TextField tx = null;
    if (fieldCache == null || !fieldCache.containsKey(fieldName)) {
        tx = new TextField(writer, null, null);
        tx.setExtraMargin(extraMarginLeft, extraMarginTop);
        tx.setBorderWidth(0);
        tx.setSubstitutionFonts(substitutionFonts);
        decodeGenericDictionary(merged, tx);
        //rect
        PdfArray rect = merged.getAsArray(PdfName.RECT);
        Rectangle box = PdfReader.getNormalizedRectangle(rect);
        if (tx.getRotation() == 90 || tx.getRotation() == 270)
            box = box.rotate();
        tx.setBox(box);
        if (fieldCache != null)
            fieldCache.put(fieldName, tx);
    }
    else {
        tx = (TextField)fieldCache.get(fieldName);
        tx.setWriter(writer);
    }
    PdfName fieldType = merged.getAsName(PdfName.FT);
    if (PdfName.TX.equals(fieldType)) {
        tx.setText(text);
        return tx.getAppearance();
    }
    if (!PdfName.CH.equals(fieldType))
        throw new DocumentException("An appearance was requested without a variable text field.");
    PdfArray opt = merged.getAsArray(PdfName.OPT);
    int flags = 0;
    PdfNumber nfl = merged.getAsNumber(PdfName.FF);
    if (nfl != null)
        flags = nfl.intValue();
    if ((flags & PdfFormField.FF_COMBO) != 0 && opt == null) {
        tx.setText(text);
        return tx.getAppearance();
    }
    if (opt != null) {
        String choices[] = new String[opt.size()];
        String choicesExp[] = new String[opt.size()];
        for (int k = 0; k < opt.size(); ++k) {
            PdfObject obj = opt.getPdfObject(k);
            if (obj.isString()) {
                choices[k] = choicesExp[k] = ((PdfString)obj).toUnicodeString();
            }
            else {
                PdfArray a = (PdfArray) obj;
                choicesExp[k] = a.getAsString(0).toUnicodeString();
                choices[k] = a.getAsString(1).toUnicodeString();
            }
        }
        if ((flags & PdfFormField.FF_COMBO) != 0) {
            for (int k = 0; k < choices.length; ++k) {
                if (text.equals(choicesExp[k])) {
                    text = choices[k];
                    break;
                }
            }
            tx.setText(text);
            return tx.getAppearance();
        }
        int idx = 0;
        for (int k = 0; k < choicesExp.length; ++k) {
            if (text.equals(choicesExp[k])) {
                idx = k;
                break;
            }
        }
        tx.setChoices(choices);
        tx.setChoiceExports(choicesExp);
        tx.setChoiceSelection(idx);
    }
    PdfAppearance app = tx.getListAppearance();
    topFirst = tx.getTopFirst();
    return app;
}
 
Example 8
Source File: RtfPageSetting.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * This method tries to fit the <code>Rectangle pageSize</code> to one of the predefined PageSize rectangles.
 * If a match is found the pageWidth and pageHeight will be set according to values determined from files
 * generated by MS Word2000 and OpenOffice 641. If no match is found the method will try to match the rotated
 * Rectangle by calling itself with the parameter rotate set to true.
 * 
 * @param pageSize the page size for which to guess the correct format
 * @param rotate Whether we should try to rotate the size befor guessing the format
 * @return <code>True</code> if the format was guessed, <code>false/<code> otherwise
 */
private boolean guessFormat(Rectangle pageSize, boolean rotate) {
    if (rotate) {
        pageSize = pageSize.rotate();
    }
    if (rectEquals(pageSize, PageSize.A3)) {
        pageWidth = 16837;
        pageHeight = 23811;
        landscape = rotate;
        return true;
    }
    if (rectEquals(pageSize, PageSize.A4)) {
        pageWidth = 11907;
        pageHeight = 16840;
        landscape = rotate;
        return true;
    }
    if (rectEquals(pageSize, PageSize.A5)) {
        pageWidth = 8391;
        pageHeight = 11907;
        landscape = rotate;
        return true;
    }
    if (rectEquals(pageSize, PageSize.A6)) {
        pageWidth = 5959;
        pageHeight = 8420;
        landscape = rotate;
        return true;
    }
    if (rectEquals(pageSize, PageSize.B4)) {
        pageWidth = 14570;
        pageHeight = 20636;
        landscape = rotate;
        return true;
    }
    if (rectEquals(pageSize, PageSize.B5)) {
        pageWidth = 10319;
        pageHeight = 14572;
        landscape = rotate;
        return true;
    }
    if (rectEquals(pageSize, PageSize.HALFLETTER)) {
        pageWidth = 7927;
        pageHeight = 12247;
        landscape = rotate;
        return true;
    }
    if (rectEquals(pageSize, PageSize.LETTER)) {
        pageWidth = 12242;
        pageHeight = 15842;
        landscape = rotate;
        return true;
    }
    if (rectEquals(pageSize, PageSize.LEGAL)) {
        pageWidth = 12252;
        pageHeight = 20163;
        landscape = rotate;
        return true;
    }
    if (!rotate && guessFormat(pageSize, true)) {
        int x = pageWidth;
        pageWidth = pageHeight;
        pageHeight = x;
        return true;
    }
    return false;
}