简体   繁体   中英

Inserting an image after text

I'm making a simple program with .NET and iText7 Inserting a signature image in a PDF document is one of the functions under production.

It has been implemented until the image is inserted into the PDF and newly saved, but I don't know if the image goes behind the text.

The Canvas function seems to be possible, but no matter how many times I look at the example, I can't see any parameters related to the placement.

It would be nice to present a keyword that can implement the function.

The sample results are attached to help understanding. In the figure, the left is the capture of the PDF in which I inserted my signature using a word processor, and the right is the capture of the PDF generated through IText.

My iText version is.Net 7.2.1. I attached the code below just in case it was necessary.

Thank you.

public void PDF_SIGN(FileInfo old_fi)
{
    string currentPath = System.IO.Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);
    String imageFile = currentPath + "\\sign.jpg";
    ImageData data = ImageDataFactory.Create(imageFile);

    string source = old_fi.FullName;
    string sourceFileName = System.IO.Path.GetFileNameWithoutExtension(source);
    string sourceFileExtenstion = System.IO.Path.GetExtension(source);
    string dest = old_fi.DirectoryName + "\\" + sourceFileName + "(signed)" + sourceFileExtenstion;

    PdfDocument pdfDoc = new PdfDocument(new PdfReader(source), new PdfWriter(dest));
    Document document = new Document(pdfDoc);

    iText.Layout.Element.Image image = new iText.Layout.Element.Image(data);
    image.ScaleAbsolute(Xsize, Ysize);
    image.SetFixedPosition(1, Xaxis, Yaxis);

    document.Add(image);

    document.Close();
    pdfDoc.Close();
}

Sample Result (Left: Gaol, Right: Current result):

示例结果(左:监狱,右:当前结果)

You can organize content you add using iText in the same pass before or behind each other simply by the order of adding, and layout elements may have background images or colors.

The previously existing content of the source document, consequentially, usually serves as a mere background of everything new. Except, that is, if you draw to a page in a content stream that precedes earlier content.

Unfortunately you cannot use the Document class for this as its renderer automatically works in the foreground. But you can use the Canvas class here; this class only works on a single object (eg a single page) but it can be initialized in a far more flexible way.

In your case, therefore, replace

Document document = new Document(pdfDoc);

iText.Layout.Element.Image image = new iText.Layout.Element.Image(data);
image.ScaleAbsolute(Xsize, Ysize);
image.SetFixedPosition(1, Xaxis, Yaxis);

document.Add(image);

document.Close();

by

iText.Layout.Element.Image image = new iText.Layout.Element.Image(data);
image.ScaleAbsolute(Xsize, Ysize);
image.SetFixedPosition(1, Xaxis, Yaxis);

PdfPage pdfPage = pdfDoc.GetFirstPage();
PdfCanvas pdfCanvas = new PdfCanvas(pdfPage.NewContentStreamBefore(), pdfPage.GetResources(), pdfDoc);
using (Canvas canvas = new Canvas(pdfCanvas, pdfPage.GetCropBox()))
{
    canvas.Add(image);
}

and you should get the desired result.

(Actually I tested that using Java and ported it to C# in writing this answer. I hope it's ported all right.)


As an aside, if you only want to put an image on the page, you don't really need the Canvas , you can directly use one of the AddImage* methods of PdfCanvas . For multiple elements to be automatically arranged, though, using the Canvas is a good idea.

Also I said above that you cannot use Document here. Actually you can if you replace the document renderer that class uses. For the task at hand that would have been an overkill, though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM