I want to crop / remove / cut away a part of a pdf page.
Somebody provided a code for me to do that in this question.
2 new problems occured:
I want to print the PDF in a different orientation
I want to cut away more of the blank space around the DHL label
Because dealing with the coordinates in iTextSharp requires (0,0) based coders to twist their mind a bit, I first wanted to rotate the PDF so that I can follow it along more easily.
I did that using the following code:
Dim testFile As String = "new pdf1.pdf"
Dim resultFile As String = "new pdf1-Rotated.pdf"
Using pdfReader As PdfReader = New PdfReader(testFile)
Using pdfStamper As PdfStamper = New PdfStamper(pdfReader, File.Create(resultFile))
For i As Integer = 1 To pdfReader.NumberOfPages
Dim rotate As Integer = pdfReader.GetPageRotation(i)
Dim pageDictionary As PdfDictionary = pdfReader.GetPageN(i)
pageDictionary.Put(PdfName.ROTATE, New PdfNumber(90))
Next
End Using
End Using
It does work.
I expected that the page rotation change would automatically affect the crop box.
However, it does not.
Dim testFile As String = "new pdf1.pdf" 'they both give the same cropbox
Dim testFile As String = "new pdf1-Rotated.pdf"'they both give the same cropbox
Using pdfReader As PdfReader = New PdfReader(testFile)
For i As Integer = 1 To pdfReader.NumberOfPages
Dim cropBox As Rectangle = pdfReader.GetCropBox(i)
Why is that so?
Related
I want to make a counter with Libreoffice Impress.
Each Time I click on a rectangle, the number inside increases.
Dim Doc As Object
Dim Page As Object
Dim Rect As Object
Doc = ThisComponent
Page = Doc.drawPages.getByName("Test")
Rect = Page.getByIndex(0)
Rect.String = CInt(Rect.String) + 1
It works everywhere except in presentation mode.
Is it possible to make it work in presentation mode ?
I've got a PdfPTable in which I am drawing simple text:
BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
Font f2 = new Font(bf, 10.0f);
PdfPTable tab = new PdfPTable(2);
table.AddCell(new PdfPCell(new Phrase("Dude", f2)));
And that's all fine and dandy. But then in order to do some fancier stuff, I'm trying to get cell events to work:
Font f2 = new Font(bf, 10.0f); // Pretend this is a global
class CellEvent : IPdfPCellEvent
{
void IPdfPCellEvent.CellLayout(PdfPCell cell, Rectangle r, PdfContentByte[] canvases)
{
ColumnText ct = new ColumnText(canvases[0]);
ct.SetSimpleColumn(r);
ct.AddElement(new PdfPCell(new Phrase("Dude", f2)));
ct.Go();
}
}
BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
PdfPTable tab = new PdfPTable(2);
PdfPCell ce = new PdfPCell();
ce.CellEvent = new CellEvent();
table.AddCell(ce);
When I do this though, the cell is drawn quite differently. My actual text is multiple lines long, and when drawn directly (adding the phrase directly to the cell) the text lines are quite close together vertically; when drawn via the cell event, there is a lot of space between the text lines and it doesn't fit in the box the PdfPTable provides for me.
Is there a different, more preferred method for drawing text inside IPdfPCellEvent.CellLayout()? Or are there just some formatting parameters I need to copy from PdfPCell into ColumnText?
There is something very wrong with this snippet:
ColumnText ct = new ColumnText(canvases[0]);
ct.SetSimpleColumn(r);
ct.AddElement(new PdfPCell(new Phrase("Dude", f2)));
ct.Go();
More specifically:
ct.AddElement(new PdfPCell(new Phrase("Dude", f2)));
You can not use the PdfPCell object outside of the context of a PdfPTable.
As for the differences between content in PdfPCell and ColumnText, you may want to read the answers to these questions:
itext ColumnText ignores alignment
Right aligning text in PdfPCell
The content of a PdfPCell is actually stored in a ColumnText object, but a PdfPCell may have a padding.
There's also text mode versus composite mode. You are talking about the distance between two lines. In text mode, this distance is defined at the level of the PdfPCell/ColumnText using the setLeading() method. In composite mode, this parameter is ignored. Instead, the leading of the elements added to the PdfPCell/ColumnText is used.
For instance: if p1, p2 and p3 are three Paragraph objects, each having a different leading, then ct will have text with three different leadings if you do this:
ct.addElement(p1);
ct.addElement(p2);
ct.addElement(p3);
I'm using the code snippet below. I would expect the text to be exactly one inch apart but it's not. What am I doing wrong?
Dim outputFile As String = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "test.pdf")
Using fs As New FileStream(outputFile, FileMode.Create, FileAccess.Write, FileShare.None)
Using doc As New Document(PageSize.LETTER)
Using writer As PdfWriter = PdfWriter.GetInstance(doc, fs)
doc.Open()
'This creates two lines of text using the iTextSharp abstractions
' doc.Add(New Paragraph("This is Paragraph 1"))
' doc.Add(New Paragraph("This is Paragraph 2"))
'This does the same as above but line spacing needs to be calculated manually
Dim cb As PdfContentByte = writer.DirectContent
cb.SaveState()
cb.SetColorFill(BaseColor.BLACK)
cb.SetFontAndSize(BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED), 12.0F)
cb.BeginText()
' rlm - dimensions are from the bottom
cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "This is cb1", 20, 20, 0)
cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "This is cb2", 20, 92, 0)
cb.EndText()
cb.RestoreState()
doc.Close()
End Using
End Using
End Using
I'm not sure how you are measuring things but I just printed your sample out at actual resolution and compared it with a ruler and their baselines are indeed one inch apart.
If you are measuring on a computer screen you are going to run into problems. Windows-based machines generally (although this is changing) run at 96 DPI, so if you view your test on a computer screen at 100% and try to measure it you'll get about 4/3 of an inch (96/72). So called "hi-DPI" settings and devices are becoming more commonplace so there's no guarantee what a screen will actually measure to.
My issue with images and iTextSharp is that I want to put a header in and the have the text below that point.
I can display the image and wrap the text but not get it to display below, even when adding New paragraph.
I am assuming there is some command around the aligmnent but I am struggling to find it even when looking at tutorials such as http://www.mikesdotnetting.com/Article/87/iTextSharp-Working-with-images
'Import Image
Dim vImagePath = "C:\sites\images\" & "Logo.jpg"
Dim jpg As Image = Image.GetInstance(vImagePath)
jpg.ScaleToFit(139.0F, 106.0F)
jpg.Alignment = Image.ALIGN_RIGHT Or Image.TEXTWRAP
jpg.IndentationLeft = 7.0F
jpg.SpacingAfter = 9.0F
jpg.BorderWidthTop = 6.0F
jpg.BorderColorTop = Color.WHITE
document.Add(jpg)
'##Main Document Header
Dim tableHeader As New PdfPTable(1) ' Number in brackets is number of columns
tableHeader.DefaultCell.Border = Rectangle.NO_BORDER
Dim phFirst As Phrase = New Phrase("Title", HeaderFont)
tableHeader.AddCell(phFirst)
document.Add(tableHeader)
I used PdfContentByte to show text in pdf in that i also used SetTextMatrix mathod for postion of that text now when my text is large it will not show in pdf show can i wrap the text show i can able to see it below is my code
PdfContentByte cb = myPDFWriter.DirectContent;
cb.BeginText();
BaseFont bf_qty123 = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false);
cb.SetFontAndSize(bf_qty123, 10f);
cb.SetTextMatrix(422,100);
cb.ShowText("longstring");
cb.EndText();
use Column like this :
Dim p As Phrase = New Phrase("your txt", FontFactory.GetFont(BaseFont.HELVETICA, BaseFont.CP1250, True, fsize))
Dim ct As New ColumnText(cb)
ct.SetSimpleColumn(p, x, y, ux, uy, 10, Element.ALIGN_LEFT)
ct.Go()
and fix width hight bloc ux, uy
see this also :
How to position and wrap long text?