Paragraph in PDFbox - pdfbox

I have a requirement to change the PDF file using iText to PDFbox. I have following doubts:
How to generate a paragraph in PDF box? (new paragraph in iText)
How to give color for the font in PDFbox? (Font.BOLD, new Color(79, 129, 189)) in iText)
Can someone give me an advice how to solve those problems?

Not sure if you found the answer for this yet or not....
As far as I've heard, PDFBox doesn't know line breaks and you'll have to format the text and position it yourself with the moveTextPositionByAmount method.
This is how I write something and change the font and color:
PDFont font = PDType1Font.HELVETICA_BOLD;
PDPageContentStream contentStream =
new PDPageContentStream(document, page, true,true);
contentStream.beginText();
contentStream.setFont(font, size);
contentStream.setNonStrokingColor(Color.BLUE);
contentStream.moveTextPositionByAmount(x,y);
contentStream.drawString(message);
contentStream.endText();
contentStream.close();

Related

How to pass font name as string in pdf file with Java iText

I am generating pdf report with few inputs like font name, font size. I tried to create a font using below code.
Font font = new Font(FontFamily.TIMES_ROMAN,50.0f,Font.UNDERLINE,BaseColor.RED);
Here, how pass font name that is TIMES_ROMAN as a string?
Here's a quick way on how you can achieve the desired behavior with iText 7:
final PdfDocument pdfDocument = new PdfDocument(new PdfWriter(DEST));
PdfFont font = PdfFontFactory.createFont(FontProgramFactory.createFont(StandardFonts.TIMES_ROMAN));
Style myStyle = new Style()
.setFontSize(50)
.setUnderline()
.setFontColor(RED)
.setFont(font);
try (final Document document = new Document(pdfDocument)) {
document.add(new Paragraph("Hello World!").addStyle(myStyle));
document.add(new Paragraph("Hello World!").setFont(font)
.setFontSize(50)
.setUnderline()
.setFontColor(RED));
}
You can also define the font on a Document level (I'm showing Style and directly on the Paragraph).

PDFBox converting colorspace of existing pdf

Problem:
I have a existing pdf which have some images and text. This pdf has mixed color space RGB and CMYK. I want to convert it into single color-space pdf preferably into CMYK.
Using : PDFBox 2.0.4
PDFBox has PDPageContentStream which can used to change the add color-space when adding data to the existing pdf like below:
PDDocument document = PDDocument.load(myPdfWithMixColorSpace);
PDPage pdPage = document.getPage(0);
PDPageContentStream imageContentStream = new PDPageContentStream(document, page);
imageContentStream.setNonStrokingColor(0, 0, 0, 255); // for cmyk color space
imageContentStream.beginText();
imageContentStream.setFont(PDType1Font.TIMES_ROMAN, 20.15f);
imageContentStream.newLineAtOffset(13.55f, 50f);
imageContentStream.showText("this is test");
imageContentStream.endText();
imageContentStream.close();
document.save("D:\\newPDF.pdf");
document.close();
newPDF.pdf has the text added to it in CMYK color, But I don't want to add text or image in CMYK color on the existing pdf, but to convert the color-space of all the content of the input pdf itself.
In summary what I want is:
Given a pdf having mix color space for image and text in it.
Then using pdfbox
Create a new pdf or update the existing pdf with cmyk color space for all its content.

How to use font in other PDF files? (itext7 PDF)

I am now trying to modify a PDF file with ONLY text content. When I use
TextRenderInfo.getFont()
it returns me a Font which is actually an indirect object.
pdf.inderect.object.belong.to.other.pdf.document.Copy.object.to.current.pdf.document
would be thrown in this case when close the PdfDocument.
Is there a way to let me reuse this Font in a new PDF file? OR, is there a way to in-place edit the text content in PDF (without changing the font, color, fontSize)?
I'm using itext7.
Thanks
First of all, from the error message I see that you are not using the latest version of iText, which is 7.0.2 at the moment. So I recommend that you update your iText version.
Secondly, it is indeed possible to use a font in another document. But to do that, you first have to copy the corresponding font object to that other document (as stated in the exception message by the way). But you should be warned that this approach has some limitations, e.g. in case of a font subset, you will only be able to use the glyphs that are present in the original font subset in the source document and will not be able to use other glyphs.
PdfFont font = textRenderInfo.getFont(); // font from source document
PdfDocument newPdfDoc = ... // new PdfDocument you want to write some text to
// copy the font dictionary to the new document
PdfDictionary fontCopy = font.getPdfObject().copyTo(newPdfDoc);
// create a PdfFont instance corresponding to the font in the new document
PdfFont newFont = PdfFontFactory.createFont(fontCopy);
// Use newFont in newPdfDoc, e.g.:
Document doc = new Document(newPdfDoc);
doc.add(new Paragraph("Hello").setFont(newFont));

iText embedded ttf font not visible in Adobe Reader

I'm stamping an existing PDF file with extra information using the iText library.
The extra information is text that should be rendered in a custom TTF font.
Problem is that the text is not visible in the Adobe Reader only.
Other PDF viewers, such as the default eVince reader in Ubuntu and the Google online PDF reader render the stamped text in the custom embedded font just fine.
I tried multiple encodings, such as Cp1251, BaseFont.Identity_H, ...
The code where the magic happens:
PdfReader pdfReader = new PdfReader(new FileInputStream(inputPdf));
PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileOutputStream("stamped.pdf"));
PdfContentByte canvas = pdfStamper.getOverContent(1);
String text = "The stamp";
BaseFont bf = BaseFont.createFont("assign.ttf", "Cp1251",BaseFont.EMBEDDED);
canvas.beginText();
canvas.setColorFill(BaseColor.BLUE);
canvas.setFontAndSize(bf, 13);
canvas.moveText(310, 600);
canvas.showText(text);
pdfStamper.close();
You have a syntax problem. Text state in PDF is marked with BT and ET. These operators are added using the beginText() and endText() methods. You have a BT, but no ET. Adobe Reader is more strict than the other viewers (that's why I prefer Adobe Reader over all other viewers: people should respect the syntax when writing code).
Add the following line before pdfStamper.close();
canvas.endText();
Better yet, read my book and you'll find out you can reduce the complexity of your code by using ColumnText.showTextAligned().

How do I figure out the font family and the font size of the words in a pdf document?

How do I figure out the font family and the font size of the words in a pdf document? We are actually trying to generate a pdf document programmatically using iText, but we are not sure how to find out the font family and the font size of the original document which needs to be generated. document properties doesn't seem to contain this information
Fonts are stored in the catalog (I suppose in a sub-catalog of type font). If you open a pdf as a text file, you should be able to find catalog entries (they begin and end with "<<" and ">>" respectively.
On a simple pdf file, i found the following:
<</Type/Font/BaseFont/Helvetica-Bold/Subtype/Type1/Encoding/WinAnsiEncoding>>
thus searching for the prefix should help you (in some pdf files, there are spaces between
the commponents but '/Type /Font' should be ok).
Of course this is a manual process, while you would probably prefer an automatic one.
On another note, we sometime use identifont or what the font to find uncommon fonts that give us problem (logo font).
regards
Guillaume
Edit : the following code will find all font in the pages. To be short, you search the dictionnary of each page for the subdictionnary "ressource" and then the subdictionnary "font". Each entry in the later is a font dictionnary, describing a font.
PdfReader reader = new PdfReader(
new FileInputStream(new File("file.pdf")));
int nbmax = reader.getNumberOfPages();
System.out.println("nb pages " + nbmax);
for (int i = 1; i <= nbmax; i++) {
System.out.println("----------------------------------------");
System.out.println("Page " + i);
PdfDictionary dico = reader.getPageN(i);
PdfDictionary ressource = dico.getAsDict(PdfName.RESOURCES);
PdfDictionary font = ressource.getAsDict(PdfName.FONT);
// we got the page fonts
Set keys = font.getKeys();
Iterator it = keys.iterator();
while (it.hasNext()) {
PdfName name = (PdfName) it.next();
PdfDictionary fontdict = font.getAsDict(name);
PdfObject typeFont = fontdict.getDirectObject(PdfName.SUBTYPE);
PdfObject baseFont = fontdict.getDirectObject(PdfName.BASEFONT);
System.out.println(baseFont.toString());
}
}
The name (variable "name" in the following code) is what is used in the text to change font. In the PDF, you'll have to find it next to a text. The following number is the size. Here for example, it's size 12. (sorry, still no code for this part).
BT
/F13 12 Tf
288 720 Td
the text to find Tj
ET
Depending on the PDF, if it hasn't been outlined you may be able to open it in Adobe Illustrator, double click the text and select some of it to see it's font family, size, etc.
If the text is outlined then use one of those online tools that PATRY suggests to find out the font.
Good luck
If you have Adobe Acrobat you can see the fonts inside and examine the objects and text streams. I wrote a blog post on this at http://pdf.jpedal.org/java-pdf-blog/bid/10479/Viewing-PDF-objects