Issue with Joining documents using Aspose.PDF

For me, this issue still persists with Aspose.PDF v19.9

I am merging two documents into my resulting document as layers and custom embedded fonts are not carried over to the result document. Here is my cleaned code:

Document docA = new Document(fileAPath);
Document docB = new Document(fileBPath);
int pageCount = Math.Min(docA.Pages.Count, docB.Pages.Count);
for (int index = 1; index <= pageCount; index++)
{
    Page docPage = resultDoc.Pages.Add();
    docPage.Contents.Clear();
    docPage.Layers = new List<Layer>();

    Layer layerA = new Layer("A", "Dokument 1");
    layerA.Contents.AddRange(docA.Pages[index].Contents.OfType<Operator>());
    docPage.Layers.Add(layerA);

    Layer layerB = new Layer("B", "Dokument 2");
    layerB.Contents.AddRange(docB.Pages[index].Contents.OfType<Operator>());
    docPage.Layers.Add(layerB);

    // more layers and content generation...
}
// disable all optimizations
OptimizationOptions optimizationOptions = new OptimizationOptions();
optimizationOptions.ImageCompressionOptions.ImageQuality = 100;
optimizationOptions.ImageCompressionOptions.ResizeImages = false;
optimizationOptions.ImageCompressionOptions.CompressImages = false;
optimizationOptions.LinkDuplcateStreams = false;
optimizationOptions.AllowReusePageContent = false;
optimizationOptions.RemovePrivateInfo = false;
optimizationOptions.RemoveUnusedObjects = false;
optimizationOptions.RemoveUnusedStreams = false;
optimizationOptions.UnembedFonts = false;
resultDoc.OptimizeResources(optimizationOptions);

After execution of this code, if I call resultDoc.FontUtilities.GetAllFonts() it still returns an empty array while the other two documents do have custom fonts embedded and I can see them with the same function call. Is there a way I can embed fonts from one document to another by doing something like this without adding new text segments?

List<Aspose.Pdf.Text.Font> fonts = new List<Aspose.Pdf.Text.Font>();
fonts.AddRange(docA.FontUtilities.GetAllFonts());
// add a new possibly empty text segment to a page
// set custom font
// set embded = true
// save

UPDATE: This hotfix works well for me now with empty text segments. Here is the code if someone else stumbles upon this issue in future:

List<Aspose.Pdf.Text.Font> fonts = new List<Aspose.Pdf.Text.Font>();
// collect all custom fonts ...
TextFragment textFragment = new TextFragment();
foreach (Aspose.Pdf.Text.Font newFont in fonts)
{
    TextSegment textSegment = new TextSegment("  ");
    TextState textState = new TextState();
    textState.Font = newFont;
    textState.Font.IsEmbedded = true;
    textSegment.TextState = textState;
    textFragment.Segments.Add(textSegment);
}
resultDoc.Pages[1].Paragraphs.Add(textFragment);

@sbwos

Your issue is related to Aspose.PDF. We are moving this thread to Aspose.PDF forum where you will be guided appropriately.

@sbwos

Thank you for contacting support.

We are glad to know that it has been sorted out. This solution will certainly help other community members as well. If you still want us to investigate this then please share your sample files so that we may proceed further.

I just want to report that my approach does not work in all cases. If you can enable private uploading for me I can send you my sample data securely.

@sbwos

Please note that the forum attachments are accessible to topic owner and Aspose staff only. However, you may also click on my username and then click message icon to privately send data or Google Drive, Dropbox, etc. link in case of huge data.

Hi @Farhan.Raza,

Any update? were you able to reproduce this behavior on your side?
I have another bug and I am not sure if I should open another support ticket. When I merge two pages as layers onto a new document, sometimes the layers are not snapped over each other and new layers have left padding. Here is my code for fonts and layers test:

Document docA = new Document(fileAPath);
Document docB = new Document(fileBPath);
Document resultDoc = new Document();
int pageCount = Math.Min(docA.Pages.Count, docB.Pages.Count);

for (int i = 1; i <= pageCount; i++)
{
    Page docPage = resultDoc.Pages.Add();
    docPage.PageInfo.Width = docB.Pages[i].CropBox.Width;
    docPage.PageInfo.Height = docB.Pages[i].CropBox.Height;
    docPage.PageInfo.IsLandscape = docB.Pages[i].PageInfo.IsLandscape;
    docPage.PageInfo.Margin.Bottom = 0;
    docPage.PageInfo.Margin.Top = 0;
    docPage.PageInfo.Margin.Left = 0;
    docPage.PageInfo.Margin.Right = 0;

    docPage.Layers = new List<Layer>();

    Layer layerA = new Layer("A", "Dokument 1");
    layerA.Contents.AddRange(docA.Pages[i].Contents.OfType<Operator>());
    docPage.Layers.Add(layerA);
    
    Layer layerB = new Layer("B", "Dokument 2");
    layerB.Contents.AddRange(docB.Pages[i].Contents.OfType<Operator>());
    docPage.Layers.Add(layerB);

    Aspose.Pdf.Rectangle docReact = new Aspose.Pdf.Rectangle(0, 0, docB.Pages[i].CropBox.Width, docB.Pages[i].CropBox.Height);
    docPage.MediaBox = docReact;
}

// embed all external fonts from previous documents to result document
List<Aspose.Pdf.Text.Font> fonts = new List<Aspose.Pdf.Text.Font>();
foreach (Aspose.Pdf.Text.Font oldFont in docA.FontUtilities.GetAllFonts())
{
    if (!fonts.Contains(oldFont))
    {
        fonts.Add(oldFont);
    }
}
foreach (Aspose.Pdf.Text.Font oldFont in docB.FontUtilities.GetAllFonts())
{
    if (!fonts.Contains(oldFont))
    {
        fonts.Add(oldFont);
    }
}

TextFragment textFragment = new TextFragment();
foreach (Aspose.Pdf.Text.Font newFont in fonts)
{
    TextSegment textSegment = new TextSegment("  ");
    TextState textState = new TextState();
    textState.Font = newFont;
    textState.Font.IsEmbedded = true;
    textSegment.TextState = textState;
    textFragment.Segments.Add(textSegment);
}
resultDoc.Pages[1].Paragraphs.Add(textFragment);
resultDoc.Save(savePath);

here is how it renders with the example data I have sent you already via pm:
BrokenFonts.png (27.6 KB)
brokenLayers-1.png (760.9 KB)
brokenLayers-2.png (29.6 KB)

@sbwos

Thank you for elaborating further.

About the problem related to embedded fonts, we have logged a ticket with ID PDFNET-47224 in our issue management system for further investigation and resolution. For layer snapping and left padding, another ticket with ID PDFNET-47225 has been logged.

Moreover, for anti-aliasing, we will be addressing your concerns in respective thread.

Hi guys,

Do you have any ETA regarding these issues? it’s been almost a month and both these issues have become a sore point for us.

@sbwos

Please note that the issue has been logged few weeks ago under free support model and will be investigated on first come first serve basis. Therefore, it may take some months to resolve. As soon as we have some definite updates regarding ticket resolution, we will let you know.

Furthermore, we also offer paid support model where issues are resolved on urgent basis and have priority over the issues logged under free support model. You may check our Paid Supportoptions for your reference.