There are two PDF-documents - A.pdf and B.pdf. They have the same page size.
I add an object Phrase to the page in both documents.
Stamper_A:=New PdfStamper(reader_A,
New System.IO.FileStream(
out_file1,
System.IO.FileMode.CreateNew));
ContentByte_A:=Stamper_A.GetOverContent(2);
ColumnText.ShowTextAligned(
ContentByte_A,
Element.ALIGN_LEFT,
phrase,
75,
680,
0);
Stamper_B:=New PdfStamper(reader_B, New System.IO.FileStream(out_file2, System.IO.FileMode.CreateNew));
ContentByte_A:=Stamper_B.GetOverContent(2);
ColumnText.ShowTextAligned(ContentByte_B, Element.ALIGN_LEFT, phrase, 75, 680,0);
But in the document out_file1 Phrase is higher than in the document out_file2.
Why is this happening?
A.pdf and B.pdf have either a different MediaBox and/or a different CropBox.
Related
I need to change the paper orientation, and then divide the text into two columns. I want like it:
I come from a large text file and I need to add all of this text in the PDF as in the photo.
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(DEST));
PageSize ps = PageSize.A4;;
Document doc = new Document(pdfDoc, ps);
BufferedReader br = new BufferedReader(new FileReader("verybigfileWithText.txt"));
while ((line = br.readLine()) != null) {
//split pdf and add text in two column without overlapping page
}
doc.close();
And I need to be able to change margin in center(between two columns) and margin top/right/bot/left. And get width every columns :)
Help me please
Pdf:
https://dropmefiles.com/HqD6f
To change the orientation of the page you can use PageSize#rotate, e.g. PageSize.A4.rotate()
To put content into two (or more) columns, you can create your own document renderer, or use an existing ColumnDocumentRenderer which suits your needs. It accepts column areas which allows you to control margins (or even position columns in a peculiar way which is probably not your use case):
Document document = new Document(pdfDocument, PageSize.A4.rotate());
Rectangle[] columnAreas = new Rectangle[] {new Rectangle(30, 30, 350, 520), new Rectangle(430, 30, 350, 520)};
ColumnDocumentRenderer renderer = new ColumnDocumentRenderer(document, columnAreas);
document.setRenderer(renderer);
document.add(new Paragraph(text).setTextAlignment(TextAlignment.JUSTIFIED));
document.close();
The result looks like this:
In iText 2 , we can use PdfContentByte to set customized page-size, however, in iText 7.1.2.
PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
PdfPage page = pdf.addNewPage();
PdfCanvas pdfCanvas = new PdfCanvas(page);
Rectangle rectangle = new Rectangle(0, 0, 2000, 800);
pdfCanvas.rectangle(rectangle);
pdfCanvas.stroke();
Canvas canvas = new Canvas(pdfCanvas, pdf, rectangle);
PdfFont font = PdfFontFactory.createFont(FontConstants.TIMES_ROMAN);
PdfFont bold = PdfFontFactory.createFont(FontConstants.TIMES_BOLD);
Text title =
new Text("The Strange Case of Dr. Jekyll and Mr. Hyde").setFont(bold);
Text author = new Text("Robert Louis Stevenson").setFont(font);
Paragraph p = new Paragraph().add(title).add(" by ").add(author);
canvas.add(p);
canvas.close();
pdf.close();
even if we set larger width, it didn't work. still keep A4 size. How can i change the pageSize correctly ?
You are adding a new page without specifying a page size, hence the default page size (A4) is used. Please take a look at the API docs for the addPage() method: addNewPage(PageSize pageSize). You need to pass a PageSize argument if you want to get a page with another size.
There's also a setDefaultPageSize() method if you want to change the default page size from A4 to something else.
The PageSize class extends the Rectangle class: http://itextsupport.com/apidocs/iText7/latest/com/itextpdf/kernel/geom/PageSize.html
What would be the best method to DRY out or chain this code using Coffeescript?
canvas=$('canvas')
canvas.getContext('2d').fillStyle='rgba(255,255,255,0.7)'
canvas.getContext('2d').font = "40px Verdana"
canvas.getContext('2d').fillText('alkatsa.com',canvas.width/2 - 120,canvas.height - 50)
I believe there’s no chaining of properties, only of methods. I’d make variables for context, width and height. This would make code more vertical:
canvas = $ 'canvas'
{width, height} = canvas
context = canvas.getContext '2d'
context.fillStyle = 'rgba(255, 255, 255, 0.7)'
context.font = '40px Verdana'
context.fillText 'example.com', width / 2 - 120, height - 50
Using iTextSharp how can I modify the display text of hyperlink in a pdf document. I can change the annotation link, but my target is to change the displaying text of the hyperlink not the actual web link. Is it possible?
I don't want to add a new anchor, I just want to modify the existing anchor with a different text, not changing existing web link.
Thanks
use this,
using (FileStream msReport = new FileStream(Server.MapPath("~") + "/App_Data/" + DateTime.Now.Ticks + ".pdf", FileMode.Create))
{
Document doc = new Document(PageSize.LETTER, 10, 10, 20, 10);
PdfWriter pdfWriter = PdfWriter.GetInstance(doc, msReport);
doc.Open();
Font link = FontFactory.GetFont("Arial", 12, Font.UNDERLINE, BaseColor.BLUE);
Anchor anchor = new Anchor("google", link);
anchor.Reference = "https://www.google.co.in";
doc.Add(anchor);
doc.Close();
}
Hi All Iam using itextpdf-5.1.0.jar, displaying Table(the values in this table are fetched from the database based on start date and end date in the input screen) and bar chart for the values in the above table.
The problem is I want to position the bar chart below the table, values that iam fetching from the database may vary depending upon start date and end date, now Iam positioning table and bar chart by giving some static values(234,567) , if there are large no of values table values and bar chart are getting overrided. Is there any other way to position table and bar chart dynamically.
Document document = new Document(PageSize.A4_LANDSCAPE, 10, 10, 10, 10);
document.open();
document.add(new Paragraph("Batch Report:", FontFactory.getFont(FontFactory.COURIER, 10, Font.BOLD, new CMYKColor(255, 255, 255, 255))));
Paragraph paragraph1 = new Paragraph();
paragraph1.setSpacingBefore(4);
document.add(paragraph1);
PdfPTable table = new PdfPTable(5);
table.setWidthPercentage(100);
PdfPCell c1;
for (BoardBean bean : listHeader) {
addColumn(bean.getID(),c1,table,myColor,btableheadercolor);
}
Add Column values to table
private void addColumn(String text,PdfPCell c1,PdfPTable dataTable,BaseColor myColor,BaseColor btablecolumncolor) {
try {
final Font tabletdcolor = new Font(Font.FontFamily.HELVETICA, 6, Font.NORMAL, BaseColor.BLACK);
c1 = new PdfPCell(new Paragraph(text, tabletdcolor));
cellStyle(c1, myColor, btablecolumncolor);
dataTable.addCell(c1);
} catch (Exception ex) {
ex.printStackTrace();
}
}
Generating bar chart
JFreeChart reportBarChart = genBatchReportBarChart(listHeader);
PdfTemplate reportTemplate = contentByte.createTemplate(280, 230);
Graphics2D reportGraphics = reportTemplate.createGraphics(280, 230, new DefaultFontMapper());
Rectangle2D reportRectangle = new Rectangle2D.Double(0, 0, 280, 230);
reportBarChart.draw(reportGraphics, reportRectangle);
reportGraphics.dispose();
contentByte.addTemplate(reportTemplate, 10, height+150);
Now in the above code iam fixing the position of bar chart and if the values are low in number its good but if they are large in number bar chart is overriding the table values.Depending upon the table values the bar chart need to be aligned how can I achieve that.
You can ask the table for its total height after you've added the table to the document, and use that table height to decide where to put the chart.
Or (even easier to achieve): you can wrap the PdfTemplate inside an Image object:
Image img = Image.getInstance(reportTemplate);
Add that image with document.add() right after you've added the table (assuming that you're adding the table with document.add()).