iTextSharp Overlay Image - vb.net

Hi guys I have an instance where I have a logo image as part of some artwork..
If a user uploads a new logo I have a form field which is larger than the default logo.
I then use that form field to position the new image.
The problem is I need to set the background colour of that form field to white so that it covers the old logo in the event that the new image is smaller than the old logo..
what I have done is:
foreach (var imageField in imageReplacements)
{
fields.SetFieldProperty(imageField.Key, "bgcolor", iTextSharp.text.Color.WHITE, null);
fields.RegenerateField(imageField.Key);
PdfContentByte overContent = stamper.GetOverContent(imageField.Value.PageNumber);
float[] logoArea = fields.GetFieldPositions(imageField.Key);
if (logoArea != null)
{
iTextSharp.text.Rectangle logoRect = new iTextSharp.text.Rectangle(logoArea[1], logoArea[2], logoArea[3], logoArea[4]);
var logo = iTextSharp.text.Image.GetInstance(imageField.Value.Location);
if (logo.Width >= logoRect.Width || logo.Height >= logoRect.Height)
{
logo.ScaleToFit(logoRect.Width, logoRect.Height);
}
logo.Alignment = iTextSharp.text.Image.ALIGN_LEFT;
logo.SetAbsolutePosition(logoRect.Left, logoArea[2] + (logoRect.Height - logo.ScaledHeight) / 2);
// left: logoArea[3] - logo.ScaledWidth + (logoRect.Width - logo.ScaledWidth) / 2
overContent.AddImage(logo);
}
}
The problem with this is that the background colour of the field is set to white and the image then doesn't appear.. i remove the SetFieldProperty and RegenerateField commands and the image replacement works fine..
is there a way to set a stacking order on layers?

Annotations (such as form fields) are always on top of page contents. Annotation Z order is just the order of the annotations array on a given page.
Page content Z order is just the order everything appears in the content stream. New drawing operators go on top of proceeding operators.
If you want to cover your old image, draw a white box over it and then draw the new logo over top that. No need to worry about annotations.
Actually, all you really need to do is not set the background color of the imageField. You're already scaling the new logo to match the size of the old one.
However, if you really must draw that white box, it's fairly simple:
overContent.setColorFill(iTextSharp.text.Color.WHITE);
overContent.rectangle( logoRect );
overcontent.fill();

Related

Flattening the AcroForm in PDFBox 2 changes font color

I have a pdf with a grey background on which there are few PDFields. I have set the value for these fields using:
PDAcroForm form = catalog.getAcroForm();
if(null != form){
//Field on top of grey background
PDField idField = form.getField("id");
if (null != idField) {
idField.setValue(value);
}
//Other fields outside grey background
}
Everything works as expected. However, the form fields are not visible when opening in ios' default PDF reader. So I'm trying to flatten the pdf.
Now, when I try to flatten the pdf using form.flatten(), the text of the PDFields on top of the grey background become white (it was black before flattening).
And just for experimenting, I tried flattening only the other fields in the PDF (and skip flattening idField) using form.flatten(fieldsToFlatten, false). When I do this, the fields on the grey background completely disappear.
Why is the font color changing when I try to flatten the pdf? Is there anyway to retain the original font color?
Thanks.

Place Text in Image on Edges

