Copying embedded fonts from one document to another

I am working on a program that copies certain contents of a PDF document to another document, and I can’t seem to get the font embedded in the document to copy over. I am using Aspose.PDF 18.6.0.0 in a C# application. My current attempt at copying over the font information is something like this:

using (Document document1 = new Document(filename1))
{
using (Document document2 = new Document(filename2))
{
var res = document2.Pages.ElementAt(0).Resources.GetFonts(true); //CreateIfAbsent is set to true just in case the document is missing fonts/text
origDoc.Pages.ElementAt(0).Resources.GetFonts(true); //this document has no font information to begin with, so I use this call simply for the CreateIfAbsent flag
string resName;
foreach (var addFont in res)
{
document1.Pages.ElementAt(0).Resources.Fonts.Add(addFont, out resName);
}
document1.Save(filename1);
}
}

Both of my PDF documents are single-page (hence why I use ElementAt(0)). When looking at both documents in Adobe Acrobat, document2 (where the font should be coming from) says that it contains the font Helvetica,
document2.PNG (6.1 KB)
while document1 (where font should be copied to) says that it contains the font Arial.
document1.PNG (4.7 KB)

Is there something obvious I am doing wrong, or is there a field in one of my objects I need to change for the font to copy over correctly?

@anoelFE

Thank you for contacting support.

Would you please share the source and generated PDF files with us so that we may investigate further to help you out. You may visit Formatting PDF Document for further information about Embedded Fonts in a PDF document.

Here is a revised version of the code I am using in an attempt to embed the original font from document2 in document1. This is the only part of my source relevant to modifying the files I am providing, and I have included more code than last time that I was using to copy text data from document2 to document1, in case that effects how copying fonts works.

using (Document document1 = new Document(“document1.pdf”))
{
using (Document document2 = new Document(“document2.pdf”))
{
//Removes the image from the document with text, so that only the text is copied from the contents.
document2.Pages.ElementAt(0).Resources.Images.Clear();

                string resname;
                var res = document2.FontUtilities.GetAllFonts();
                document1.Pages.ElementAt(0).Resources.GetFonts(true);
                document1.EmbedStandardFonts = true;

                for (int i = 0; i < res.Length; i++)
                {
                    Console.WriteLine("Found Font: {0}", res[i].FontName); //Prints "Found Font: Helvetica"
                    document1.Pages.ElementAt(0).Resources.Fonts.Add(res[i], out resname);
                    Console.WriteLine("Font added: {0} as {1}", document1.Pages.ElementAt(0).Resources.Fonts.Last().FontName, resname); //Prints "Font added: Arial as C0_0"
                    document1.Pages.ElementAt(0).Resources.Fonts.Last().IsEmbedded = true;
                }

                //Loop copies text and other elements from document2 to document1
                for (int j = 0; j < document2.Pages.ElementAt(0).Contents.Count; j++)
                {
                    document1.Pages.ElementAt(0).Contents.Add(document2.Pages.ElementAt(0).Contents.ElementAt(j));
                }
                document1.Save("document1-output.pdf");
            }
        }

Here are example documents that I have run my program on. The documents are named to match their role in the code posted above (document1.pdf has no text or font, document2.pdf has text and font information that should be copied to document1.pdf, and document1-output.pdf is the resulting document lacking the proper font information).

document1.pdf (1.5 MB)
document2.pdf (757.0 KB)
document1-output.pdf (2.0 MB)

Please let me know what I should be doing to properly copy over the font data from document2.pdf.

I inspected the file using a hex editor, and was able to figure out a way of making the text visible in Adobe Acrobat. I don’t understand the specifics of it, but the hex “/Font<</C0_0 14 0 R>>” in the document, when edited to be /Font<</F0 14 0 R>>, fixes the issue of the text not showing.

Please let me know if this is a part of the issue, and if there is something I can do to prevent having to edit the PDF hex each time I run this code on a document.

EDIT: Although text becomes visible/selectable in Adobe Acrobat, the contents of the text appear to be lost, or simply get messed up because of the nature of this fix. Almost all characters become Unicode box characters. This is not a solution to my problem.

@anoelFE

Thank you for elaborating it further.

We have worked with the data shared by you and have found out that source PDF file does not contain any embedded font, as you can verify by adding a Watch for res variable or with a breakpoint, you will notice IsEmbedded property for Helvetica font is set to false. Thus the font file(TTF) is not embedded in source PDF file and therefore is not copied to other PDF file. Moreover, you can simply copy a whole page from one PDF document to another PDF document instead of copying all contents, as the lines of code below:

            //Insert last page of document2 as 5th page of document1
            document1.Pages.Insert(5, document2.Pages[document2.Pages.Count]);

            //Add last page of document2 as next page of document1
            document1.Pages.Add(document2.Pages[document2.Pages.Count]);

You may visit Concatenate PDF Files for further information about this.Furthermore, the text in document1-output.pdf file is displayed fine so kindly elaborate the problem of not showing the text.