📄 pdfcontentbyte.java
字号:
x1 = x2;
x2 = tmp;
}
if (y2 > y1) {
tmp = y1;
y1 = y2;
y2 = tmp;
}
float fragAngle;
int Nfrag;
if (Math.abs(extent) <= 90f) {
fragAngle = extent;
Nfrag = 1;
}
else {
Nfrag = (int)(Math.ceil(Math.abs(extent)/90f));
fragAngle = extent / Nfrag;
}
float x_cen = (x1+x2)/2f;
float y_cen = (y1+y2)/2f;
float rx = (x2-x1)/2f;
float ry = (y2-y1)/2f;
float halfAng = (float)(fragAngle * Math.PI / 360.);
float kappa = (float)(Math.abs(4. / 3. * (1. - Math.cos(halfAng)) / Math.sin(halfAng)));
ArrayList pointList = new ArrayList();
for (int i = 0; i < Nfrag; ++i) {
float theta0 = (float)((startAng + i*fragAngle) * Math.PI / 180.);
float theta1 = (float)((startAng + (i+1)*fragAngle) * Math.PI / 180.);
float cos0 = (float)Math.cos(theta0);
float cos1 = (float)Math.cos(theta1);
float sin0 = (float)Math.sin(theta0);
float sin1 = (float)Math.sin(theta1);
if (fragAngle > 0f) {
pointList.add(new float[]{x_cen + rx * cos0,
y_cen - ry * sin0,
x_cen + rx * (cos0 - kappa * sin0),
y_cen - ry * (sin0 + kappa * cos0),
x_cen + rx * (cos1 + kappa * sin1),
y_cen - ry * (sin1 - kappa * cos1),
x_cen + rx * cos1,
y_cen - ry * sin1});
}
else {
pointList.add(new float[]{x_cen + rx * cos0,
y_cen - ry * sin0,
x_cen + rx * (cos0 + kappa * sin0),
y_cen - ry * (sin0 - kappa * cos0),
x_cen + rx * (cos1 - kappa * sin1),
y_cen - ry * (sin1 + kappa * cos1),
x_cen + rx * cos1,
y_cen - ry * sin1});
}
}
return pointList;
}
/**
* Draws a partial ellipse inscribed within the rectangle x1,y1,x2,y2,
* starting at startAng degrees and covering extent degrees. Angles
* start with 0 to the right (+x) and increase counter-clockwise.
*
* @param x1 a corner of the enclosing rectangle
* @param y1 a corner of the enclosing rectangle
* @param x2 a corner of the enclosing rectangle
* @param y2 a corner of the enclosing rectangle
* @param startAng starting angle in degrees
* @param extent angle extent in degrees
*/
public void arc(float x1, float y1, float x2, float y2, float startAng, float extent)
{
ArrayList ar = bezierArc(x1, y1, x2, y2, startAng, extent);
if (ar.size() == 0)
return;
float pt[] = (float [])ar.get(0);
moveTo(pt[0], pt[1]);
for (int k = 0; k < ar.size(); ++k) {
pt = (float [])ar.get(k);
curveTo(pt[2], pt[3], pt[4], pt[5], pt[6], pt[7]);
}
}
/**
* Draws an ellipse inscribed within the rectangle x1,y1,x2,y2.
*
* @param x1 a corner of the enclosing rectangle
* @param y1 a corner of the enclosing rectangle
* @param x2 a corner of the enclosing rectangle
* @param y2 a corner of the enclosing rectangle
*/
public void ellipse(float x1, float y1, float x2, float y2)
{
arc(x1, y1, x2, y2, 0f, 360f);
}
/**
* Creates a new template.
* <P>
* Creates a new template that is nothing more than a form XObject. This template can be included
* in this <CODE>PdfContentByte</CODE> or in another template. Templates are only written
* to the output when the document is closed permitting things like showing text in the first page
* that is only defined in the last page.
*
* @param width the bounding box width
* @param height the bounding box height
* @return the templated created
*/
public PdfTemplate createTemplate(float width, float height)
{
checkWriter();
PdfTemplate template = new PdfTemplate(writer);
template.setWidth(width);
template.setHeight(height);
writer.addDirectTemplateSimple(template);
return template;
}
/**
* Adds a template to this content.
*
* @param template the template
* @param a an element of the transformation matrix
* @param b an element of the transformation matrix
* @param c an element of the transformation matrix
* @param d an element of the transformation matrix
* @param e an element of the transformation matrix
* @param f an element of the transformation matrix
*/
public void addTemplate(PdfTemplate template, float a, float b, float c, float d, float e, float f)
{
checkWriter();
PdfName name = pdf.addTemplateToPage(template);
content.append("q ");
content.append(a).append(' ');
content.append(b).append(' ');
content.append(c).append(' ');
content.append(d).append(' ');
content.append(e).append(' ');
content.append(f).append(" cm ");
content.append(name.toString()).append(" Do Q\n");
}
/**
* Adds a template to this content.
*
* @param template the template
* @param x the x location of this template
* @param y the y location of this template
*/
public void addTemplate(PdfTemplate template, float x, float y)
{
addTemplate(template, 1, 0, 0, 1, x, y);
}
/**
* Changes the current color for filling paths (device dependent colors!).
* <P>
* Sets the color space to <B>DeviceCMYK</B> (or the <B>DefaultCMYK</B> color space),
* and sets the color to use for filling paths.</P>
* <P>
* This method is described in the 'Portable Document Format Reference Manual version 1.3'
* section 8.5.2.1 (page 331).</P>
* <P>
* Following the PDF manual, each operand must be a number between 0 (no ink) and
* 1 (maximum ink). This method however accepts only integers between 0x00 and 0xFF.</P>
*
* @param cyan the intensity of cyan
* @param magenta the intensity of magenta
* @param yellow the intensity of yellow
* @param black the intensity of black
*/
public final void setCMYKColorFill(int cyan, int magenta, int yellow, int black) {
content.append((float)(cyan & 0xFF) / 0xFF);
content.append(' ');
content.append((float)(magenta & 0xFF) / 0xFF);
content.append(' ');
content.append((float)(yellow & 0xFF) / 0xFF);
content.append(' ');
content.append((float)(black & 0xFF) / 0xFF);
content.append(" k\n");
}
/**
* Changes the current color for stroking paths (device dependent colors!).
* <P>
* Sets the color space to <B>DeviceCMYK</B> (or the <B>DefaultCMYK</B> color space),
* and sets the color to use for stroking paths.</P>
* <P>
* This method is described in the 'Portable Document Format Reference Manual version 1.3'
* section 8.5.2.1 (page 331).</P>
* Following the PDF manual, each operand must be a number between 0 (miniumum intensity) and
* 1 (maximum intensity). This method however accepts only integers between 0x00 and 0xFF.
*
* @param cyan the intensity of red
* @param magenta the intensity of green
* @param yellow the intensity of blue
* @param black the intensity of black
*/
public final void setCMYKColorStroke(int cyan, int magenta, int yellow, int black) {
content.append((float)(cyan & 0xFF) / 0xFF);
content.append(' ');
content.append((float)(magenta & 0xFF) / 0xFF);
content.append(' ');
content.append((float)(yellow & 0xFF) / 0xFF);
content.append(' ');
content.append((float)(black & 0xFF) / 0xFF);
content.append(" K\n");
}
/**
* Changes the current color for filling paths (device dependent colors!).
* <P>
* Sets the color space to <B>DeviceRGB</B> (or the <B>DefaultRGB</B> color space),
* and sets the color to use for filling paths.</P>
* <P>
* This method is described in the 'Portable Document Format Reference Manual version 1.3'
* section 8.5.2.1 (page 331).</P>
* <P>
* Following the PDF manual, each operand must be a number between 0 (miniumum intensity) and
* 1 (maximum intensity). This method however accepts only integers between 0x00 and 0xFF.</P>
*
* @param red the intensity of red
* @param green the intensity of green
* @param blue the intensity of blue
*/
public final void setRGBColorFill(int red, int green, int blue) {
content.append((float)(red & 0xFF) / 0xFF);
content.append(' ');
content.append((float)(green & 0xFF) / 0xFF);
content.append(' ');
content.append((float)(blue & 0xFF) / 0xFF);
content.append(" rg\n");
}
/**
* Changes the current color for stroking paths (device dependent colors!).
* <P>
* Sets the color space to <B>DeviceRGB</B> (or the <B>DefaultRGB</B> color space),
* and sets the color to use for stroking paths.</P>
* <P>
* This method is described in the 'Portable Document Format Reference Manual version 1.3'
* section 8.5.2.1 (page 331).</P>
* Following the PDF manual, each operand must be a number between 0 (miniumum intensity) and
* 1 (maximum intensity). This method however accepts only integers between 0x00 and 0xFF.
*
* @param red the intensity of red
* @param green the intensity of green
* @param blue the intensity of blue
*/
public final void setRGBColorStroke(int red, int green, int blue) {
content.append((float)(red & 0xFF) / 0xFF);
content.append(' ');
content.append((float)(green & 0xFF) / 0xFF);
content.append(' ');
content.append((float)(blue & 0xFF) / 0xFF);
content.append(" RG\n");
}
/**
* Check if we have a valid PdfWriter.
*
* @throw NullPointerException the writer is invalid
*/
protected void checkWriter()
{
if (writer == null)
throw new NullPointerException("The writer in PdfContentByte is null.");
}
/**
* Show an array of text.
* @param text array of text
*/
void showText(PdfTextArray text) {
if (state.fontDetails == null)
throw new NullPointerException("Font and size must be set before writing any text");
content.append("[");
ArrayList arrayList = text.getArrayList();
boolean lastWasNumber = false;
for (int k = 0; k < arrayList.size(); ++k) {
PdfObject obj = (PdfObject)arrayList.get(k);
if (obj.isString()) {
showText2(obj.toString());
lastWasNumber = false;
}
else {
if (lastWasNumber)
content.append(' ');
else
lastWasNumber = true;
content.append(obj.toString());
}
}
content.append("]TJ\n");
}
/**
* Gets the <CODE>PdfWriter</CODE> in use by this object.
* @return the <CODE>PdfWriter</CODE> in use by this object
*/
PdfWriter getPdfWriter()
{
return writer;
}
/**
* Gets the <CODE>PdfDocument</CODE> in use by this object.
* @return the <CODE>PdfDocument</CODE> in use by this object
*/
PdfDocument getPdfDocument()
{
return pdf;
}
/**
* Implements a link to other part of the document. The jump will
* be made to a local destination with the same name, that must exist.
* @param name the name for this link
* @param llx the lower left x corner of the activation area
* @param lly the lower left y corner of the activation area
* @param urx the upper right x corner of the activation area
* @param ury the upper right y corner of the activation area
*/
public void localGoto(String name, float llx, float lly, float urx, float ury)
{
pdf.localGoto(name, llx, lly, urx, ury);
}
/**
* The local destination to where a local goto with the same
* name will jump.
* @param name the name of this local destination
* @param destination the <CODE>PdfDestination</CODE> with the jump coordinates
* @return <CODE>true</CODE> if the local destination was added,
* <CODE>false</CODE> if a local destination with the same name
* already exists
*/
public boolean localDestination(String name, PdfDestination destination)
{
return pdf.localDestination(name, destination);
}
/**
* Gets a duplicate of this <CODE>PdfContentByte</CODE>. All
* the members are copied by reference but the buffer stays different.
* @return a copy of this <CODE>PdfContentByte</CODE>
*/
public PdfContentByte getDuplicate()
{
return new PdfContentByte(writer);
}
/**
* Implements a link to another document.
* @param filename the filename for the remote document
* @param name the name to jump to
* @param llx the lower left x corner of the activation area
* @param lly the lower left y corner of the activation area
* @param urx the upper right x corner of the activation area
* @param ury the upper right y corner of the activation area
*/
public void remoteGoto(String filename, String name, float llx, float lly, float urx, float ury)
{
remoteGoto(filename, name, llx, lly, urx, ury);
}
/**
* Implements a link to another document.
* @param filename the filename for the remote document
* @param page the page to jump to
* @param llx the lower left x corner of the activation area
* @param lly the lower left y corner of the activation area
* @param urx the upper right x corner of the activation area
* @param ury the upper right y corner of the activation area
*/
public void remoteGoto(String filename, int page, float llx, float lly, float urx, float ury)
{
pdf.remoteGoto(filename, page, llx, lly, urx, ury);
}
/**
* Adds a round rectangle to the current path.
*
* @param x x-coordinate of the starting point
* @param y y-coordinate of the starting point
* @param w width
* @param h height
* @param r radius of the arc corner
*/
public void roundRectangle(float x, float y, float w, float h, float r)
{
float b = 0.4477f;
moveTo(x + r, y);
lineTo(x + w - r, y);
curveTo(x + w - r * b, y, x + w, y + r * b, x + w, y + r);
lineTo(x + w, y + h - r);
curveTo(x + w, y + h - r * b, x + w - r * b, y + h, x + w - r, y + h);
lineTo(x + r, y + h);
curveTo(x + r * b, y + h, x, y + h - r * b, x, y + h - r);
lineTo(x, y + r);
curveTo(x, y + r * b, x + r * b, y, x + r, y);
}
/** Implements an action in an area.
* @param action the <CODE>PdfAction</CODE>
* @param llx the lower left x corner of the activation area
* @param lly the lower left y corner of the activation area
* @param urx the upper right x corner of the activation area
* @param ury the upper right y corner of the activation area
*/
public void setAction(PdfAction action, float llx, float lly, float urx, float ury) {
pdf.setAction(action, llx, lly, urx, ury);
}
public void setLiteral(String s) {
content.append(s);
}
public void setLiteral(char c) {
content.append(c);
}
public void setLiteral(float n) {
content.append(n);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -