Create PDPage with right format using PDFBox - pdf

I'm trying to write a simple method that creates a new PDDocument and adds PDPage with the right page format as supplied by user inputs.
Input from User can be one of the following:
"A4-Portrait"
"A4-Landscape"
"A3-Portrait"
"A3-Landscape"
Expected output:
If input is "A3-Landscape" , return PDPage with this format.
while I can see ways to create PDPage in A3 format using
PDPage page = new PDPage(PDRectangle.A3);
Can you help how efficiently and easily can I choose the right format and its orienatation dynamically?

You can do it like this:
PDRectangle A4L = new PDRectangle(PDRectangle.A4.getHeight(), PDRectangle.A4.getWidth());

Related

Data in text is not proper format after converting PDF to text

I have 1 PDF file. I need to fetch key values.
I am able to convert PDF to text by using pdfBox and itext api's but text is not in proper format. I need to get the values from the text and have to do some calculation.
Below is the piece of code:
PDFTextStripperByArea stripper = new PDFTextStripperByArea();
stripper.setSortByPosition(true);
PDFTextStripper pstripper = new PDFTextStripper();
String text = pstripper.getText(pddoc);
String lines[] = text.split("\\r?\\n");
for (String line : lines) {
System.out.println(line);
}
pddoc.close();
Could you please help me on this?
Thanks.

iText to Aspose PDF Conversion. What to do for footer conversion?

Our original generation library is using PdfPageEventHelper in iText to render the page footer with something like page x of y.
How would I go about doing the same thing using Aspose's PDF library instead?
You can add text stamp in PDF documents using Aspose.PDF for .NET. Also, it allows you to specify X,Y position for the stamp. Following code snippet can be used to add the Text Stamp in PDF:
// Open document
Document pdfDocument = new Document("AddTextStamp.pdf");
// Create text stamp
TextStamp textStamp = new TextStamp("Sample Stamp");
// Set whether stamp is background
textStamp.Background = true;
// Set origin
textStamp.XIndent = 100;
textStamp.YIndent = 100;
// Rotate stamp
textStamp.Rotate = Rotation.on90;
// Set text properties
textStamp.TextState.Font = FontRepository.FindFont("Arial");
textStamp.TextState.FontSize = 14.0F;
textStamp.TextState.FontStyle = FontStyles.Bold;
textStamp.TextState.FontStyle = FontStyles.Italic;
textStamp.TextState.ForegroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Aqua);
// Add stamp to particular page
pdfDocument.Pages[1].AddStamp(textStamp);
// Save output document
pdfDocument.Save("AddTextStamp_out.pdf");
PS: This is Asad Ali and I work as Developer Evangelist at Aspose.

Generate PDF from gsp page

I am using grails 2.5.2.
I have created a table which shows all the data from database to gsp page and now i need to save that shown data in a pdf format with a button click.What will be the best way to show them into a PDF and save it to my directory. please Help
You can use itext for converting HTML into pdf using the code below:
public void createPdf(HttpServletResponse response, String args, String css, String pdfTitle) {
response.setContentType("application/force-download")
response.setHeader("Content-Disposition", "attachment;filename=${pdfTitle}.pdf")
Document document = new Document()
Rectangle one = new Rectangle(900, 600)
document.setPageSize(one)
PdfWriter writer = PdfWriter.getInstance(document, response.getOutputStream())
document.open()
ByteArrayInputStream bis = new ByteArrayInputStream(args.toString().getBytes())
ByteArrayInputStream cis = new ByteArrayInputStream(css.toString().getBytes())
XMLWorkerHelper.getInstance().parseXHtml(writer, document, bis, cis)
document.close()
}
Though answering this question late,take a look at grails export plugin.It will be useful if you want to export your data to excel and pdf( useful only if there is no in pre-defined template to export).
Got idea from itext. Used itext 2.1.7 and posted all the values to pdf from a controller method. Used images as background and paragraph and phrase to show values from database.

PDFBOX filling document from my html fields

I've got a problem with PDFBOX.
So i created a simple code:
public class DelegationsPdf{
public Delegations getPDF(Delegations delegations) throws IOException{
PDDocument delegation = new PDDocument();
PDDocumentCatalog catalog = delegation.getDocumentCatalog();
PDAcroForm pdAcroForm = catalog.getAcroForm();
PDPage page = new PDPage();
delegation.addPage(page);
page = delegation.getPage(0);
PDPageContentStream content = new PDPageContentStream(delegation, page);
content.beginText();
content.setFont(PDType1Font.TIMES_ROMAN, 20);
content.setLeading(14.5f);
content.newLineAtOffset(275, 750);
content.showText("Delegations");
content.endText();
content.beginText();
content.newLineAtOffset(50 ,650);
content.setFont(PDType1Font.TIMES_ROMAN, 12);
content.showText("Worker:");
content.endText();
// FILL THE FIELD
content.beginText();
PDField name = pdAcroForm.getField(delegations.getName());
content.newLineAtOffset(100 ,650);
content.setFont(PDType1Font.TIMES_ROMAN, 12);
pdAcroForm.getFields().add(name);
content.endText();
content.close();
delegation.save("C:/delegation.pdf");
delegation.close();
return delegations;
}
So please tell my why the code under comment field "/fill the field" doesnt work properly? It gets information about name from my delegations class. Can you help me? When i run this and click button, I've got error with NULL.
So, finally, problem solved with your help.
Firstly, I had to know that when I "download" my value which I want to fill from HTML form, I need to create a field (form) in pdfbox and fill it then.
Secondly, I need to use PDType0Font.load() for special characters (UTF 8).
Thanks

Text is reverse in generated pdf

I am using pdfbox to add a line to pdf file. but the text i am adding is reversed.
File file = new File(filePath);
PDDocument document = PDDocument.load(file);
PDPage page = document.getPage(0);
PDPageContentStream contentStream = new PDPageContentStream(document, page,PDPageContentStream.AppendMode.APPEND,true);
int stampFontSize = grailsApplication.config.pdfStamp.stampFontSize ? grailsApplication.config.pdfStamp.stampFontSize : 20
contentStream.beginText();
contentStream.setFont(PDType1Font.TIMES_ROMAN, stampFontSize);
int leftOffset = grailsApplication.config.pdfStamp.leftOffset ? grailsApplication.config.pdfStamp.leftOffset : 10
int bottomOffset = grailsApplication.config.pdfStamp.bottomOffset ? grailsApplication.config.pdfStamp.bottomOffset : 20
contentStream.moveTextPositionByAmount(grailsApplication.config.xMove,grailsApplication.config.yMove)
contentStream.newLineAtOffset(leftOffset, bottomOffset)
String text = "i have added this line...!!!!";
contentStream.showText(text);
contentStream.endText();
contentStream.close();
document.save(new File(filePath));
document.close();
byte[] pdfData;
pdfData = Files.readAllBytes(file.toPath());
return pdfData;
i tried using moveTextPositionByAmount method but this does not seem to have any effect on text. why is my text reversed and how can i set it to correct orientation.
Your code is not causing the mirrored output by itself, so the cause must be inside the PDF you are stamping. Unfortunately you did not provide the PDF in question, so we have to guess here.
Most likely the issue is caused by the pre-existing page content having set the current transformation matrix to a mirroring affine transformation without resetting it at the end.
If that indeed is the case, PDFBox provides an easy work-around:
You construct your PDPageContentStream like this:
PDPageContentStream contentStream = new PDPageContentStream(document, page,PDPageContentStream.AppendMode.APPEND,true);
There is another constructor accepting an additional boolean argument. If you use that constructor setting the additional argument to true, PDFBox attempts to reset the graphics state of the content:
PDPageContentStream contentStream = new PDPageContentStream(document, page,PDPageContentStream.AppendMode.APPEND,true,true);
Beware: If this indeed fixes the issue, the coordinates and offsets you currently use rely on the transformation matrix being changed as it is. In that case you will have to update them accordingly.
Alternatively introducing a counter-mirroring may help, e.g. by setting the text matrix like this at the start of each of your text objects:
contentStream.beginText();
contentStream.setTextMatrix(new Matrix(1f, 0f, 0f, -1f, 0f, 0f));
Thereafter all y coordinate changes need to be negated, in particular the second argument of contentStream.moveTextPositionByAmount and contentStream.newLineAtOffset.
(By the way, moveTextPositionByAmount and newLineAtOffset do the same, the former merely is the deprecated variant, so you might want to use the latter in both cases.)