本文整理汇总了Java中com.lowagie.text.pdf.PdfTemplate.setWidth方法的典型用法代码示例。如果您正苦于以下问题:Java PdfTemplate.setWidth方法的具体用法?Java PdfTemplate.setWidth怎么用?Java PdfTemplate.setWidth使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.lowagie.text.pdf.PdfTemplate
的用法示例。
在下文中一共展示了PdfTemplate.setWidth方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: readWMF
import com.lowagie.text.pdf.PdfTemplate; //导入方法依赖的package包/类
/** Reads the WMF into a template.
* @param template the template to read to
* @throws IOException on error
* @throws DocumentException on error
*/
public void readWMF(PdfTemplate template) throws IOException, DocumentException {
setTemplateData(template);
template.setWidth(getWidth());
template.setHeight(getHeight());
InputStream is = null;
try {
if (rawData == null){
is = url.openStream();
}
else{
is = new java.io.ByteArrayInputStream(rawData);
}
MetaDo meta = new MetaDo(is, template);
meta.readAll();
}
finally {
if (is != null) {
is.close();
}
}
}
示例2: createPdfDocument
import com.lowagie.text.pdf.PdfTemplate; //导入方法依赖的package包/类
public String createPdfDocument(String fileName, Image image) {
Document document = new Document();
File file = null;
try {
int w = image.getWidth(null);
int h = image.getHeight(null);
file = new File(fileName);
PdfWriter writer = PdfWriter.getInstance(document,
new FileOutputStream(file));
document.open();
PdfContentByte cb = writer.getDirectContent();
PdfTemplate tp = cb.createTemplate(w, h);
Graphics2D g2 = tp.createGraphics(w, h);
g2.setStroke(new BasicStroke(0.1f));
tp.setWidth(w);
tp.setHeight(h);
g2.drawImage(image, 0, 0, w, h, 0, 0, w, h, null);
g2.dispose();
cb.addTemplate(tp, 72, 720 - h);
} catch (DocumentException de) {
return de.getMessage();
} catch (IOException ioe) {
return ioe.getMessage();
}
document.close();
return null;
}
示例3: addTextVertical
import com.lowagie.text.pdf.PdfTemplate; //导入方法依赖的package包/类
public void addTextVertical(PdfPCell cell, String text, boolean bold) throws Exception {
if (text==null) return;
if (text.indexOf("<span")>=0)
text = text.replaceAll("</span>","").replaceAll("<span .*>", "");
Font font = PdfFont.getFont(bold);
BaseFont bf = font.getBaseFont();
float width = bf.getWidthPoint(text, font.getSize());
PdfTemplate template = iWriter.getDirectContent().createTemplate(2 * font.getSize() + 4, width);
template.beginText();
template.setColorFill(Color.BLACK);
template.setFontAndSize(bf, font.getSize());
template.setTextMatrix(0, 2);
template.showText(text);
template.endText();
template.setWidth(width);
template.setHeight(font.getSize() + 2);
//make an Image object from the template
Image img = Image.getInstance(template);
img.setRotationDegrees(270);
//embed the image in a Chunk
Chunk ck = new Chunk(img, 0, 0);
if (cell.getPhrase()==null) {
cell.setPhrase(new Paragraph(ck));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
} else {
cell.getPhrase().add(ck);
}
}
示例4: main
import com.lowagie.text.pdf.PdfTemplate; //导入方法依赖的package包/类
/**
* PdfTemplates can be wrapped in an Image.
*/
@Test
public void main() throws Exception {
// step 1: creation of a document-object
Rectangle rect = new Rectangle(PageSize.A4);
rect.setBackgroundColor(new Color(238, 221, 88));
Document document = new Document(rect, 50, 50, 50, 50);
// step 2: we create a writer that listens to the document
PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream("templateImages.pdf"));
// step 3: we open the document
document.open();
// step 4:
PdfTemplate template = writer.getDirectContent().createTemplate(20, 20);
BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI,
BaseFont.NOT_EMBEDDED);
String text = "Vertical";
float size = 16;
float width = bf.getWidthPoint(text, size);
template.beginText();
template.setRGBColorFillF(1, 1, 1);
template.setFontAndSize(bf, size);
template.setTextMatrix(0, 2);
template.showText(text);
template.endText();
template.setWidth(width);
template.setHeight(size + 2);
template.sanityCheck();
Image img = Image.getInstance(template);
img.setRotationDegrees(90);
Chunk ck = new Chunk(img, 0, 0);
PdfPTable table = new PdfPTable(3);
table.setWidthPercentage(100);
table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
table.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE);
PdfPCell cell = new PdfPCell(img);
cell.setPadding(4);
cell.setBackgroundColor(new Color(0, 0, 255));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell("I see a template on my right");
table.addCell(cell);
table.addCell("I see a template on my left");
table.addCell(cell);
table.addCell("I see a template everywhere");
table.addCell(cell);
table.addCell("I see a template on my right");
table.addCell(cell);
table.addCell("I see a template on my left");
Paragraph p1 = new Paragraph("This is a template ");
p1.add(ck);
p1.add(" just here.");
p1.setLeading(img.getScaledHeight() * 1.1f);
document.add(p1);
document.add(table);
Paragraph p2 = new Paragraph("More templates ");
p2.setLeading(img.getScaledHeight() * 1.1f);
p2.setAlignment(Element.ALIGN_JUSTIFIED);
img.scalePercent(70);
for (int k = 0; k < 20; ++k)
p2.add(ck);
document.add(p2);
// step 5: we close the document
document.close();
}
示例5: main
import com.lowagie.text.pdf.PdfTemplate; //导入方法依赖的package包/类
/**
* Example with vertical text in Cells.
*/
@Test
public void main() throws Exception {
// step1
Document document = new Document(PageSize.A4, 50, 50, 50, 50);
// step2
PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream("VerticalText.pdf"));
// step3
document.open();
// step4
// make a PdfTemplate with the vertical text
PdfTemplate template = writer.getDirectContent().createTemplate(20, 20);
BaseFont bf = BaseFont.createFont("Helvetica", "winansi", false);
String text = "Vertical";
float size = 16;
float width = bf.getWidthPoint(text, size);
template.beginText();
template.setRGBColorFillF(1, 1, 1);
template.setFontAndSize(bf, size);
template.setTextMatrix(0, 2);
template.showText(text);
template.endText();
template.setWidth(width);
template.setHeight(size + 2);
// make an Image object from the template
Image img = Image.getInstance(template);
img.setRotationDegrees(90);
PdfPTable table = new PdfPTable(3);
table.setWidthPercentage(100);
table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
table.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE);
PdfPCell cell = new PdfPCell(img);
cell.setPadding(4);
cell.setBackgroundColor(new Color(0, 0, 255));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell("I see a template on my right");
table.addCell(cell);
table.addCell("I see a template on my left");
table.addCell(cell);
table.addCell("I see a template everywhere");
table.addCell(cell);
table.addCell("I see a template on my right");
table.addCell(cell);
table.addCell("I see a template on my left");
document.add(table);
// step5
document.close();
}
示例6: gravaPdf
import com.lowagie.text.pdf.PdfTemplate; //导入方法依赖的package包/类
public boolean gravaPdf(String sArquivo) {
boolean bRetorno = false;
com.lowagie.text.Document document = new com.lowagie.text.Document();
try {
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(sArquivo));
// step 3: we open the document
document.open();
// step 4: we grab the ContentByte and do some stuff with it
// we create a fontMapper and read all the fonts in the font
// directory
// DefaultFontMapper mapper = new DefaultFontMapper();
// mapper.insertDirectory("c:\\windows\\fonts");
// we create a template and a Graphics2D object that corresponds
// with it
int wt = 0;
int ht = 0;
// int wg = 0;
// int hg = 0;
PdfContentByte cb = writer.getDirectContent();
PdfTemplate tp = null;
Graphics2D g2 = null;
PrinterJob printJob = PrinterJob.getPrinterJob();
Paper paper = printJob.defaultPage().getPaper();
PaginaPad pf = new PaginaPad(paper);
wt = ( int ) ( pf.getWidth() );
ht = ( int ) ( pf.getHeight() );
if (( iMXPdf != -1 ) && ( iMYPdf != -1 )) {
paper.setImageableArea(iMXPdf, iMYPdf, wt, ht);
pf = new PaginaPad(paper);
}
// wg = (int)(pf.getImageableWidth());
// hg = (int)(pf.getImageableHeight());
// System.out.println("wt:"+wt+" ht:"+ht+" wg:"+wg+" hg:"+hg);
// PrintRequestAttributeSet aset = Funcoes.getImpAtrib();
// w = printJob.
for (int i = 0; i < getNumPags(); i++) {
if (i > 0) {
document.newPage();
}
tp = cb.createTemplate(wt, ht);
g2 = tp.createGraphics(wt, ht);
try {
prepPagina(g2, pf, i);
}
catch (PrinterException pe) {
Funcoes.mensagemErro(null, "Erro criando graphics: " + pe.getMessage());
}
tp.setWidth(wt);
tp.setHeight(ht);
g2.dispose();
cb.addTemplate(tp, 0, 0);
}
}
catch (DocumentException de) {
System.err.println(de.getMessage());
}
catch (IOException ioe) {
System.err.println(ioe.getMessage());
}
// step 5: we close the document
document.close();
return bRetorno;
}
示例7: writePDF
import com.lowagie.text.pdf.PdfTemplate; //导入方法依赖的package包/类
private static void writePDF(Pageable pageable, OutputStream output)
{
try {
final PageFormat pf = pageable.getPageFormat(0);
final com.lowagie.text.Document document =
new com.lowagie.text.Document(new Rectangle(
(int) pf.getWidth(), (int) pf.getHeight()));
final PdfWriter writer = PdfWriter.getInstance(
document, output);
writer.setPdfVersion(PdfWriter.VERSION_1_2);
document.open();
final DefaultFontMapper mapper = new DefaultFontMapper();
//Elaine 2009/02/17 - load additional font from directory set in PDF_FONT_DIR of System Configurator
String pdfFontDir = MSysConfig.getValue(PDF_FONT_DIR, "");
if(pdfFontDir != null && pdfFontDir.trim().length() > 0)
{
pdfFontDir = pdfFontDir.trim();
File dir = new File(pdfFontDir);
if(dir.exists() && dir.isDirectory())
mapper.insertDirectory(pdfFontDir);
}
//
final float w = (float) pf.getWidth();
final float h = (float) pf.getHeight();
final PdfContentByte cb = writer.getDirectContent();
for (int page = 0; page < pageable.getNumberOfPages(); page++) {
if (page != 0) {
document.newPage();
}
final PdfTemplate tp = cb.createTemplate(w, h);
final Graphics2D g2 = tp.createGraphics(w, h, mapper);
tp.setWidth(w);
tp.setHeight(h);
pageable.getPrintable(page).print(g2, pf, page);
g2.dispose();
cb.addTemplate(tp, 0, 0);
}
document.close();
} catch (Exception e) {
e.printStackTrace();
}
}
示例8: main
import com.lowagie.text.pdf.PdfTemplate; //导入方法依赖的package包/类
/**
* PdfTemplates can be wrapped in an Image.
*
* @param args
* no arguments needed
*/
public static void main(String[] args) {
System.out.println("PdfTemplate wrapped in an Image");
// step 1: creation of a document-object
Rectangle rect = new Rectangle(PageSize.A4);
rect.setBackgroundColor(new Color(238, 221, 88));
Document document = new Document(rect, 50, 50, 50, 50);
try {
// step 2: we create a writer that listens to the document
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(android.os.Environment.getExternalStorageDirectory() + java.io.File.separator + "droidtext" + java.io.File.separator + "templateImages.pdf"));
// step 3: we open the document
document.open();
// step 4:
PdfTemplate template = writer.getDirectContent().createTemplate(20, 20);
BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
String text = "Vertical";
float size = 16;
float width = bf.getWidthPoint(text, size);
template.beginText();
template.setRGBColorFillF(1, 1, 1);
template.setFontAndSize(bf, size);
template.setTextMatrix(0, 2);
template.showText(text);
template.endText();
template.setWidth(width);
template.setHeight(size + 2);
template.sanityCheck();
Image img = Image.getInstance(template);
img.setRotationDegrees(90);
Chunk ck = new Chunk(img, 0, 0);
PdfPTable table = new PdfPTable(3);
table.setWidthPercentage(100);
table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
table.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE);
PdfPCell cell = new PdfPCell(img);
cell.setPadding(4);
cell.setBackgroundColor(new Color(0, 0, 255));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell("I see a template on my right");
table.addCell(cell);
table.addCell("I see a template on my left");
table.addCell(cell);
table.addCell("I see a template everywhere");
table.addCell(cell);
table.addCell("I see a template on my right");
table.addCell(cell);
table.addCell("I see a template on my left");
Paragraph p1 = new Paragraph("This is a template ");
p1.add(ck);
p1.add(" just here.");
p1.setLeading(img.getScaledHeight() * 1.1f);
document.add(p1);
document.add(table);
Paragraph p2 = new Paragraph("More templates ");
p2.setLeading(img.getScaledHeight() * 1.1f);
p2.setAlignment(Element.ALIGN_JUSTIFIED);
img.scalePercent(70);
for (int k = 0; k < 20; ++k)
p2.add(ck);
document.add(p2);
// step 5: we close the document
document.close();
} catch (Exception de) {
System.err.println(de.getMessage());
}
}
示例9: main
import com.lowagie.text.pdf.PdfTemplate; //导入方法依赖的package包/类
/**
* Example with vertical text in Cells.
*
* @param args
* no arguments needed
*/
public static void main(String[] args) {
System.out.println("Vertical text in cells");
// step1
Document document = new Document(PageSize.A4, 50, 50, 50, 50);
try {
// step2
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(android.os.Environment.getExternalStorageDirectory() + java.io.File.separator + "droidtext" + java.io.File.separator + "VerticalText.pdf"));
// step3
document.open();
// step4
// make a PdfTemplate with the vertical text
PdfTemplate template = writer.getDirectContent().createTemplate(20, 20);
BaseFont bf = BaseFont.createFont("Helvetica", "winansi", false);
String text = "Vertical";
float size = 16;
float width = bf.getWidthPoint(text, size);
template.beginText();
template.setRGBColorFillF(1, 1, 1);
template.setFontAndSize(bf, size);
template.setTextMatrix(0, 2);
template.showText(text);
template.endText();
template.setWidth(width);
template.setHeight(size + 2);
// make an Image object from the template
Image img = Image.getInstance(template);
img.setRotationDegrees(90);
PdfPTable table = new PdfPTable(3);
table.setWidthPercentage(100);
table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
table.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE);
PdfPCell cell = new PdfPCell(img);
cell.setPadding(4);
cell.setBackgroundColor(new Color(0, 0, 255));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell("I see a template on my right");
table.addCell(cell);
table.addCell("I see a template on my left");
table.addCell(cell);
table.addCell("I see a template everywhere");
table.addCell(cell);
table.addCell("I see a template on my right");
table.addCell(cell);
table.addCell("I see a template on my left");
document.add(table);
} catch (Exception de) {
de.printStackTrace();
}
// step5
document.close();
}