PDF Tookit - Add simple graphics and get text width

Hi,

We provide two components to work with Pdf documents; Aspose.Pdf and Aspose.Pdf.Kit.

Aspose.Pdf is used to create pdf files from scratch, while Aspose.Pdf.Kit provides the functionality to edit existing pdf documents.

Both of these components provide features like adding shapes (i.e. lines, circles, ellipse, rectangle etc.). FormattedText class allows you to get TextHeight and TextWidth. Most of the objects provide the facility to be positioned at particular coordinates. BorderInfo class in Aspose.Pdf allows you specify the border width and color as well.

There are a lot of other features available in the library. I would suggest you to explore both of these components to see if they suit your needs. Once you install the components you'll have the documentation intalled as well. It'll help you learn about the features.

If you have any other queries, please do let us know.

Regards,

Hello Jon,

Thanks for considering Aspose.

Adding more to Shahzad's comments, If you need to add text stamp to an existing Pdf file then, PdfFileStamp offers the capability to add text or Image to any page of the Pdf file. For more related information, please visit http://www.aspose.com/documentation/file-format-components/aspose.pdf.kit-for-.net-and-java/add-logo.html

If you need to adhere text to existing Pdf file, you can specify the particular position over the page where to Adhere the text. For more related information and sample code please visit http://www.aspose.com/documentation/file-format-components/aspose.pdf.kit-for-.net-and-java/adhere-text.html

Regarding your query to add Graphics objects, Aspose.Pdf.Kit has a class named PdfContentEditor which offers many methods like CreateLine,CreateMarkup, CreatePolygon, CreatePolyLine, CreateSquareCircle, DrawCurve and many more. For more related information please visit PdfContentEditor.

Thanks,

I went ahead and wrote a test program that will apply multiple lines of stamp text to each page of the document. Some documents contain a mix of portrait and landscape pages, therefore I need to add the stamp text page by page cause i use the page height and width to place the stamp. As I am interating thru the page count when I go from a landscape page to a portrait, it appears the stamp text is getting added but it's not visible...I can highlight the stamp text and copy and paste to a text document to verify it's there but its just not visible in the PDF document.

The stamp info (i.e. text, color, font, etc) is loaded in a list and it iterated thru for each page and does not change...

The sample pdfs are attached.

My code:

static void EDMSStampPDFTest()

{

ConsoleApplication1.PDFServicesWS.ServiceWS pdfServices = new ConsoleApplication1.PDFServicesWS.ServiceWS();

pdfServices.Timeout = 4800000;

ConsoleApplication1.PDFServicesWS.PDFDocument pdfDocument = new ConsoleApplication1.PDFServicesWS.PDFDocument();

pdfDocument.FileContent = ReadFromFile(@"C:\ifass_extract\stamp\2.pdf");

pdfDocument.FileName = @"C:\ifass_extract\stamp\2.pdf";

ConsoleApplication1.PDFServicesWS.PDFStampBox pdfStampBox = new ConsoleApplication1.PDFServicesWS.PDFStampBox();

pdfStampBox.StampLineList = new ConsoleApplication1.PDFServicesWS.PDFStampLine[4];

pdfStampBox.StampHeight = 80;

pdfStampBox.StampWidth = 200;

pdfStampBox.StampTop = 30;

pdfStampBox.StampRight = 30;

pdfStampBox.StampRotation = -5;

pdfStampBox.ShowBorder = true;

pdfStampBox.BorderWidth = 1;

ConsoleApplication1.PDFServicesWS.PDFStampLine pdfStampLine1 = new ConsoleApplication1.PDFServicesWS.PDFStampLine();

pdfStampLine1.FontFamily = ConsoleApplication1.PDFServicesWS.FontStyle.HelveticaBold;

pdfStampLine1.FontSize = 12;

pdfStampLine1.FontColorRed = 139;

pdfStampLine1.FontColorGreen = 35;

pdfStampLine1.FontColorBlue = 35;

pdfStampLine1.YOffset = 4;

pdfStampLine1.Text = "APPROVED";

pdfStampBox.StampLineList[0] = pdfStampLine1;

ConsoleApplication1.PDFServicesWS.PDFStampLine pdfStampLine2 = new ConsoleApplication1.PDFServicesWS.PDFStampLine();

pdfStampLine2.FontFamily = ConsoleApplication1.PDFServicesWS.FontStyle.Helvetica;

pdfStampLine2.FontSize = 8;

pdfStampLine2.FontColorRed = 0;

pdfStampLine2.FontColorGreen = 0;

pdfStampLine2.FontColorBlue = 0;

pdfStampLine2.YOffset = 3;

pdfStampLine2.Text = "Date Received: Date Of Action:";

pdfStampBox.StampLineList[1] = pdfStampLine2;

ConsoleApplication1.PDFServicesWS.PDFStampLine pdfStampLine3 = new ConsoleApplication1.PDFServicesWS.PDFStampLine();

pdfStampLine3.FontFamily = ConsoleApplication1.PDFServicesWS.FontStyle.Helvetica;

pdfStampLine3.FontSize = 10;

pdfStampLine3.FontColorRed = 0;

pdfStampLine3.FontColorGreen = 0;

pdfStampLine3.FontColorBlue = 0;

pdfStampLine3.YOffset = 3;

pdfStampLine3.Text = "11/25/2001 12/10/2002";

pdfStampBox.StampLineList[2] = pdfStampLine3;

ConsoleApplication1.PDFServicesWS.PDFStampLine pdfStampLine4 = new ConsoleApplication1.PDFServicesWS.PDFStampLine();

pdfStampLine4.FontFamily = ConsoleApplication1.PDFServicesWS.FontStyle.Helvetica;

pdfStampLine4.FontSize = 8;

pdfStampLine4.FontColorRed = 0;

pdfStampLine4.FontColorGreen = 0;

pdfStampLine4.FontColorBlue = 0;

pdfStampLine4.YOffset = 5;

pdfStampLine4.Text = "FL OFFICE OF INSURANCE REGULATION";

pdfStampBox.StampLineList[3] = pdfStampLine4;

pdfDocument = pdfServices.StampDocument(pdfDocument, pdfStampBox);

SaveToFile(pdfDocument.FileContent, @"C:\ifass_extract\stamp\edms_stamped.pdf");

}

public PDFDocument StampDocument(PDFDocument pdfDocument, PDFStampBox pdfStampBox)

{

PDFCommon common = new PDFCommon();

PDFDocument stampedPdf = new PDFDocument();

MemoryStream inStream = common.ByteArrayToMStream(pdfDocument.FileContent);

MemoryStream outStream = new MemoryStream();

PdfFileInfo fileInfo = new PdfFileInfo(inStream);

int numPages = fileInfo.NumberofPages;

Aspose.Pdf.Kit.PdfFileStamp stamper = new PdfFileStamp(inStream, outStream);

for (int i = 1; i <= numPages; i++)

{

float iTextWidth = 0; float iTextHeight = 0;

//Get height, width and rotation of the current page

float iPageHeight = 0; float iPageWidth = 0; int iRotation = 0;

iPageHeight = fileInfo.GetPageHeight(i);

iPageWidth = fileInfo.GetPageWidth(i);

iRotation = fileInfo.GetPageRotation(i);

float StampX = 0; float StampY = 0; float iOffset = 0;

StampX = iPageWidth - (pdfStampBox.StampWidth + pdfStampBox.StampRight);

StampY = iPageHeight - (pdfStampBox.StampHeight + pdfStampBox.StampTop);

iOffset = pdfStampBox.StampTop;

// For each line of the stamp as defined in the ini file

foreach (PDFStampLine stampLine in pdfStampBox.StampLineList)

{

Stamp stamp = new Stamp();

stamp.Pages = new int[] { i };

Aspose.Pdf.Kit.FormattedText ft = new FormattedText(stampLine.Text,

new Aspose.Pdf.Kit.FontColor(stampLine.FontColorRed, stampLine.FontColorGreen, stampLine.FontColorBlue),

stampLine.FontFamily,

EncodingType.Winansi,

false,

stampLine.FontSize);

// Set stamp information and apply to PDF document

iTextWidth = ft.TextWidth; iTextHeight = ft.TextHeight;

stamp.SetOrigin(StampX + (pdfStampBox.StampWidth - iTextWidth) / 2, (iPageHeight - iOffset));

iOffset = iOffset + iTextHeight + stampLine.YOffset;

stamp.Rotation = pdfStampBox.StampRotation;

stamp.BindLogo(ft);

stamper.AddStamp(stamp);

}

}

stamper.Close();

stampedPdf.IsSuccessful = true;

stampedPdf.FileName = pdfDocument.FileName.Replace(".pdf", "_stamped.pdf");

stampedPdf.FileContent = common.MStreamToByteArray(outStream);

return stampedPdf;

}

Hi,

I have seen the issue; we're investigating the issue in detail at our end and would let you know about the resolution the earliest possible.

We appreciate your patience.

Regards,

Hi,

This issue has been logged is our bug tracking system as PDFKITNET-7514. You'll be updated as we resolve the issue.

We appreciate your patience.

Regards,


Hi,

The issue PDKITNET-7514 has been resolved. If you need the fix for this issue before our next release, please download the beta version 3.3.1.6 from this link (the attachment in my latest reply to that post).

Thanks,

It appears 3.3.1.6 did not fix my particular issue. I still get stamp text that is invisible when transitioning from a page in landscape to portrait… See attached output (using same code as above).

