Telerik Forums
Telerik Document Processing Forum
5 answers
361 views

I am having trouble positioning text using text fragment.  I'm not sure if its a scaling issue during import or what

Below is an extract that failes

I created a PDF completely gray with a black box starting at 100x100 pixels from the top left corner (https://www.pastefile.com/sjKGiJ)

The Original PDF was created by printing a blank page from work through adobe acrobat (for sizing) and then edited in illustrator to add the image.

Via the processor

The file is the imported using PdfStreamWriter and result written to a memory stream

The memorystream is the imported using RadFixedDocument  and then text is added at 100x100 position.

Result is written to a file

Resulting file (https://www.pastefile.com/4HZwvR)

The text does not appear at 100x100 but at at ~ 74x70ish

the text font is not Helvetica but Arial

the font size is not 10pt but 5.63 pt

Code below (result is test4.pdf)

 

Imports Telerik.Windows.Documents.Fixed.FormatProviders.Pdf
Imports Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.Streaming
Imports Telerik.Windows.Documents.Fixed.Model
 
Public Class Form1
    Public pageLetter As New System.Windows.Size(816, 1056)
 
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
 
        'Load file into byte array
        Dim page1 As Byte() = ReadFile("d:\test2.pdf")
 
        Dim originalsize As String = ""
        '  Load source document
        Dim sourceDocument As New IO.MemoryStream(page1)
        Dim sourceDocumentPDF As New PdfFileSource(sourceDocument)
 
        Dim resultIO As New IO.MemoryStream
        Using fileWriter As PdfStreamWriter = New PdfStreamWriter(resultIO)
 
            ' Create file same size as source
            Using targetPDF As PdfPageStreamWriter = fileWriter.BeginPage(pageLetter)
 
 
                ' Store original size to write it out later
                originalsize = sourceDocumentPDF.Pages(0).Size.Width & "x" & sourceDocumentPDF.Pages(0).Size.Height
                ' Write file
                targetPDF.ContentPosition.Clear()
                targetPDF.ContentPosition.Translate(0, 0)
                targetPDF.ContentPosition.Scale(1, 1)
                targetPDF.WriteContent(sourceDocumentPDF.Pages(0))
 
            End Using
 
        End Using
 
 
        Dim provider As New PdfFormatProvider()
        Dim doc1 As RadFixedDocument = provider.Import(resultIO.ToArray)
        Dim pg As RadFixedPage = doc1.Pages(0)
 
        Dim t As Telerik.Windows.Documents.Fixed.Model.Text.TextFragment = New Telerik.Windows.Documents.Fixed.Model.Text.TextFragment()
        t.Font = Fonts.FontsRepository.Helvetica
        t.FontSize = Telerik.Windows.Documents.Media.Unit.DipToPoint(10)
        t.Text = "TEST TEXT PDF SIZE" & originalsize
        pg.Content.Add(t)
 
        t.Position.Translate(100, 100)
        Dim b() As Byte = provider.Export(doc1)
        Dim io2 As New IO.FileInfo("d:\test4.pdf")
        Dim sw As IO.FileStream = io2.OpenWrite()
        sw.Write(b, 0, b.Length)
        sw.Close()
 
 
 
    End Sub
 
    Shared Function ReadFile(filename As String) As Byte()
 
        Dim io As New IO.FileInfo(filename)
 
        Dim fs As IO.FileStream = io.OpenRead
        Dim b() As Byte
        ReDim b(io.Length)
        fs.Read(b, 0, io.Length)
        fs.Close()
        Return b
 
    End Function
 
End Class
Tanya
Telerik team
 answered on 02 Sep 2019
1 answer
310 views

Hi, 

We have a threaded service where we export excel documents to PDF documents (using Telerik.Windows.Documents.Spreadsheet.FormatProviders.Pdf.PdfFormatProvider.Export) and then merge the PDF with other PDF documents (using Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.PdfFormatProvider.Import and RadFixedDocument.Merge). We make sure to dispose the Workbook of the excel document and the MemoryStream from exporting to PDF. 

After some time we start getting the following error, until we restart the service.

One or more errors occurred.
   at System.Threading.Tasks.Task.WaitAll(Task[] tasks, Int32 millisecondsTimeout, CancellationToken cancellationToken)
   at Telerik.Windows.Documents.Utilities.TasksHelper.DoAsync(Action[] actions)
   at Telerik.Windows.Documents.Spreadsheet.Layout.RadWorksheetLayout.CalculateCellContentSizes(List`1 cellsToCalculateContentSize, CellProperties[] cellsFontProperties)
   at Telerik.Windows.Documents.Spreadsheet.Layout.RadWorksheetLayout.UpdateCellContentSizesCache()
   at Telerik.Windows.Documents.Spreadsheet.Layout.RadWorksheetLayout.Measure()
   at Telerik.Windows.Documents.Spreadsheet.Model.Workbook.UpdateWorksheetLayout(RadWorksheetLayout worksheetLayout, Worksheet worksheet, Boolean isForPrinting)
   at Telerik.Windows.Documents.Spreadsheet.Model.Shapes.ShapeCollection.GetTopLeftPoint()
   at Telerik.Windows.Documents.Spreadsheet.Model.Shapes.ShapeCollection.GetTopLeftCellIndex()
   at Telerik.Windows.Documents.Spreadsheet.Model.Shapes.ShapeCollection.RecalculateUsedCellRange()
   at Telerik.Windows.Documents.Spreadsheet.Model.Worksheet.get_TotalUsedCellRange()
   at Telerik.Windows.Documents.Spreadsheet.Layout.PageLayout.AddPages(Worksheet worksheet, Boolean ignorePrintArea)
   at Telerik.Windows.Documents.Spreadsheet.Layout.PageLayout.PreparePages(Workbook workbook, Boolean ignorePrintArea)
   at Telerik.Windows.Documents.Spreadsheet.FormatProviders.Pdf.PdfFormatProvider.ExportOverride(Workbook workbook, Stream output)
   ...
   ...
 Inner exception: The operation completed successfully
    at MS.Win32.UnsafeNativeMethods.RegisterClassEx(WNDCLASSEX_D wc_d)
    at MS.Win32.HwndWrapper..ctor(Int32 classStyle, Int32 style, Int32 exStyle, Int32 x, Int32 y, Int32 width, Int32 height, String name, IntPtr parent, HwndWrapperHook[] hooks)
    at System.Windows.Threading.Dispatcher..ctor()
    at System.Windows.Threading.Dispatcher.get_CurrentDispatcher()
    at System.Windows.Media.TextFormatting.TextFormatter.FromCurrentDispatcher(TextFormattingMode textFormattingMode)
    at System.Windows.Media.FormattedText.LineEnumerator..ctor(FormattedText text)
    at System.Windows.Media.FormattedText.DrawAndCalculateMetrics(DrawingContext dc, Point drawingOffset, Boolean getBlackBoxMetrics)
    at System.Windows.Media.FormattedText.get_Metrics()
    at Telerik.Windows.Documents.Core.TextMeasurer.RadTextMeasurer.MeasureTextWithWrapping(TextProperties textProperties, FontProperties fontProperties, Double wrappingWidth)
    at Telerik.Windows.Documents.Spreadsheet.Measurement.RadTextMeasurer.Measure(String text, FontProperties fontProperties, Nullable`1 wrappingWidth)
    at Telerik.Windows.Documents.Spreadsheet.Measurement.RadTextMeasurer.MeasureMultiline(String text, FontProperties fontProperties, Nullable`1 wrappingWidth)
    at Telerik.Windows.Documents.Spreadsheet.Layout.LayoutHelper.DefaultTextMeasuringMethod(String text, FontProperties fontProperties, Nullable`1 wrappingWidth)
    at Telerik.Windows.Documents.Spreadsheet.Layout.LayoutHelper.CalculateCellContentSize(ICellValue cellValue, CellValueFormat format, FontProperties fontProperties, Double cellIndent, Nullable`1 wrappingWidth, Func`4 measureMultilineText)
    at Telerik.Windows.Documents.Spreadsheet.Layout.RadWorksheetLayout.<>c__DisplayClass4.<CalculateCellContentSizes>b__1()
    at System.Threading.Tasks.Task.Execute()

 

Any help on why this happens and how we can fix it would be appreciated. 

Boby
Telerik team
 answered on 29 Aug 2019
3 answers
277 views

I am trying to export my document but I am getting the error attached to the thread.

I can fix this by going into the properties of the document and checking the unblock box. Is there a way to do this in the export to be able to just open the document without having to go into its properties? I get the same error on the demo page for generating word documents https://demos.telerik.com/aspnet-ajax/wordsprocessing/generate-documents/defaultcs.aspx

 

Here is my code for export

RadFlowDocument document = report.GenerateDoc();             

DocxFormatProvider provider = new DocxFormatProvider();             

MemoryStream ms = new MemoryStream();             

provider.Export(document, ms);             

byte[] byteArray = ms.ToArray();             

ms.Dispose();             

return File(byteArray, "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "AAR.docx");

 

Tanya
Telerik team
 answered on 22 Aug 2019
2 answers
474 views

Hello, tell me please, can I do such a thing as merge/group cells in table with the same values when I export datatable from SQL base to word document

if so answer with code example

what exactly I want represent on attached files

Peshito
Telerik team
 answered on 19 Aug 2019
2 answers
883 views

When writing to a FileStream with PdfStreamWriter.WritePage, either implicitly or after the PdfStreamWriter loses scope...  A PDF document gets created correctly specifically with the ending portion of the PDF document that handles the %EOF and the portions just prior to that.

When this same process is done with a MemoryStream (not a file) for the purposes of taking that "PDF DOCUMENT" and saving the byte() to a database, that ending part of the PDF document doesn't get created... and thus it becomes a corrupt (unreadable) PDF file.  This ending portion of the PDF document that I'm referring to looks like this: (example):

xref
0 26
0000000000 65535 f
0000350400 00000 n
0000350089 00000 n
0000000017 00000 n
0000000204 00000 n
0000000440 00000 n
0000000699 00000 n
0000150134 00000 n
0000150319 00000 n
0000150473 00000 n
0000150732 00000 n
0000232360 00000 n
0000232502 00000 n
0000232814 00000 n
0000232842 00000 n
0000233014 00000 n
0000233544 00000 n
0000233799 00000 n
0000346061 00000 n
0000346138 00000 n
0000346322 00000 n
0000346637 00000 n
0000346892 00000 n
0000350305 00000 n
0000350459 00000 n
0000350528 00000 n
trailer
<</Size 26 /Root 24 0 R /ID [<b3a969414ce82cc43f271ef24e965a21> <b3a969414ce82cc43f271ef24e965a21>] >>
startxref
350975
%%EOF

 

What do I have to do after .WritePage with a backing MemoryStream... to eventually grab that byte() with a complete PDF document that includes the %%EOF piece... like for example:

Dim contentLength As Int64 = msOutputStream.Length
 Dim aryBuffer(contentLength - 1) As Byte
 aryBuffer = msOutputStream.ToArray

After the WritePage, the above will NOT include that ending segment of the PDF document/file.

I can't really .Close the MemoryStream, because then it isn't accessible at all... Does it need to be a part of a USING statement in some way?  I have the PdfStreamWriter in a USING... and I do NOT need to have the FileStream as part of a USING and it works correctly.

 

Some full sample code:

Dim documentsToMerge As String() = {"d:\SignaturePageToAppend.pdf"}
Dim msOutputStream As MemoryStream = New MemoryStream()
 
Using memoryWriter As Pdf.Streaming.PdfStreamWriter = New Pdf.Streaming.PdfStreamWriter(msOutputStream)
    Dim pdfOutputProv As Pdf.PdfFormatProvider = New Pdf.PdfFormatProvider(msOutputStream)
 
    For Each documentName As String In documentsToMerge
 
        'This is initializing the MemoryStream with a good Byte() PDF from a DB
        Dim msAddedPagePDF As MemoryStream = New MemoryStream(docAddedPage.FSDocument)
 
        Using fileToMerge As Pdf.Streaming.PdfFileSource = New Pdf.Streaming.PdfFileSource(msAddedPagePDF)
 
            Dim pdfAddedPage1 As Pdf.Streaming.PdfPageSource = fileToMerge.Pages.FirstOrDefault
            memoryWriter.WritePage(pdfAddedPage1)
 
        End Using
 
 
        'TBD
        Dim contentLength As Int64 = msOutputStream.Length
        Dim aryBuffer(contentLength - 1) As Byte
        aryBuffer = msOutputStream.ToArray
 
        'aryBuffer will NOT include the ending PDF segment to complete the PDF document
         
        'Saving Byte() to DB
        'docSource.FSDocument = aryBuffer
        'trDocumentRepository.Update(docSource)
        'trDocumentRepository.FlushAndSaveChanges()
 
    Next
End Using

 

 

 

Tanya
Telerik team
 answered on 19 Aug 2019
1 answer
178 views

Hi Admin,
With using RadFixedDocument, I tried to import a PDF AcroForm that have signature

then fill data then export to byte array.

But It's always throw exception when use function export

Can you support?

Thank you

Nikolay Demirev
Telerik team
 answered on 19 Aug 2019
4 answers
860 views

Hi admin,

With Telerik reporting (RadPdfProcessing), could we able to do bellow?

1. Load existed an AcroForm as template
2. Auto fill data to this template with data in the database

3. Generate to new non editable PDF file

If can do, please post the guideline

 

Thank you for supporting

 

Best regards,

 

Nikolay Demirev
Telerik team
 answered on 16 Aug 2019
1 answer
194 views

How do you add symbols, specifically "registered" and "copyright" to the pdf export?  Id displays in Visual Studio when creating the text, but it doesn't display after the PDF is generated.

 

Thanks!

Peshito
Telerik team
 answered on 15 Aug 2019
1 answer
186 views

Hi all,

Does Telerik RadFlowDocument or RadFlowDocumentEditor support Track Changes & Lock Tracking of Microsoft Office files(.DOCX)?

Is there any alternative for the same?

 

Martin
Telerik team
 answered on 12 Aug 2019
1 answer
186 views

Dear Concerned,

I am successfully able to merge multiple documents (.DOCX) in to a single document using RadFlowDocument.Merge function.

Now I want to split this document back into multiple documents. please suggest how to proceed?

 

Martin
Telerik team
 answered on 08 Aug 2019
Narrow your results
Selected tags
Tags
+? more
Top users last month
Jay
Top achievements
Rank 3
Iron
Iron
Iron
yw
Top achievements
Rank 2
Iron
Iron
Stefan
Top achievements
Rank 2
Iron
Iron
Iron
Kao Hung
Top achievements
Rank 1
Iron
Bohdan
Top achievements
Rank 2
Iron
Iron
Iron
Want to show your ninja superpower to fellow developers?
Top users last month
Jay
Top achievements
Rank 3
Iron
Iron
Iron
yw
Top achievements
Rank 2
Iron
Iron
Stefan
Top achievements
Rank 2
Iron
Iron
Iron
Kao Hung
Top achievements
Rank 1
Iron
Bohdan
Top achievements
Rank 2
Iron
Iron
Iron
Want to show your ninja superpower to fellow developers?
Want to show your ninja superpower to fellow developers?