import java.io.File;
import java.io.IOException;
import com.gnostice.pdfone.PDFOne;
import com.gnostice.pdfone.PdfDocument;
import com.gnostice.pdfone.PdfException;
import com.gnostice.pdfone.PdfMeasurement;
import com.gnostice.pdfone.PdfPage;
import com.gnostice.pdfone.PdfRect;
import com.gnostice.pdfone.PdfTextFormatter;
import com.gnostice.pdfone.PdfWriter;
public class PdfTextFormatter_Examples
{
// Activates the component PDFOne.jar
static
{
PDFOne.activate("T95VZE:W8HBPVA:74VQ8QV:LO4V8",
"9B1HRZAP:X5853ERNE:5EREMEGRQ:TX1R10");
}
public static void main(String[] args) throws IOException,
PdfException
{
PdfTextFormatter_Examples obj = new
PdfTextFormatter_Examples();
// To try other examples, add the obj.<example_method>
// accordingly. For example, try:
// obj.FirstLinePosition_Example();
obj.getAlignment_Example();
}
// This code segment retrieves the current setting for text
// alignment and writes some text explaining the same.
public void getAlignment_Example() throws IOException,
PdfException
{
PdfWriter writer = PdfWriter.fileWriter(
new File(
"PdfTextFormatter_getAlignment_"
+ "example.pdf"));
PdfDocument document = new PdfDocument(writer);
PdfPage page = new PdfPage();
PdfTextFormatter formatter = new PdfTextFormatter();
formatter = page.getTextFormatter();
page.setMeasurementUnit(PdfMeasurement.MU_INCHES);
// Checks current setting for text alignment
switch (formatter.getAlignment())
{
case PdfTextFormatter.LEFT:
page.writeText(
"Text is aligned to the left margin.",
new PdfRect(2, 1, 4, 1));
break;
case PdfTextFormatter.RIGHT:
page.writeText(
"Text is aligned to the right margin.",
new PdfRect(2, 1, 4, 1));
break;
case PdfTextFormatter.CENTER:
page.writeText(
"Text is centered on the page.",
new PdfRect(2, 1, 4, 1));
break;
case PdfTextFormatter.JUSTIFIED:
page.writeText(
"Text is justified to the side margins.",
new PdfRect(2, 1, 4, 1));
break;
}
// Draws margins for a better perspective
page.drawLine(2, 1, 2, 4.2);
page.drawLine(6, 1, 6, 4.2);
document.add(page);
document.setOpenAfterSave(true);
document.write();
writer.dispose();
}
// This code segment writes a paragraph four times but with
// different alignment settings.
public void setAlignment_Example() throws IOException,
PdfException
{
PdfWriter writer = PdfWriter.fileWriter(
new File(
"PdfTextFormatter_setAlignment_"
+ "example.pdf"));
PdfDocument document = new PdfDocument(writer);
PdfPage page = new PdfPage();
PdfTextFormatter formatter = new PdfTextFormatter();
formatter = page.getTextFormatter();
page.setMeasurementUnit(PdfMeasurement.MU_INCHES);
// Writes left-aligned text
formatter.setAlignment(PdfTextFormatter.LEFT);
page.writeText(
"Four score and seven years ago...",
new PdfRect(2, 1, 3, 1));
// Writes right-aligned text
formatter.setAlignment(PdfTextFormatter.RIGHT);
page.writeText(
"Four score and seven years ago...",
new PdfRect(2, 2, 3, 1));
// Writes center-aligned text
formatter.setAlignment(PdfTextFormatter.CENTER);
page.writeText(
"Four score and seven years ago...",
new PdfRect(2, 3, 3, 1));
// Writes justified text
formatter.setAlignment(PdfTextFormatter.JUSTIFIED);
page.writeText(
"Four score and seven years ago...",
new PdfRect(2, 4, 3, 1));
// Draws margins for perspective
page.drawLine(2, 1, 2, 4.2);
page.drawLine(5, 1, 5, 4.2);
document.add(page);
document.setOpenAfterSave(true);
document.write();
writer.dispose();
}
// This code segment obtains current setting for text wrapping
// and writes a few lines of text. It then changes the setting
// and writes the same lines of text again at a different
// location to illustrate the difference.
public void Wrap_Example() throws IOException, PdfException
{
PdfWriter writer = PdfWriter.fileWriter(
new File(
"PdfTextFormatter_Wrap_"
+ "example.pdf"));
PdfDocument document = new PdfDocument(writer);
PdfPage page = new PdfPage();
PdfTextFormatter formatter = new PdfTextFormatter();
formatter = page.getTextFormatter();
// Obtains current setting for text wrapping
boolean wrap = formatter.isWrap();
// Writes text under current setting
if (wrap == PdfTextFormatter.WRAP)
{
page.writeText(
"[Text set to wrap.] "
+ "Four score and seven years ago our fathers "
+ "brought forth on this continent a new "
+ "nation conceived in liberty and dedicated "
+ "to the proposition that all men are created "
+ "equal.",
new PdfRect(100, 50, 400, 100));
// Changes text wrap setting
formatter.setWrap(PdfTextFormatter.NO_WRAP);
}
else
{
page.writeText(
"[Text set to wrap.] "
+ "Four score and seven years ago our fathers "
+ "brought forth on this continent a new "
+ "nation conceived in liberty and dedicated "
+ "to the proposition that all men are created "
+ "equal.",
new PdfRect(100, 50, 400, 100));
// Changes text wrap setting
formatter.setWrap(PdfTextFormatter.WRAP);
}
// Obtains new setting for text wrapping
wrap = formatter.isWrap();
// Writes text under new setting
if (wrap == PdfTextFormatter.WRAP)
{
page.writeText(
"[Text set to wrap.] "
+ "Four score and seven years ago our fathers "
+ "brought forth on this continent a new "
+ "nation conceived in liberty and dedicated "
+ "to the proposition that all men are created "
+ "equal.",
new PdfRect(100, 100, 400, 100));
}
else
{
page.writeText(
"[Text set not to wrap.] "
+ "Four score and seven years ago our fathers "
+ "brought forth on this continent a new "
+ "nation conceived in liberty and dedicated "
+ "to the proposition that all men are created "
+ "equal.",
new PdfRect(100, 100, 400, 100));
}
document.add(page);
document.setOpenAfterSave(true);
document.write();
writer.dispose();
}
// This code segment writes a few lines of text. First time, the
// text is written using the default setting for the first line.
// Second time, the setting is changed to 3 inches.
public void FirstLinePosition_Example() throws IOException,
PdfException
{
PdfWriter writer = PdfWriter.fileWriter(
new File(
"PdfTextFormatter_"
+ "FirstLinePosition_"
+ "example.pdf"));
PdfDocument document = new PdfDocument(writer);
PdfPage page = new PdfPage();
PdfTextFormatter formatter = new PdfTextFormatter();
formatter = page.getTextFormatter();
// Sets page measurement unit to inches
page.setMeasurementUnit(PdfMeasurement.MU_INCHES);
// Retrieves current first line position
double position = formatter.getFirstLinePosition();
// Writes a few lines of text
page.writeText(
"Four score and seven years ago our fathers "
+ "brought forth on this continent a new "
+ "nation conceived in liberty and dedicated "
+ "to the proposition that all men are created "
+ "equal. "
+ " [First line at "
+ position
+ " inches]",
new PdfRect(2, 1, 4, 3));
// Sets new first line position to 3 inches
formatter.setFirstLinePosition(3);
// Obtains new first line position
position = formatter.getFirstLinePosition();
// Writes the lines of text again
page.writeText(
"Four score and seven years ago our fathers "
+ "brought forth on this continent a new "
+ "nation conceived in liberty and dedicated "
+ "to the proposition that all men are created "
+ "equal. "
+ " [First line at "
+ position
+ " inches]",
new PdfRect(2, 3, 4, 3));
document.add(page);
document.setOpenAfterSave(true);
document.write();
writer.dispose();
}
// This code segment writes a few lines of text. First time, the
// last line is written using default setting for justification.
// Second time, the last line is written using the opposition
// setting.
public void JustifyLastLine_Example() throws IOException,
PdfException
{
PdfWriter writer = PdfWriter.fileWriter(
new File(
"PdfTextFormatter_JustifyLastLine_"
+ "example.pdf"));
PdfDocument document = new PdfDocument(writer);
PdfPage page = new PdfPage();
PdfTextFormatter formatter = new PdfTextFormatter();
formatter = page.getTextFormatter();
formatter.setAlignment(PdfTextFormatter.JUSTIFIED);
// Obtains current alignment setting for last line
boolean justified = formatter.isJustifyLastLine();
// Writes a few lines of text
page.writeText(
"[Last line justification: "
+ justified
+ "] "
+ "Four score and seven years ago our fathers "
+ "brought forth on this continent a new nation "
+ "conceived in liberty and dedicated to the "
+ "proposition that all men are created equal.",
new PdfRect(100, 50, 350, 50));
// Sets last line setting to opposite of current setting
formatter.setJustifyLastLine(!justified);
// Obtains new alignment setting for last line
justified = formatter.isJustifyLastLine();
// Writes the lines of text again
page.writeText(
"[Last line justification: "
+ justified
+ "] "
+ "Four score and seven years ago our fathers "
+ "brought forth on this continent a new nation "
+ "conceived in liberty and dedicated to the "
+ "proposition that all men are created equal.",
new PdfRect(100, 100, 350, 50));
document.add(page);
document.setOpenAfterSave(true);
document.write();
writer.dispose();
}
}