Aspose generator migration - terrible

The new aspose may be the latest and greatest but the documentation is extremely lacking.

What is the replacement for:

  1. DestinationType (used to be Pdf.DestinationType)

  2. How do you set a default font for a document (used to be Pdf.TextInfo)

  3. What replaces a “Section” when creating a doc from scratch

  4. What replaces a “Text”

  5. What is a basic example for creating and saving a PDF document from scratch

What’s the difference between a TextParagraph and a TextFragment

When is the documentation going to be updated?

@asposeGuy

I would like to share with you that we have logged an investigation ticket with ID PDFNET-43806 in our issue management system. As soon as the concerned team shares an update about replacement of Pdf.DestinationType property, we will share the information with you accordingly.

A default font can be set with SystemFontsSubstitution.DefaultFont property.

There is no Section element in the new DOM (Document Object Model) approach. We can use a Page instance, instead of the Section instance, because the Section in old Aspose.Pdf.Generator namespace has been changed to Page in new Aspose.Pdf DOM approach.

In order to understand the new DOM approach, please go through this help topic: Introduction to the DOM API.

If that ‘Text’ is referring to Aspose.Pdf.Text namespace then this namespace exists which provides classes that allow to extract text, add text, manipulate existing text of a document.

In these two lines of code below, a Document object is initiated from scratch and is then saved as a PDF document.

        Document document = new Document();
        document.Save(dataDir + "test.pdf");

For further information on Working with Text, you may visit this documentation article for your kind reference.

A TextFragment represents a fragment of Pdf text. Phisycally, a PDF’s text representation is very complex. The text “hello world” may consist of several physically independent text segments. The Aspose.Pdf text model basically establishes that TextFragment object provides single logic operation set over physical. For example, in text search scenario, TextFragment is logical “hello world” text representation. While generating a PDF from scratch, TextFragments are used which add text/paragraph in Paragraph Collection of a PDF page, as TextFragment is a paragraph level object.

Whereas, a TextParagraph represents text paragraphs as multi-line text object, it is used to add text in an existing page. TextParagraph limits the text to specific page and does not adds a page, consequently the content is overlapped. TextBuilder.AppendParagraph method does not append a paragraph into Page Paragraph Collection, but maintains its own paragraphs collection which relates only to text.

We are continually working to improve and update documentation. You will hopefully be noticing further improvements soon.

I hope this will be helpful. Please share if you need any further assistance.

Thank you for the reply.

  1. In the previous versions. The document would just flow as you added new sections and text. How can the same be accomplished now? I have saved and compiled the examples but everything seems to be absolute positioning and if no position is set it dumps the text at the very bottom left of the page. Need to just set margins, start at the top and freely add paragraphs, etc and have the page wrap when a the end of the page is reached.

  2. How do you create a font setting (family, size, color, bold, etc) and then use it over and over again throughout a document (TextInfo in the old Generator)?

Document pdfDocument = new Document();
Page pdfPage = pdfDocument.Pages.Add();
TextBuilder textBuilder = new TextBuilder(pdfPage);

TextFragment textFragment = new TextFragment(“Hello World”);
textBuilder.AppendText(textFragment);

TextParagraph paragraph = new TextParagraph();
paragraph.AppendLine(@"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent eu quam vestibulum, efficitur mi quis, finibus nulla. Praesent vitae pretium quam. Vestibulum mattis tortor nec blandit eleifend. Etiam metus mauris, blandit id lacus vel, gravida mattis sem. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Etiam feugiat nec ante in porta. Vestibulum viverra enim interdum ligula sollicitudin rhoncus. Ut porttitor, risus quis varius aliquet, magna arcu tincidunt mauris, ut dapibus justo purus ut turpis. ");
textBuilder.AppendParagraph(paragraph);

pdfDocument.Save(“output.pdf”);

@asposeGuy

Thanks for getting back to us.