This tool I wrote in Visual Basic 2010 should add an author text to images. The user is able to set the font opacity and position. To make things easier I wanted some position presets as one can see in the bottom right corner. The calculation I am using is (bottom right in this case:
Dim textSize As Size = TextRenderer.MeasureText(tagString + curText, curFont)
tmpPos = New Point(srcImg.Width - textSize.Width - 10, srcImg.Height - textSize.Height - 10)
As you can see this works perfectly for this example picture. Where as on some the text just clips out.
First One: 1024x768 | Detected Font Size: 680x72
Second One: 1688x1125 | Detected Font Size: 680x72
I suspect this has something to do with the aspect ratio of the images but I do not know how to fix it.
The text is drawn like that:
brush = New SolidBrush(color.FromArgb(alpha, color))
gr = Graphics.FromImage(editImg)
gr.DrawString(tagString + text, font, brush, pos)
HauptBild.Image = editImg
I found this http://www.codeproject.com/Articles/20923/Mouse-Position-over-Image-in-a-PictureBox and it answered my questions.
is this problem only occuring in your preview or also in the converted File? Please post the Code how you save the New Image. I think you have Set a sizemode in your picturebox which is the Problem. Try it without the sizemode.
Will be better to see more your code, but, as i understand by TextRenderer class it is System.Windows.Forms. Just do not use Graphics, created from control (i suppose it is pictureBox with sizemode:Zoom), use Graphics, created from your image instead.
Here is code (sorry, C#), which loads image from file, draws text starting from the same coordinate and places on puctureBox1. Text always starts from Point(100,100).
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "Image files|*.jpeg;*.png;*.jpg;*.gif;*.bmp";
if(openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
Bitmap orig=(Bitmap)Bitmap.FromFile(openFileDialog1.FileName);
//workaround for images with color table, see remarks here https://msdn.microsoft.com/en-us/library/system.drawing.graphics.fromimage(v=vs.110).aspx
Bitmap bmp=orig.Clone(new Rectangle(0, 0, orig.Width, orig.Height), System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
Graphics g = Graphics.FromImage(bmp);
g.DrawString("hello", new Font(this.Font.FontFamily,30,FontStyle.Bold ) , new System.Drawing.SolidBrush(System.Drawing.Color.Yellow ), new Point(100, 100));
this.pictureBox1.Image = bmp;
orig.Dispose();
}
catch (Exception ex)
{
MessageBox.Show("Something goes wrong: " + ex.Message+ "\\n"+ ex.StackTrace );
}
}

Add flat amount to canvas size

I want to have some automated process (either action or script) that will copy selection(assumes something has already been selected), place in new canvas, increase canvas size by exactly 10 pixels in height & width, then save it to the desktop.
I'm currently using an action and it works correctly with the exception of the 10 pixels part. I can do something like 10% by using the percentage adjustment in the canvas size menu, but I can't figure out how to do exactly 10 pixels. During the recording, if I just increase the canvas size by 10 pixels it'll record that exact amount (say it was 100x100, it records that I resized canvas to 110x100). So when I play that action on a selection that's of size 50x50 it resizes it to 110x110.... So the problem is that the action records the literal value of the canvas resize and not the add 10 pixels part...
Any ideas here?
This can be scripted as well, but if you've already got an action set up, try modifying your action to do the following:
paste the selection into new document larger than you expect to ever require
expand the selection by 10 px
crop the document to the selection
save
Or, to script it you can tweak the following sample. It assumes you have already copied your image to the clipboard:
#target photoshop
app.preferences.rulerUnits = Units.PIXELS;
app.preferences.typeUnits = TypeUnits.PIXELS;
var doc = app.documents.add('1000px');
var lyr = doc.artLayers.add();
doc.activeLayer = lyr;
doc.paste ();
var bnds = lyr.bounds;
var unitsToAdd = new UnitValue(10, 'px');
bnds[0] = bnds[0] - unitsToAdd;
bnds[1] = bnds[1] - unitsToAdd;
bnds[2] = bnds[2] + unitsToAdd;
bnds[3] = bnds[3] + unitsToAdd;
doc.crop(bnds) ;
doc.saveAs(new File('/c/temp/temp.psd'));

How is iBooks rendering the index view so quickly when viewing a PDF? Or: how do draw UIImages on UIScrollView directly without UIImageView?

I have a 140 pages test PDF (the full Adobe PDF specification, http://wwwimages.adobe.com/www.adobe.com/content/dam/Adobe/en/devnet/pdf/pdfs/adobe_supplement_iso32000.pdf) and open it in iBooks. Then switch to index (thumbnail) view. If I scroll to the end fast enough I can see that I can scroll faster than iBooks renders the pages but it catches up pretty quickly on iPad 2.
I learn two things from this:
First iBooks is showing 140 empty squares in the right size and then populates the preview.
iBooks really renders all of the previews and keeps them in memory (if I scroll around I cannot spot any re-rendering)
I also tested with another Adobe Spec that has 700+ pages: exactly same behavior! Fascinating!
The question is how are they doing it? I wrote some code that gets each page of the PDF as an image, adds it to a UIImageView and adds that to the scrollview.
I use the same technique and layout as iBooks does. It renders just as quick as iBooks but memory consumption is insane and especially when scrolling the app gets totally stuck after a while. Can anybody point me in the right direction? I already removed the PDF rendering for testing and it is really fast, so I can pin it down to the thumbnail generation.
EDIT:
If from the code below the PDF generation is removed and an empty UIImageView is returned instead, the performance is still extremely weak. So my assumption is that the UIImageView is causing the problem. How can I draw the PDF thumbs onto my UIScrollView without the requirement of 140 UIImageViews?
For those firm in Monotouch, here's the code I'm using to render the thumbs, maybe it shows an obvious weakness:
/// <summary>
/// Gets the low res page preview of a PDF page. Does a quick image render of the page.
/// </summary>
/// <param name="iPage">the number of the page to render</param>
/// <param name="oTergetRect">the target rect to fit the PDF page into</param>
/// <returns>
/// The low res page image view.
/// </returns>
public static UIImageView GetLowResPagePreview (CGPDFPage oPdfPage, RectangleF oTargetRect)
{
RectangleF oOriginalPdfPageRect = oPdfPage.GetBoxRect (CGPDFBox.Media);
RectangleF oPdfPageRect = PdfViewerHelpers.RotateRectangle( oPdfPage.GetBoxRect (CGPDFBox.Media), oPdfPage.RotationAngle);
// If preview is requested for the PDF index view, render a smaller version.
if (!oTargetRect.IsEmpty)
{
// Resize the PDF page so that it fits the target rectangle.
oPdfPageRect = new RectangleF (new PointF (0, 0), GetFittingBox (oTargetRect.Size, oPdfPageRect.Size));
}
// Create a low res image representation of the PDF page to display before the TiledPDFView
// renders its content.
int iWidth = Convert.ToInt32 ( oPdfPageRect.Size.Width );
int iHeight = Convert.ToInt32 ( oPdfPageRect.Size.Height );
CGColorSpace oColorSpace = CGColorSpace.CreateDeviceRGB();
CGBitmapContext oContext = new CGBitmapContext(null, iWidth, iHeight, 8, iWidth * 4, oColorSpace, CGImageAlphaInfo.PremultipliedLast);
// First fill the background with white.
oContext.SetFillColor (1.0f, 1.0f, 1.0f, 1.0f);
oContext.FillRect (oOriginalPdfPageRect);
// Scale the context so that the PDF page is rendered
// at the correct size for the zoom level.
oContext.ConcatCTM ( oPdfPage.GetDrawingTransform ( CGPDFBox.Media, oPdfPageRect, 0, true ) );
oContext.DrawPDFPage (oPdfPage);
CGImage oImage = oContext.ToImage();
UIImage oBackgroundImage = UIImage.FromImage( oImage );
oContext.Dispose();
oImage.Dispose ();
oColorSpace.Dispose ();
UIImageView oBackgroundImageView = new UIImageView (oBackgroundImage);
oBackgroundImageView.Frame = new RectangleF (new PointF (0, 0), oPdfPageRect.Size);
oBackgroundImageView.ContentMode = UIViewContentMode.ScaleToFill;
oBackgroundImageView.UserInteractionEnabled = false;
oBackgroundImageView.AutoresizingMask = UIViewAutoresizing.None;
return oBackgroundImageView;
}
internal static RectangleF RotateRectangle ( RectangleF oRect, int iRotationAngle )
{
if ( iRotationAngle == 90 || iRotationAngle == 270 )
{
return new RectangleF (oRect.X, oRect.Y, oRect.Height, oRect.Width);
}
return oRect;
}
You shouldn't be using 140 UIImageViews !!! use only just enough to fill the area and then recycle the ones that are no longer displayed.
How did Apple implement UITableView ?? Do you think they keep all tableview cells in memory??
Look at the PhotoScroller sample code and the corresponding WWDC 2010 video. I think it is named "Desigining apps with scrollViews"
WWDC 2011 video of similar name is continuation of the same trick of view reuse.
Hope this helps.
Have you checked the size of each UIImageView? Perhaps each of your thumbnails is actually the size of a full page.
Perhaps iBooks doesn't put each thumbnail in a UIImageView? Maybe the app is using something from CoreAnimation or even OpenGL ES?

Ready An Existing PDF Page Size (ex. 8.5 x 11, 11 x 17) VB.Net

Like the title says i'd like to read an existing pdf page size with VB.Net. I've been working with Itext.Sharp, and the Acrobat.dll. Is this possible??
There are a number of different "Boxes" a given page can have:
Media Box (required): The initial page size when printing viewing.
Crop Box (optional): Supersedes the media box. Defaults to match the media box. Must be a subset or match the media box.
There's also art/trim/bleed boxes, but they don't matter as much and are much less common.
So, the page size:
PdfReader reader = new PdfReader(myPath);
// gets the MEDIA BOX
Rectangle pageRect = reader.getPageSize(1); // 1 -> first page
// gets the crop box if present, or the media box if not.
Rectangle cropRect = reader.getCropBox(1);
// and finally
Rectangle artBox = reader.getBoxSize( 1, "art");
// could be "art", "bleed", "crop", "media", or "trim"
I'd go with getCropBox().
I also recommend checking out the JavaDoc for things like this. At the very least you would have come up with getPageSize() on your own. No, it's not C#. Yes, it's very useful.
http://api.itextpdf.com/
Also note that these Rectangles need not be based on 0,0 (which would be the lower left corner on an unrotated page).
Further, you should check the page's rotation, getPageRotation(int), and swap height and width if the rotation is 90 or 270. There is getPageSizeWithRotation(int), but it only works with the media box, so I'd do it yourself if I were you. It's only a few extra lines of code:
// rotation has to be 0, 90, 180, or 270. "360" isn't kosher IIRC.
if (reader.getPageRotation(pageNum) % 180 != 0) {
float tmp = width;
width = height;
height = tmp;
}