本文整理匯總了Java中com.lowagie.text.pdf.PdfContentByte.addTemplate方法的典型用法代碼示例。如果您正苦於以下問題:Java PdfContentByte.addTemplate方法的具體用法?Java PdfContentByte.addTemplate怎麽用?Java PdfContentByte.addTemplate使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.lowagie.text.pdf.PdfContentByte
的用法示例。
在下文中一共展示了PdfContentByte.addTemplate方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: exportVectorGraphics
import com.lowagie.text.pdf.PdfContentByte; //導入方法依賴的package包/類
private void exportVectorGraphics(String formatName, File outputFile) throws ImageExportException {
Component component = printableComponent.getExportComponent();
int width = component.getWidth();
int height = component.getHeight();
try (FileOutputStream fs = new FileOutputStream(outputFile)) {
switch (formatName) {
case PDF:
// create pdf document with slightly increased width and height
// (otherwise the image gets cut off)
Document document = new Document(new Rectangle(width + 5, height + 5));
PdfWriter writer = PdfWriter.getInstance(document, fs);
document.open();
PdfContentByte cb = writer.getDirectContent();
PdfTemplate tp = cb.createTemplate(width, height);
Graphics2D g2 = tp.createGraphics(width, height, new DefaultFontMapper());
component.print(g2);
g2.dispose();
cb.addTemplate(tp, 0, 0);
document.close();
break;
case SVG:
exportFreeHep(component, fs, new SVGGraphics2D(fs, new Dimension(width, height)));
break;
case EPS:
exportFreeHep(component, fs, new PSGraphics2D(fs, new Dimension(width, height)));
break;
default:
// cannot happen
break;
}
} catch (Exception e) {
throw new ImageExportException(I18N.getMessage(I18N.getUserErrorMessagesBundle(),
"error.image_export.export_failed"), e);
}
}
示例2: startPage
import com.lowagie.text.pdf.PdfContentByte; //導入方法依賴的package包/類
/**
* Any work to start off printing a page of the document
* m_currentPage will contain the page being printed, and
* m_currentTemplatePage will contain the template page number to base this
* new page on.
* @throws FormPrintException Thrown if there is any form processing problems
*/
protected void startPage() throws FormPrintException {
log.debug("startPage: Page=" + getCurrentPage());
Rectangle r = m_templateReader.getPageSize(getCurrentTemplatePage());
log.debug( "Page Size : t="+r.getTop()+",l="+r.getLeft()+",b="+r.getBottom()+",r="+r.getRight()+", rot="+r.getRotation() );
r = m_templateReader.getPageSizeWithRotation(getCurrentTemplatePage());
log.debug( "Page Size w/Rot: t="+r.getTop()+",l="+r.getLeft()+",b="+r.getBottom()+",r="+r.getRight()+", rot="+r.getRotation() );
// Get rotation quadrent 0..3
int q = (m_templateReader.getPageSizeWithRotation(getCurrentTemplatePage()).getRotation() % 360)/90;
float tX = (q==2?r.getTop():0)+(q==3?r.getRight():0);
float tY = (q==1?r.getTop():0)+(q==2?r.getRight():0);
float sX=1f, sY=1f;
double angle = -r.getRotation()*(Math.PI/180f);
double transformA = sX * Math.cos(angle);
double transformB = sY * Math.sin(angle);
double transformC = -sX * Math.sin(angle);
double transformD = sY * Math.cos(angle);
double transformE = tX;
double transformF = tY;
m_generatedDoc.setPageSize(m_templateReader.getPageSizeWithRotation(getCurrentTemplatePage()) );
//m_generatedDoc.setPageSize(m_templateReader.getPageSize(getCurrentTemplatePage()) );
/**
* try {
* m_generatedDoc.newPage();
* } catch (DocumentException e) {
* log.error("Error Creating New Page - " + e.getMessage() ,e);
* throw new EngineProcessingException("Error Creating New Page - " + e.getMessage());
* }
**/
m_generatedDoc.newPage();
PdfImportedPage page = m_writer.getImportedPage(m_templateReader, getCurrentTemplatePage() );
PdfContentByte cb = m_writer.getDirectContent();
//cb.addTemplate(page, 1f, 0, 0, 1f, 0, 0);
cb.addTemplate(page, (float)transformA, (float)transformB, (float)transformC,
(float)transformD, (float)transformE, (float)transformF);
log.debug("Matrix = [A=" + transformA +", B=" + transformB +", C=" + transformC +
", D=" + transformD +", E=" + transformE +", F=" + transformF +" ]");
}
示例3: onEndPage
import com.lowagie.text.pdf.PdfContentByte; //導入方法依賴的package包/類
public void onEndPage(PdfWriter writer, Document document) {
PdfContentByte cb = writer.getDirectContent();
cb.saveState();
String text = "Page " + writer.getPageNumber() + " of ";
// height where text starts
float textBase = document.bottom() - getBaseOffset();
float textSize = getFont().getWidthPoint(text, getFontSize());
float width = document.getPageSize().getWidth();
float center = width / 2.0f;
cb.beginText();
cb.setFontAndSize(getFont(), getFontSize());
cb.setTextMatrix(document.left(), textBase);
cb.showTextAligned(PdfContentByte.ALIGN_CENTER, text, center, textBase, 0);
cb.endText();
cb.addTemplate(total, center + (textSize / 2.0f), textBase);
cb.restoreState();
}
示例4: convertToPdf
import com.lowagie.text.pdf.PdfContentByte; //導入方法依賴的package包/類
/**
* Converts a JFreeChart to PDF syntax.
* @param filename the name of the PDF file
* @param chart the JFreeChart
* @param width the width of the resulting PDF
* @param height the height of the resulting PDF
*/
public static void convertToPdf(JFreeChart chart, int width, int height, String filename) {
// step 1
Document document = new Document(new Rectangle(width, height));
try {
// step 2
PdfWriter writer;
writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream(filename));
// step 3
document.open();
// step 4
PdfContentByte cb = writer.getDirectContent();
PdfTemplate tp = cb.createTemplate(width, height);
Graphics2D g2d = tp.createGraphics(width, height, new DefaultFontMapper());
Rectangle2D r2d = new Rectangle2D.Double(0, 0, width, height);
chart.draw(g2d, r2d);
g2d.dispose();
tp.sanityCheck();
cb.addTemplate(tp, 0, 0);
cb.sanityCheck();
}
catch(DocumentException de) {
de.printStackTrace();
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
// step 5
document.close();
}
示例5: main
import com.lowagie.text.pdf.PdfContentByte; //導入方法依賴的package包/類
/**
* Demonstrates the use of layers.
*
* @param args
* no arguments needed
*/
@Test
public void main() throws Exception {
// step 1: creation of a document-object
Document document = new Document(PageSize.A4, 50, 50, 50, 50);
// step 2: creation of the writer
PdfWriter writer = PdfWriter.getInstance(document,
PdfTestBase.getOutputStream("optionalcontent.pdf"));
writer.setPdfVersion(PdfWriter.VERSION_1_5);
writer.setViewerPreferences(PdfWriter.PageModeUseOC);
// step 3: opening the document
document.open();
// step 4: content
PdfContentByte cb = writer.getDirectContent();
Phrase explanation = new Phrase(
"Automatic layers, form fields, images, templates and actions",
new Font(Font.HELVETICA, 18, Font.BOLD, Color.red));
ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, explanation, 50,
650, 0);
PdfLayer l1 = new PdfLayer("Layer 1", writer);
PdfLayer l2 = new PdfLayer("Layer 2", writer);
PdfLayer l3 = new PdfLayer("Layer 3", writer);
PdfLayer l4 = new PdfLayer("Form and XObject Layer", writer);
PdfLayerMembership m1 = new PdfLayerMembership(writer);
m1.addMember(l2);
m1.addMember(l3);
Phrase p1 = new Phrase("Text in layer 1");
Phrase p2 = new Phrase("Text in layer 2 or layer 3");
Phrase p3 = new Phrase("Text in layer 3");
cb.beginLayer(l1);
ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, p1, 50, 600, 0f);
cb.endLayer();
cb.beginLayer(m1);
ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, p2, 50, 550, 0);
cb.endLayer();
cb.beginLayer(l3);
ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, p3, 50, 500, 0);
cb.endLayer();
TextField ff = new TextField(writer, new Rectangle(200, 600, 300, 620),
"field1");
ff.setBorderColor(Color.blue);
ff.setBorderStyle(PdfBorderDictionary.STYLE_SOLID);
ff.setBorderWidth(TextField.BORDER_WIDTH_THIN);
ff.setText("I'm a form field");
PdfFormField form = ff.getTextField();
form.setLayer(l4);
writer.addAnnotation(form);
Image img = Image.getInstance(PdfTestBase.RESOURCES_DIR
+ "pngnow.png");
img.setLayer(l4);
img.setAbsolutePosition(200, 550);
cb.addImage(img);
PdfTemplate tp = cb.createTemplate(100, 20);
Phrase pt = new Phrase("I'm a template", new Font(Font.HELVETICA, 12,
Font.NORMAL, Color.magenta));
ColumnText.showTextAligned(tp, Element.ALIGN_LEFT, pt, 0, 0, 0);
tp.setLayer(l4);
tp.setBoundingBox(new Rectangle(0, -10, 100, 20));
cb.addTemplate(tp, 200, 500);
ArrayList<Object> state = new ArrayList<Object>();
state.add("toggle");
state.add(l1);
state.add(l2);
state.add(l3);
state.add(l4);
PdfAction action = PdfAction.setOCGstate(state, true);
Chunk ck = new Chunk("Click here to toggle the layers", new Font(
Font.HELVETICA, 18, Font.NORMAL, Color.yellow)).setBackground(
Color.blue).setAction(action);
ColumnText.showTextAligned(cb, Element.ALIGN_CENTER, new Phrase(ck),
250, 400, 0);
cb.sanityCheck();
// step 5: closing the document
document.close();
}
示例6: main
import com.lowagie.text.pdf.PdfContentByte; //導入方法依賴的package包/類
/**
* Painting Patterns.
*
* @param args
* no arguments needed
*/
@Test
public void main() throws Exception {
// step 1: creation of a document-object
Document document = new Document(PageSize.A4, 50, 50, 50, 50);
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter writer = PdfWriter.getInstance(document,
PdfTestBase.getOutputStream("pattern.pdf"));
// step 3: we open the document
document.open();
// step 4: we add some content
PdfContentByte cb = writer.getDirectContent();
PdfTemplate tp = cb.createTemplate(400, 300);
PdfPatternPainter pat = cb.createPattern(15, 15, null);
pat.rectangle(5, 5, 5, 5);
pat.fill();
pat.sanityCheck();
PdfSpotColor spc_cmyk = new PdfSpotColor("PANTONE 280 CV",
new CMYKColor(0.9f, .2f, .3f, .1f));
SpotColor spot = new SpotColor(spc_cmyk, 0.25f);
tp.setPatternFill(pat, spot, .9f);
tp.rectangle(0, 0, 400, 300);
tp.fill();
tp.sanityCheck();
cb.addTemplate(tp, 50, 50);
PdfPatternPainter pat2 = cb.createPattern(10, 10, null);
pat2.setLineWidth(2);
pat2.moveTo(-5, 0);
pat2.lineTo(10, 15);
pat2.stroke();
pat2.moveTo(0, -5);
pat2.lineTo(15, 10);
pat2.stroke();
cb.setLineWidth(1);
cb.setColorStroke(Color.black);
cb.setPatternFill(pat2, Color.red);
cb.rectangle(100, 400, 30, 210);
cb.fillStroke();
cb.setPatternFill(pat2, Color.green);
cb.rectangle(150, 400, 30, 100);
cb.fillStroke();
cb.setPatternFill(pat2, Color.blue);
cb.rectangle(200, 400, 30, 130);
cb.fillStroke();
cb.setPatternFill(pat2, new GrayColor(0.5f));
cb.rectangle(250, 400, 30, 80);
cb.fillStroke();
cb.setPatternFill(pat2, new GrayColor(0.7f));
cb.rectangle(300, 400, 30, 170);
cb.fillStroke();
cb.setPatternFill(pat2, new GrayColor(0.9f));
cb.rectangle(350, 400, 30, 40);
cb.fillStroke();
cb.sanityCheck();
// step 5: we close the document
document.close();
}
示例7: add
import com.lowagie.text.pdf.PdfContentByte; //導入方法依賴的package包/類
public void add(Document docIn) throws IOException {
PdfContentByte cb = writer.getDirectContent();
ByteArrayInputStream in = new ByteArrayInputStream(docIn.getDocument());
PdfReader reader = new PdfReader(in);
for (int i = 1; i <= reader.getNumberOfPages(); i++) {
doc.newPage();
//import the page from source pdf
PdfImportedPage page = writer.getImportedPage(reader, i);
//add the page to the destination pdf
cb.addTemplate(page, 0, 0);
}
}
示例8: saveAsPDF
import com.lowagie.text.pdf.PdfContentByte; //導入方法依賴的package包/類
/**
* @param file The file to save to.
* @return <code>true</code> on success.
*/
public boolean saveAsPDF(File file) {
HashSet<Row> changed = expandAllContainers();
try {
PrintManager settings = mCharacter.getPageSettings();
PageFormat format = settings != null ? settings.createPageFormat() : createDefaultPageFormat();
Paper paper = format.getPaper();
float width = (float) paper.getWidth();
float height = (float) paper.getHeight();
adjustToPageSetupChanges(true);
setPrinting(true);
com.lowagie.text.Document pdfDoc = new com.lowagie.text.Document(new com.lowagie.text.Rectangle(width, height));
try (FileOutputStream out = new FileOutputStream(file)) {
PdfWriter writer = PdfWriter.getInstance(pdfDoc, out);
int pageNum = 0;
PdfContentByte cb;
pdfDoc.open();
cb = writer.getDirectContent();
while (true) {
PdfTemplate template = cb.createTemplate(width, height);
Graphics2D g2d = template.createGraphics(width, height, new DefaultFontMapper());
if (print(g2d, format, pageNum) == NO_SUCH_PAGE) {
g2d.dispose();
break;
}
if (pageNum != 0) {
pdfDoc.newPage();
}
g2d.setClip(0, 0, (int) width, (int) height);
print(g2d, format, pageNum++);
g2d.dispose();
cb.addTemplate(template, 0, 0);
}
pdfDoc.close();
}
return true;
} catch (Exception exception) {
return false;
} finally {
setPrinting(false);
closeContainers(changed);
}
}
示例9: main
import com.lowagie.text.pdf.PdfContentByte; //導入方法依賴的package包/類
/**
* Reads the pages of an existing PDF file, adds pagenumbers and a watermark.
*/
@Test
public void main() throws Exception {
// we create a reader for a certain document
PdfReader reader = new PdfReader(PdfTestBase.RESOURCES_DIR +"ChapterSection.pdf");
int n = reader.getNumberOfPages();
// we create a stamper that will copy the document to a new file
PdfStamper stamp = new PdfStamper(reader,PdfTestBase.getOutputStream("watermark_pagenumbers.pdf"));
// adding some metadata
HashMap<String, String> moreInfo = new HashMap<String, String>();
moreInfo.put("Author", "Bruno Lowagie");
stamp.setMoreInfo(moreInfo);
// adding content to each page
int i = 0;
PdfContentByte under;
PdfContentByte over;
Image img = Image.getInstance(PdfTestBase.RESOURCES_DIR +"watermark.jpg");
BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.EMBEDDED);
img.setAbsolutePosition(200, 400);
while (i < n) {
i++;
// watermark under the existing page
under = stamp.getUnderContent(i);
under.addImage(img);
// text over the existing page
over = stamp.getOverContent(i);
over.beginText();
over.setFontAndSize(bf, 18);
over.setTextMatrix(30, 30);
over.showText("page " + i);
over.setFontAndSize(bf, 32);
over.showTextAligned(Element.ALIGN_LEFT, "DUPLICATE", 230, 430, 45);
over.endText();
}
// adding an extra page
stamp.insertPage(1, PageSize.A4);
over = stamp.getOverContent(1);
over.beginText();
over.setFontAndSize(bf, 18);
over.showTextAligned(Element.ALIGN_LEFT, "DUPLICATE OF AN EXISTING PDF DOCUMENT", 30, 600, 0);
over.endText();
// adding a page from another document
PdfReader reader2 = new PdfReader(PdfTestBase.RESOURCES_DIR +"SimpleAnnotations1.pdf");
under = stamp.getUnderContent(1);
under.addTemplate(stamp.getImportedPage(reader2, 3), 1, 0, 0, 1, 0, 0);
// closing PdfStamper will generate the new PDF file
stamp.close();
}
示例10: main
import com.lowagie.text.pdf.PdfContentByte; //導入方法依賴的package包/類
/**
* Reads the pages of an existing PDF file and puts 2 pages from the
* existing doc into one of the new doc.
*/
@Test
public void main() throws Exception {
// we create a reader for a certain document
PdfReader reader = new PdfReader(PdfTestBase.RESOURCES_DIR + "ChapterSection.pdf");
// we retrieve the total number of pages
int n = reader.getNumberOfPages();
// we retrieve the size of the first page
Rectangle psize = reader.getPageSize(1);
float width = psize.getHeight();
float height = psize.getWidth();
// step 1: creation of a document-object
Document document = new Document(new Rectangle(width, height));
// step 2: we create a writer that listens to the document
PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream("2on1.pdf"));
// step 3: we open the document
document.open();
// step 4: we add content
PdfContentByte cb = writer.getDirectContent();
int i = 0;
int p = 0;
while (i < n) {
document.newPage();
p++;
i++;
PdfImportedPage page1 = writer.getImportedPage(reader, i);
cb.addTemplate(page1, .5f, 0, 0, .5f, 60, 120);
if (i < n) {
i++;
PdfImportedPage page2 = writer.getImportedPage(reader, i);
cb.addTemplate(page2, .5f, 0, 0, .5f, width / 2 + 60, 120);
}
BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
cb.beginText();
cb.setFontAndSize(bf, 14);
cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "page " + p + " of " + ((n / 2) + (n % 2 > 0 ? 1 : 0)),
width / 2, 40, 0);
cb.endText();
}
// step 5: we close the document
document.close();
}
示例11: main
import com.lowagie.text.pdf.PdfContentByte; //導入方法依賴的package包/類
/**
*/
@Test
public void main() throws Exception {
// step 1: creation of a document-object
Document document = new Document();
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream("templates.pdf"));
// step 3: we open the document
document.open();
// step 4: we grab the ContentByte and do some stuff with it
PdfContentByte cb = writer.getDirectContent();
// we create a PdfTemplate
PdfTemplate template = cb.createTemplate(500, 200);
// we add some graphics
template.moveTo(0, 200);
template.lineTo(500, 0);
template.stroke();
template.setRGBColorStrokeF(255f, 0f, 0f);
template.circle(250f, 100f, 80f);
template.stroke();
// we add some text
template.beginText();
BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252,
BaseFont.NOT_EMBEDDED);
template.setFontAndSize(bf, 12);
template.setTextMatrix(100, 100);
template.showText("Text at the position 100,100 (relative to the template!)");
template.endText();
template.sanityCheck();
// we add the template on different positions
cb.addTemplate(template, 0, 0);
cb.addTemplate(template, 0, 1, -1, 0, 500, 200);
cb.addTemplate(template, .5f, 0, 0, .5f, 100, 400);
// we go to a new page
document.newPage();
cb.addTemplate(template, 0, 400);
cb.addTemplate(template, 2, 0, 0, 2, -200, 400);
cb.sanityCheck();
// step 5: we close the document
document.close();
}
示例12: main
import com.lowagie.text.pdf.PdfContentByte; //導入方法依賴的package包/類
/**
* Creates a PDF document with shapes, lines and text at specific X and Y coordinates.
*/
@Test
public void main() throws Exception {
// step 1: creation of a document-object
Document document = new Document();
try {
// step 2: creation of the writer
PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream( "XandY.pdf"));
// step 3: we open the document
document.open();
// step 4:
PdfContentByte cb = writer.getDirectContent();
// we create a PdfTemplate
PdfTemplate template = cb.createTemplate(25, 25);
// we add some crosses to visualize the coordinates
template.moveTo(13, 0);
template.lineTo(13, 25);
template.moveTo(0, 13);
template.lineTo(50, 13);
template.stroke();
template.sanityCheck();
// we add the template on different positions
cb.addTemplate(template, 216 - 13, 720 - 13);
cb.addTemplate(template, 360 - 13, 360 - 13);
cb.addTemplate(template, 360 - 13, 504 - 13);
cb.addTemplate(template, 72 - 13, 144 - 13);
cb.addTemplate(template, 144 - 13, 288 - 13);
cb.moveTo(216, 720);
cb.lineTo(360, 360);
cb.lineTo(360, 504);
cb.lineTo(72, 144);
cb.lineTo(144, 288);
cb.stroke();
BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
cb.beginText();
cb.setFontAndSize(bf, 12);
cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "(3\", 10\")", 216 + 25, 720 + 5, 0);
cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "(5\", 5\")", 360 + 25, 360 + 5, 0);
cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "(5\", 7\")", 360 + 25, 504 + 5, 0);
cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "(1\", 2\")", 72 + 25, 144 + 5, 0);
cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "(2\", 4\")", 144 + 25, 288 + 5, 0);
cb.endText();
cb.sanityCheck();
}
catch(DocumentException de) {
System.err.println(de.getMessage());
}
catch(IOException ioe) {
System.err.println(ioe.getMessage());
}
// step 5: we close the document
document.close();
}
示例13: main
import com.lowagie.text.pdf.PdfContentByte; //導入方法依賴的package包/類
/**
* Adding a template using different transformation matrices.
*/
@Test
public void main() throws Exception {
// step 1: creation of a document-object
Document document = new Document(PageSize.A4);
// step 2: creation of the writer
PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream("transformations.pdf"));
// step 3: we open the document
document.open();
// step 4:
PdfContentByte cb = writer.getDirectContent();
// we create a PdfTemplate
PdfTemplate template = cb.createTemplate(120, 120);
// we add some graphics
template.moveTo(30, 10);
template.lineTo(90, 10);
template.lineTo(90, 80);
template.lineTo(110, 80);
template.lineTo(60, 110);
template.lineTo(10, 80);
template.lineTo(30, 80);
template.closePath();
template.stroke();
template.sanityCheck();
// we add the template on different positions
cb.addTemplate(template, 0, 0);
cb.addTemplate(template, 0, 1, -1, 0, 200, 600);
cb.addTemplate(template, .5f, 0, 0, .5f, 100, 400);
cb.sanityCheck();
// we go to a new page
document.newPage();
cb.addTemplate(template, 0, 500);
cb.addTemplate(template, 2, 0, -1, 2, 200, 300);
cb.sanityCheck();
// step 5: we close the document
document.close();
}
示例14: main
import com.lowagie.text.pdf.PdfContentByte; //導入方法依賴的package包/類
/**
* Changes the default coordinate system so that the origin is in the upper left corner
* instead of the lower left corner.
*/
@Test
public void main() throws Exception {
// step 1: creation of a document-object
Document document = new Document(PageSize.A4);
try {
// step 2: creation of the writer
PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream( "upsidedown.pdf"));
// step 3: we open the document
document.open();
// step 4:
PdfContentByte cb = writer.getDirectContent();
cb.concatCTM(1f, 0f, 0f, -1f, 0f, PageSize.A4.getHeight());
// we create a PdfTemplate
PdfTemplate template = cb.createTemplate(25, 25);
// we add some crosses to visualize the coordinates
template.moveTo(13, 0);
template.lineTo(13, 25);
template.moveTo(0, 13);
template.lineTo(50, 13);
template.stroke();
template.sanityCheck();
// we add the template on different positions
cb.addTemplate(template, 216 - 13, 720 - 13);
cb.addTemplate(template, 360 - 13, 360 - 13);
cb.addTemplate(template, 360 - 13, 504 - 13);
cb.addTemplate(template, 72 - 13, 144 - 13);
cb.addTemplate(template, 144 - 13, 288 - 13);
cb.moveTo(216, 720);
cb.lineTo(360, 360);
cb.lineTo(360, 504);
cb.lineTo(72, 144);
cb.lineTo(144, 288);
cb.stroke();
BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
cb.beginText();
cb.setFontAndSize(bf, 12);
cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "(3\", 10\")", 216 + 25, 720 + 5, 0);
cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "(5\", 5\")", 360 + 25, 360 + 5, 0);
cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "(5\", 7\")", 360 + 25, 504 + 5, 0);
cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "(1\", 2\")", 72 + 25, 144 + 5, 0);
cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "(2\", 4\")", 144 + 25, 288 + 5, 0);
cb.endText();
cb.sanityCheck();
}
catch(DocumentException de) {
System.err.println(de.getMessage());
}
catch(IOException ioe) {
System.err.println(ioe.getMessage());
}
// step 5: we close the document
document.close();
}
示例15: main
import com.lowagie.text.pdf.PdfContentByte; //導入方法依賴的package包/類
/**
* Changes the transformation matrix with AffineTransform.
*/
@Test
public void main() throws Exception {
// step 1: creation of a document-object
Document document = new Document(PageSize.A4);
// step 2: creation of the writer
PdfWriter writer = PdfWriter.getInstance(document,
PdfTestBase.getOutputStream("affinetransformation.pdf"));
// step 3: we open the document
document.open();
// step 4:
PdfContentByte cb = writer.getDirectContent();
cb.transform(AffineTransform.getScaleInstance(1.2, 0.75));
// we create a PdfTemplate
PdfTemplate template = cb.createTemplate(25, 25);
// we add some crosses to visualize the coordinates
template.moveTo(13, 0);
template.lineTo(13, 25);
template.moveTo(0, 13);
template.lineTo(50, 13);
template.stroke();
template.sanityCheck();
// we add the template on different positions
cb.addTemplate(template, 216 - 13, 720 - 13);
cb.addTemplate(template, 360 - 13, 360 - 13);
cb.addTemplate(template, 360 - 13, 504 - 13);
cb.addTemplate(template, 72 - 13, 144 - 13);
cb.addTemplate(template, 144 - 13, 288 - 13);
cb.moveTo(216, 720);
cb.lineTo(360, 360);
cb.lineTo(360, 504);
cb.lineTo(72, 144);
cb.lineTo(144, 288);
cb.stroke();
BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252,
BaseFont.NOT_EMBEDDED);
cb.beginText();
cb.setFontAndSize(bf, 12);
cb.showTextAligned(PdfContentByte.ALIGN_CENTER,
"(3\" * 1.2, 10\" * .75)", 216 + 25, 720 + 5, 0);
cb.showTextAligned(PdfContentByte.ALIGN_CENTER,
"(5\" * 1.2, 5\" * .75)", 360 + 25, 360 + 5, 0);
cb.showTextAligned(PdfContentByte.ALIGN_CENTER,
"(5\" * 1.2, 7\" * .75)", 360 + 25, 504 + 5, 0);
cb.showTextAligned(PdfContentByte.ALIGN_CENTER,
"(1\" * 1.2, 2\" * .75)", 72 + 25, 144 + 5, 0);
cb.showTextAligned(PdfContentByte.ALIGN_CENTER,
"(2\" * 1.2, 4\" * .75)", 144 + 25, 288 + 5, 0);
cb.endText();
cb.sanityCheck();
// step 5: we close the document
document.close();
}