How to draw graphics to PDF using iText?


jimdrang :

I'm trying to complete an example that draws a graph and writes it to a PDF, but I keep getting the error that the PDF has no pages. If I add something simple with document.add() after opening it works fine, I never see the graph. Here is my code:

Document document = new Document();
PdfWriter writer = new PdfWriter();
response.setContentType("application/pdf");
response.setHeader("Content-Disposition",
    " attachment; filename=\"Design.pdf\"");

writer = PdfWriter.getInstance(document, response.getOutputStream());

document.open();    
PdfContentByte cb = writer.getDirectContent();
Graphics2D graphics2D = cb.createGraphics(36, 54);
graphics2D.drawString("Hello World", 36, 54);
graphics2D.dispose();   
document.close();

Do I need to do something else to add the graphic to the document, or am I using the syntax incorrectly?

subject:

Is there Document doc = new Document(PageSize.A4);any difference?

I don't know if you need to add Paragraphsomething like this:

doc.add(new Paragraph(...));

We also use doc.add(ImgRaw);add images.

Related


How to draw graphics to PDF using iText?

jimdrang : I'm trying to complete an example that draws a graph and writes it to a PDF, but I keep getting the error that the PDF has no pages. If I add something simple with document.add() after opening it works fine, I never see the graph. Here is my code: D

How to draw graphics to PDF using iText?

jimdrang : I'm trying to complete an example that draws a graph and writes it to a PDF, but I keep getting the error that the PDF has no pages. If I add something simple with document.add() after opening it works fine, I never see the graph. Here is my code: D

How to draw path on pdf canvas using itext pdf?

User 123456 I am implementing a pdf editing application using itextpdf. I added a text to the pdf and used the canvas.drawPath() method to draw a name on the android system canvas like I want to draw a name on the pdfCanvas? I tried some examples of drawing cu

How to draw path on pdf canvas using itext pdf?

User 123456 I am implementing a pdf editing application using itextpdf. I added a text to the pdf and used the canvas.drawPath() method to draw a name on the android system canvas, just like I want to draw a name on the pdfCanvas? I tried some examples of draw

Draw a rectangle in a PDF document using iText

