Inserting an Image into a PDF using XDP

1.3k views Asked by At

Problem:

I'm trying to get an image to be displayed on a PDF that is generated using a XDP file.

What I'm currently trying:

I have a XDP file that is exported from a PDF. I have a textbox with the tag "my_image" on the pdf. Here's the code I'm using to encode the string that is inserted as the value of the "my_image" tag.

var bytes = System.IO.File.ReadAllBytes(@"C:\image.jpg");
var data = Convert.ToBase64String(bytes);

When I launch the PDF using the XDP, the image does not display, just the Base64 Encoded string value.

Any ideas?

1

There are 1 answers

3
Chris Haas On BEST ANSWER

I made a simple PDF in LiveCycle Designer with a single Image Field called ImageField1. I used pretty much the same code as you to create the base64 encoded image except that I included the line wraps which shouldn't technically be needed:

var testImage = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Untitled.png");
var bytes = System.IO.File.ReadAllBytes(testImage);
var data = Convert.ToBase64String(bytes, Base64FormattingOptions.InsertLineBreaks);

All together this is my XDP file that works with my simple PDF. I know its not a complete answer but hopefully you can compare what works for me with your code.

<?xml version="1.0" encoding="UTF-8"?>
<?xfa generator="XFA2_4" APIVersion="3.0.11111.0"?>
<xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
<xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/"
><xfa:data
><topmostSubform
><ImageField1 xfa:contentType="image/png" href=""
>iVBORw0KGgoAAAANSUhEUgAAAMYAAAC2CAYAAACCufKcAAAAAXNSR0IArs4c6QAAAARnQU1BAACx
jwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAApDSURBVHhe7Z27j9xUFIf5Q+hCRarQbZcKKlIl
VahIFSpEgyiQKJAoEAVSikgUSClSREoRpUE0URpEE6VBNFEqqFYiy+SxWfIa9jfBxDvr8T1n5o59
fc9naYXI2h6f79xv73v81pwDAhA4RuAtmEAAAscJIAalAgIdBBCDYgEBxKAMQMBGgBrDxomzghFA
jGAJJ1wbAcSwceKsYAQQI1jCCddGADFsnDgrGAHECJZwwrURQAwbJ84KRgAxgiWccG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JAGJkAslt
6iKAGHXlk2gyEUCMTCC5TV0EEKOufBJNJgKIkQkkt6mLwL8Ifb+k376AhQAAAABJRU5ErkJggg==</ImageField1
></topmostSubform
></xfa:data
></xfa:datasets
>
<pdf href="Untitled1_distributed.pdf" xmlns="http://ns.adobe.com/xdp/pdf/"
/></xdp:xdp>