Java Code Examples for com.lowagie.text.pdf.PdfTemplate#moveTo()
The following examples show how to use
com.lowagie.text.pdf.PdfTemplate#moveTo() .
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: TemplatesTest.java From itext2 with GNU Lesser General Public License v3.0 | 4 votes |
/** */ @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(); }
Example 2
Source File: XandYcoordinatesTest.java From itext2 with GNU Lesser General Public License v3.0 | 4 votes |
/** * 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(); }
Example 3
Source File: TransformationsTest.java From itext2 with GNU Lesser General Public License v3.0 | 4 votes |
/** * 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(); }
Example 4
Source File: UpsideDownTest.java From itext2 with GNU Lesser General Public License v3.0 | 4 votes |
/** * 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(); }
Example 5
Source File: AffineTransformationTest.java From itext2 with GNU Lesser General Public License v3.0 | 4 votes |
/** * 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(); }
Example 6
Source File: SpotColorsTest.java From itext2 with GNU Lesser General Public License v3.0 | 4 votes |
/** * Demonstrates the use of spotcolors. */ @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("spotcolor.pdf")); BaseFont bf = BaseFont.createFont("Helvetica", "winansi", BaseFont.NOT_EMBEDDED); // step 3: we open the document document.open(); // step 4: we grab the ContentByte and do some stuff with it PdfContentByte cb = writer.getDirectContent(); // step 5: we instantiate PdfSpotColor // Note: I made up these names unless someone give me a PANTONE swatch as gift ([email protected]) PdfSpotColor spc_cmyk = new PdfSpotColor("PANTONE 280 CV", new CMYKColor(0.9f, .2f, .3f, .1f)); PdfSpotColor spc_rgb = new PdfSpotColor("PANTONE 147", new Color(114, 94, 38)); PdfSpotColor spc_g = new PdfSpotColor("PANTONE 100 CV", new GrayColor(0.9f)); // Stroke a rectangle with CMYK alternate cb.setColorStroke(spc_cmyk, .5f); cb.setLineWidth(10f); // draw a rectangle cb.rectangle(100, 700, 100, 100); // add the diagonal cb.moveTo(100, 700); cb.lineTo(200, 800); // stroke the lines cb.stroke(); // Fill a rectangle with CMYK alternate cb.setColorFill(spc_cmyk, 0.25f); cb.rectangle(250, 700, 100, 100); cb.fill(); // Stroke a circle with RGB alternate cb.setColorStroke(spc_rgb, 0.9f); cb.setLineWidth(5f); cb.circle(150f, 500f, 100f); cb.stroke(); // Fill the circle with RGB alternate cb.setColorFill(spc_rgb, 0.9f); cb.circle(150f, 500f, 50f); cb.fill(); // example with colorfill cb.setColorFill(spc_g, 0.5f); cb.moveTo(100f, 200f); cb.lineTo(200f, 250f); cb.lineTo(400f, 150f); cb.fill(); // cb.sanityCheck is called during newPage(). document.newPage(); String text = "Some text to show"; document.add(new Paragraph(text, new Font(Font.HELVETICA, 24, Font.NORMAL, new SpotColor(spc_cmyk, 0.25f)))); document.add(new Paragraph(text, new Font(Font.HELVETICA, 24, Font.NORMAL, new SpotColor(spc_cmyk, 0.5f)))); // example with template PdfTemplate t = cb.createTemplate(500f, 500f); // Stroke a rectangle with CMYK alternate t.setColorStroke(new SpotColor(spc_cmyk, .5f)); t.setLineWidth(10f); // draw a rectangle t.rectangle(100, 10, 100, 100); // add the diagonal t.moveTo(100, 10); t.lineTo(200, 100); // stroke the lines t.stroke(); // Fill a rectangle with CMYK alternate t.setColorFill(spc_g, 0.5f); t.rectangle(100, 125, 100, 100); t.fill(); t.beginText(); t.setFontAndSize(bf, 20f); t.setTextMatrix(1f, 0f, 0f, 1f, 10f, 10f); t.showText("Template text upside down"); t.endText(); t.rectangle(0, 0, 499, 499); t.stroke(); t.sanityCheck(); cb.addTemplate(t, -1.0f, 0.00f, 0.00f, -1.0f, 550f, 550f); cb.sanityCheck(); // step 5: we close the document document.close(); }
Example 7
Source File: DestinationsTest.java From itext2 with GNU Lesser General Public License v3.0 | 4 votes |
/** * Creates a document with some goto actions. * */ @Test public void main() throws Exception { // step 1: creation of a document-object Document document = new Document(); // step 2: PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream("Destinations.pdf")); // step 3: writer.setViewerPreferences(PdfWriter.PageModeUseOutlines); 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(25, 25); // we add some crosses to visualize the destinations template.moveTo(13, 0); template.lineTo(13, 25); template.moveTo(0, 13); template.lineTo(50, 13); template.stroke(); // we add the template on different positions cb.addTemplate(template, 287, 787); cb.addTemplate(template, 187, 487); cb.addTemplate(template, 487, 287); cb.addTemplate(template, 87, 87); // we define the destinations PdfDestination d1 = new PdfDestination(PdfDestination.XYZ, 300, 800, 0); PdfDestination d2 = new PdfDestination(PdfDestination.FITH, 500); PdfDestination d3 = new PdfDestination(PdfDestination.FITR, 200, 300, 400, 500); PdfDestination d4 = new PdfDestination(PdfDestination.FITBV, 100); PdfDestination d5 = new PdfDestination(PdfDestination.FIT); // we define the outlines PdfOutline out1 = new PdfOutline(cb.getRootOutline(), d1, "root"); PdfOutline out2 = new PdfOutline(out1, d2, "sub 1"); new PdfOutline(out1, d3, "sub 2"); new PdfOutline(out2, d4, "sub 2.1"); new PdfOutline(out2, d5, "sub 2.2"); // step 5: we close the document document.close(); }
Example 8
Source File: ColumnIrregularTest.java From itext2 with GNU Lesser General Public License v3.0 | 4 votes |
/** * Demonstrates the use of ColumnText. */ @Test public void main() throws Exception { // step 1: creation of a document-object Document document = new Document(); // step 2: creation of the writer PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream("columnirregular.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(); PdfTemplate t = cb.createTemplate(600, 800); Image caesar = Image.getInstance(PdfTestBase.RESOURCES_DIR + "caesar_coin.jpg"); cb.addImage(caesar, 100, 0, 0, 100, 260, 595); t.setGrayFill(0.75f); t.moveTo(310, 112); t.lineTo(280, 60); t.lineTo(340, 60); t.closePath(); t.moveTo(310, 790); t.lineTo(310, 710); t.moveTo(310, 580); t.lineTo(310, 122); t.stroke(); cb.addTemplate(t, 0, 0); ColumnText ct = new ColumnText(cb); ct.addText(new Phrase( "GALLIA est omnis divisa in partes tres, quarum unam incolunt Belgae, aliam Aquitani, tertiam qui ipsorum lingua Celtae, nostra Galli appellantur. Hi omnes lingua, institutis, legibus inter se differunt. Gallos ab Aquitanis Garumna flumen, a Belgis Matrona et Sequana dividit. Horum omnium fortissimi sunt Belgae, propterea quod a cultu atque humanitate provinciae longissime absunt, minimeque ad eos mercatores saepe commeant atque ea quae ad effeminandos animos pertinent important, proximique sunt Germanis, qui trans Rhenum incolunt, quibuscum continenter bellum gerunt. Qua de causa Helvetii quoque reliquos Gallos virtute praecedunt, quod fere cotidianis proeliis cum Germanis contendunt, cum aut suis finibus eos prohibent aut ipsi in eorum finibus bellum gerunt.\n", FontFactory.getFont(FontFactory.HELVETICA, 12))); ct.addText(new Phrase( "[Eorum una, pars, quam Gallos obtinere dictum est, initium capit a flumine Rhodano, continetur Garumna flumine, Oceano, finibus Belgarum, attingit etiam ab Sequanis et Helvetiis flumen Rhenum, vergit ad septentriones. Belgae ab extremis Galliae finibus oriuntur, pertinent ad inferiorem partem fluminis Rheni, spectant in septentrionem et orientem solem. Aquitania a Garumna flumine ad Pyrenaeos montes et eam partem Oceani quae est ad Hispaniam pertinet; spectat inter occasum solis et septentriones.]\n", FontFactory.getFont(FontFactory.HELVETICA, 12))); ct.addText(new Phrase( "Apud Helvetios longe nobilissimus fuit et ditissimus Orgetorix. Is M. Messala, [et P.] M. Pisone consulibus regni cupiditate inductus coniurationem nobilitatis fecit et civitati persuasit ut de finibus suis cum omnibus copiis exirent: perfacile esse, cum virtute omnibus praestarent, totius Galliae imperio potiri. Id hoc facilius iis persuasit, quod undique loci natura Helvetii continentur: una ex parte flumine Rheno latissimo atque altissimo, qui agrum Helvetium a Germanis dividit; altera ex parte monte Iura altissimo, qui est inter Sequanos et Helvetios; tertia lacu Lemanno et flumine Rhodano, qui provinciam nostram ab Helvetiis dividit. His rebus fiebat ut et minus late vagarentur et minus facile finitimis bellum inferre possent; qua ex parte homines bellandi cupidi magno dolore adficiebantur. Pro multitudine autem hominum et pro gloria belli atque fortitudinis angustos se fines habere arbitrabantur, qui in longitudinem milia passuum CCXL, in latitudinem CLXXX patebant.\n", FontFactory.getFont(FontFactory.HELVETICA, 12))); ct.addText(new Phrase( "His rebus adducti et auctoritate Orgetorigis permoti constituerunt ea quae ad proficiscendum pertinerent comparare, iumentorum et carrorum quam maximum numerum coemere, sementes quam maximas facere, ut in itinere copia frumenti suppeteret, cum proximis civitatibus pacem et amicitiam confirmare. Ad eas res conficiendas biennium sibi satis esse duxerunt; in tertium annum profectionem lege confirmant. Ad eas res conficiendas Orgetorix deligitur. Is sibi legationem ad civitates suscipit. In eo itinere persuadet Castico, Catamantaloedis filio, Sequano, cuius pater regnum in Sequanis multos annos obtinuerat et a senatu populi Romani amicus appellatus erat, ut regnum in civitate sua occuparet, quod pater ante habuerit; itemque Dumnorigi Haeduo, fratri Diviciaci, qui eo tempore principatum in civitate obtinebat ac maxime plebi acceptus erat, ut idem conaretur persuadet eique filiam suam in matrimonium dat. Perfacile factu esse illis probat conata perficere, propterea quod ipse suae civitatis imperium obtenturus esset: non esse dubium quin totius Galliae plurimum Helvetii possent; se suis copiis suoque exercitu illis regna conciliaturum confirmat. Hac oratione adducti inter se fidem et ius iurandum dant et regno occupato per tres potentissimos ac firmissimos populos totius Galliae sese potiri posse sperant.\n", FontFactory.getFont(FontFactory.HELVETICA, 12))); ct.addText(new Phrase( "Ea res est Helvetiis per indicium enuntiata. Moribus suis Orgetoricem ex vinculis causam dicere coegerunt; damnatum poenam sequi oportebat, ut igni cremaretur. Die constituta causae dictionis Orgetorix ad iudicium omnem suam familiam, ad hominum milia decem, undique coegit, et omnes clientes obaeratosque suos, quorum magnum numerum habebat, eodem conduxit; per eos ne causam diceret se eripuit. Cum civitas ob eam rem incitata armis ius suum exequi conaretur multitudinemque hominum ex agris magistratus cogerent, Orgetorix mortuus est; neque abest suspicio, ut Helvetii arbitrantur, quin ipse sibi mortem consciverit.", FontFactory.getFont(FontFactory.HELVETICA, 12))); float[] left1 = { 70, 790, 70, 60 }; float[] right1 = { 300, 790, 300, 700, 240, 700, 240, 590, 300, 590, 300, 106, 270, 60 }; float[] left2 = { 320, 790, 320, 700, 380, 700, 380, 590, 320, 590, 320, 106, 350, 60 }; float[] right2 = { 550, 790, 550, 60 }; int status = 0; int column = 0; while ((status & ColumnText.NO_MORE_TEXT) == 0) { if (column == 0) { ct.setColumns(left1, right1); column = 1; } else { ct.setColumns(left2, right2); column = 0; } status = ct.go(); ct.setYLine(790); ct.setAlignment(Element.ALIGN_JUSTIFIED); status = ct.go(); if ((column == 0) && ((status & ColumnText.NO_MORE_COLUMN) != 0)) { document.newPage(); cb.addTemplate(t, 0, 0); cb.addImage(caesar, 100, 0, 0, 100, 260, 595); } } // step 5: we close the document document.close(); }