📄 pdfpage.java
字号:
* <p> * Please note the substitution will <i>only</i> take place if the current styles' font * has the required characters defined. The 14 standard fonts do, and most others * should. * </p> * <p> * Finally, the algorithm isn't perfect - it's more likely to work if spaces are placed * before opening quotes, and closing quotes are followed by a space or punctuation. * If it's still making mistakes, you can prevent a quote from being "turned" by * surrounding the character with U+200C (zero-width non-joiner) characters. * </p> * @param text the text to substitute * @return the passed in string with the appropriate quote characters for the Locale * in place of characters single quote (', 0x27) and double quote (", 0x22) * @since 1.1 */ public String requote(String text) { char[] c = text.toCharArray(); // TODO - find proper locale PDFStyle style = getStyle(); if (style!=null && style.getFont()!=null && style.getFont().requote(c,0,c.length, Locale.getDefault())) { return new String(c,0,c.length); } else { return text; } } /** * <p> * Draw a barcode at the specified position. The type of barcode * is specified by the <code>type</code> parameter, and may be * one of {@link #BARCODE39}, {@link #BARCODE39CHECKSUM}, * {@link #BARCODE39X}, {@link #BARCODE39XCHECKSUM}, * {@link #BARCODE25}, {@link #BARCODE25CHECKSUM}, * {@link #BARCODE128}, {@link #BARCODEEAN13} or {@link #BARCODECODABAR}. * Each of these algorithms has restrictions on what characters * can be displayed, and an exception is thrown if an illegal * character is given. * </p><p> * The width of the resulting barcode in points is returned. * The height of the barcode is 15% of the width, with a * minimum height of 18 points. If text is displayed, you can * add another <code>(8*width)</code> points to the height. * </p><p> * @param type the type of barcode to print * @param code the string to print * @param x the left-most position of the barcode on the page * @param y the vertical center of the barcode on the page * @param showtext whether to show the human-readable equivalent * of the barcode immediately beneath the code * @param width the width of the thinnest bar in points. Acceptable * values depend on your scanner. The recommended minimum is 0.54 * points, or 0.0075 inches (0.19mm). If in doubt, use "1". * @return the width of the resulting barcode, in points * @throws IllegalArgumentException if the characters or the barcode * type is invalid * @since 1.1.5 */ public float drawBarCode(int type, String code, float x, float y, boolean showtext, float width) throws IllegalArgumentException { return drawBarCode(type, code, cx(x), cy(y), showtext, width, 18, 2.8f); } /** * <p> * Draw a barcode at the specified position. Identical to the other * barcode routine, but allows two extra properties to be specified * for full control over the resulting code - the <code>height</code>, * which is the height of the barcode in points, and the <code>ratio</code>, * which is the thickbar/thinbar ratio for those codes that only use * two bar widths (CODE39 and CODE25). * </p><p> * The specified height will always be rounded up to 18 points or 15% of the * width of the barcode, whichever is greater. * </p><p> * The ratio should always be 2.0 and 3.0 - the default is 2.8. For most * algorithms, if the thinnest bar has a width of less than 1.5 points * then the ratio should be between 2.0 and 2.2. * </p> * @param type the type of barcode to print * @param code the string to print * @param x the left-most position of the barcode on the page * @param y the vertical center of the barcode on the page * @param showtext whether to show the human-readable equivalent * of the barcode immediately beneath the code * @param width the width of the thinnest bar in points. Acceptable * values depend on your scanner. The recommended minimum is 0.54 * points, or 0.0075 inches (0.19mm). If in doubt, use "1" * @param height the height of the barcode in points. Minimum value is 18 * @param ratio the ratio of the thickest bar in the barcode to the thinnest, * if applicable. Valid values are between 2.0 and 3.0. For multiple-width * codes like Code128, this is ignored. If in doubt, try "2.8" * @return the width of the resulting barcode, in points * @throws IllegalArgumentException if the characters or the barcode * type is invalid * @since 1.1.13 */ public float drawBarCode(int type, String code, float x, float y, boolean showtext, float width, int height, float ratio) throws IllegalArgumentException { int newtype; if (type==BARCODE39) newtype=org.faceless.pdf2.BarCode.CODE39; else if (type==BARCODE39CHECKSUM) newtype=org.faceless.pdf2.BarCode.CODE39_CHECKSUM; else if (type==BARCODE39X) newtype=org.faceless.pdf2.BarCode.CODE39X; else if (type==BARCODE39XCHECKSUM) newtype=org.faceless.pdf2.BarCode.CODE39X_CHECKSUM; else if (type==BARCODE25) newtype=org.faceless.pdf2.BarCode.INTERLEAVED25; else if (type==BARCODE25CHECKSUM) newtype=org.faceless.pdf2.BarCode.INTERLEAVED25_CHECKSUM; else if (type==BARCODE128) newtype=org.faceless.pdf2.BarCode.CODE128; else if (type==BARCODEEAN13) newtype=org.faceless.pdf2.BarCode.EAN13; else if (type==BARCODEUPCA) newtype=org.faceless.pdf2.BarCode.UPCA; else if (type==BARCODECODABAR) newtype=org.faceless.pdf2.BarCode.CODABAR; else throw new IllegalArgumentException("Unknown barcode type"); org.faceless.pdf2.BarCode codeo = new org.faceless.pdf2.BarCode(type, code); codeo.setShowText(showtext); codeo.setBarWidth(width); codeo.setHeight(height); codeo.setBarRatio(ratio); float barwidth=codeo.getWidth(); float fontheight = (showtext ? width*8 : 0)*1.25f; float barheight=height+fontheight; page.drawBarCode(codeo, cx(x), cy(y)+(barheight/2)-(fontheight/2), cx(x)+barwidth, cy(y)-(barheight/2)-(fontheight/2)); return barwidth; } /** * Set the XML metadata associated with this object. See * {@link PDF#setMetaData} for more information. * @param xmldata the XML data to embed into the document, or <tt>null</tt> to clear any existing metadata. No validation is performed on this input. * @since 1.1.12 */ public void setMetaData(String xmldata) { page.setMetaData(xmldata); } /** * Return any XML metadata associated with this object. See the * {@link PDF#getMetaData} for more information * @return a {@link java.io.Reader} containing the source of the XML or <tt>null</tt> if no metadata is available. * @throws IOException if the metadata can't be extracted * @since 1.1.12 */ public Reader getMetaData() throws IOException { return page.getMetaData(); } /** * <p> * Add the contents of the specified page to this page, at the specified * position. The page to be added is treated in a similar way to an image * in the {@link #drawImage} method - it's scaled to fit the specified * rectangle, but it's up to the user to preserve the original aspect ratio. * </p><p> * It is anticipated that this method will be used with the {@link PDFReader} * class to allow pages to stitched together, overlaid, changed from * Letter to A4 and so on. * </p> * <p> * Here's an example showing two pages being placed next to eachother in a * "2-up" layout. * </p> * <pre> * void drawTwoUp(PDFPage page1, PDFPage page2, PDFPage dest) * { * dest.setCanvas(0,0,dest.getWidth(),dest.getHeight(), PDFPage.PERCENT, PDFPage.PAGETOP); * dest.drawPage(page1, 0, 0, 50, 100); // from (0%,0%) to (50%,100%) * dest.drawPage(page2, 50, 0, 100, 100); // from (50%,0%) to (100%,100%) * } * </pre> * <b>Note</b>. For simply copying pages from one document to another, it's * <i>considerably</i> faster, and easier, to join the two pages together by * manipulating the list of pages returned from {@link PDF#getPages}. * * @param page The page whose contents are to be drawn onto this page * @param x1 the X co-ordinate of the first corner of the image * @param y1 the Y co-ordinate of the first corner of the image * @param x2 the X co-ordinate of the second corner of the image * @param y2 the Y co-ordinate of the second corner of the image * * @since 1.1.12 */ public void drawPage(PDFPage page, float x1, float y1, float x2, float y2) { page.page.flush(); org.faceless.pdf2.PDFCanvas canvas = new org.faceless.pdf2.PDFCanvas(page.page); this.page.drawCanvas(canvas, cx(x1), cy(y1), cx(x2), cy(y2)); if (page.page.getAnnotations().size()>0) { org.faceless.pdf2.PDFPage clone = new org.faceless.pdf2.PDFPage(page.page); x1 = canvasx(x1); y1 = canvasy(y1); x2 = canvasx(x2); y2 = canvasy(y2); if (x1>x2) { float t=x1; x1=x2; x2=t; } if (y1>y2) { float t=y1; y1=y2; y2=t; }// System.err.println("clone="+clone); List annots = clone.getAnnotations(); for (int i=0;i<annots.size();i++) { org.faceless.pdf2.PDFAnnotation annot = (org.faceless.pdf2.PDFAnnotation)annots.get(i); float[] f = annot.getRectangle(); if (f!=null) {// System.err.println("page="+page.getWidth()+"x"+page.getHeight()+" x1="+x1+" y1="+y1+" x2="+x2+" y2="+y2);// System.err.println("WAS F="+f[0]+","+f[1]+"-"+f[2]+","+f[3]); f[0] = (f[0]/clone.getWidth()*(x2-x1))+x1; f[1] = (f[1]/clone.getHeight()*(y2-y1))+y1; f[2] = (f[2]/clone.getWidth()*(x2-x1))+x1; f[3] = (f[3]/clone.getHeight()*(y2-y1))+y1; annot.setRectangle(f[0], f[1], f[2], f[3]);// System.err.println("NOW F="+f[0]+","+f[1]+"-"+f[2]+","+f[3]); } this.page.getAnnotations().add(annot); } } } /** * <p> * Draw a line of text at the specified position. A simple way to draw * a single line of text. The co-ordinates specify the position of the * baseline of the first character - for other positions (e.g. to align * the top of the text), adjust the co-ordinates by the return value from * {@link PDFStyle#getTextTop} and friends. * </p> * @param text the line of text to draw * @param x the X co-ordinate to draw the text at * @param y the Y co-ordinate to draw the text at */ public void drawText(String text, float x, float y) { page.drawText(text,cx(x),cy(y)); } /** * <p> * Draw a line of text at a the specified position, and set it to * link to the specified action. A shorthand combination of * <code>drawText</code> and <code>beginTextLink</code>. * </p><p> * <i>Note that this method will not work as advertised if the position * of the text has been modified via the <code>rotate</code>, <code>scale</code> * or <code>translate</code> methods. This is a shortcoming inherent in * the PDF document specification</i>. See the {@link PDFAnnotation} class * documentation for more information. * </p> * @param text the line of text to draw * @param x the X co-ordinate to draw the text at * @param y the Y co-ordinate to draw the text at * @param action the action to perform when the text is clicked on * @since 1.1 */ public void drawTextLink(String text, float x, float y, PDFAction action) { page.drawTextLink(text,cx(x),cy(y),action.action); } /** * <p> * Begin a paragraph of text. The parameters specify the rectangle * measured in the current canvas units that will fully contain the text. * Left-to-right text will wrap when it reaches the right margin and * continue being rendered until the bottom margin is reached, after which * the text will not be rendered and all calls to <code>drawText</code> * will return -1. This "overflowed" text can be rendered in a new block * by calling <code>continueText</code> * </p> * <p><b>Note:</b> The <code>beginText</code>/<code>drawText</code>/<code>endText</code> * methods date from the 1.0 release of the PDF library, and while they * are suitable for simple text layout, more complex layout is best done * with the {@link LayoutBox} class. In particular these methods have issues * with the height calculations of text, and with what to do when the box * defined by <code>beginText</code> is full. * </p> * * @see LayoutBox * @param x1 the X co-ordinate of the first corner of the text rectangle. * @param y1 the Y co-ordinate of the first corner of the text rectangle. * @param x2 the X co-ordinate of the second corner of the text rectangle. * @param y2 the Y co-ordinate of the second corner of the text rectangle. * @throws IllegalStateException if beginText has already been called * (<code>beginText-endText</code> pairs can't be nested). */ public void beginText(float x1, float y1, float x2, float y2) { page.beginText(cx(x1),cy(y1),cx(x2),cy(y2)); } /** * <p> * As for beginText, but continue any text that overflowed from the * specified page. If the page being continued does not have an * unclosed <code>beginText</code> call, this method is identical * to calling <code>beginText</code> on the current page. * </p><p> * Since 1.1, this method automatically determines whether the new * text block should have any leading blank lines trimmed, or whether * the new block is contiguous with the old one. * </p> * <p><b>Note:</b> The <code>beginText</code>/<code>drawText</code>/<code>endText</code> * methods date from the 1.0 release of the PDF library, and while they * are suitable for simple text layout, more complex layout is
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -