Unable to rotate image in PDFBox - pdfbox

I am trying to change the angle of the image by using matrix.Its a needle image which points the value in a chart.
If i am giving "at.rotate(Math.toRadians(45));" , then its looking like it went inside the page, but i need it in the page.
Here is the code i m trying
//Retrieving the pages of the document
PDPage page = document.getPage(0);
//Creating PDImageXObject object
PDImageXObject needle = PDImageXObject.createFromFile("SpeedometerNeedle-300dpi-ActualSize.png"
,document);
PDPageContentStream contentStream = new PDPageContentStream(document, page, AppendMode.APPEND, true, true);
//Drawing the image in the PDF document
// draw 90° rotated, placed on the right of the first image
Matrix at = new Matrix(needle.getHeight() / 4, 0, 0, needle.getWidth() / 4,120, 565);
at.rotate(Math.toRadians(45));
// contentStream.drawImage(needle, 100, 565, needle.getWidth() / 4, needle.getHeight() / 4);
contentStream.drawImage(needle,at);
contentStream.close();
I am using org.apache.pdfbox:pdfbox:2.0.1 version ,So if i am trying to use
AffineTransform at = new AffineTransform(ximage.getHeight() / 2, 0, 0, ximage.getWidth() / 2, x + ximage1.getWidth(), y);
at.rotate(Math.toRadians(90));
contentStream.drawXObject(ximage, at);
Then drawXObject method is shown as deprecated.
So suggest me how to use it.

Related

Add Images as Icons to PDF Document using PDFBox

I'm trying to mimic the behavior of the Acrobat JS call doc.importIcon() using PDF Box to import an larger number of images as icons into a PDF document.
I've already created and added an PDAppearanceStream to the AP Dictionary.
Unfortunately the Icon is not accessible through doc.getIcon() JS call later in the PDF JS.
Maybe someone of you had the same problem. I guess I'm missing just a small piece here.
My current code looks like this:
PDDocumentCatalog docCatalog = document.getDocumentCatalog();
PDDocumentNameDictionary nameDict = docCatalog.getNames();
COSDictionary dic = nameDict.getCOSObject().getCOSDictionary(COSName.AP);
COSArray names = (COSArray) dic.getDictionaryObject(COSName.NAMES);
PDImageXObject imgNeu = PDImageXObject.createFromFileByContent(new File("C:\\FancyPicture.jpg"), document);
float width = imgNeu.getWidth();
float height = imgNeu.getHeight();
PDAppearanceStream pdAppearanceStream = new PDAppearanceStream(document);
pdAppearanceStream.setResources(new PDResources());
try (PDPageContentStream pdPageContentStream = new PDPageContentStream(document, pdAppearanceStream))
{
pdPageContentStream.drawImage(imgNeu, 0, 0, width, height);
}
pdAppearanceStream.setBBox(new PDRectangle(width, height));
pdAppearanceStream.setFormType(1);
COSArray defaultRes = new COSArray();
defaultRes.add(COSName.getPDFName("PDF"));
defaultRes.add(COSName.getPDFName("ImageC"));
pdAppearanceStream.getResources().getCOSObject().setItem(COSName.PROC_SET, defaultRes);
Matrix mat = new Matrix(1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f);
pdAppearanceStream.setMatrix(mat.createAffineTransform());
COSDictionary strDic = pdAppearanceStream.getCOSObject();
strDic.setString(COSName.NAME, "FRM");
COSString key = new COSString("img_01");
names.add(key.getCOSObject());
names.add(pdAppearanceStream.getCOSObject());
It turns out as if the JS runtime within Acrobat requires the COSString elems encoded as UTF-16 incl. Byte order mark
After proper encoding of the icon names and using COSString(byte[]) everything works as expected.

How to make stamped image uneditable using iTextSharp?

My goal is stamp an image on a 3D PDF that behaves like a watermark (end-user cannot select, edit, resize, or delete the image).
I tried making an Annotation as shown below, but the image ("ClassificationBlock.png" in Resources) can be resized and deleted on the output PDF. Is that an inherent behavior of "PdfAnnotation" rectangles or is there a property I can define that will keep the image essentially read-only?
using (PdfStamper stamp = new PdfStamper(reader, fs))
.
.
.
Rectangle stampRect2 = null;
System.Drawing.Image imageBTM2 = System.Drawing.Image.FromHbitmap(Properties.Resources.ClassificationBlock.GetHbitmap());
Image stampImage2 = iTextSharp.text.Image.GetInstance(imageBTM2, System.Drawing.Imaging.ImageFormat.Png);
Rectangle location2 = new Rectangle(0, 0, stampImage2.Width, stampImage2.Height);
PdfAnnotation pdfStamp2 = PdfAnnotation.CreateStamp(stamp.Writer, location2, null, "ImageText");
stampImage2.SetAbsolutePosition(0, 0);
PdfAppearance app2 = stamp.GetOverContent(1).CreateAppearance(stampImage2.Width, stampImage2.Height);
app2.AddImage(stampImage2);
pdfStamp2.SetAppearance(PdfName.N, app2);
pdfStamp2.SetPage();
stamp.AddAnnotation(pdfStamp2, 1);
stampRect2 = location2;
stamp.FormFlattening = true;
stamp.Close();
reader.Close();
fs.Close();
I've also tried it by mimicking another user's attempt at watermark text via pdfContentBytes, but I can't get the image to even display on the PDF.
stamp.FormFlattening = false;
iTextSharp.text.Rectangle pageRectangle = reader.GetPageSizeWithRotation(1);
PdfContentByte pdfData = stamp.GetOverContent(1);
pdfData.SetFontAndSize(BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, BaseFont.NOT_EMBEDDED), 10);
PdfGState graphicsState = new PdfGState();
graphicsState.FillOpacity = 0.5F;
pdfData.SetGState(graphicsState);
pdfData.BeginText();
System.Drawing.Image imageBTM2 = System.Drawing.Image.FromHbitmap(Properties.Resources.TEKLAPDF_InstructionBlock.GetHbitmap());
iTextSharp.text.Image stampImage2 = iTextSharp.text.Image.GetInstance(imageBTM2, System.Drawing.Imaging.ImageFormat.Png);
float width = pageRectangle.Width;
float height = pageRectangle.Height;
stampImage2.ScaleToFit(width, height);
stampImage2.SetAbsolutePosition(width / 2 - stampImage2.Width / 2, height / 2 - stampImage2.Height / 2);
stampImage2.SetAbsolutePosition(50, 50);
stampImage2.Rotation = 0;
pdfData.AddImage(stampImage2);
pdfData.EndText();
Any ideas on how best to accomplish this? This is driving me crazy.
EDIT*****************************
These are the current avenues I've pursued. Any ideas on how to "watermark" the 3D PDF?
//Stamp Image Method (works on 2D PDF and 3D PDF BUT results in EDITABLE stamp)
System.Drawing.Image imageBTM2 = System.Drawing.Image.FromHbitmap(Properties.Resources.ClassificationBlock.GetHbitmap());
Image stampImage2 = iTextSharp.text.Image.GetInstance(imageBTM2, System.Drawing.Imaging.ImageFormat.Png);
Rectangle stampRect2 = null;
Rectangle location2 = new Rectangle(0, 0, stampImage2.Width, stampImage2.Height);
PdfAnnotation pdfStamp2 = PdfAnnotation.CreateStamp(stamp.Writer, location2, null, "ImageText");
stampImage2.SetAbsolutePosition(0, 0);
PdfAppearance app2 = stamp.GetUnderContent(1).CreateAppearance(stampImage2.Width, stampImage2.Height);
app2.AddImage(stampImage2);
pdfStamp2.SetAppearance(PdfName.N, app2);
pdfStamp2.SetPage();
stamp.AddAnnotation(pdfStamp2, 1);
stampRect2 = location2;
//Watermark Layering Method (works only on 2D PDF)
var layers = stamp.GetPdfLayers();
var imgLayer = new PdfLayer("StackoverflowImage", stamp.Writer);
PdfContentByte cb = stamp.GetUnderContent(1);
cb.BeginLayer(imgLayer);
stampImage2.ScalePercent(100f);
stampImage2.SetAbsolutePosition(pageWidth/2, pageHeight/2);
cb.AddImage(stampImage2);
cb.EndLayer();
//Jan's Watermark method (works only on 2D PDF)
PdfContentByte over = stamp.GetOverContent(1);
stampImage2.SetAbsolutePosition(pageWidth / 2, pageHeight / 2);
PdfLayer imgLayer = new PdfLayer("StackoverflowImage", stamp.Writer);
imgLayer.OnPanel = false;
over.BeginLayer(imgLayer);
over.AddImage(stampImage2);
over.EndLayer();
stamp.Close();
reader.Close();
First off: You most likely cannot prevent selection of the image.
Second: I do itext in Java, so you'll probably end up uppercasing first character of method names...
For the remainder or your question you could try to add this image to a layer:
PdfContentByte over = stamp.getOverContent(1)
Image img = ...//code to get your image;
img.setAbsolutePosition(x, y); //at your postion
PdfLayer imgLayer = new PdfLayer("StackoverflowImage", stamper.getWriter());
imgLayer.setOnPanel(false);
over.beginLayer(imgLayer);
over.addImage(img);
over.endLayer();
SOLVED! Using the "Stamp Image Method" as described above, I just needed to change the properties of the stamp itself (changing FLAGS to LOCKED and READ-ONLY). This results in a stamp that is above the 3D PDF layer AND cannot be resized, edited, or deleted. So the code is now:
//Stamp Image Method
System.Drawing.Image imageBTM2 = System.Drawing.Image.FromHbitmap(Properties.Resources.ClassificationBlock.GetHbitmap());
Image stampImage2 = iTextSharp.text.Image.GetInstance(imageBTM2, System.Drawing.Imaging.ImageFormat.Png);
Rectangle stampRect2 = null;
Rectangle location2 = new Rectangle(0, 0, stampImage2.Width, stampImage2.Height);
PdfAnnotation pdfStamp2 = PdfAnnotation.CreateStamp(stamp.Writer, location2, null, "ImageText");
pdfStamp2.Flags = iTextSharp.text.pdf.PdfAnnotation.FLAGS_LOCKED;
pdfStamp2.Flags = iTextSharp.text.pdf.PdfAnnotation.FLAGS_READONLY;
stampImage2.SetAbsolutePosition(0, 0);
PdfAppearance app2 = stamp.GetUnderContent(1).CreateAppearance(stampImage2.Width, stampImage2.Height);
app2.AddImage(stampImage2);
pdfStamp2.SetAppearance(PdfName.N, app2);
pdfStamp2.SetPage();
stamp.AddAnnotation(pdfStamp2, 1);
stampRect2 = location2;

Unable to add margins in iTextSharp document having images

Requirement:
A large image (dynamic) needs to be split and shown in PDF pages. If image can't be accomodated in one page then we need to add another page and try to fit the remaining portion and so on.
So far I am able to split the image in multiple pages, however it appears that they are completely ignoring the margin values and so images are shown without any margins.
Please see below code:
string fileStringReplace = imageByteArray.Replace("data:image/jpeg;base64,", "");
Byte[] imageByte = Convert.FromBase64String(fileStringReplace);
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(imageByte);
float w = image.ScaledWidth;
float h = image.ScaledHeight;
float cropHeight = 1500f;
iTextSharp.text.Rectangle page = new iTextSharp.text.Rectangle(1150f, cropHeight);
var x = page.Height;
Byte[] created;
iTextSharp.text.Document document = new iTextSharp.text.Document(page, 20f, 20f, 20f, 40f); --This has no impact
using (var outputMemoryStream = new MemoryStream())
{
PdfWriter writer = PdfWriter.GetInstance(document, outputMemoryStream);
writer.CloseStream = false;
document.Open();
PdfContentByte canvas = writer.DirectContentUnder;
float usedHeights = h;
while (usedHeights >= 0)
{
usedHeights -= cropHeight;
document.SetPageSize(new iTextSharp.text.Rectangle(1150f, cropHeight));
canvas.AddImage(image, w, 0, 0, h, 0, -usedHeights);
document.NewPage();
}
document.Close();
created = outputMemoryStream.ToArray();
outputMemoryStream.Write(created, 0, created.Length);
outputMemoryStream.Position = 0;
}
return created;
I also tried to set margin in the loop by document.SetMargins() - but that's not working.
You are mixing different things.
When you create margins, be it while constructing the Document instance or by using the setMargins() method, you create margins for when you let iText(Sharp) decide on the layout. That is: the margins will be respected when you do something like document.Add(image).
However, you do not allow iText to create the layout. You create a PdfContentByte named canvas and you decide to add the image to that canvas using a transformation matrix. This means that you will calculate the a, b, c, d, e, and f value needed for the AddImage() method.
You are supposed to do that Math. If you want to see a margin, then the values w, 0, 0, h, 0, and -usedHeights are wrong, and you shouldn't blame iTextSharp, you should blame your lack of insight in analytical geometrics (that's the stuff you learn in high school at the age of 16).
This might be easier for you:
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(imageByte);
float w = image.ScaledWidth;
float h = image.ScaledHeight;
// For the sake of simplicity, I don't crop the image, I just add 20 user units
iTextSharp.text.Rectangle page = new iTextSharp.text.Rectangle(w + 20, h + 20);
iTextSharp.text.Document document = new iTextSharp.text.Document(page);
PdfWriter writer = PdfWriter.GetInstance(document, outputMemoryStream);
// Please drop the line that prevents closing the output stream!
// Why are so many people making this mistake?
// Who told you you shouldn't close the output stream???
document.Open();
// We define an absolute position for the image
// it will leave a margin of 10 to the left and to the bottom
// as we created a page that is 20 user units to wide and to high,
// we will also have a margin of 10 to the right and to the top
img.SetAbsolutePosition(10, 10);
document.Add(Image);
document.Close();
Note that SetAbsolutePosition() also lets you take control, regardless of the margins, as an alternative, you could use:
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(imageByte);
float w = image.ScaledWidth;
float h = image.ScaledHeight;
// For the sake of simplicity, I don't crop the image, I just add 20 user units
iTextSharp.text.Rectangle page = new iTextSharp.text.Rectangle(w + 20, h + 20);
iTextSharp.text.Document document = new iTextSharp.text.Document(page, 10, 10, 10, 10);
PdfWriter writer = PdfWriter.GetInstance(document, outputMemoryStream);
// Please drop the line that prevents closing the output stream!
// Why are so many people making this mistake?
// Who told you you shouldn't close the output stream???
document.Open();
// We add the image to the document, and we let iTextSharp decide where to put it
// As there is just sufficient space to fit the image inside the page, it should fit,
// But be aware of the existence of a leading; that could create side-effects
// such as forwarding the image to the next page because it doesn't fit vertically
document.Add(Image);
document.Close();

How to make PdfLayer.SetPrint work with PdfStamper?

I'm adding watermarks on existing PDF using the iText PdfStamper class. And I want these watermarks to be switched to on or off, so I'm using the class PdfLayer.
But I also want these watermarks to be always visible when the file is printed : I'm using the function PdfLayer.setPrint() then.
This is this last step that unfortunately doesn't work as expected.
Here's my code :
PdfReader reader = new PdfReader("C:/Temp/input.pdf");
PdfStamper stamp = new PdfStamper(reader, new FileOutputStream("C:/Temp/output.pdf"));
PdfWriter writer = stamp.getWriter();
PdfLayer layer = new PdfLayer("Watermarks", writer);
layer.setOn(true);
layer.setPrint("Watermarks", true);
BaseFont bf = BaseFont.createFont();
PdfContentByte cb = stamp.getOverContent(1);
cb.beginText();
cb.setFontAndSize(bf, 18);
cb.beginLayer(layer);
cb.showTextAligned(Element.ALIGN_LEFT, "Watermark line 1", 50, 55, 0);
cb.showTextAligned(Element.ALIGN_LEFT, "Watermark line 2", 50, 40, 0);
cb.endLayer();
cb.endText();
stamp.close();
reader.close();
When I check the layer properties from Adobe Reader (version 10), I see that the "Initial State : Print" property stays at "Prints When Visible" while it should be "Always Print".
I also tried creating layers on a new PDF document and there the setPrint() works.
What am I doing wrong ?
I have the same issue. My code want to add a image as watermark on every page of original pdf. And the watermark can only be viewed, not allow to print. I use PdfStamper and PdfLayer.setPrint() too. But it did not work. I read the itext java source and found a way to make it work. Here is code :
PdfWriter writer = stamp.getWriter();
PdfLayer layer = new PdfLayer("Watermarks", writer);
layer.setOn(true);
layer.setOnPanel(false);
layer.setPrint("watermark", false);
writer.addToBody(layer.getPdfObject(), layer.getRef());
It call addToBody after setPrint. This works well.
I have the same problem. As a workaround, you can use new Document and getImportedPage instead of pdfStamper.
Unfortunately, you loose the hyperlink because all pages are converted to images. I tried to use PdfCopy but I reproduced the same issue. I am really interested in a solution allowing me to add a watermark without changing the source file.
Degraded sample solution :
PdfReader pdfReaderS = new PdfReader(filepathS);
Document document = new Document(pdfReaderS.getPageSizeWithRotation(1));
PdfWriter pdfWriterD = PdfWriter.getInstance(document, new FileOutputStream(filepathD));
document.open();
PdfContentByte pdfContentByteD = pdfWriterD.getDirectContent();
BaseFont baseFont = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.EMBEDDED);
int n = pdfReaderS.getNumberOfPages();
PdfLayer pdfLayer = new PdfLayer("Watermark", pdfWriterD);
pdfLayer.setPrint("Print", true);
pdfLayer.setView(visibleScreen);
for (int i = 1; i <= n; i++) {
Rectangle pageSizeS =pdfReaderS.getPageSizeWithRotation(i);
float pageWidth = pageSizeS.getWidth() / 2;
float pageheight = pageSizeS.getHeight() / 2;
float degree = (float)(Math.toDegrees(Math.atan(pageSizeS.getHeight()/pageSizeS.getWidth())));
document.setPageSize(pageSizeS);
document.newPage();
PdfImportedPage pdfImportedPage = pdfWriterD.getImportedPage(pdfReaderS, i);
int rotation = pdfReaderS.getPageRotation(i); //This value can be 0, 90, 180 or 270.
if (rotation == 0)
pdfContentByteD.addTemplate(page, 1, 0, 0, 1, 0, 0);
else if (rotation == 90)
pdfContentByteD.addTemplate(page, 0, -1, 1, 0, 0, pageSizeS.getHeight());
else if (rotation == 180)
pdfContentByteD.addTemplate(page, -1, 0, 0, -1, pageSizeS.getHeight(), pageSizeS.getWidth());
else if (rotation == 270)
pdfContentByteD.addTemplate(page, 0, 1, -1, 0, pageSizeS.getWidth(), 0);
pdfContentByteD.beginLayer(pdfLayer);
pdfContentByteD.beginText();
pdfContentByteD.setFontAndSize(baseFont, policeSize);
pdfContentByteD.setColorFill(col);
pdfContentByteD.showTextAligned(PdfContentByte.ALIGN_CENTER, text, pageWidth, pageheight, degree);
pdfContentByteD.endText();
pdfContentByteD.endLayer();
}
document.close();
pdfReaderS.close();

How to change text height in pdfbox

PDFBOX / JSF
Im trying to change the font height of a given text. I know how to change the fontsize only.
PDPageContentStream contentStreambc = new PDPageContentStream(doc1, page, true, true);
contentStreambc.setFont( fonta, 16 );
contentStreambc.beginText();
contentStreambc.moveTextPositionByAmount(200, 320);
contentStreambc.drawString( "abcdef");
contentStreambc.endText();
contentStreambc.close();
The code works fine. But How I change the font height ?
thanks in advance stack members.
If you need something like this
you can create it with this code:
PDRectangle rec = new PDRectangle(220, 70);
PDDocument document = null;
document = new PDDocument();
PDPage page = new PDPage(rec);
document.addPage(page);
PDPageContentStream content = new PDPageContentStream(document, page, true, true);
content.beginText();
content.moveTextPositionByAmount(7, 55);
content.setFont(PDType1Font.HELVETICA, 12);
content.drawString("Normal text (size 12)");
content.setTextMatrix(1, 0, 0, 1.5f, 7, 30);
content.drawString("Stretched text (size 12, factor 1.5)");
content.setTextMatrix(1, 0, 0, 2f, 7, 5);
content.drawString("Stretched text (size 12, factor 2)");
content.endText();
content.close();
document.save("SimplePdfStretchedText.pdf");
The code stretches the text by setting the text matrix accordingly; for details cf. chapter 9 of the PDF specification ISO 32000-1.
PS: As you mention bar codes in a comment to another answer, this should indeed allow you to make higher bar codes while keeping the distances.