addind text with itext7 - c#

I want to add text into every page of my pdf which is rotaded from bottom to top.
Like this:
Here is my code:
PdfDocument srcDocument = new PdfDocument(new PdfReader(file));
PdfDocument destDocument = new PdfDocument(new PdfWriter(newfile));
int pagesCount = srcDocument.GetNumberOfPages();
for (int i = 1; i <= pagesCount; i++)
{
srcDocument.CopyPagesTo(i, i, destDocument);
PdfCanvas pdfCanvas = new PdfCanvas(srcDocument.GetPage(i));
}
srcDocument.Close();
destDocument.Close();
In this I got stuck. I don't know how to go further : write and rotate text.

So, as per the comments on the original question, you basically need to print on a Canvas object, and rotate the Paragraph. The Rectangle basically defines the coordinates where you will place the Canvas. Something like this should work:
PdfDocument srcDocument = new PdfDocument(new PdfReader(file));
PdfDocument destDocument = new PdfDocument(new PdfWriter(newfile));
FontProgram fontProgram =
FontProgramFactory.CreateFont(#"C:\temp\calibri.ttf");
PdfFont calibri = PdfFontFactory.CreateFont(fontProgram, PdfEncodings.WINANSI);
int pagesCount = srcDocument.GetNumberOfPages();
for (int i = 1; i <= pagesCount; i++)
{
srcDocument.CopyPagesTo(i, i, destDocument);
PdfCanvas pdfCanvas = new PdfCanvas(destDocument.GetPage(i));
Canvas canvas = new Canvas(pdfCanvas, new Rectangle(36, 750, 100, 50));
canvas.Add(new Paragraph("0001").SetRotationAngle(1.5708).SetFont(calibri).SetFontSize(4));
canvas.Close();
}
srcDocument.Close();
destDocument.Close();
Alternatively, you can set a Style, if you plan on reusing a lot, something like:
Style rotatedStuff = new Style()
.SetRotationAngle(1.5708)
.SetFont(calibri)
.SetFontSize(4);
and then just apply it to the Paragraph with the AddStyle() method.
Mind you that if you provide with a float number, it's in rads, so 90° is 1.5708 rad (1 Degree (°) = 0.01745 Radian (rad)).

Related

iText7: Adding a PDF inside a Cell (cutoff at end of page)

We are creating a PDF, it contains a large table, with headers (bold), sub-headers (non bold), rows and cells.
One of the requirements is to add an already existing PDF (can be multiple pages) inside the cell (under the sub-header). And this is where we are struggling. What we have tried so far:
Converting the PDF to a FormXObject, and adding that into the cell.
FontProvider fontProvider = new FontProvider();
ConverterProperties props = new ConverterProperties();
props.SetFontProvider(fontProvider);
var historyId = text.Replace("=Spectec_template", "");
var pdfTemplate = templateToPdfClient.GetPdf(historyId, "HI").Result;
PdfDocument srcDoc = new PdfDocument(new PdfReader(pdfTemplate.FileStream));
for (int i = 1; i < srcDoc.GetNumberOfPages() + 1; i++)
{
Cell newCell = new Cell(1, columnInfo.ColumnSpan).SetFont(PdfFontFactory.CreateFont(_fontName));
PdfPage origPage = srcDoc.GetPage(i);
Rectangle rect = origPage.GetPageSize();
PdfFormXObject pageCopy = origPage.CopyAsFormXObject(pdf);
Image image = new Image(pageCopy);
image.SetBorder(Border.NO_BORDER);
image.SetMaxWidth(UnitValue.CreatePercentValue(100));
image.SetMaxHeight(UnitValue.CreatePercentValue(50));
Div div = new Div();
div.Add(image.SetMaxWidth(UnitValue.CreatePercentValue(100)));
newCell.Add(div);
newCell = ApplyLayoutsToCell(newCell, cellStyles);
table.AddCell(newCell);
}
But the end result is that we only see 1 page and it's completely overlapping at the end of the page.
When we set image.SetAutoScale(true); the PDF is visible but it's extremely small.
When adding the Image to a Paragraph instead of a DIV the PDF pages display next to each other.
Any ideas, suggestions?
Thank you
Thanks to #mkl and #KJ I figured this out.
I ended up adding each PDF page in a new Cell, and setting the scaling of image to 0.6f.
for (int i = 1; i < srcDoc.GetNumberOfPages() + 1; i++)
{
newCell = new Cell(1, columnInfo.ColumnSpan).SetFont(PdfFontFactory.CreateFont(_fontName));
PdfPage origPage = srcDoc.GetPage(i);
PdfFormXObject pageCopy = origPage.CopyAsFormXObject(pdf);
Image image = new Image(pageCopy);
image.SetBorder(Border.NO_BORDER);
image.Scale(0.6f, 0.6f);
newCell.Add(image);
newCell = ApplyLayoutsToCell(newCell, cellStyles);
table.AddCell(newCell);
}

iText7 how to set an existing PDF document's margin and save the newly created file?

I'm using iText7 in C# and I have a PDF file in which I have to change the margin programmatically.
I've tried out everything with not much success, the only thing that kinda worked for me is this snippet of code
PdfDocument pdfDoc = new PdfDocument(new PdfReader(sourceFile), new PdfWriter(destinationFile));
float margin = 30;
for (int i = 0; i < pdfDoc.GetNumberOfPages(); i++)
{
PdfPage page = pdfDoc.GetPage(i+1);
Rectangle mediaBox = page.GetMediaBox();
Rectangle newMediabox = new Rectangle(mediaBox.GetLeft() - margin, mediaBox.GetBottom() + margin, mediaBox.GetWidth(), mediaBox.GetHeight());
page.SetMediaBox(newMediabox);
}
pdfDoc.Close();
I said kinda, because all it does is pushing the content of the PDF file.
Is there any way I can change the margin of an existing PDF file?
Any help would be greatly appreciated!
UPDATE
I have tried Gustav's answer, but nothing changed
Here is my code:
public static void Main(String[] args)
{
RegisterPdfImproved(source, target);
}
public static void RegisterPdfImproved(string sourceFilename, string targetFilename)
{
using (PdfDocument pdf = new PdfDocument(new PdfReader(sourceFilename), new PdfWriter(targetFilename)))
using (Document document = new Document(pdf))
{
document.SetMargins(2000, 2000, 2000, 2000);
//Paragraph paragraph = new Paragraph(registration).AddStyle(RegistrationStyle()).SetMarginTop(0);
//document.Add(paragraph);
}
}
private static Style RegistrationStyle()
{
// Fixed design values for font and rectangle.
PdfFont font = PdfFontFactory.CreateFont(StandardFonts.HELVETICA);
const float fontSize = 8F;
const float rightPadding = 3F;
TextAlignment textAlignment = TextAlignment.RIGHT;
iText.Kernel.Colors.Color borderColor = ColorConstants.RED;
iText.Kernel.Colors.Color fillColor = ColorConstants.WHITE;
const float borderWidth = 0.7F;
Style style = new Style()
.SetFont(font)
.SetFontSize(fontSize)
.SetPaddingRight(rightPadding)
.SetTextAlignment(textAlignment)
.SetBackgroundColor(fillColor)
.SetBorder(new SolidBorder(borderColor, borderWidth));
return style;
}
Maybe I did soemthing wrong?

iText 7 watermark canvas not appearing correctly in some special 3d pdf

I have been using iText7 latest .net package to create watermark annotation on pdf document. I see for most of the noraml pdf files watermark is stamped properly in the desired position however for some special 3d(dimensional) pdf files the watermark is strangely not appearning properly, what I believe that some part of the watermark is gettting cut by the other 3d layers. Here is the screenshot of distortion experienced. I am trying to understand and solve this problem but unable to get any clue. Can someone help? Here is the sample pdf in case you want to take a look.
Here is the very short relevant code which I am using to embed watermark.
float watermarkTrimmingRectangleWidth = 500;
float watermarkTrimmingRectangleHeight = 500;
float formWidth = 500;
float formHeight = 500;
float formXOffset = -40;
float formYOffset = 0;
float xTranslation = 50;
float yTranslation = 25;
double rotationInRads = Math.PI / 4.2;
PdfDocument pdfDoc = new PdfDocument(new PdfReader(filePath), new PdfWriter(destinationfile));
var numberOfPages = pdfDoc.GetNumberOfPages();
PdfPage page = null;
for (var i = 1; i <= numberOfPages; i++)
{
page = pdfDoc.GetPage(i);
Rectangle ps = page.GetPageSize();
float bottomLeftX = ps.GetWidth() / 2 - watermarkTrimmingRectangleWidth / 2;
float bottomLeftY = ps.GetHeight() / 2 - watermarkTrimmingRectangleHeight / 2;
Rectangle watermarkTrimmingRectangle = new Rectangle(bottomLeftX, bottomLeftY, ps.GetWidth(), watermarkTrimmingRectangleHeight);
PdfWatermarkAnnotation watermark = new PdfWatermarkAnnotation(watermarkTrimmingRectangle);
AffineTransform transform = new AffineTransform();
transform.Translate(xTranslation, yTranslation);
transform.Rotate(rotationInRads);
PdfFixedPrint fixedPrint = new PdfFixedPrint();
watermark.SetFixedPrint(fixedPrint);
Rectangle formRectangle = new Rectangle(formXOffset, formYOffset, formWidth, formHeight);
//Observation: font XObject will be resized to fit inside the watermark rectangle
PdfFormXObject form = new PdfFormXObject(formRectangle);
PdfExtGState gs1 = new PdfExtGState().SetFillOpacity(0.8f).SetStrokeOpacity(1.5f);
PdfCanvas canvas = new PdfCanvas(form, pdfDoc);
float[] transformValues = new float[6];
transform.GetMatrix(transformValues);
canvas.SaveState()
.BeginText().SetExtGState(gs1)
.SetTextMatrix(transformValues[0], transformValues[1], transformValues[2], transformValues[3], transformValues[4], transformValues[5])
.SetFontAndSize(font, fontSize)
.ShowText("Restricted Internal")
.EndText()
.RestoreState();
canvas.Release();
watermark.SetAppearance(PdfName.N, new PdfAnnotationAppearance(form.GetPdfObject()));
watermark.SetFlags(PdfAnnotation.READ_ONLY);
page.AddAnnotation(watermark);
}
page?.Flush();
pdfDoc.Close();
Your watermark annotation is drawn on top of some form fields.
Because the display order for page elements is page content, annotations, form fields, your annotation appearance is blocked by the form fields.

iText7 PDF renders correctly but prints incorrect in Adobe Reader

I have a task of adding watermarks to a lot of existing PDFs and I use iText7 for this in C#. The result can be seen in this picture (Blank pdf used)
It renders fine everywhere and prints perfectly from both Chrome and Edge. However, when printed from Adobe Acrobat Reader, this is what happens:
Anyone knowing more about PDF than I, who can help with this issue? I am using version 7.1.13 of iText.
The test pdf is available here:
https://potanteststorage.blob.core.windows.net/pdf/Test.pdf
C# Code:
public static void AddProductionWatermarks(string sourceFile, string destinationPath)
{
float watermarkTrimmingRectangleWidth = 75;
float watermarkTrimmingRectangleHeight = 250;
//Custom text
float formWidth = 75;
float formHeight = 250;
float formXOffset = 0;
float formYOffset = 0;
float xTranslation = 50;
float yTranslation = 0;
double rotationInRads = Math.PI / 2;
PdfFont font = PdfFontFactory.CreateFont(StandardFonts.TIMES_ROMAN);
float fontSize = 12;
PdfDocument pdfDoc = new PdfDocument(new PdfReader(sourceFile), new PdfWriter(destinationPath));
var numberOfPages = pdfDoc.GetNumberOfPages();
PdfPage page = null;
for (var i = 1; i <= numberOfPages; i++)
{
page = pdfDoc.GetPage(i);
Rectangle ps = page.GetPageSize();
//PRODUCTION watermark -------------------------------------------------------
float prodBottomLeftX = -20;
float prodBottomLeftY = ps.GetHeight() / 2;
Rectangle prodWatermarkTrimmingRectangle = new Rectangle(prodBottomLeftX, prodBottomLeftY, watermarkTrimmingRectangleWidth, watermarkTrimmingRectangleHeight);
PdfWatermarkAnnotation prodWatermark = new PdfWatermarkAnnotation(prodWatermarkTrimmingRectangle);
AffineTransform transform2 = new AffineTransform();
transform2.Translate(xTranslation, yTranslation);
transform2.Rotate(rotationInRads);
PdfFixedPrint fixedPrint2 = new PdfFixedPrint();
prodWatermark.SetFixedPrint(fixedPrint2);
PdfFormXObject form2 = new PdfFormXObject(formRectangle);
PdfCanvas canvas2 = new PdfCanvas(form2, pdfDoc);
transform2.GetMatrix(transformValues);
canvas2.SaveState()
.BeginText().SetColor(new DeviceRgb(255, 36, 0), true)
.SetTextMatrix(transformValues[0], transformValues[1], transformValues[2], transformValues[3], transformValues[4], transformValues[5])
.SetFontAndSize(font, fontSize)
.ShowText("PRODUCTION")
.EndText()
.RestoreState();
canvas2.Release();
prodWatermark.SetAppearance(PdfName.N, new PdfAnnotationAppearance(form2.GetPdfObject()));
prodWatermark.SetFlags(PdfAnnotation.PRINT);
page.AddAnnotation(prodWatermark);
}
page?.Flush();
pdfDoc.Close();
}
You use FixedPrint dictionaries in your annotations:
PdfFixedPrint fixedPrint2 = new PdfFixedPrint();
prodWatermark.SetFixedPrint(fixedPrint2);
This additional entry for Watermark annotations specifies how this annotation shall be drawn relative to the dimensions of the target media during printing. Thus, you ask for special treatment of the watermark during printing.
So if you indeed want special treatment during printing (merely not the current treatment), simply set the PdfFixedPrint properties accordingly.
If you don't want any special treatment during printing, don't set a PdfFixedPrint object at all.

Adding text as watermark using itext7 C#

I am using the below code to add watermark to my pdf.
private void Merge(List<string> src, string dest)
{
iTextKernel.PdfWriter writer = new iTextKernel.PdfWriter(dest);
iTextKernel.PdfDocument pdfDocument1 = new iTextKernel.PdfDocument(new iTextKernel.PdfReader(src[0]), writer);
pdfDocument1.AddEventHandler(PdfDocumentEvent.END_PAGE, new WatermarkingEventHandler());
for (int i = 1, max = src.Count; i < max; i++)
{
iTextKernel.PdfDocument pdfDocument2 = new iTextKernel.PdfDocument(new iTextKernel.PdfReader(src[i]));
var pagesCount = pdfDocument2.GetNumberOfPages();
pdfDocument2.CopyPagesTo(1, pagesCount, pdfDocument1);
pdfDocument2.Close();
}
pdfDocument1.Close();
protected class WatermarkingEventHandler : IEventHandler {
public void HandleEvent(Event e) {
PdfDocumentEvent docEvent = (PdfDocumentEvent) e;
iTextKernel.PdfDocument pdfDoc = docEvent.GetDocument();
iTextKernel.PdfPage page = docEvent.GetPage();
iText.Kernel.Font.PdfFont font = null;
try {
font = PdfFontFactory.CreateFont(FontConstants.HELVETICA_BOLD);
} catch (IOException ex) {
//_log.Error(ex);
}
PdfCanvas canvas = new PdfCanvas(page.NewContentStreamBefore(), page.GetResources(), pdfDoc);
new Canvas(canvas, pdfDoc, page.GetPageSize())
.SetFontColor(iText.Kernel.Colors.DeviceGray.LIGHT_GRAY)
.SetFontSize(60)
.SetFont(font)
.ShowTextAligned(new Paragraph("FOR YOUR RECORDS ONLY: DO NOT SUBMIT"), 298, 421, pdfDoc.GetPageNumber(page),
TextAlignment.CENTER, VerticalAlignment.MIDDLE, 45);
}
But am getting the watermark only in the last page that too hidden under the contents. Could you please modify this code so that i could get the watermark on all the pages and shown over the contents.
Please take a look at the iText 7 for C# jump-start tutorial, more specifically Chapter 5: Manipulating an existing PDF document. Scroll to the part where it says: "Adding a header, footer, and watermark" and look at the example:
PdfDocument pdfDoc = new PdfDocument(new PdfReader(src), new PdfWriter(dest));
Document document = new Document(pdfDoc);
Rectangle pageSize;
PdfCanvas canvas;
int n = pdfDoc.GetNumberOfPages();
for (int i = 1; i <= n; i++) {
PdfPage page = pdfDoc.GetPage(i);
pageSize = page.GetPageSize();
canvas = new PdfCanvas(page);
//Draw header text
}
pdfDoc.close();
As you can see, we only need one PdfDocument instance, but instead of passing only a PdfWriter, we also pass a PdfReader instance. We will read the file with path src and we will write to a file with path dest.
You want to add content to each page. This means that you have to loop over each page (from 1 to n). Get the PdfPage object for each page i and replace the line //Draw header text with whatever it is you want to do.
In your case, you add an image underneath the existing content. That is the normal thing to do, but you say that the watermark is covered by the existing content. That happens for instance when the actual content consists of images (e.g. scanned pages). If you add a watermark under the pages of a PDF that consists of scanned pages, you will never see the watermark.
In that case, you have to add the content on top of the existing content, but it is best to make the watermark transparent:
Paragraph p = new Paragraph("FOR YOUR RECORDS ONLY: DO NOT SUBMIT").SetFontSize(60);
canvas.SaveState();
PdfExtGState gs1 = new PdfExtGState().SetFillOpacity(0.2f);
canvas.SetExtGState(gs1);
document.ShowTextAligned(p, pageSize.GetWidth() / 2, pageSize.GetHeight() / 2, pdfDoc.GetPageNumber(page), TextAlignment.CENTER, VerticalAlignment.MIDDLE, 45);
canvas.RestoreState();
Note that in the tutorial, we are using pageSize.GetWidth() / 2 and pageSize.GetHeight() / 2 as coordinates, which means that we assume that the lower-left corner of the page has the coordinate (0, 0). That may not be the case. You may have to add the x-offset and the y-offset to that value.

Categories

Resources