Hi,

We're sorry for the inconvenience.

We'll look into the matter and will update you accordingly.

We appreciate your patience.

Regards,

Hi,

We have added a new property to the class of Stamp, to resolve the issue. Please refer to the following code.

Thanks,

public void gemini_7514()<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

{

FileStream aFS = new FileStream(In_Temp + @"\gemini_7514\orig_2.pdf", FileMode.Open, FileAccess.Read);

FileStream aMS1 = new FileStream(OutPath + @"\gemini_7514_2_out.pdf", FileMode.Create, FileAccess.Write);

String[] sStamp = new string[4] { "APPROVED", "Date Received: Date Of Action:", "11/25/2001 12/10/2002", "FL OFFICE OF INSURANCE REGULATION" };

PdfFileInfo fileInfo = new PdfFileInfo(aFS);

int numPages = fileInfo.NumberofPages;

PdfFileStamp aPFS2 = new PdfFileStamp(aFS, aMS1);

for (int i = 1; i <= numPages; i++)

{

float iTextWidth = 0; float iTextHeight = 0;

//Get height, width and rotation of the current page

float iPageHeight = 0; float iPageWidth = 0; int iRotation = 0;

iPageHeight = fileInfo.GetPageHeight(i);

iPageWidth = fileInfo.GetPageWidth(i);

iRotation = fileInfo.GetPageRotation(i);

float StampX = 0; float StampY = 0; float iOffset = 0;

StampX = iPageWidth - (200 + 30);

StampY = iPageHeight - (80 + 30);

iOffset = 30;

for (int k = 0; k < sStamp.Length; k++)

{

Stamp logoStamp = new Stamp();

FormattedText formatText = new FormattedText(sStamp[k], System.Drawing.Color.FromArgb(255, 0, 0), "Arial", EncodingType.Winansi, true, 8);

logoStamp.BindLogo(formatText);

logoStamp.Rotation = -5;

//logoStamp.IsBackground = false;

iTextWidth = formatText.TextWidth; iTextHeight = formatText.TextHeight;

logoStamp.SetOrigin(StampX + (200 - iTextWidth) / 2, iPageHeight - iOffset);

logoStamp.PageNumber = i; //Note: a new property Stamp.PageNumber is added since this version

iOffset = iOffset + iTextHeight + 4;

aPFS2.AddStamp(logoStamp);

}

}

aPFS2.Close();

aFS.Close();

aMS1.Close();

}

I used the new property and am still encountering issues…different ones. The stamp text now appears on each page where it belongs but it appears that the stamp text is being applied to every page on or after the PageNumber property. I included 2 pdfs to show this. The one labeled stamped_page_1.pdf I only iterated one time thru the code (i.e. PageNumber = 1) and it applied the stamp text to every page. The other pdf labeled stamped all pages i iterated thru all pages and the stamp text appears multiple times on each page. I also included the original pdf i used.

Hi,



We are working over this issue and will reply to you soon.

Hi,

I am sorry that i hava not understood your problem, please post your code and it can help me to investigate your problem.

I used the code you provided in a previous post https://forum.aspose.com/t/123493

...and have the same issues...I'm using Aspose.Pdf.Kit 3.3.1.6 and it looks like regardless of what you specify for the new property PageNumber, the stamp is being applied to all pages...that's what it looks like to me anyway. Attached are the input and output pdfs for reference.

Hi,

We are looking into the details of this problem and soon you will be updated with the status of correction. Your patience in this regard is highly appreciated.

Hi,

Please visit this link to download the Beta version 3.3.1.8 of Aspose.Pdf.Kit.dll and try it. I hope the problem be resolved in this version.

Thanks,

The issue appears to be resolved. The stamping is working like I expect it. Thanks!

The issues you have found earlier (filed as 7514;7514) have been fixed in [this update](http://www.aspose.com/community/files/51/file-format-components/aspose.pdf.kit-for-.net-and-java/entry171362.aspx).

I noticed another issue with stamping...when I stamp portrait or landscape PDF documents that are Letter size (8.5 x 11 and 11 X 8.5 respectively) the stamp values appear where they should. But when I attempt to stamp a landscape 10 x 7.5 pdf document (2.pdf) the stamp text is cut off at the top.

Sample code was the same code used in a previous thread in this post 166951 (gemini_7514 method) and the PDF and stamped PDF files are attached.

Hi Jon,

I have reproduced the problem at my end and logged it as PDFKITNET-10763 in our issue tracking system. Our team will be looking into the matter and you’ll be updated via this forum as the issue is resolved.

We’re sorry for the inconvenience.
Regards,

Attached is another PDF (src and stamped files) where the stamp is not getting placed correctly. This may help having more pdfs to test…