Click here to Skip to main content
15,847,903 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am new to this topic.

What i have done so far,

1) I have read existing pdf document

2) I have created copy of existing pdf using pdfStamper of iTextSharp

3) and create three layer on new pdf using iTextSharp.

Now i want to open this pdf in adobe reader and i want to add comments on these three layer separately as regular pdf commenting option.

But problem is, it is not saving on respective layer, it is saving on pdf.

On/Off action of pdf layer for comment is not working.

can you guide me how to proceed further.

Thank You all
Posted
Updated 6-Dec-17 7:33am

1 solution

string originalFile = "Original1.pdf";
string copyOfOriginal = "Re-copia.pdf";

byte[] bytes = Convert.FromBase64String(archivo);

System.IO.FileStream stream = new FileStream(originalFile, FileMode.CreateNew);
System.IO.BinaryWriter writer = new BinaryWriter(stream);
writer.Write(bytes, 0, bytes.Length);
writer.Close();

PdfReader reader1 = new PdfReader(originalFile);
using (FileStream fs = new FileStream(copyOfOriginal, FileMode.Create, FileAccess.Write, FileShare.None))
// Creating iTextSharp.text.pdf.PdfStamper object to write
// Data from iTextSharp.text.pdf.PdfReader object to FileStream object
using (PdfStamper stamper = new PdfStamper(reader1, fs))
{

    int pageCount = reader1.NumberOfPages;

    // Create New Layer for Watermark
    PdfLayer layer = new PdfLayer("WatermarkLayer", stamper.Writer);
    // Loop through each Page
    for (int i = pageCount; i <= pageCount; i++)
    {
        // Getting the Page Size
        Rectangle rect = reader1.GetPageSize(i);



        // Get the ContentByte object
        PdfContentByte cb = stamper.GetUnderContent(i);

        // Tell the cb that the next commands should be "bound" to this new layer
        cb.BeginLayer(layer);
        cb.SetFontAndSize(BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED), 50);

        PdfGState gState = new PdfGState();
        cb.SetGState(gState);


        string codbartest = codBarras;
        BarcodePDF417 bcpdf417 = new BarcodePDF417();
        //Asigna el código de barras en base64 a la propiedad text del objeto..
        bcpdf417.Text = ASCIIEncoding.ASCII.GetBytes(codbartest);
        Image imgpdf417 = bcpdf417.GetImage();
        imgpdf417.SetAbsolutePosition(50, 50);
        imgpdf417.ScalePercent(100);
        cb.AddImage(imgpdf417);
        // Close the layer
        cb.EndLayer();
    }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900