本文整理匯總了Java中com.itextpdf.text.pdf.PdfWriter.getDirectContent方法的典型用法代碼示例。如果您正苦於以下問題:Java PdfWriter.getDirectContent方法的具體用法?Java PdfWriter.getDirectContent怎麽用?Java PdfWriter.getDirectContent使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.itextpdf.text.pdf.PdfWriter
的用法示例。
在下文中一共展示了PdfWriter.getDirectContent方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: buildPdfDocument
import com.itextpdf.text.pdf.PdfWriter; //導入方法依賴的package包/類
public void buildPdfDocument(OutputStream toWrite, final String name, final int treeCount, String[] codeFragments, String imagePath) throws Exception {
// create pdf
final Document doc = new Document();
final PdfWriter pdfWriter = PdfWriter.getInstance(doc, toWrite);
pdfWriter.setEncryption(null, null, PdfWriter.ALLOW_DEGRADED_PRINTING | PdfWriter.ALLOW_PRINTING, PdfWriter.STANDARD_ENCRYPTION_128);
_imagePath = imagePath;
doc.open();
PdfContentByte cb = pdfWriter.getDirectContent();
PdfHelper.createHeaderBlock(cb,1,1);
PdfHelper.createCircleAndText(cb, "Gutschein", 298f, 705f, 75, textFontForCircle, 0x7F, 0xAD, 0x09);
createBlueBlock(cb, treeCount);
createGreyBlock(cb, codeFragments);
pdfHelper.addLogo(cb, _imagePath, 262f, 35f);
doc.close();
}
示例2: print
import com.itextpdf.text.pdf.PdfWriter; //導入方法依賴的package包/類
public void print(String plot_pdf) {
try {
float width = jframe.getSize().width,
height = jframe.getSize().height;
Document document = new Document(new Rectangle(width, height));
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(plot_pdf));
document.open();
PdfContentByte canvas = writer.getDirectContent();
PdfTemplate template = canvas.createTemplate(width, height);
Graphics2D g2d = new PdfGraphics2D(template, width, height);
jframe.paint(g2d);
g2d.dispose();
canvas.addTemplate(template, 0, 0);
document.close();
} catch (FileNotFoundException | DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
示例3: writeChartToPDF
import com.itextpdf.text.pdf.PdfWriter; //導入方法依賴的package包/類
/**
* Save the chart as pdf.
*
* @param chart
* chart that should be saved
* @param fileName
* file name under which chart should be saved
*/
public static void writeChartToPDF(final JFreeChart chart,
final String fileName) {
PdfWriter writer = null;
com.itextpdf.text.Document document = new com.itextpdf.text.Document(
PageSize.A4);
final int width = (int) PageSize.A4.getWidth();
final int height = (int) PageSize.A4.getHeight();
try {
writer = PdfWriter.getInstance(document, new FileOutputStream(
fileName + ".pdf"));
document.open();
PdfContentByte contentByte = writer.getDirectContent();
PdfTemplate template = contentByte.createTemplate(width, height);
Graphics2D graphics2d = template.createGraphics(width, height,
new DefaultFontMapper());
Rectangle2D rectangle2d = new Rectangle2D.Double(0, 0, width,
height);
chart.draw(graphics2d, rectangle2d);
graphics2d.dispose();
contentByte.addTemplate(template, 0, 0);
} catch (Exception e) {
e.printStackTrace();
}
document.close();
}
示例4: writePdfDataToOutputStream
import com.itextpdf.text.pdf.PdfWriter; //導入方法依賴的package包/類
public void writePdfDataToOutputStream(OutputStream toWrite, final int treeCount, final String text, final String name, final String number, String imagePath) throws Exception {
// create pdf
final Document doc = new Document();
final PdfWriter pdfWriter = PdfWriter.getInstance(doc, toWrite);
pdfWriter.setEncryption(null, null, PdfWriter.ALLOW_DEGRADED_PRINTING | PdfWriter.ALLOW_PRINTING, PdfWriter.STANDARD_ENCRYPTION_128);
final Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT+2"), Locale.GERMAN);
cal.setTimeInMillis(System.currentTimeMillis());
final String date = cal.get(Calendar.DAY_OF_MONTH) + "." + (cal.get(Calendar.MONTH) + 1) + "." + cal.get(Calendar.YEAR);
_imagePath = imagePath;
doc.open();
PdfContentByte cb = pdfWriter.getDirectContent();
PdfHelper.createHeaderBlock(cb,1,1);
PdfHelper.createCircleAndText(cb, "Zertifikat", 298f, 665f, 75f, textFontForCircle, 0x9E, 0x3C, 0x59);
createTreeCountAndCustomTextBlock(cb, text, treeCount);
createLawTextDateAndSignatureBlock(cb, number, date);
pdfHelper.addLogo(cb, _imagePath, 262f, 20f);
doc.close();
}
示例5: writePdfDataToOutputStream
import com.itextpdf.text.pdf.PdfWriter; //導入方法依賴的package包/類
public void writePdfDataToOutputStream(OutputStream toWrite, final int treeCount, final String text, final String name, final String number, String imagePath) throws Exception {
// create pdf
final Document doc = new Document();
final PdfWriter pdfWriter = PdfWriter.getInstance(doc, toWrite);
pdfWriter.setEncryption(null, null, PdfWriter.ALLOW_DEGRADED_PRINTING | PdfWriter.ALLOW_PRINTING, PdfWriter.STANDARD_ENCRYPTION_128);
final Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT+2"), Locale.GERMAN);
cal.setTimeInMillis(System.currentTimeMillis());
final String date = cal.get(Calendar.DAY_OF_MONTH) + "." + (cal.get(Calendar.MONTH) + 1) + "." + cal.get(Calendar.YEAR);
_imagePath = imagePath;
doc.open();
PdfContentByte cb = pdfWriter.getDirectContent();
PdfHelper.createHeaderBlock(cb,1,1);
PdfHelper.createCircleAndText(cb, "Zertifikat", 298f, 665f, 75f, textFontForCircle, 0x9E, 0x3C, 0x59);
createTreeCountAndCustomTextBlock(cb, text, treeCount);
createLawTextDateAndSignatureBlock(cb, number, date);
pdfHelper.addLogo(cb, imagePath, 262f, 20f);
doc.close();
}
示例6: createAnnotation
import com.itextpdf.text.pdf.PdfWriter; //導入方法依賴的package包/類
PdfAnnotation createAnnotation(PdfWriter writer, Rectangle rect, String contents) throws DocumentException, IOException {
PdfContentByte cb = writer.getDirectContent();
PdfAppearance cs = cb.createAppearance(rect.getWidth(), rect.getHeight());
cs.rectangle(0 , 0, rect.getWidth(), rect.getHeight());
cs.fill();
cs.setFontAndSize(BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED), 12);
cs.beginText();
cs.setLeading(12 + 1.75f);
cs.moveText(.75f, rect.getHeight() - 12 + .75f);
cs.showText(contents);
cs.endText();
return PdfAnnotation.createFreeText(writer, rect, contents, cs);
}
示例7: drawSexyTriangle
import com.itextpdf.text.pdf.PdfWriter; //導入方法依賴的package包/類
private static void drawSexyTriangle(PdfWriter writer, boolean rotated)
{
PdfContentByte canvas = writer.getDirectContent();
float x = 36;
float y = 400;
float side = 70;
PdfShading axial = rotated ?
PdfShading.simpleAxial(writer, PageSize.A4.getRight() - y, x, PageSize.A4.getRight() - y, x + side, BaseColor.PINK, BaseColor.BLUE)
: PdfShading.simpleAxial(writer, x, y, x + side, y, BaseColor.PINK, BaseColor.BLUE);
PdfShadingPattern shading = new PdfShadingPattern(axial);
canvas.setShadingFill(shading);
canvas.moveTo(x,y);
canvas.lineTo(x + side, y);
canvas.lineTo(x + (side / 2), (float)(y + (side * Math.sin(Math.PI / 3))));
canvas.closePathFillStroke();
}
示例8: exportPdf
import com.itextpdf.text.pdf.PdfWriter; //導入方法依賴的package包/類
private static void exportPdf(OutputStream ostream, Collection<GridElement> entities, FontHandler diagramFont) throws IOException {
try {
FontMapper mapper = new PdfFontMapper();
Rectangle bounds = DrawPanel.getContentBounds(Config.getInstance().getPrintPadding(), entities);
com.itextpdf.text.Document document = new com.itextpdf.text.Document(new com.itextpdf.text.Rectangle(bounds.getWidth(), bounds.getHeight()));
PdfWriter writer = PdfWriter.getInstance(document, ostream);
document.open();
Graphics2D graphics2d = new PdfGraphics2D(writer.getDirectContent(), bounds.getWidth(), bounds.getHeight(), mapper);
// We shift the diagram to the upper left corner, so we shift it by (minX,minY) of the contextBounds
Dimension trans = new Dimension(bounds.getX(), bounds.getY());
graphics2d.translate(-trans.getWidth(), -trans.getHeight());
paintEntitiesIntoGraphics2D(graphics2d, entities, diagramFont);
graphics2d.dispose();
document.close();
} catch (com.itextpdf.text.DocumentException e) {
throw new IOException(e.getMessage());
}
}
示例9: renderGraph
import com.itextpdf.text.pdf.PdfWriter; //導入方法依賴的package包/類
@Override
public void renderGraph(JGraph<?> graph, File file) throws PortException {
// Get graph bounds. If not available, do nothing (probably empty graph)
Rectangle2D bounds = graph.getGraphBounds();
if (bounds == null) {
return;
}
Rectangle bound = new Rectangle((float) bounds.getWidth(), (float) bounds.getHeight());
try (FileOutputStream fos = new FileOutputStream(file)) {
Document document = new Document(bound);
// Open file, create PDF document
PdfWriter writer = PdfWriter.getInstance(document, fos);
// Set some metadata
document.addCreator(Version.getAbout());
// Open document, get graphics
document.open();
PdfContentByte cb = writer.getDirectContent();
boolean onlyShapes = true;
//The embedded fonts most likely do not contain all necessary glyphs, so using outlines instead
// onlyShapes makes PDF considerably bigger, but no alternative at the moment
PdfGraphics2D pdf2d =
new PdfGraphics2D(cb, (float) bounds.getWidth(), (float) bounds.getHeight(),
new DefaultFontMapper(), onlyShapes, false, (float) 100.0);
// Render
toGraphics(graph, pdf2d);
// Cleanup
pdf2d.dispose();
document.close();
} catch (DocumentException | IOException e) {
throw new PortException(e);
}
}
示例10: exportChartAsPDF
import com.itextpdf.text.pdf.PdfWriter; //導入方法依賴的package包/類
public static void exportChartAsPDF(
JFreeChart chart,
Rectangle bounds,
File file) throws IOException, DocumentException {
System.out.println(file.getPath());
PdfWriter writer = null;
com.itextpdf.text.Document document = new com.itextpdf.text.Document();
document.addCreator("CanReg5");
document.addCreationDate();
writer = PdfWriter.getInstance(document, new FileOutputStream(
file));
document.open();
PdfContentByte contentByte = writer.getDirectContent();
PdfTemplate template = contentByte.createTemplate(bounds.width, bounds.height);
Graphics2D graphics2d = template.createGraphics(bounds.width, bounds.height,
new DefaultFontMapper());
Rectangle2D rectangle2d = new Rectangle2D.Double(0, 0, bounds.width,
bounds.height);
chart.draw(graphics2d, rectangle2d);
graphics2d.dispose();
contentByte.addTemplate(template, 0, 0);
document.close();
}
示例11: render
import com.itextpdf.text.pdf.PdfWriter; //導入方法依賴的package包/類
public void render( PdfWriter writer, Document document ) throws PdfRenderException{
writer.setPageEvent(page);
if( page.getBlocks() == null || page.getBlocks().length == 0 ){
writer.setPageEmpty( false );
writer.newPage();
}else{
document.newPage();
PdfContentByte cb = writer.getDirectContent();
for( BaseElement block : page.getBlocks() ){
block.onRender( cb );
}
}
}
示例12: exportToPDF
import com.itextpdf.text.pdf.PdfWriter; //導入方法依賴的package包/類
/**
* Exports the 3d graph as a pdf
*
* @param file The pdf file to which the data should be written
* @param panel The chart panel that has to be exported
*/
public static void exportToPDF(File file, Chart3DPanel panel){
PdfWriter out = null;
Document document = new com.itextpdf.text.Document(PageSize.A4.rotate());
int width = 800, height = 500;
try{
out = PdfWriter.getInstance(document, new FileOutputStream(file));
document.open();
PdfContentByte contentByte = out.getDirectContent();
PdfTemplate template = contentByte.createTemplate(width, height);
@SuppressWarnings("deprecation")
Graphics2D graphics2d = template.createGraphics(width, height,new DefaultFontMapper());
Rectangle2D rectangle2d = new Rectangle2D.Double(0, 0, width,height);
panel.getChart().draw(graphics2d, rectangle2d);
graphics2d.dispose();
contentByte.addTemplate(template, 0, 0);
} catch(Exception e){
// in case any error occurs tell the user what the error is (sometimes useful if there is a problem of writing rights)
JOptionPane.showMessageDialog(GUIPrism.getGUI(), e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
return;
}
document.close();
}
示例13: start
import com.itextpdf.text.pdf.PdfWriter; //導入方法依賴的package包/類
public void start() {
try {
this.document = new Document(new Rectangle(this.bounds.getX() + this.bounds.getWidth(), this.bounds.getY() + this.bounds.getHeight()) );
PdfWriter writer = PdfWriter.getInstance(this.document, this.stream);
this.document.open();
this.cb = writer.getDirectContent();
} catch(Throwable e){
TGErrorManager.getInstance(this.context).handleError(e);
}
}
示例14: tiff2Pdf
import com.itextpdf.text.pdf.PdfWriter; //導入方法依賴的package包/類
public void tiff2Pdf(File singleTifFile, File pdfFile) {
// based on tiff2pdf from itext toolbox 0.0.2
// (cfr.http://itexttoolbox.sourceforge.net/doku.php?id=download&DokuWiki=
// ecde1bfec0b8cca87dd8c6c042183992)
try {
RandomAccessFileOrArray ra = new RandomAccessFileOrArray(
singleTifFile.getAbsolutePath());
// RandomAccessFileOrArray ra = new
// RandomAccessFileOrArray(tempByteArray);
int comps = TiffImage.getNumberOfPages(ra);
Document document = new Document(PageSize.A4);
float width = PageSize.A4.getWidth();
float height = PageSize.A4.getHeight();
Image img = TiffImage.getTiffImage(ra, 1);
document.setPageSize(PageSize.A4);
PdfWriter writer = PdfWriter.getInstance(document,
new FileOutputStream(pdfFile));
// pdf/a
// from
// http://www.opensubscriber.com/message/[email protected]
// .net/7593470.html
// check that it is really pdf/a:
// http://www.intarsys.de/produkte/pdf-a-live/pdf-a-check-1
// => 2 warnings
// Keine eindeutige ID gefunden
// Kein History-Eintrag vorhanden
writer.setPDFXConformance(PdfWriter.PDFA1B);
document.open();
PdfDictionary outi = new PdfDictionary(PdfName.OUTPUTINTENT);
outi.put(PdfName.OUTPUTCONDITIONIDENTIFIER, new PdfString(
"sRGB IEC61966-2.1"));
outi.put(PdfName.INFO, new PdfString("sRGB IEC61966-2.1"));
outi.put(PdfName.S, PdfName.GTS_PDFA1);
ICC_Profile icc = ICC_Profile.getInstance(Thread.currentThread()
.getContextClassLoader().getResourceAsStream(
"/srgb.profile"));
PdfICCBased ib = new PdfICCBased(icc);
ib.remove(PdfName.ALTERNATE);
outi.put(PdfName.DESTOUTPUTPROFILE, writer.addToBody(ib)
.getIndirectReference());
writer.getExtraCatalog().put(PdfName.OUTPUTINTENTS,
new PdfArray(outi));
PdfContentByte cb = writer.getDirectContent();
for (int c = 0; c < comps; ++c) {
img = TiffImage.getTiffImage(ra, c + 1);
if (img != null) {
document.setPageSize(PageSize.A4);
document.newPage();
img.setAbsolutePosition(0, 0);
img.scaleToFit(width, height);
cb.addImage(img);
logger.debug("Finished page " + (c + 1));
}
}
ra.close();
writer.createXmpMetadata();// pdfa
document.close();
} catch (Throwable e) {
// catch Throwable because we encountere a java.lang.InternalError
// cfr. http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6503430
// probably better to move to later java version for poller
logger.error("Pdf not created", e);
}
}
示例15: saveAsPDF
import com.itextpdf.text.pdf.PdfWriter; //導入方法依賴的package包/類
/**
* Stores the logo in PDF format
* @param width
* @param height
* @param fileName
*/
public void saveAsPDF(int width, int height, String fileName)
{
if (chart == null)
{
makeSequenceLogo();
}
PdfWriter writer = null;
Rectangle pagesize = new Rectangle(width, height);
Document document = new Document(pagesize);
try
{
writer = PdfWriter.getInstance(document, new FileOutputStream(fileName));
document.open();
PdfContentByte contentByte = writer.getDirectContent();
PdfTemplate template = contentByte.createTemplate(width, height);
Graphics2D graphics2d = template.createGraphics(width, height, new DefaultFontMapper());
Rectangle2D rectangle2d = new Rectangle2D.Double(0, 0, width, height);
chart.draw(graphics2d, rectangle2d);
graphics2d.dispose();
contentByte.addTemplate(template, 0, 0);
}
catch (Exception e)
{
e.printStackTrace();
}
document.close();
}