Milhous: Is it possible to draw rectangles in PDF documents in iText? Milhous: Here is the solution. Thanks Dylan McClung. PdfWriter writer = ...; PdfContentByte cb = writer.getDirectContent(); cb.saveState(); cb.setColorStroke(Color.black); cb.rectangle(x,y,x

Draw a rectangle in a PDF document using iText

Milhous: Is it possible to draw rectangles in PDF documents in iText? Milhous: Here is the solution. Thanks Dylan McClung. PdfWriter writer = ...; PdfContentByte cb = writer.getDirectContent(); cb.saveState(); cb.setColorStroke(Color.black); cb.rectangle(x,y,x

Draw a rectangle in a PDF document using iText

Milhous: Is it possible to draw rectangles in PDF documents in iText? Milhous: Here is the solution. Thanks Dylan McClung. PdfWriter writer = ...; PdfContentByte cb = writer.getDirectContent(); cb.saveState(); cb.setColorStroke(Color.black); cb.rectangle(x,y,x

itext pdf, how to draw a string to a specific position?

Rogers I am trying to output some strings to PDF using Itext. Currently, I can print them to the document, but I want to draw/write them to a specific (x,y) in the document. How will I deal with it? Or are there any tutorials or resources that can help you? An

Vector Graphics in iText PDF

Marcus Downing: We use iText to generate PDFs from Java (based in part on suggestions from this site). However, embedding a copy of the logo in an image format like GIF can cause people to look a little weird as they zoom in and out. Ideally, we would like to

Vector Graphics in iText PDF

Marcus Downing: We use iText to generate PDFs from Java (based in part on suggestions from this site). However, embedding a copy of the logo in an image format like GIF can cause people to look a little weird as they zoom in and out. Ideally, we would like to

Vector Graphics in iText PDF

Marcus Downing: We use iText to generate PDFs from Java (based in part on suggestions from this site). However, embedding a copy of the logo in an image format like GIF can cause people to look a little weird as they zoom in and out. Ideally, we would like to

Vector Graphics in iText PDF

Marcus Downing: We use iText to generate PDFs from Java (based in part on suggestions from this site). However, embedding a copy of the logo in an image format like GIF can cause people to look a little weird as they zoom in and out. Ideally, we would like to

How to draw on JPanel using graphics

Room: I'm working on a project and I've made a menu and a menu, JPanelbut now I want to draw that menu and JPaneladd some HUD to the game. I'm providing a picture for the final panel of the game, I want to add a blue rectangle with the player's avatar on the l

How to draw on JPanel using graphics

Room: I'm working on a project and I've made a menu and a menu, JPanelbut now I want to draw that menu and JPaneladd some HUD to the game. I'm providing a picture for the final panel of the game, I want to add a blue rectangle with the player's avatar on the l

How to draw on JPanel using graphics

Room: I'm working on a project and I've made a menu and a menu, JPanelbut now I want to draw that menu and JPaneladd some HUD to the game. I'm providing a picture for the final panel of the game, I want to add a blue rectangle with the player's avatar on the l

itext7 - How to draw horizontal dashed lines in PDF?

suushiemaniac: I am using the community version of itext7 (version 7.1.9) . I want to create a PDF document in which I insert paragraphs of text and horizontal separators like this: some interesting text ----------- more interesting text ----------- still inte

itext7 - How to draw horizontal dashed lines in PDF?

suushiemaniac: I am using the community version of itext7 (version 7.1.9) . I want to create a PDF document in which I insert paragraphs of text and horizontal separators like this: some interesting text ----------- more interesting text ----------- still inte

itext7 - How to draw horizontal dashed lines in PDF?

suushiemaniac: I am using the community version of itext7 (version 7.1.9) . I want to create a PDF document in which I insert paragraphs of text and horizontal separators like this: some interesting text ----------- more interesting text ----------- still inte

itext7 - How to draw horizontal dashed lines in PDF?

suushiemaniac: I am using the community version of itext7 (version 7.1.9) . I want to create a PDF document in which I insert paragraphs of text and horizontal separators like this: some interesting text ----------- more interesting text ----------- still inte

itext7 - How to draw horizontal dashed lines in PDF?

suushiemaniac: I am using the community version of itext7 (version 7.1.9) . I want to create a PDF document in which I insert paragraphs of text and horizontal separators like this: some interesting text ----------- more interesting text ----------- still inte

itext7 - How to draw horizontal dashed lines in PDF?

suushiemaniac: I am using the community version of itext7 (version 7.1.9) . I want to create a PDF document in which I insert paragraphs of text and horizontal separators like this: some interesting text ----------- more interesting text ----------- still inte

How to draw graphics when using the CubicSpline function

math I have two arrays, xand y. I want to create natural cubic splines for arrays. I don't know how to draw the spline exactly. import numpy as np from scipy.interpolate import CubicSpline # Calculate 5 natural cubic spline polynomials for 6 points. # (x,y) =

How to draw graphics when using the CubicSpline function

math I have two arrays, xand y. I want to create natural cubic splines for arrays. I don't know how to draw the spline exactly. import numpy as np from scipy.interpolate import CubicSpline # Calculate 5 natural cubic spline polynomials for 6 points. # (x,y) =

How to draw svg graphics using painter class

Argent I am trying to learn how to use the drawing function. So far I have been able to draw some circles on the QWidget using the QPainter class like this: In MainWindow.hi add: virtual void paintEvents(QPaintEvent *event); Then in MainWindow.cpp I add: vo

How to draw graphics when using the CubicSpline function

math I have two arrays, xand y. I want to create natural cubic splines for arrays. I don't know how to draw the spline exactly. import numpy as np from scipy.interpolate import CubicSpline # Calculate 5 natural cubic spline polynomials for 6 points. # (x,y) =

How to draw graphics when using the CubicSpline function

math I have two arrays, xand y. I want to create natural cubic splines for arrays. I don't know how to draw the spline exactly. import numpy as np from scipy.interpolate import CubicSpline # Calculate 5 natural cubic spline polynomials for 6 points. # (x,y) =