As per your requirements, we recommend you to use TextFragment - as shared earlier, it is paragraph level object and while generating PDF from scratch, you may add text using TextFragment, which will become part of PDF at the time you save the document. Whereas, TextBuilder is more suitable, in order to add text in an existing page of PDF document.

Please check following code snippet, where we have tried to cover all the checks from your above requirements:

// TextState object (TextInfo in old generator)
TextState textState = new TextState();
textState.Font = FontRepository.FindFont("Arial");
textState.FontSize = 10;
textState.FontStyle = FontStyles.Bold;
textState.ForegroundColor = Color.Red;

// Create a new document
Document doc = new Document();
// Add page in the document
Page page = doc.Pages.Add();

string longString = "Lorem ipsum ";
for(int i = 0; i < 1000; i++)
{
  longString += " Lorem ipsum ";
}

// create a text fragment object
TextFragment longText = new TextFragment(longString);
// add text to the page
page.Paragraphs.Add(longText);

// add another page
Page page2 = doc.Pages.Add();
// create another text fragment
TextFragment shortText = new TextFragment("A quick brown fox, jumps over the lazy dog.");
// TextState object (TextInfo in old generator)
shortText.TextState.ApplyChangesFrom(textState);

// add text to second page
page2.Paragraphs.Add(shortText);
// set page margins
page2.PageInfo.Margin = new MarginInfo(10, 10, 10, 10);

// save the document
doc.Save(dataDir + "doc.out.pdf");

doc.out.pdf (28.7 KB)

In case of any further assistance, please feel free to let us know.

Thank you.

  1. In the past, you could create a new “Text” object and immediately assign its font (e.g. new Text (“Test”, FONT)). This was fairly efficient. It seems this is not possible with the new TextFragment. Is the only way to apply a TextState to create the TextFragment, apply the state. and then add it to the page?

  2. As long as you create the first page, it will continue to wrap as long as you continue adding fragments, correct?

  3. Is there a replacement for IsHtmlTagSupported

@asposeGuy

Thank for getting back to us.

In the DOM (Document Object Model), you can deal with PDF document as well as its contents at object level. You may create a TextState object and reuse it for several TextFragments and also you can set TextState separately for a single TextFragment by accessing it like TextFragment.TextState.Font etc.

Yes, your understanding is correct about this. In case the text in TextFragment exceeds the height of Page, it automatically adds a new page in the PDF document for remaining text.

In DOM model, HtmlFragment (just like TextFragment) has been introduced, by using which you may add HTML string inside PDF. For more information, please check “Add HTML String using DOM” article in our API documentation.

Thank you.

  1. Regarding the first question, I was referring to code efficiency:

this
section.Paragraphs.Add(new Text(“hello world”, usethisfont));

has been replaced by this:
TextFragment newText = new TextFragment(“hello world”);
newText.TextState.ApplyChangesFrom(usethisfont);
page.Paragraphs.Add(newText);

As you can see, the first option was far more efficient.

  1. Please provide an example of this:

“A default font can be set with SystemFontsSubstitution.DefaultFont1 property.”

@asposeGuy

I would like to share with you that the former approach may appear efficient but we make changes to the API for the better, to add more features and bug fixes. As far as setting default font is concerned, at the moment, it is not working as expected. So an investigation ticket with ID PDFNET-43815 has been logged in our issue management system for further investigation and resolution. We will update you as soon as it will be investigated.

Actually upon further testing, the new line is very buggy and inconsistent:

This works:
TextFragment t = new TextFragment (“hello\r\nI am a new line\nhi”);
and produces
Hello
I am a new line
hi

This doesn’t:
TextFragment t = new TextFragment (“hello\rI am a new line\nhi”);
and produces
Hello I am a new line hi
It seems that once you use \r\n in a fra

Can you please advise what the consistent escape is for new line?

@asposeGuy

Thanks for your inquiry.

Please note that TextFragment does not accept "\n" for new line feed, instead you can use "\r\n" or Environment.NewLine, in order to add a new line inside text. In case of any further assistance, please feel free to let us know.