PDFBox - Setting Cover Sheet for PDF Portfolio - java

I am creating a PDF portfolio from scratch using PDFBox 2.0.16. I create the PDF and add attachments. Once the attachments are added, I then create the PDF Portfolio. The following is the code to create the PDF Portfolio/Package.
COSDictionary collectionDictionary = new COSDictionary();
collectionDictionary.setName( COSName.TYPE, "Collection" );
collectionDictionary.setName( "PDF Filename", "T" );
PDDocumentCatalog catalog = pdfDoc.getDocumentCatalog();
catalog.getCOSObject().setItem( "Collection", collectionDictionary );
pdfDoc.save( pdfFile );
When I open the PDF, the Cover Sheet is a blank page. I would like the Cover Sheet to be the first attachment in the PDF Portfolio/Package so that when a user opens the PDF, the 1st attachment in the portfolio is viewed.

What somewhat worked for me (it positioned to this file, as if I had clicked on it in the list) is this:
collectionDictionary.setString(COSName.D, "Attachment 1");
where "Attachment 1" is the name used in the name tree in this example.

Related

I uploaded a pdf file using sendkeys, deleted that file and again uploaded a different pdf. While doing so, it is taking uploading 1st pdf not 2nd pdf

I uploaded a pdf file using sendkeys, deleted that file and again uploaded different pdf. While doing so, it is taking uploading 1st pdf not 2nd pdf.
Here is my code:
public void deWebsitePDFUpload() {
Actions actions = new Actions(driver);
actions.moveToElement(dewebsitePDFUploadAccordian).click().build().perform();
dewebsitePDFUploadIcon.sendKeys(
"C:\\Users\\user123\\eclipse-workspace\\test\\sample-files\\Design_Asklepios_20150529-V02.pdf");
uploadTitle.sendKeys("Uploading German website PDF");
uploadSaveBtn.click();
Actions actions1 = new Actions(driver);
actions1.moveToElement(dewebsitePDFUploadAccordian).click().build().perform();
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("(//a[#class='btn icon'])[2]"))).click();
System.out.println("delted");
dewebsitePDFUploadIcon.sendKeys(
"C:\\Users\\user123\\eclipse-workspace\\test\\sample-files\\Reuters_Branchen_en.pdf");
uploadTitle.sendKeys("Uploading German 2nd website PDF");
uploadSaveBtn.click();
}

Add HTML Markup using java Apache PDFBOX

I have been using PDFBOX and EasyTable which extends PDFBOX to draw datatables. I have hit a problem whereby I have a java object with a string of HTML data that I need to be added to the PDF using PDFBOX. A dig at the documentation seems not to bear any fruits.
The code below is a snippet hello world, which I want on the pdf been generated to have H1 formatting.
// Create a document and add a page to it
PDDocument document = new PDDocument();
PDPage page = new PDPage();
document.addPage( page );
// Create a new font object selecting one of the PDF base fonts
PDFont font = PDType1Font.HELVETICA_BOLD;
// Start a new content stream which will "hold" the to be created content
PDPageContentStream contentStream = new PDPageContentStream(document, page);
// Define a text content stream using the selected font, moving the cursor and drawing the text "Hello World"
contentStream.beginText();
contentStream.setFont( font, 12 );
contentStream.moveTextPositionByAmount( 100, 700 );
contentStream.drawString( "<h1>HelloWorld</h1>" );
contentStream.endText();
// Make sure that the content stream is closed:
contentStream.close();
// Save the results and ensure that the document is properly closed:
document.save( "Hello World.pdf");
document.close();
}
Use jerico to format the html to free text while mapping correctly the output of tags.
sample
public String extractAllText(String htmlText){
return new net.htmlparser.jericho
.Source(htmlText)
.getRenderer()
.setMaxLineLength(Integer.MAX_VALUE)
.setNewLine(null)
.toString();
}
Include on your gradle or maven:
compile group: 'net.htmlparser.jericho', name: 'jericho-html', version: '3.4'
PDFBox does not know HTML, at least not for creating content.
Thus, with plain PDFBox you have to parse the HTML yourself and derive special text drawing characteristics from the tags text is in.
E.g. when you encounter "<h1>HelloWorld</h1>", you have to extract the text "HelloWorld" and use the information that it is in a h1 tag to select an appropriate prime header font and font size to draw that "HelloWorld".
Alternatively you can look for a library doing that HTML parsing and transforming to PDF text drawing instructions for PDFBox, e.g. Open HTML to PDF.

Content invisible using Itext in Java

I'm using the iText library with Java and I have an issue, let me explain:
I have a Word file converted into a PDF and i want to add a table at the end of the document, this works perfectly except when the Word file hasn’t active the feature "No Color" at the tab "Page Layout", the table adds it as "invisible".
I've already tried to change the property Font from the Chunk object that created the content, but it does not work, the table continues showing as "invisible".
How I can solve this
The following code is the one that I use to create the table:
Font font = new Font(1,fontSize, Font.BOLD);
font.setColor(0,0,0);
Chunk name = new Chunk(v_emp.getNombre(),font);
Chunk tipo = new Chunk(v_emp.getDireccion(),font);
Paragraph paragraph = new Paragraph();
paragraph.add(tipo);
paragraph.add(new Chunk("\n"));
paragraph.add(name);
PdfPCell cell1 = new PdfPCell(paragraph);
cell1.setPadding(2);
table.addCell(cell1);
Try selecting the table in PDF with your mouse and see if the table is there and still it is invisible.
If it is there, go to the file menu and do print as pdf.

How I use a custom font with the Batik SVG library?

I'm working on a Java program which creates templates for clothes. The user enters the word they want to see on the item of clothing and the system creates a PDF template. To create the template I create an SVG document programatically then use Batik to transcode the SVG to the PDF format.
My client now wants to be able to use custom fonts to create the templates. I was wondering if it's possible to use fonts like a TTF with the Batik transcoder? If so how do you go about setting up the SVG?
First you need to transform the font file from TTF to SVG with batik's ttf2svg, once you have the converted file you have to add reference in the 'defs' section of your SVG document.
this is how i did it:
Element defs = doc.createElementNS(svgNS, "defs");
Element fontface = doc.createElementNS(svgNS, "font-face");
fontface.setAttributeNS(null, "font-family", "DroidSansRegular");
Element fontfacesrc = doc.createElementNS(svgNS, "font-face-src");
Element fontfaceuri = doc.createElementNS(svgNS, "font-face-uri");
fontfaceuri.setAttributeNS(svgNS, "xlink:href", "fonts/DroidSans-webfont.svg#DroidSansRegular");
Element fontfaceformat = doc.createElementNS(svgNS, "font-face-format");
fontfaceformat.setAttributeNS(svgNS, "string", "svg");
fontfaceuri.appendChild(fontfaceformat);
fontfacesrc.appendChild(fontfaceuri);
fontface.appendChild(fontfacesrc);
defs.appendChild(fontface);
svgRoot.appendChild(defs);
when creating the text element set the font family just like any other font
Element txtElem = doc.createElementNS(svgNS, "text");
txtElem.setAttributeNS(svgNS, "style", "font-family:DroidSansRegular;font-size:" + fontsize + ";stroke:#000000;#fill:#00ff00;");
txtElem.setTextContent("some text");
svgRoot.appendChild(txtElem);
i got the info from this article: http://frabru.de/c.php/article/SVGFonts-usage hope it helps.
Just add the following element as a child of <svg/>: <style type="text/css"> Then, similar to HTML...

JasperReports, export to docx - footer formatting

I am new to JasperReports.
How can I make word document more user friendly with JasperReports?
I create simple document, and convert in docx.
Footer still not have absolute position. If you focus in content and insert new column above/below,
the footer will move to the next page.
How to create static/absolute footer in JR report? (.docx extension report)
Jasper Reports uses tables to render docx format, if you select all your document and enable all cells boundaries you'll note each page is a table.
http://www.smartjava.org/content/create-complex-word-docx-documents-programatically-docx4j
Probably you'll need to read an excel file and fill into a docx with programatically with docx4j library in order to mantain header and footer in a word template.

Categories