How to rotate text and images in a rotated PDF? - pdf

I am editing PDFs using PDFsharp (editing in the sense of adding images, text etc.), and that works perfectly well.
gfx.DrawImage(img, pointX, pointY, width, height);
gfx.DrawString(text, thumbFont, XBrushes.Black,
new XRect(pointX + 10, pointY + height + 2, width - 10, 5), XStringFormats.TopLeft);
But when I am trying to edit a PDF which has Rotate property as 270, the added text, images are coming horizontally.
The actual problem that I am facing is, the PDF is not actually rotated, it just changes the Page.Rotate property, so my question is how to add text and image at the correct co-ordinate and in correct orientation to have rotation of 270.

The rotation is applied to the page at display time. 270° clockwise are 90° counter-clockwise.
AFAIK you have to rotate the text you are adding to the PDF 270° CCW (90° CW) so the text shows normally on the rotated PDF page.

Related

Is there a way to set the dimensions of a paragraph in itext7?

Trying to set the height along with setting the rotation on a Paragraph object results in the following error:
ERROR c.i.layout.renderer.BlockRenderer - Rotation was not correctly processed for ParagraphRenderer
Also, the height is sent to infinity resulting in an apparently empty page which I'm not sure if it's the expected behavior or not.
If I don't set the height, the text shows up rotated (which is what I want). But my task is to place the text at certain coordinates with certain dimensions and rotation. The placement is fine, but not being able to set the height results in assigning the rotation points to the wrong values and in turn displacing the final position of the text.
Paragraph paragraph = new Paragraph(text);
paragraph.setHeight(height);
paragraph.setMargins(0, 0, 0, 0);
paragraph.setProperty(ROTATION_POINT_X, x);
paragraph.setProperty(ROTATION_POINT_Y, y);
paragraph.setProperty(ROTATION_ANGLE, asFloat(toRadians(rotation))));
document.add(paragraph);
Setting the margins to 0 doesn't seem to have any influence either. The text looks to keep some sort of margin anyway.
So is there a way to control what the dimensions of the object displayed are? I could also settle for a way to make the rotation pivot from the center of the object.
You should set both width and height at the same time for rotated objects.
Here is an example code:
Paragraph paragraph = new Paragraph("Hello world");
paragraph.setWidth(200);
paragraph.setHeight(20);
paragraph.setBackgroundColor(ColorConstants.RED);
paragraph.setMargins(0, 0, 0, 100);
paragraph.setProperty(ROTATION_POINT_X, paragraph.getWidth().getValue() / 2);
paragraph.setProperty(ROTATION_POINT_Y, paragraph.getHeight().getValue() / 2);
paragraph.setRotationAngle(Math.PI / 180 * 45);
document.add(paragraph);
Visual result:

How to save a scaled image inside QGRaphicsView using QFileDialog

I have a user interface with:
N.1 Push button (used to upload images)
N.2 QGraphicsView (left and right)
N.1 Push button that takes a print screen of the current image loaded on QGraphicsView left
Using the mouse is possible to:
1) zoom-in/zoom-out from the image
2) draw rectangles on the image.
I want to take the print screen of the image according to the zoom-in or zoom-out area I am using. However, once the file is saved it shows the entire image (wrong because I wanted only the enlarged or shrank part) with the rectangles drawn (this is correct).
According to this post QFileDialog was used in a similar way I am trying to do. I successfully used QFileDialog::getSaveFileName() to save the image. However it is not entirely solving the problem.
Below the pushbutton that takes care of taking the print screen of the image in the QGraphicsView left:
void MainWindow::on_addNewRecordBtn_clicked()
{
leftScene->clearSelection(); // Selections would also render to the file
leftScene->setSceneRect(leftScene->itemsBoundingRect()); // Re-shrink the scene to it's bounding contents
QImage image(leftScene->sceneRect().size().toSize(), QImage::Format_ARGB32); // Create the image with the exact size of the shrunk scene
image.fill(Qt::transparent); // Start all pixels transparent
QPainter painter(&image);
leftScene->render(&painter);
image.save(QFileDialog::getSaveFileName(this, tr("New Image Name"), QDir::rootPath(),
"Name (*.jpg *.jpeg *.png *.tiff *.tif)"));
}
The expected result would be saving the zoomed image (zoom.jpg for example) like this:
However when I save the image (zoom.jpg) the result that I am obtaining is constantly the entire image with the drawn features:
So if anyone needs, it is possible to take a print screen of an image no matter what the zoom in. Meaning you can zoom-in and zoom-out and take a print screen.
The following statement will do the job, grabbing the image your present (zoomed in or out status):
QImage image = ui->leftView->grab().toImage();
The only glitch is that the scroll bars horizontal and vertical (depending on the zoom) are also printed in your image. You can avoid that by setting them off right before taking the print screen and putting them back on right after.
Basically my previous function can be better written as follows:
void MainWindow::on_addNewRecordBtn_clicked()
{
leftScene->setSceneRect(leftScene->itemsBoundingRect());
// Setting off the scroll bars
ui->leftView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
ui->leftView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
QImage image = ui->leftView->grab().toImage();
image.save(QFileDialog::getSaveFileName(this, tr("New Image Name"), QDir::rootPath(),
"Name (*.jpg *.jpeg *.png *.tiff *.tif)"));
// Putting the scroll bars back on
ui->leftView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
ui->leftView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
}
Hope this will be helpful in case you encountered my same problem.

Qt5 QtChart drop vertical lines while using QScatterSeries

When I am using QScatterSeries, I can very easily draw point at (x, y). However, instead of points I would like to draw short lines, like in the figure below. How can I get about doing so?
I tried using RectangleMarker, but it just draws a fat square. I would prefer a thin line about 2px wide and 20px in height.
Is there a way I can add custom marker shapes?
Here are the code and the settings I use to transform my points into lines :
//create scatter series to draw point
m_pSeries1 = new QtCharts::QScatterSeries();
m_pSeries1->setName("trig");
m_pSeries1->setMarkerSize(100.0);
//draw a thin rectangle (50 to 50)
QPainterPath linePath;
linePath.moveTo(50, 0);
linePath.lineTo(50, 100);
linePath.closeSubpath();
//adapt the size of the image with the size of your rectangle
QImage line1(100, 100, QImage::Format_ARGB32);
line1.fill(Qt::transparent);
QPainter painter1(&line1);
painter1.setRenderHint(QPainter::Antialiasing);
painter1.setPen(QColor(0, 0, 0));
painter1.setBrush(painter1.pen().color());
painter1.drawPath(linePath);
//attach your image of rectangle to your series
m_pSeries1->setBrush(line1);
m_pSeries1->setPen(QColor(Qt::transparent));
//then use the classic QtChart pipeline...
You can play the marker size, the dimension of the image and the drawing pattern in the painter to adapt the size and shape of the rectangle to obtain a line.
In the picture, it's the black line. As you can see you can repeat the process for other series.
Keep in mind that you cannot use the openGL acceleration:
m_pSeries0->setUseOpenGL(true);
My work is based on the QtCharts/QScatterSeries example : QScatterSeries example
Hope it will help you.
Florian

Fabricjs: Auto add border when export egText to PDF

I'm exporting a Fabricjs egText to PDF on Node server using PDFKit module.
It auto adds a border around my text when export to PDF.
On drawing canvas: On drawing canvas img
On PDF: On PDF img
As you see, it has a very light border around text.
And if I set the backgroundColor of text to rgba(255, 255, 255, 0), the border is disappear.
Do you know how to fix it or any sugguestion ?
When i add a white blackground rectangle under the text, the border is disappear on the part of text that on the rectangle, at the rest, the border still active.
The image here: Two parts of text Img

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;
}